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