Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 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 <glog/logging.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "common/libs/fs/shared_fd.h" |
| 22 | |
| 23 | // Requests and constants below are defined in kernel documentation file: |
| 24 | // https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt |
| 25 | namespace vadb { |
| 26 | namespace usbip { |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 27 | |
| 28 | //////////////////////////////////////////////////////////////////////////////// |
| 29 | // COMMANDS |
| 30 | //////////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | // Command numbers. Commands are valid only once USB device is attached. |
| 33 | enum Command : uint32_t { |
| 34 | kUsbIpCmdReqSubmit = 1, // Submit request |
| 35 | kUsbIpCmdReqUnlink = 2, // Unlink request |
| 36 | kUsbIpCmdRepSubmit = 3, // Submit response |
| 37 | kUsbIpCmdRepUnlink = 4, // Unlink response |
| 38 | }; |
| 39 | |
| 40 | // Direction of data flow. |
| 41 | enum Direction : uint32_t { |
| 42 | kUsbIpDirectionOut = 0, |
| 43 | kUsbIpDirectionIn = 1, |
| 44 | }; |
| 45 | |
| 46 | // Setup structure is explained in great detail here: |
| 47 | // - http://www.beyondlogic.org/usbnutshell/usb6.shtml |
| 48 | // - http://www.usbmadesimple.co.uk/ums_4.htm |
| 49 | struct CmdRequest { |
| 50 | uint8_t type; |
| 51 | uint8_t cmd; |
| 52 | uint16_t value; |
| 53 | uint16_t index; |
| 54 | uint16_t length; |
| 55 | } __attribute__((packed)); |
| 56 | |
| 57 | // CmdHeader precedes any command request or response body. |
| 58 | struct CmdHeader { |
| 59 | Command command; |
| 60 | uint32_t seq_num; |
| 61 | uint16_t bus_num; |
| 62 | uint16_t dev_num; |
| 63 | Direction direction; |
| 64 | uint32_t endpoint; // valid values: 0-15 |
| 65 | } __attribute__((packed)); |
| 66 | |
| 67 | // Command data for submitting an USB request. |
| 68 | struct CmdReqSubmit { |
| 69 | uint32_t transfer_flags; |
| 70 | uint32_t transfer_buffer_length; |
| 71 | uint32_t start_frame; |
| 72 | uint32_t number_of_packets; |
| 73 | uint32_t deadline_interval; |
| 74 | CmdRequest setup; |
| 75 | } __attribute__((packed)); |
| 76 | |
| 77 | // Command response for submitting an USB request. |
| 78 | struct CmdRepSubmit { |
| 79 | uint32_t status; // 0 = success. |
| 80 | uint32_t actual_length; |
| 81 | uint32_t start_frame; |
| 82 | uint32_t number_of_packets; |
| 83 | uint32_t error_count; |
| 84 | CmdRequest setup; |
| 85 | } __attribute__((packed)); |
| 86 | |
| 87 | // Unlink USB request. |
| 88 | struct CmdReqUnlink { |
| 89 | uint32_t seq_num; |
| 90 | uint32_t reserved[6]; |
| 91 | } __attribute__((packed)); |
| 92 | |
| 93 | // Unlink USB response. |
| 94 | struct CmdRepUnlink { |
| 95 | uint32_t status; |
| 96 | uint32_t reserved[6]; |
| 97 | } __attribute__((packed)); |
| 98 | |
| 99 | // Diagnostics. |
| 100 | std::ostream& operator<<(std::ostream& out, const CmdHeader& header); |
| 101 | std::ostream& operator<<(std::ostream& out, const CmdReqSubmit& data); |
| 102 | std::ostream& operator<<(std::ostream& out, const CmdRepSubmit& data); |
| 103 | std::ostream& operator<<(std::ostream& out, const CmdReqUnlink& data); |
| 104 | std::ostream& operator<<(std::ostream& out, const CmdRepUnlink& data); |
| 105 | |
| 106 | } // namespace usbip |
| 107 | } // namespace vadb |