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
3 // found in the LICENSE file.
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.
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.
14 // Direction, Recipient, RequestType, and TransferType all map to their
15 // namesakes within the USB specification.
16 enum Direction
{in, out};
17 enum Recipient
{device
, _interface
, endpoint, other
};
18 enum RequestType
{standard
, class
, vendor
, reserved
};
19 enum TransferType
{control, interrupt
, isochronous
, bulk
};
21 // For isochronous mode, SynchronizationType and UsageType map to their
22 // namesakes within the USB specification.
23 enum SynchronizationType
{asynchronous
, adaptive
, synchronous
};
24 enum UsageType
{data
, feedback
, explicitFeedback
};
27 // An opaque ID for the USB device. It remains unchanged until the device is
30 // The device vendor ID.
36 dictionary ConnectionHandle
{
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).
42 // The device vendor ID.
48 [noinline_doc
] dictionary EndpointDescriptor
{
53 // Transfer direction.
55 // Maximum packet size.
56 long maximumPacketSize
;
57 // Transfer synchronization mode (isochronous only).
58 SynchronizationType? synchronization
;
59 // Endpoint usage hint.
61 // Polling interval (interrupt and isochronous only).
62 long? pollingInterval
;
63 // Extra descriptor data associated with this endpoint.
64 ArrayBuffer extra_data
;
67 [noinline_doc
] dictionary InterfaceDescriptor
{
68 // The interface number.
70 // The interface alternate setting number (defaults to <code>0</code).
71 long alternateSetting
;
72 // The USB interface class.
74 // The USB interface sub-class.
75 long interfaceSubclass
;
76 // The USB interface protocol.
77 long interfaceProtocol
;
78 // Description of the interface.
79 DOMString? description
;
80 // Available endpoints.
81 EndpointDescriptor
[] endpoints
;
82 // Extra descriptor data associated with this interface.
83 ArrayBuffer extra_data
;
86 [noinline_doc
] dictionary ConfigDescriptor
{
87 // The configuration number.
88 long configurationValue
;
89 // Description of the configuration.
90 DOMString? description
;
91 // The device is self-powered.
93 // The device supports remote wakeup.
95 // The maximum power needed by this device in milliamps (mA).
97 // Available interfaces.
98 InterfaceDescriptor
[] interfaces
;
99 // Extra descriptor data associated with this configuration.
100 ArrayBuffer extra_data
;
103 dictionary ControlTransferInfo
{
104 // The transfer direction (<code>"in"</code> or <code>"out"</code>).
107 // The transfer target. The target given by <code>index</code> must be
108 // claimed if <code>"interface"</code> or <code>"endpoint"</code>.
112 RequestType requestType
;
114 // The <code>bRequest</code> field, see <i>Universal Serial Bus Specification
115 // Revision 1.1</i> § 9.3.
117 // The <code>wValue</code> field, see <i>Ibid</i>.
119 // The <code>wIndex</code> field, see <i>Ibid</i>.
122 // The amount of data to receive (required only by input transfers).
125 // The data to transmit (required only by output transfers).
129 dictionary GenericTransferInfo
{
130 // The transfer direction (<code>"in"</code> or <code>"out"</code>).
133 // The target endpoint address. The interface containing this endpoint must
137 // The amount of data to receive (required only by input transfers).
140 // The data to transmit (required only by output transfers).
144 dictionary IsochronousTransferInfo
{
145 // Transfer parameters. The transfer length or data buffer specified in this
146 // parameter block is split along <code>packetLength</code> boundaries to
147 // form the individual packets of the transfer.
148 GenericTransferInfo transferInfo
;
150 // The total number of packets in this transfer.
153 // The length of each of the packets in this transfer.
157 dictionary TransferResultInfo
{
158 // A value of <code>0</code> indicates that the transfer was a success.
159 // Other values indicate failure.
162 // The data returned by an input transfer. <code>undefined</code> for output
167 [noinline_doc
] dictionary DeviceFilter
{
170 // Device product ID, checked only if the vendor ID matches.
172 // USB interface class, matches any interface on the device.
173 long? interfaceClass
;
174 // USB interface sub-class, checked only if the interface class matches.
175 long? interfaceSubclass
;
176 // USB interface protocol, checked only if the interface sub-class matches.
177 long? interfaceProtocol
;
180 dictionary EnumerateDevicesOptions
{
181 [deprecated
="Equivalent to setting $(ref:DeviceFilter.vendorId)."]
183 [deprecated
="Equivalent to setting $(ref:DeviceFilter.productId)."]
185 // A device matching any given filter will be returned. An empty filter list
186 // will return all devices the app has permission for.
187 DeviceFilter
[]? filters
;
190 dictionary EnumerateDevicesAndRequestAccessOptions
{
191 // The device vendor ID.
195 // The interface ID to request access to.
196 // Only available on Chrome OS. It has no effect on other platforms.
200 dictionary DevicePromptOptions
{
201 // Allow the user to select multiple devices.
203 // Filter the list of devices presented to the user. If multiple filters are
204 // provided devices matching any filter will be displayed.
205 DeviceFilter
[]? filters
;
208 callback VoidCallback
= void ();
209 callback GetDevicesCallback
= void (Device
[] devices
);
210 callback RequestAccessCallback
= void (boolean success
);
211 callback OpenDeviceCallback
= void (ConnectionHandle
handle);
212 callback FindDevicesCallback
= void (ConnectionHandle
[] handles
);
213 callback GetConfigurationCallback
= void (ConfigDescriptor config
);
214 callback ListInterfacesCallback
= void (InterfaceDescriptor
[] descriptors
);
215 callback CloseDeviceCallback
= void ();
216 callback TransferCallback
= void (TransferResultInfo info
);
217 callback ResetDeviceCallback
= void(boolean success
);
219 interface Functions
{
220 // Enumerates connected USB devices.
221 // |options|: The properties to search for on target devices.
222 static
void getDevices
(EnumerateDevicesOptions options
,
223 GetDevicesCallback
callback);
225 // Presents a device picker to the user and returns the $(ref:Device)s
227 // If the user cancels the picker devices will be empty. A user gesture
228 // is required for the dialog to display. Without a user gesture, the
229 // callback will run as though the user cancelled.
230 // |options|: Configuration of the device picker dialog box.
231 // |callback|: Invoked with a list of chosen $(ref:Device)s.
232 static
void getUserSelectedDevices
(DevicePromptOptions options
,
233 GetDevicesCallback
callback);
235 // Requests access from the permission broker to a device claimed by
236 // Chrome OS if the given interface on the device is not claimed.
238 // |device|: The $(ref:Device) to request access to.
239 // |interfaceId|: The particular interface requested.
240 [deprecated
="This function was Chrome OS specific and calling it on other
241 platforms would fail. This operation is now implicitly performed as part of
242 $(ref:openDevice) and this function will return <code>true</code> on all
244 static
void requestAccess
(Device device
,
246 RequestAccessCallback
callback);
248 // Opens a USB device returned by $(ref:getDevices).
249 // |device|: The $(ref:Device) to open.
250 static
void openDevice
(Device device
, OpenDeviceCallback
callback);
252 // Finds USB devices specified by the vendor, product and (optionally)
253 // interface IDs and if permissions allow opens them for use.
255 // If the access request is rejected or the device fails to be opened a
256 // connection handle will not be created or returned.
258 // Calling this method is equivalent to calling $(ref:getDevices) followed
259 // by $(ref:openDevice) for each device.
261 // |options|: The properties to search for on target devices.
262 static
void findDevices
(EnumerateDevicesAndRequestAccessOptions options
,
263 FindDevicesCallback
callback);
265 // Closes a connection handle. Invoking operations on a handle after it
266 // has been closed is a safe operation but causes no action to be taken.
267 // |handle|: The $(ref:ConnectionHandle) to close.
268 static
void closeDevice
(ConnectionHandle
handle,
269 optional CloseDeviceCallback
callback);
271 // Gets the configuration descriptor for the currently selected
273 // |handle|: An open connection to the device.
274 static
void getConfiguration
(ConnectionHandle
handle,
275 GetConfigurationCallback
callback);
277 // Lists all interfaces on a USB device.
278 // |handle|: An open connection to the device.
279 static
void listInterfaces
(ConnectionHandle
handle,
280 ListInterfacesCallback
callback);
282 // Claims an interface on a USB device.
283 // Before data can be transfered to an interface or associated endpoints the
284 // interface must be claimed. Only one connection handle can claim an
285 // interface at any given time. If the interface is already claimed, this
288 // $(ref:releaseInterface) should be called when the interface is no longer
291 // |handle|: An open connection to the device.
292 // |interfaceNumber|: The interface to be claimed.
293 static
void claimInterface
(ConnectionHandle
handle, long interfaceNumber
,
294 VoidCallback
callback);
296 // Releases a claimed interface.
297 // |handle|: An open connection to the device.
298 // |interfaceNumber|: The interface to be released.
299 static
void releaseInterface
(ConnectionHandle
handle, long interfaceNumber
,
300 VoidCallback
callback);
302 // Selects an alternate setting on a previously claimed interface.
303 // |handle|: An open connection to the device where this interface has been
305 // |interfaceNumber|: The interface to configure.
306 // |alternateSetting|: The alternate setting to configure.
307 static
void setInterfaceAlternateSetting
(ConnectionHandle
handle,
308 long interfaceNumber
,
309 long alternateSetting
,
310 VoidCallback
callback);
312 // Performs a control transfer on the specified device.
314 // Control transfers refer to either the device, an interface or an
315 // endpoint. Transfers to an interface or endpoint require the interface to
318 // |handle|: An open connection to the device.
319 static
void controlTransfer
(ConnectionHandle
handle,
320 ControlTransferInfo transferInfo
,
321 TransferCallback
callback);
323 // Performs a bulk transfer on the specified device.
324 // |handle|: An open connection to the device.
325 // |transferInfo|: The transfer parameters.
326 static
void bulkTransfer
(ConnectionHandle
handle,
327 GenericTransferInfo transferInfo
,
328 TransferCallback
callback);
330 // Performs an interrupt transfer on the specified device.
331 // |handle|: An open connection to the device.
332 // |transferInfo|: The transfer parameters.
333 static
void interruptTransfer
(ConnectionHandle
handle,
334 GenericTransferInfo transferInfo
,
335 TransferCallback
callback);
337 // Performs an isochronous transfer on the specific device.
338 // |handle|: An open connection to the device.
339 static
void isochronousTransfer
(ConnectionHandle
handle,
340 IsochronousTransferInfo transferInfo
,
341 TransferCallback
callback);
343 // Tries to reset the USB device.
344 // If the reset fails, the given connection handle will be closed and the
345 // USB device will appear to be disconnected then reconnected.
346 // In this case $(ref:getDevices) or $(ref:findDevices) must be called again
347 // to acquire the device.
349 // |handle|: A connection handle to reset.
350 static
void resetDevice
(ConnectionHandle
handle,
351 ResetDeviceCallback
callback);