1 // Copyright (c) 2012 The Chromium 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 5 // Use the <code>chrome.usb</code> API to interact with connected USB 6 // devices. This API provides access to USB operations from within the context 7 // of an app. Using this API, apps can function as drivers for hardware devices. 8 namespace usb { 9 10 // Direction, Recipient, RequestType, and TransferType all map to their 11 // namesakes within the USB specification. 12 enum Direction {in, out}; 13 enum Recipient {device, _interface, endpoint, other}; 14 enum RequestType {standard, class, vendor, reserved}; 15 enum TransferType {control, interrupt, isochronous, bulk}; 16 17 // For isochronous mode, SynchronizationType and UsageType map to their 18 // namesakes within the USB specification. 19 enum SynchronizationType {asynchronous, adaptive, synchronous}; 20 enum UsageType {data, feedback, explicitFeedback}; 21 22 // Returned by |getDevices| to identify a connected USB device. 23 dictionary Device { 24 // The id of the USB device. It remains unchanged until the device is 25 // unplugged. 26 long device; 27 long vendorId; 28 long productId; 29 }; 30 31 // Returned by |openDevice| to be used for USB communication. 32 // Every time a device is opened, a new connection handle is created. 33 // 34 // A connection handle represents the underlying data structure that contains 35 // all the data we need to communicate with a USB device, including the status 36 // of interfaces, the pending transfers, the descriptors, and etc. A connectin 37 // handle id is different from a USB device id. 38 // 39 // All connection handles can work together if the device allows it. 40 // The connection handle will be automatically closed when the app is reloaded 41 // or suspended. 42 // 43 // When a connection handle is closed, all the interfaces it claimed will be 44 // released and all the transfers in progress will be canceled immediately. 45 dictionary ConnectionHandle { 46 // The id of the USB connection handle. 47 long handle; 48 long vendorId; 49 long productId; 50 }; 51 52 dictionary EndpointDescriptor { 53 long address; 54 TransferType type; 55 Direction direction; 56 long maximumPacketSize; 57 58 // Used for isochronous mode. 59 SynchronizationType? synchronization; 60 UsageType? usage; 61 62 // If this is an interrupt endpoint, this will be 1-255. 63 long? pollingInterval; 64 }; 65 66 dictionary InterfaceDescriptor { 67 long interfaceNumber; 68 long alternateSetting; 69 long interfaceClass; 70 long interfaceSubclass; 71 long interfaceProtocol; 72 DOMString? description; 73 EndpointDescriptor[] endpoints; 74 }; 75 76 // ControlTransferInfo represents that parameters to a single USB control 77 // transfer. 78 dictionary ControlTransferInfo { 79 // The direction of this transfer. 80 Direction direction; 81 82 // The intended recipient for this transfer. 83 Recipient recipient; 84 85 // The type of this request. 86 RequestType requestType; 87 88 long request; 89 long value; 90 long index; 91 92 // If this transfer is an input transfer, then this field must be set to 93 // indicate the expected data length. If this is an output transfer, then 94 // this field is ignored. 95 long? length; 96 97 // The data payload carried by this transfer. If this is an output transfer 98 // then this field must be set. 99 ArrayBuffer? data; 100 }; 101 102 // GenericTransferInfo is used by both bulk and interrupt transfers to 103 // specify the parameters of the transfer. 104 dictionary GenericTransferInfo { 105 // The direction of this transfer. 106 Direction direction; 107 108 long endpoint; 109 110 // If this is an input transfer then this field indicates the size of the 111 // input buffer. If this is an output transfer then this field is ignored. 112 long? length; 113 114 // If this is an output transfer then this field must be populated. 115 // Otherwise, it will be ignored. 116 ArrayBuffer? data; 117 }; 118 119 // IsochronousTransferInfo describes a single multi-packet isochronous 120 // transfer. 121 dictionary IsochronousTransferInfo { 122 // All of the normal transfer parameters are encapsulated in the 123 // transferInfo parameters. Note that the data specified in this parameter 124 // block is split along packetLength boundaries to form the individual 125 // packets of the transfer. 126 GenericTransferInfo transferInfo; 127 128 // The total number of packets in this transfer. 129 long packets; 130 131 // The length of each of the packets in this transfer. 132 long packetLength; 133 }; 134 135 dictionary TransferResultInfo { 136 // A value of 0 indicates that the transfer was a success. Other values 137 // indicate failure. 138 long? resultCode; 139 140 // If the transfer was an input transfer then this field will contain all 141 // of the input data requested. 142 ArrayBuffer? data; 143 }; 144 145 // Describes the properties of devices which are found via |getDevices|. 146 dictionary EnumerateDevicesOptions { 147 long vendorId; 148 long productId; 149 }; 150 151 // Describes the properties of devices which are found via |findDevices|. 152 dictionary EnumerateDevicesAndRequestAccessOptions { 153 long vendorId; 154 long productId; 155 // The interface id to request access against. 156 // Only available on ChromeOS. It has no effect on other platforms. 157 long? interfaceId; 158 }; 159 160 callback VoidCallback = void (); 161 callback GetDevicesCallback = void (Device[] devices); 162 callback RequestAccessCallback = void (boolean sucess); 163 callback OpenDeviceCallback = void (ConnectionHandle handle); 164 callback FindDevicesCallback = void (ConnectionHandle[] handles); 165 callback ListInterfacesCallback = void (InterfaceDescriptor[] descriptors); 166 callback CloseDeviceCallback = void (); 167 callback TransferCallback = void (TransferResultInfo info); 168 callback ResetDeviceCallback = void(boolean result); 169 170 interface Functions { 171 // Lists USB devices specified by vendorId/productId/interfaceId tuple. 172 // |options|: The properties to search for on target devices. 173 // |callback|: Invoked with a list of |Device|s on complete. 174 static void getDevices(EnumerateDevicesOptions options, 175 GetDevicesCallback callback); 176 177 // This method is ChromeOS specific. Calling this method on other platforms 178 // will fail. 179 // Requests access from the permission broker to an OS claimed device if the 180 // given interface on the device is not claimed. 181 // 182 // |device|: The device to request access to. 183 // |interfaceId|: 184 static void requestAccess(Device device, 185 long interfaceId, 186 RequestAccessCallback callback); 187 188 // Opens a USB device returned by |getDevices|. 189 // |device|: The device to open. 190 // |callback|: Invoked with the created ConnectionHandle on complete. 191 static void openDevice(Device device, OpenDeviceCallback callback); 192 193 // Finds USB devices specified by the vendorId/productId/interfaceId tuple 194 // and, if permissions allow, opens them for use. 195 // 196 // On Chrome OS, you can specify the interfaceId. In that case the method 197 // will request access from permission broker in the same way as in 198 // |requestUsbAcess|. 199 // 200 // If the access request is rejected, or the device is failed to be opened, 201 // its connection handle will not be created or returned. 202 // 203 // Calling this method is equivalent to calling |getDevices| followed by 204 // a series of |requestAccess| (if it is on ChromeOs) and |openDevice| 205 // calls, and returning all the successfully opened connection handles. 206 // 207 // |options|: The properties to search for on target devices. 208 // |callback|: Invoked with the opened ConnectionHandle on complete. 209 static void findDevices(EnumerateDevicesAndRequestAccessOptions options, 210 FindDevicesCallback callback); 211 212 // Closes a connection handle. Invoking operations on a device after it 213 // has been closed is a safe operation, but causes no action to be taken. 214 // |handle|: The connection handle to close. 215 // |callback|: The callback to invoke once the device is closed. 216 static void closeDevice(ConnectionHandle handle, 217 optional CloseDeviceCallback callback); 218 219 // Lists all the interfaces on the USB device. 220 // |handle|: The device from which the interfaces should be listed. 221 // |callback|: The callback to invoke when the interfaces are enumerated. 222 static void listInterfaces(ConnectionHandle handle, 223 ListInterfacesCallback callback); 224 225 // Claims an interface on the specified USB device. 226 // Before you can transfer data with endpoints, you must claim their parent 227 // interfaces. Only one connection handle on the same host can claim each 228 // interface. If the interface is already claimed, this call will fail. 229 // 230 // You shall call releaseInterface when the interface is not needed anymore. 231 // 232 // |handle|: The device on which the interface is to be claimed. 233 // |interface|: The interface number to be claimed. 234 // |callback|: The callback to invoke once the interface is claimed. 235 static void claimInterface(ConnectionHandle handle, long interfaceNumber, 236 VoidCallback callback); 237 238 // Releases a claim to an interface on the provided device. 239 // |handle|: The device on which the interface is to be released. 240 // |interface|: The interface number to be released. 241 // |callback|: The callback to invoke once the interface is released. 242 static void releaseInterface(ConnectionHandle handle, long interfaceNumber, 243 VoidCallback callback); 244 245 // Selects an alternate setting on a previously claimed interface on a 246 // device. 247 // |handle|: The device on which the interface settings are to be set. 248 // |interface|: The interface number to be set. 249 // |alternateSetting|: The alternate setting to set. 250 // |callback|: The callback to invoke once the interface setting is set. 251 static void setInterfaceAlternateSetting(ConnectionHandle handle, 252 long interfaceNumber, 253 long alternateSetting, 254 VoidCallback callback); 255 256 // Performs a control transfer on the specified device. See the 257 // ControlTransferInfo structure for the parameters required to make a 258 // transfer. 259 // 260 // Conceptually control transfer talks to the device itself. You do not need 261 // to claim interface 0 to perform a control transfer. 262 // 263 // |handle|: A connection handle to make the transfer on. 264 // |transferInfo|: The parameters to the transfer. See ControlTransferInfo. 265 // |callback|: Invoked once the transfer has completed. 266 static void controlTransfer(ConnectionHandle handle, 267 ControlTransferInfo transferInfo, 268 TransferCallback callback); 269 270 // Performs a bulk transfer on the specified device. 271 // |handle|: A connection handle to make the transfer on. 272 // |transferInfo|: The parameters to the transfer. See GenericTransferInfo. 273 // |callback|: Invoked once the transfer has completed. 274 static void bulkTransfer(ConnectionHandle handle, 275 GenericTransferInfo transferInfo, 276 TransferCallback callback); 277 278 // Performs an interrupt transfer on the specified device. 279 // |handle|: A connection handle to make the transfer on. 280 // |transferInfo|: The parameters to the transfer. See GenericTransferInfo. 281 // |callback|: Invoked once the transfer has completed. 282 static void interruptTransfer(ConnectionHandle handle, 283 GenericTransferInfo transferInfo, 284 TransferCallback callback); 285 286 // Performs an isochronous transfer on the specific device. 287 // |handle|: A connection handle to make the transfer on. 288 // |transferInfo|: The parameters to the transfer. See 289 // IsochronousTransferInfo. 290 // |callback|: Invoked once the transfer has been completed. 291 static void isochronousTransfer(ConnectionHandle handle, 292 IsochronousTransferInfo transferInfo, 293 TransferCallback callback); 294 295 // Tries to reset the USB device and restores it to the previous status. 296 // If the reset fails, the given connection handle will be closed and the 297 // USB device will appear to be disconnected then reconnected. 298 // In that case you must call |getDevices| or |findDevices| again to acquire 299 // the device. 300 // 301 // |handle|: A connection handle to reset. 302 // |callback|: Invoked once the device is reset with a boolean indicating 303 // whether the reset is completed successfully. 304 static void resetDevice(ConnectionHandle handle, 305 ResetDeviceCallback callback); 306 }; 307 }; 308