• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 #pragma once
17 
18 #include <stdint.h>
19 
20 namespace usb_forward {
21 
22 // Commands that can be executed over serial port.
23 // Use magic value to avoid accidental interpretation of commonly seen numbers.
24 enum Command : uint32_t {
25   // Get device list.
26   // Request format:
27   // - RequestHeader{}
28   // Response format:
29   // - ResponseHeader{}
30   // - int32_t(num_devices)
31   // - num_devices times:
32   //   - DeviceInfo{}
33   //   - DeviceInfo.num_interfaces times:
34   //     - InterfaceInfo{}
35   CmdDeviceList = 0xcfad0001,
36 
37   // Attach specified device.
38   // Request format:
39   // - RequestHeader{}
40   // - AttachRequestHeader{}
41   // Response format:
42   // - ResponseHeader{}
43   CmdAttach,
44 
45   // Execute command on attached USB device.
46   // Request format:
47   // - RequestHeader{}
48   // - ControlTransfer{}
49   // - if transfer direction is host -> device
50   //   - uint8_t[ControlTransfer.length] data
51   // Response format:
52   // - ResponseHeader{}
53   // - if transfer direction is device -> host
54   //   - int32_t(actual length)
55   //   - uint8_t[actual length] bytes
56   CmdControlTransfer,
57 
58   // Execute transfer on attached USB device.
59   // Request format:
60   // - RequestHeader{}
61   // - DataTransfer{}
62   // - if transfer direction is host -> device
63   //   - uint8_t[DataTransfer.length] data
64   // Response format:
65   // - ResponseHeader{}
66   // - if transfer direction is host -> device
67   //   - int32_t(actual length)
68   //   - int32_t[actual length] bytes
69   CmdDataTransfer,
70 
71   // Heartbeat is used to detect whether device is alive.
72   // This is a trivial request/response mechanism.
73   // Response status indicates whether server is ready.
74   // Request format:
75   // - RequestHeader{}
76   // Response format:
77   // - ResponseHeader{}
78   CmdHeartbeat,
79 };
80 
81 // Status represents command execution result, using USB/IP compatible values.
82 enum Status : uint32_t {
83   // StatusSuccess indicates successful command execution.
84   StatusSuccess = 0,
85 
86   // StatusFailure indicates error during command execution.
87   StatusFailure = 1
88 };
89 
90 struct RequestHeader {
91   Command command;
92   uint32_t tag;
93 };
94 
95 struct ResponseHeader {
96   Status status;
97   uint32_t tag;
98 };
99 
100 // DeviceInfo describes individual USB device that was found attached to the
101 // bus.
102 struct DeviceInfo {
103   uint16_t vendor_id;
104   uint16_t product_id;
105   uint16_t dev_version;
106   uint8_t dev_class;
107   uint8_t dev_subclass;
108   uint8_t dev_protocol;
109   uint8_t bus_id;
110   uint8_t dev_id;
111   uint8_t speed;
112   uint8_t num_configurations;
113   uint8_t num_interfaces;
114   uint8_t cur_configuration;
115 } __attribute__((packed));
116 
117 // InterfaceInfo describes individual interface attached to a USB device.
118 struct InterfaceInfo {
119   uint8_t if_class;
120   uint8_t if_subclass;
121   uint8_t if_protocol;
122   uint8_t if_reserved;
123 } __attribute__((packed));
124 
125 // AttachRequest specifies which device on which bus needs to be attached.
126 struct AttachRequest {
127   uint8_t bus_id;
128   uint8_t dev_id;
129 } __attribute__((packed));
130 
131 // ControlTransfer specifies target bus and device along with USB request.
132 struct ControlTransfer {
133   uint8_t bus_id;
134   uint8_t dev_id;
135   uint8_t type;
136   uint8_t cmd;
137   uint16_t value;
138   uint16_t index;
139   uint16_t length;
140   uint32_t timeout;
141 } __attribute__((packed));
142 
143 // DataTransfer is used to exchange data between host and device.
144 struct DataTransfer {
145   uint8_t bus_id;
146   uint8_t dev_id;
147   uint8_t endpoint_id;
148   uint8_t is_host_to_device;
149   int32_t length;
150   uint32_t timeout;
151 } __attribute__((packed));
152 
153 }  // namespace usb_forward
154