David Valleau | f376421 | 2018-12-06 15:48:17 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Fletcher Woodruff | 6cdda0a | 2020-04-22 11:14:07 -0600 | [diff] [blame] | 5 | #ifndef OP_COMMANDS_H__ |
| 6 | #define OP_COMMANDS_H__ |
David Valleau | 64c8149 | 2018-10-26 13:24:37 -0700 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * This file defines the supported OP command messages from the usbip userspace |
| 10 | * protocol, as well as some utility functions for processing them. |
| 11 | * |
| 12 | * In the context of the defined messages: |
| 13 | * "Req" is used in messages that submit a request. |
| 14 | * "Rep" is used in messages which reply to a request. |
| 15 | * |
| 16 | * For more information about the usbip protocol refer to the following |
| 17 | * documentation: |
| 18 | * https://www.kernel.org/doc/Documentation/usb/usbip_protocol.txt |
| 19 | * https://en.opensuse.org/SDB:USBIP |
| 20 | */ |
| 21 | |
Fletcher Woodruff | 76e9e78 | 2020-04-30 16:51:26 -0600 | [diff] [blame] | 22 | #include <cstdlib> |
| 23 | #include <vector> |
| 24 | |
David Valleau | 64c8149 | 2018-10-26 13:24:37 -0700 | [diff] [blame] | 25 | #include "device_descriptors.h" |
| 26 | #include "smart_buffer.h" |
| 27 | #include "usbip_constants.h" |
| 28 | |
David Valleau | 64c8149 | 2018-10-26 13:24:37 -0700 | [diff] [blame] | 29 | // Forward declaration of UsbPrinter. |
| 30 | class UsbPrinter; |
| 31 | |
| 32 | // Contains the header values that are contained within all of the "OP" messages |
| 33 | // used by usbip. |
| 34 | struct OpHeader { |
| 35 | uint16_t version; // usbip version. |
| 36 | uint16_t command; // op command type. |
| 37 | int status; // op request status. |
| 38 | }; |
| 39 | |
| 40 | // Generic device descriptor used by OpRepDevlist and OpRepImport. |
| 41 | struct OpRepDevice { |
| 42 | char usbPath[256]; |
| 43 | char busID[32]; |
| 44 | int busnum; |
| 45 | int devnum; |
| 46 | int speed; |
| 47 | uint16_t idVendor; |
| 48 | uint16_t idProduct; |
| 49 | uint16_t bcdDevice; |
| 50 | uint8_t bDeviceClass; |
| 51 | uint8_t bDeviceSubClass; |
| 52 | uint8_t bDeviceProtocol; |
| 53 | uint8_t bConfigurationValue; |
| 54 | uint8_t bNumConfigurations; |
| 55 | uint8_t bNumInterfaces; |
| 56 | }; |
| 57 | |
| 58 | // The OpReqDevlistMessage message contains the same information as OpHeader. |
| 59 | typedef OpHeader OpReqDevlist; |
| 60 | |
| 61 | // The header used in an OpRepDevlist message, the only difference from |
| 62 | // OpHeader is that it contains |numExportedDevices|. |
| 63 | struct OpRepDevlistHeader { |
| 64 | OpHeader header; |
| 65 | int numExportedDevices; |
| 66 | }; |
| 67 | |
| 68 | // Basic interface descriptor used by OpRepDevlist. |
| 69 | struct OpRepDevlistInterface { |
| 70 | uint8_t bInterfaceClass; |
| 71 | uint8_t bInterfaceSubClass; |
| 72 | uint8_t bInterfaceProtocol; |
| 73 | uint8_t padding; |
| 74 | }; |
| 75 | |
| 76 | // Defines the OpRepDevlist used to respond to a OpReqDevlist message. |
| 77 | struct OpRepDevlist { |
| 78 | OpRepDevlistHeader header; |
| 79 | // Since our program is used to provide a virtual USB device, we only include |
| 80 | // a single OpRepDevice in our response. |
| 81 | OpRepDevice device; |
| 82 | OpRepDevlistInterface* interfaces; |
| 83 | }; |
| 84 | |
| 85 | // Defines the OpReqImport request used to request a device for import. |
| 86 | struct OpReqImport { |
| 87 | OpHeader header; |
| 88 | char busID[32]; |
| 89 | }; |
| 90 | |
| 91 | // Defins the OpRepImport response which indicates whether the requested device |
| 92 | // was successfully exported. |
| 93 | struct OpRepImport { |
| 94 | OpHeader header; |
| 95 | OpRepDevice device; |
| 96 | }; |
| 97 | |
| 98 | // Sets the corresponding members of |header| using the given values. |
| 99 | void SetOpHeader(uint16_t command, int status, OpHeader* header); |
| 100 | |
| 101 | // Sets the corresponding members of |devlist_header| using the given values. |
| 102 | void SetOpRepDevlistHeader(uint16_t command, int status, int numExportedDevices, |
| 103 | OpRepDevlistHeader* header); |
| 104 | |
| 105 | // Sets the members of |device| using the corresponding values in |
| 106 | // |dev_dsc| and |config|. |
| 107 | void SetOpRepDevice(const UsbDeviceDescriptor& dev_dsc, |
| 108 | const UsbConfigurationDescriptor& configuration, |
| 109 | OpRepDevice* device); |
| 110 | |
| 111 | // Assigns the values from |interfaces| into |rep_interfaces|. |
| 112 | void SetOpRepDevlistInterfaces( |
| 113 | const std::vector<UsbInterfaceDescriptor>& interfaces, |
| 114 | OpRepDevlistInterface** rep_interfaces); |
| 115 | |
| 116 | // Creates the OpRepDevlist message used to respond to requests to list the |
| 117 | // host's exported USB devices. |
| 118 | void CreateOpRepDevlist(const UsbDeviceDescriptor& device, |
| 119 | const UsbConfigurationDescriptor& config, |
| 120 | const std::vector<UsbInterfaceDescriptor>& interfaces, |
| 121 | OpRepDevlist* list); |
| 122 | |
| 123 | // Creates the OpRepImport message used to respond to a request to attach a |
| 124 | // host USB device. |
| 125 | void CreateOpRepImport(const UsbDeviceDescriptor& device, |
| 126 | const UsbConfigurationDescriptor& config, |
| 127 | OpRepImport* rep); |
| 128 | |
| 129 | // Convert the various elements of an "OpRep" message into network |
| 130 | // byte order and pack them into a SmartBuffer to be used for transferring along |
| 131 | // a socket. |
David Valleau | 46c3f41 | 2018-11-14 10:25:47 -0800 | [diff] [blame] | 132 | SmartBuffer PackOpHeader(OpHeader header); |
| 133 | SmartBuffer PackOpRepDevice(OpRepDevice device); |
| 134 | SmartBuffer PackOpRepDevlistHeader(OpRepDevlistHeader devlist_header); |
| 135 | SmartBuffer PackOpRepDevlist(OpRepDevlist devlist); |
| 136 | SmartBuffer PackOpRepImport(OpRepImport import); |
David Valleau | 64c8149 | 2018-10-26 13:24:37 -0700 | [diff] [blame] | 137 | |
| 138 | // Convert |header| into host uint8_t order. |
| 139 | void UnpackOpHeader(OpHeader* header); |
| 140 | |
Fletcher Woodruff | 6cdda0a | 2020-04-22 11:14:07 -0600 | [diff] [blame] | 141 | #endif // OP_COMMANDS_H__ |