blob: c094088b8eb6d2adf31f69a5e0b85e2593a2e0c2 [file] [log] [blame]
David Valleauf3764212018-12-06 15:48:17 -08001// 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 Woodruff6cdda0a2020-04-22 11:14:07 -06005#ifndef OP_COMMANDS_H__
6#define OP_COMMANDS_H__
David Valleau64c81492018-10-26 13:24:37 -07007
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 Woodruff76e9e782020-04-30 16:51:26 -060022#include <cstdlib>
23#include <vector>
24
David Valleau64c81492018-10-26 13:24:37 -070025#include "device_descriptors.h"
26#include "smart_buffer.h"
27#include "usbip_constants.h"
28
David Valleau64c81492018-10-26 13:24:37 -070029// Forward declaration of UsbPrinter.
30class UsbPrinter;
31
32// Contains the header values that are contained within all of the "OP" messages
33// used by usbip.
34struct 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.
41struct 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.
59typedef OpHeader OpReqDevlist;
60
61// The header used in an OpRepDevlist message, the only difference from
62// OpHeader is that it contains |numExportedDevices|.
63struct OpRepDevlistHeader {
64 OpHeader header;
65 int numExportedDevices;
66};
67
68// Basic interface descriptor used by OpRepDevlist.
69struct 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.
77struct 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.
86struct 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.
93struct OpRepImport {
94 OpHeader header;
95 OpRepDevice device;
96};
97
98// Sets the corresponding members of |header| using the given values.
99void SetOpHeader(uint16_t command, int status, OpHeader* header);
100
101// Sets the corresponding members of |devlist_header| using the given values.
102void 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|.
107void SetOpRepDevice(const UsbDeviceDescriptor& dev_dsc,
108 const UsbConfigurationDescriptor& configuration,
109 OpRepDevice* device);
110
111// Assigns the values from |interfaces| into |rep_interfaces|.
112void 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.
118void 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.
125void 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 Valleau46c3f412018-11-14 10:25:47 -0800132SmartBuffer PackOpHeader(OpHeader header);
133SmartBuffer PackOpRepDevice(OpRepDevice device);
134SmartBuffer PackOpRepDevlistHeader(OpRepDevlistHeader devlist_header);
135SmartBuffer PackOpRepDevlist(OpRepDevlist devlist);
136SmartBuffer PackOpRepImport(OpRepImport import);
David Valleau64c81492018-10-26 13:24:37 -0700137
138// Convert |header| into host uint8_t order.
139void UnpackOpHeader(OpHeader* header);
140
Fletcher Woodruff6cdda0a2020-04-22 11:14:07 -0600141#endif // OP_COMMANDS_H__