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).
128 // Request timeout (in milliseconds). The default value <code>0</code>
129 // indicates no timeout.
133 dictionary GenericTransferInfo
{
134 // The transfer direction (<code>"in"</code> or <code>"out"</code>).
137 // The target endpoint address. The interface containing this endpoint must
141 // The amount of data to receive (required only by input transfers).
144 // The data to transmit (required only by output transfers).
147 // Request timeout (in milliseconds). The default value <code>0</code>
148 // indicates no timeout.
152 dictionary IsochronousTransferInfo
{
153 // Transfer parameters. The transfer length or data buffer specified in this
154 // parameter block is split along <code>packetLength</code> boundaries to
155 // form the individual packets of the transfer.
156 GenericTransferInfo transferInfo
;
158 // The total number of packets in this transfer.
161 // The length of each of the packets in this transfer.
165 dictionary TransferResultInfo
{
166 // A value of <code>0</code> indicates that the transfer was a success.
167 // Other values indicate failure.
170 // The data returned by an input transfer. <code>undefined</code> for output
175 [noinline_doc
] dictionary DeviceFilter
{
178 // Device product ID, checked only if the vendor ID matches.
180 // USB interface class, matches any interface on the device.
181 long? interfaceClass
;
182 // USB interface sub-class, checked only if the interface class matches.
183 long? interfaceSubclass
;
184 // USB interface protocol, checked only if the interface sub-class matches.
185 long? interfaceProtocol
;
188 dictionary EnumerateDevicesOptions
{
189 [deprecated
="Equivalent to setting $(ref:DeviceFilter.vendorId)."]
191 [deprecated
="Equivalent to setting $(ref:DeviceFilter.productId)."]
193 // A device matching any given filter will be returned. An empty filter list
194 // will return all devices the app has permission for.
195 DeviceFilter
[]? filters
;
198 dictionary EnumerateDevicesAndRequestAccessOptions
{
199 // The device vendor ID.
203 // The interface ID to request access to.
204 // Only available on Chrome OS. It has no effect on other platforms.
208 dictionary DevicePromptOptions
{
209 // Allow the user to select multiple devices.
211 // Filter the list of devices presented to the user. If multiple filters are
212 // provided devices matching any filter will be displayed.
213 DeviceFilter
[]? filters
;
216 callback VoidCallback
= void ();
217 callback GetDevicesCallback
= void (Device
[] devices
);
218 callback RequestAccessCallback
= void (boolean success
);
219 callback OpenDeviceCallback
= void (ConnectionHandle
handle);
220 callback FindDevicesCallback
= void (ConnectionHandle
[] handles
);
221 callback GetConfigurationCallback
= void (ConfigDescriptor config
);
222 callback ListInterfacesCallback
= void (InterfaceDescriptor
[] descriptors
);
223 callback CloseDeviceCallback
= void ();
224 callback TransferCallback
= void (TransferResultInfo info
);
225 callback ResetDeviceCallback
= void(boolean success
);
227 interface Functions
{
228 // Enumerates connected USB devices.
229 // |options|: The properties to search for on target devices.
230 static
void getDevices
(EnumerateDevicesOptions options
,
231 GetDevicesCallback
callback);
233 // Presents a device picker to the user and returns the $(ref:Device)s
235 // If the user cancels the picker devices will be empty. A user gesture
236 // is required for the dialog to display. Without a user gesture, the
237 // callback will run as though the user cancelled.
238 // |options|: Configuration of the device picker dialog box.
239 // |callback|: Invoked with a list of chosen $(ref:Device)s.
240 static
void getUserSelectedDevices
(DevicePromptOptions options
,
241 GetDevicesCallback
callback);
243 // Requests access from the permission broker to a device claimed by
244 // Chrome OS if the given interface on the device is not claimed.
246 // |device|: The $(ref:Device) to request access to.
247 // |interfaceId|: The particular interface requested.
248 [deprecated
="This function was Chrome OS specific and calling it on other
249 platforms would fail. This operation is now implicitly performed as part of
250 $(ref:openDevice) and this function will return <code>true</code> on all
252 static
void requestAccess
(Device device
,
254 RequestAccessCallback
callback);
256 // Opens a USB device returned by $(ref:getDevices).
257 // |device|: The $(ref:Device) to open.
258 static
void openDevice
(Device device
, OpenDeviceCallback
callback);
260 // Finds USB devices specified by the vendor, product and (optionally)
261 // interface IDs and if permissions allow opens them for use.
263 // If the access request is rejected or the device fails to be opened a
264 // connection handle will not be created or returned.
266 // Calling this method is equivalent to calling $(ref:getDevices) followed
267 // by $(ref:openDevice) for each device.
269 // |options|: The properties to search for on target devices.
270 static
void findDevices
(EnumerateDevicesAndRequestAccessOptions options
,
271 FindDevicesCallback
callback);
273 // Closes a connection handle. Invoking operations on a handle after it
274 // has been closed is a safe operation but causes no action to be taken.
275 // |handle|: The $(ref:ConnectionHandle) to close.
276 static
void closeDevice
(ConnectionHandle
handle,
277 optional CloseDeviceCallback
callback);
279 // Select a device configuration.
281 // This function effectively resets the device by selecting one of the
282 // device's available configurations. Only configuration values greater
283 // than <code>0</code> are valid however some buggy devices have a working
284 // configuration <code>0</code> and so this value is allowed.
285 // |handle|: An open connection to the device.
286 static
void setConfiguration
(ConnectionHandle
handle,
287 long configurationValue
,
288 VoidCallback
callback);
290 // Gets the configuration descriptor for the currently selected
292 // |handle|: An open connection to the device.
293 static
void getConfiguration
(ConnectionHandle
handle,
294 GetConfigurationCallback
callback);
296 // Lists all interfaces on a USB device.
297 // |handle|: An open connection to the device.
298 static
void listInterfaces
(ConnectionHandle
handle,
299 ListInterfacesCallback
callback);
301 // Claims an interface on a USB device.
302 // Before data can be transfered to an interface or associated endpoints the
303 // interface must be claimed. Only one connection handle can claim an
304 // interface at any given time. If the interface is already claimed, this
307 // $(ref:releaseInterface) should be called when the interface is no longer
310 // |handle|: An open connection to the device.
311 // |interfaceNumber|: The interface to be claimed.
312 static
void claimInterface
(ConnectionHandle
handle, long interfaceNumber
,
313 VoidCallback
callback);
315 // Releases a claimed interface.
316 // |handle|: An open connection to the device.
317 // |interfaceNumber|: The interface to be released.
318 static
void releaseInterface
(ConnectionHandle
handle, long interfaceNumber
,
319 VoidCallback
callback);
321 // Selects an alternate setting on a previously claimed interface.
322 // |handle|: An open connection to the device where this interface has been
324 // |interfaceNumber|: The interface to configure.
325 // |alternateSetting|: The alternate setting to configure.
326 static
void setInterfaceAlternateSetting
(ConnectionHandle
handle,
327 long interfaceNumber
,
328 long alternateSetting
,
329 VoidCallback
callback);
331 // Performs a control transfer on the specified device.
333 // Control transfers refer to either the device, an interface or an
334 // endpoint. Transfers to an interface or endpoint require the interface to
337 // |handle|: An open connection to the device.
338 static
void controlTransfer
(ConnectionHandle
handle,
339 ControlTransferInfo transferInfo
,
340 TransferCallback
callback);
342 // Performs a bulk transfer on the specified device.
343 // |handle|: An open connection to the device.
344 // |transferInfo|: The transfer parameters.
345 static
void bulkTransfer
(ConnectionHandle
handle,
346 GenericTransferInfo transferInfo
,
347 TransferCallback
callback);
349 // Performs an interrupt transfer on the specified device.
350 // |handle|: An open connection to the device.
351 // |transferInfo|: The transfer parameters.
352 static
void interruptTransfer
(ConnectionHandle
handle,
353 GenericTransferInfo transferInfo
,
354 TransferCallback
callback);
356 // Performs an isochronous transfer on the specific device.
357 // |handle|: An open connection to the device.
358 static
void isochronousTransfer
(ConnectionHandle
handle,
359 IsochronousTransferInfo transferInfo
,
360 TransferCallback
callback);
362 // Tries to reset the USB device.
363 // If the reset fails, the given connection handle will be closed and the
364 // USB device will appear to be disconnected then reconnected.
365 // In this case $(ref:getDevices) or $(ref:findDevices) must be called again
366 // to acquire the device.
368 // |handle|: A connection handle to reset.
369 static
void resetDevice
(ConnectionHandle
handle,
370 ResetDeviceCallback
callback);
374 // Event generated when a device is added to the system. Events are only
375 // broadcast to apps and extensions that have permission to access the
376 // device. Permission may have been granted at install time, when the user
377 // accepted an optional permission (see $(ref:permissions.request)), or
378 // through $(ref:getUserSelectedDevices).
379 static
void onDeviceAdded
(Device device
);
381 // Event generated when a device is removed from the system. See
382 // $(ref:onDeviceAdded) for which events are delivered.
383 static
void onDeviceRemoved
(Device device
);