-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.html
2937 lines (2931 loc) · 113 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>
WebHID API
</title>
<script src='https://www.w3.org/Tools/respec/respec-w3c' async class=
'remove'></script>
<script class='remove'>
var respecConfig = {
specStatus: "CG-DRAFT",
latestVersion: null,
editors: [{
name: "Matt Reynolds", w3cid: "105511",
company: "Google", companyURL: "https://www.google.com/"
}],
github: "WICG/webhid",
group: "wicg",
xref: ["DOM", "HTML", "INFRA", "PERMISSIONS-POLICY", "POINTEREVENTS", "UIEVENTS", "GAMEPAD", "SERVICE-WORKERS"],
localBiblio: {
"USBIF-HID-CLASS": {
"authors": [
"Mike Bergman"
],
"etAl": true,
"href": "https://www.usb.org/document-library/device-class-definition-hid-111",
"title": "Device Class Definition for Human Interface Devices (HID) version 1.11",
"date": "May 27, 2001",
"status": "Specification",
"publisher": "USB Implementers Forum",
},
"USBIF-HID-USAGE": {
"authors": [
"David Abzarian"
],
"etAl": true,
"href": "https://www.usb.org/document-library/hid-usage-tables-122",
"title": "HID Usage Tables for Universal Serial Bus (USB) version 1.22",
"date": "April 6, 2021",
"status": "Specification",
"publisher": "USB Implementers Forum",
},
},
};
</script>
</head>
<body>
<section id='abstract'>
<p>
This document describes an API for providing access to devices that
support the Human Interface Device (HID) protocol.
</p>
</section>
<section id='sotd'>
<!-- This section is filled automatically by ReSpec. -->
</section>
<section class="informative">
<h2>
Introduction
</h2>
<p>
A HID (Human Interface Device) is a type of device that takes input
from or provides output to humans. It also refers to the HID protocol,
a standard for bi-directional communication between a host and a device
that is designed to simplify the installation procedure. The HID
protocol was originally developed for USB devices but has since been
implemented over many other protocols, including Bluetooth.
</p>
<p>
The web platform already supports input from many HID devices.
Keyboards, pointing devices, and gamepads are all typically implemented
using the HID protocol. However, this support relies on the operating
system's HID drivers that transform the HID input into high-level input
APIs. Devices that are not well supported by the host's HID driver are
often inaccessible to web pages. Similarly, the outputs on most devices
that are not supported by the host's HID driver are inaccessible.
</p>
<p>
See also the related <a href="EXPLAINER.html">Explainer</a> document.
</p>
</section>
<section class="informative" id='motivating-applications'>
<h2>
Motivating Applications
</h2>
<section>
<h3>
Niche devices
</h3>
<p>
The most common classes of HID input devices are already
well-supported on the web platform through existing high-level input
APIs. For instance, mouse input is accessible through
{{PointerEvent}} and keyboard input is accessible through
{{KeyboardEvent}}. Input from these devices is handled using the
host's native HID driver and typically does not require
device-specific drivers or configuration to work correctly. WebHID is
not intended for devices like these that are already well-supported
through high-level input APIs.
</p>
<p>
For some classes of HID devices, the web platform supports some
features of the device but limits access to other features. For
instance, [[GAMEPAD]] supports the input capabilities of most game
controllers but does not support less common capabilities like LED
indicators or audio. These features are often not well-supported by
host APIs and adding support within the user agent can lead to
significant complexity. WebHID would give applications an alternative
when the functionality provided by the high-level API is incomplete.
</p>
<p>
Many HID devices are not supported through any web platform API. The
HID specification describes a wide array of devices that could be
supported through HID, including virtual reality controls, flight
simulators, medical equipment, and more. WebHID would allow these
devices to be used without requiring additional drivers or
modification to the user agent.
</p>
</section>
<section>
<h3>
Prototyping, hobbyists, and educational devices
</h3>
<p>
HID is attractive for prototyping and hobbyist applications because
it allows devices to use the host's generic HID driver instead of
requiring a driver for each host where the device will be used. The
simplified installation procedure also makes it easier for educators
to deploy a device to a classroom of students without modifying the
system configuration on each host. Providing access to HID devices
through the web platform would further reduce installation
requirements, particularly for devices that are currently only
supported through host-specific applications.
</p>
</section>
</section>
<section class="informative" id='security-and-privacy'>
<h2>
Security and Privacy Considerations
</h2>
<section id='abusing-access'>
<h3>
Abusing Access to a Device
</h3>
<p>
HID peripherals may expose powerful functionality that should not be
made accessible to the page without explicit consent from the user.
For instance, a HID peripheral may have sensors that allow it to
collect information about its surroundings. A device may store
private information that should not be revealed or overwritten. Many
devices expose functionality that allow the device firmware to be
upgraded. Operating systems typically do not restrict access to HID
devices from applications, and this access can occasionally be abused
to damage the device or corrupt the data stored on it.
</p>
<p>
In some cases, a device may expose functionality that should not be
accessible at all from a web page. Specific devices may be blocked by
recognizing the device by its [=vendor ID=] and [=product ID=] and
hiding such devices during enumeration. The [=report descriptor=]
allows a device to describe its own capabilities, and this
information can be used to block classes of devices even when the
[=vendor ID=] and [=product ID=] cannot be known in advance. This can
be accomplished by inspecting the [=HID usage=] values assigned to
collections within the [=report descriptor=]; for instance, access to
keyboard-like devices can be denied by denying access to devices that
include a [=top-level collection=] with the [=HID usage=] for a
keyboard.
</p>
<p>
To mitigate abuse, a device will not be made available to the page
until the user has granted explicit access. Access must first be
requested by the page and will be granted once the user has selected
the device from a chooser displaying all available devices. Once a
page has been granted access, an indicator will be displayed to
indicate the device is in use.
</p>
</section>
<section id='attacking-a-device'>
<h3>
Attacking a Device
</h3>
<p>
The HID protocol is extremely versatile, and a wide variety of
devices have been designed that take advantage of this versatility.
As a result, there are many possibilities for how access to a device
could allow a malicious page to damage the device itself. It is
relatively common for HID peripherals to allow firmware updates to be
sent using custom HID reports. Device manufacturers must take care to
design the firmware upgrade mechanism to verify that the firmware
binary is authentic, and should also protect against downgrades that
could reduce the security of a device. Untrusted upgrades could be
used to add new capabilities to a device or cause it to masquerade as
an entirely different type of device.
</p>
<p>
Some devices may be damaged when inputs are provided outside of the
expected range. For instance, the rumble feature on a gamepad could
become damaged if a page sends an invalid rumble command.
</p>
<p>
In general, these types of attacks are device-specific and cannot be
mitigated at the API level.
</p>
</section>
<section id='attacking-the-host'>
<h3>
Attacking the Host
</h3>
<p>
If a malicious page gains access to a device, in some cases this
access can be used to attack the host. A major concern is whether the
device can be used to generate trusted input events. These events
serve as a proxy for user intent and can be used to access more
powerful web platform features.
</p>
<p>
Mice and keyboards are typically implemented as HID peripherals, and
are also capable of generating trusted input. A HID keyboard or mouse
may contain advanced features like programmable macros, which store a
sequence of inputs and allow the sequence to be played back at a
later time. Device manufacturers must take care to design such
features in a way that would prevent a malicious app from
reprogramming the device with an unexpected input sequence, and must
also protect against triggering macro playback without the user's
explicit consent.
</p>
</section>
<section id='mitigation'>
<h3>
Mitigation
</h3>
<p>
To mitigate security and privacy risks, it is recommended that user
agents implement a chooser-based flow for requesting device access.
When a page requests access to a HID device, the user agent should
display a dialog that informs the user that the page wants to access
a device. The dialog should display a list of connected HID devices
and ask the user to select a device that will be exposed to the page.
To reduce the possibility of accidental clicks, the dialog should
require two separate interactions: one interaction to select the
device from the list and a second interaction to confirm the
selection and dismiss the dialog.
</p>
</section>
</section>
<section data-dfn-for="Navigator">
<h2>
Extensions to the `Navigator` interface
</h2>
<pre class="idl">
[SecureContext] partial interface Navigator {
[SameObject] readonly attribute HID hid;
};
</pre>
<h3>
<dfn>hid</dfn> attribute
</h3>
<p>
When getting, the {{Navigator/hid}} attribute always returns the same
instance of the {{HID}} object.
</p>
</section>
<section data-dfn-for="WorkerNavigator">
<h2>
Extensions to the `WorkerNavigator` interface
</h2>
<pre class="idl">
[Exposed=(DedicatedWorker,ServiceWorker), SecureContext]
partial interface WorkerNavigator {
[SameObject] readonly attribute HID hid;
};
</pre>
<h3>
<dfn>hid</dfn> attribute
</h3>
<p>
When getting, the {{WorkerNavigator/hid}} attribute always returns the same
instance of the {{HID}} object.
</p>
</section>
<section data-dfn-for="HID">
<h2>
`HID` interface
</h2>
<pre class="idl">
[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HID : EventTarget {
attribute EventHandler onconnect;
attribute EventHandler ondisconnect;
Promise<sequence<HIDDevice>> getDevices();
[Exposed=Window] Promise<sequence<HIDDevice>> requestDevice(
HIDDeviceRequestOptions options);
};
</pre>
<div class="note">
<p>
The user agent's implement may choose to conditionally expose
{{hid}} attribute of the {{Navigator}} when the [=relevant global object=]
of the {{Navigator}} is {{ServiceWorkerGlobalScope}}. For example,
in the <b>Browser extension contexts</b> of [[browserext]].
</p>
</div>
<p>
Instances of {{HID}} are created with the internal slots described in
the following table:
</p>
<table class="simple">
<tr>
<th>
Internal slot
</th>
<th>
Initial value
</th>
<th>
Description (non-normative)
</th>
</tr>
<tr>
<td>
<dfn data-dfn-for="HID">[[\devices]]</dfn>
</td>
<td>
An empty [=ordered set=]
</td>
<td>
An [=ordered set=] of {{HIDDevice}} instances representing the
[=HID interfaces=] currently available on the system
</td>
</tr>
</table>
<dl>
<dt>
<dfn>onconnect</dfn> attribute
</dt>
<dd>
{{HID/onconnect}} is an [=event handler IDL attribute=] for the
<dfn>connect</dfn> event type.
</dd>
<dt>
<dfn>ondisconnect</dfn> attribute
</dt>
<dd>
{{HID/ondisconnect}} is an [=event handler IDL attribute=] for the
<dfn>disconnect</dfn> event type.
</dd>
</dl>
<p>
When a [=HID interface=] becomes available on the system, run the
following steps:
</p>
<div class="note">
<p>
A <dfn>HID interface</dfn> is a logical interface that can be used to
communicate with a device over the HID protocol. Each [=HID
interface=] has exactly one [=report descriptor=] that describes the
set of reports supported by that interface. A single physical device
may expose more than one [=HID interface=]. Devices with multiple
[=HID interfaces=] run these steps once for each interface.
</p>
<p>
For example, USB devices are implemented as a collection of
interfaces, each of which may specify an interface class. When a
device has multiple USB interfaces that use the HID class, each
interface is enumerated as a separate [=HID interface=].
</p>
</div>
<ol>
<li>Let |device:HIDDevice| be a {{HIDDevice}}.
</li>
<li>Set |device|.{{HIDDevice/[[vendorId]]}} to the [=vendor ID=] of the
device that exposes the interface.
</li>
<li>Set |device|.{{HIDDevice/[[productId]]}} to the [=product ID=] of
the device that exposes the interface.
</li>
<li>Set |device|.{{HIDDevice/[[productName]]}} to the [=product name=]
of the device that exposes the interface.
</li>
<li>Set |device|.{{HIDDevice/[[collections]]}} to the result of
[=parsing the Report descriptor=].
</li>
<li>Let |hid:HID| be the result of calling |device|'s [=relevant global
object=]'s {{Navigator}} object's {{Navigator/hid}} getter if |device|'s
[=relevant global object=] is a {{Window}} object, otherwise be the
result of calling |device|'s [=relevant global object=]'s {{WorkerNavigator}}
object's {{WorkerNavigator/hid}} getter.
</li>
<li>[=list/Append=] |device| to |hid|.{{HID/[[devices]]}}.
</li>
<li>If the user has allowed the site to access |device| as the result
of a previous call to {{HID/requestDevice()}}, then [=queue a global
task=] on the [=relevant global object=] of |hid| using the [=HID
device task source=] to [=fire an event=] named {{connect}} at |hid|
using {{HIDConnectionEvent}} with its {{HIDConnectionEvent/device}}
attribute initialized to |device|.
</li>
</ol>
<p>
To <dfn data-lt="parsing the report descriptor">parse the report
descriptor</dfn>, run the following steps:
</p>
<div class="note">
<p>
The system will typically read the [=report descriptor=] [=byte
sequence=] for each [=HID interface=] when a device is first
connected and cache the values in the system registry. If the
[=report descriptor=] is not available but the system provides
information that can be used to reconstruct the layout of each
report, the user agent may use an alternate algorithm instead of
parsing the [=report descriptor=].
</p>
<p>
The [=report descriptor=] format is described in [[USBIF-HID-CLASS]]
section 6.2.2. The algorithm below is intended to implement the same
parsing algorithm described in that section.
</p>
<p>
The [=report descriptor=] contains three classes of items. <dfn>Main
items</dfn> are used to define and group data fields. <dfn>Global
items</dfn> and <dfn>Local items</dfn> describe the data fields.
[=Local items=] define characteristics that only apply to the next
[=Main item=], while [=Global items=] define characteristics that
affect all subsequently defined [=Main items=].
</p>
<p>
Each item has a <dfn>item data field</dfn> containing 0, 1, 2, or 4
bytes of data associated with the item and a <dfn>bSize field</dfn>
that describes how many bytes are used to store the [=item data
field=]. Each item also has a tag that identifies its type.
</p>
</div>
<ol>
<li>Let |items| be the result of converting the [=report descriptor=]
[=byte sequence=] into a [=list=] of [=Main items=], [=Global items=],
and [=Local items=].
</li>
<li>Initialize |collections:sequence<HIDCollectionInfo>| to be an
empty [=sequence=] of {{HIDCollectionInfo}}.
</li>
<li>Initialize |ancestors| to be an empty [=list=].
</li>
<li>Initialize |localState| to be an empty [=ordered map=].
</li>
<li>Initialize |usages| to be an empty [=sequence=] of `unsigned long`.
</li>
<li>Initialize |stringIndices| to be an empty [=list=].
</li>
<li>Initialize |globalStack| to be an empty [=stack=].
</li>
<li>Initialize |initialGlobalState| to be an empty [=ordered map=].
</li>
<li>Initialize |reportId| to 0.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"none"}}.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/unitExponent}}"] to 0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorLengthExponent}}"] to
0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorMassExponent}}"] to 0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorTimeExponent}}"] to 0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorTemperatureExponent}}"]
to 0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorCurrentExponent}}"] to
0.
</li>
<li>Set
|initialGlobalState|["{{HIDReportItem/unitFactorLuminousIntensityExponent}}"]
to 0.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/logicalMinimum}}"] to 0.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/logicalMaximum}}"] to 0.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/physicalMinimum}}"] to 0.
</li>
<li>Set |initialGlobalState|["{{HIDReportItem/physicalMaximum}}"] to 0.
</li>
<li>[=stack/Push=] |initialGlobalState| onto |globalStack|.
</li>
<li>[=list/For each=] |item| of |items|:
<div class="note">
<p>
[=Main items=] are described in [[USBIF-HID-CLASS]] sections
6.2.2.4 through 6.2.2.6. An item with the <dfn>Input tag</dfn>,
<dfn>Output tag</dfn>, or <dfn>Feature tag</dfn> defines a field
within an input, output, or feature report. The [=item data
field=] of a [=Main item=] with an [=Input tag=], [=Output tag=],
or [=Feature tag=] is a bit field containing several boolean
parameters describing the data field. An item with the
<dfn>Collection tag</dfn> defines a new collection nested within
the current collection, or a new [=top-level collection=] if
there is no current collection. The [=item data field=] of a
[=Main item=] with a [=Collection tag=] contains the
<dfn>collection type</dfn>. An item with the <dfn>End Collection
tag</dfn> closes the current collection.
</p>
</div>
<ul>
<li>If |item| is a [=Main item=]:
<ol>
<li>If |item| has the [=Input tag=], [=Output tag=], or
[=Feature tag=]:
<ol>
<li>Let |globalState| be the last [=list/item=] of
|globalStack|.
</li>
<li>Let |reportId:octet| be
|globalState|["{{HIDReportInfo/reportId}}"].
</li>
<li>Let |reportItem:HIDReportItem| be the result of
[=creating a HID report item=] with |item|, |localState|,
|globalState|, |usages|, and |strings|.
</li>
<li>[=list/For each=] |collection:HIDCollectionInfo| of
|ancestors|:
<ol>
<li>Let |reports:sequence<HIDCollectionInfo>| be:
<ul>
<li>
|collection|["{{HIDCollectionInfo/inputReports}}"]
if |item| has the [=Input tag=],
</li>
<li>
|collection|["{{HIDCollectionInfo/outputReports}}"]
if |item| has the [=Output tag=], or
</li>
<li>
|collection|["{{HIDCollectionInfo/featureReports}}"]
if |item| has the [=Feature tag=].
</li>
</ul>
</li>
<li>
<p>
If |reports| contains an item with its
{{HIDReportInfo/reportId}} attribute equal to
|reportId|, then let |report| be that item.
</p>
<p>
Otherwise:
</p>
<ol>
<li>Let |report:HIDReportInfo| be a [=new=]
{{HIDReportInfo}} dictionary.
</li>
<li>Set |report|["{{HIDReportInfo/reportId}}"] to
|reportId|.
</li>
<li>Set |report|["{{HIDReportInfo/items}}"] to an
empty [=sequence=] of {{HIDReportItem}}.
</li>
<li>[=list/Append=] |report| to |reports|.
</li>
</ol>
</li>
<li>[=list/Append=] |item| to
|report|["{{HIDReportInfo/items}}"].
</li>
</ol>
</li>
</ol>
</li>
<li>If |item| has the [=Collection tag=]:
<ol>
<li>Let |collection:HIDCollectionInfo| be a [=new=]
{{HIDCollectionInfo}} dictionary.
</li>
<li>Let |usage| be the first [=list/item=] of |usages|.
</li>
<li>If the [=list/size=] of |usages| is greater than 1,
remove the first [=list/item=] of |usages|.
</li>
<li>Set |collection|["{{HIDCollectionInfo/usagePage}}"] to
|globalState|["{{HIDCollectionInfo/usagePage}}"].
</li>
<li>Set |collection|["{{HIDCollectionInfo/usage}}"] to
|usage|.
</li>
<li>Set |collection|["{{HIDCollectionInfo/type}}"] to the
value representing the collection's type.
</li>
<li>Set |collection|["{{HIDCollectionInfo/children}}"] to
an empty [=sequence=] of {{HIDCollectionInfo}}.
</li>
<li>Set |collection|["{{HIDCollectionInfo/inputReports}}"]
to an empty [=sequence=] of {{HIDReportInfo}}.
</li>
<li>Set |collection|["{{HIDCollectionInfo/outputReports}}"]
to an empty [=sequence=] of {{HIDReportInfo}}.
</li>
<li>Set
|collection|["{{HIDCollectionInfo/featureReports}}"] to an
empty [=sequence=] of {{HIDReportInfo}}.
</li>
<li>
<p>
If |ancestors| [=list/is empty=], [=list/append=]
|collection| to |collections|.
</p>
<p>
Otherwise:
</p>
<ol>
<li>Let |parent:HIDCollectionInfo| be the last
[=list/item=] of |ancestors|.
</li>
<li>[=list/Append=] |collection| to
|parent|["{{HIDCollectionInfo/children}}"].
</li>
</ol>
</li>
<li>[=list/Append=] |collection| to |ancestors|.
</li>
</ol>
</li>
<li>If |item| has the [=End Collection tag=], then
[=list/remove=] the last [=list/item=] of |ancestors|.
</li>
<li>Set |localState| to be an empty [=ordered map=].
</li>
<li>Set |usages| to be an empty [=sequence=] of `unsigned
long`.
<div class="note">
<p>
[=Global items=] are described in section 6.2.2.7. An
item with the <dfn>Usage Page tag</dfn>, <dfn>Logical
Minimum tag</dfn>, <dfn>Logical Maximum tag</dfn>,
<dfn>Physical Minimum tag</dfn>, <dfn>Physical Maximum
tag</dfn>, <dfn>Unit Exponent tag</dfn>, <dfn>Unit
tag</dfn>, <dfn>Report Size tag</dfn>, <dfn>Report ID
tag</dfn>, or <dfn>Report Count tag</dfn> modifies the
respective field of the global item state table. An item
with the <dfn>Push tag</dfn> pushes a copy of the current
global item state table onto the item state stack. An
item with the <dfn>Pop tag</dfn> replaces the global item
state table with the top of the item state stack. The
global Report ID value is not affected when manipulating
the item state stack.
</p>
</div>
</li>
<li>If |item| is a [=Global item=]:
<ol>
<li>Let |globalState| be the last [=list/item=] of
|globalStack|.
</li>
<li>Let |value:unsigned long| be the [=item data field=]
interpreted as an `unsigned long`.
</li>
<li>If |item| has the [=Usage Page tag=], then set
|globalState|["{{HIDCollectionInfo/usagePage}}"] to
|value|.
</li>
<li>If |item| has the [=Logical Minimum tag=], then set
|globalState|["{{HIDReportItem/logicalMinimum}}"] to
|value|.
</li>
<li>If |item| has the [=Logical Maximum tag=], then set
|globalState|["{{HIDReportItem/logicalMaximum}}"] to
|value|.
</li>
<li>If |item| has the [=Physical Minimum tag=], then set
|globalState|["{{HIDReportItem/physicalMinimum}}"] to
|value|.
</li>
<li>If |item| has the [=Physical Maximum tag=], then set
|globalState|["{{HIDReportItem/physicalMaximum}}"] to
|value|.
</li>
<li>If |item| has the [=Report Size tag=], then set
|globalState|["{{HIDReportItem/reportSize}}"] to |value|.
</li>
<li>If |item| has the [=Report ID tag=], then set
|globalState|["{{HIDReportInfo/reportId}}"] to |value|.
</li>
<li>If |item| has the [=Report Count tag=], then set
|globalState|["{{HIDReportItem/reportCount}}"] to |value|.
</li>
<li>If |item| has the [=Unit tag=]:
<ol>
<li>Let |nibbles| be the result of interpreting |value|
as a [=sequence=] of eight 4-bit signed two's
complement integers starting from the low-order bits.
</li>
<li>If |nibbles|[0] is:
<ul>
<li>0, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"none"}}
</li>
<li>1, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"si-linear"}}
</li>
<li>2, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"si-rotation"}}
</li>
<li>3, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"english-linear"}}
</li>
<li>4, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"english-rotation"}}
</li>
<li>-1, then set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"vendor-defined"}}
</li>
<li>Otherwise, set
|globalState|["{{HIDReportItem/unitSystem}}"] to
{{HIDUnitSystem/"reserved"}}
</li>
</ul>
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorLengthExponent}}"]
to |nibbles|[1].
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorMassExponent}}"]
to |nibbles|[2].
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorTimeExponent}}"]
to |nibbles|[3].
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorTemperatureExponent}}"]
to |nibbles|[4].
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorCurrentExponent}}"]
to |nibbles|[5].
</li>
<li>Set
|globalState|["{{HIDReportItem/unitFactorLuminousIntensityExponent}}"]
to |nibbles|[6].
</li>
</ol>
</li>
<li>If |item| has the [=Unit Exponent tag=], then set
|globalState|["{{HIDReportItem/unitExponent}}"] to the
result of interpreting the lowest 4 bits of the [=item data
field=] as a signed two's complement integer.
</li>
<li>If |item| has the [=Push tag=], then [=stack/push=] a
copy of |globalState| onto |globalStack|.
</li>
<li>If |item| has the [=Pop tag=], then [=stack/pop=] from
|globalStack|.
</li>
</ol>
</li>
</ol>
<div class="note">
<p>
[=Local items=] are described in section 6.2.2.8. An item
with the <dfn>Usage tag</dfn>, <dfn>Usage Minimum tag</dfn>,
<dfn>Usage Maximum tag</dfn>, <dfn>String Index tag</dfn>,
<dfn>String Minimum tag</dfn>, or <dfn>String Maximum
tag</dfn> modifies the respective field of the local item
state table. The local item state is cleared after each
[=Main item=].
</p>
</div>
</li>
<li>If |item| is a [=Local item=]:
<ol>
<li>Let |value:unsigned long| be the [=item data field=]
interpreted as an `unsigned long`.
</li>
<li>If |item| has the [=Usage tag=]:
<ol>
<li>If |item|'s [=bSize field=] is 1 or 2:
<ol>
<li>Let |usageId:unsigned short| be an `unsigned short`
containing the lower 16 bits of |value|.
</li>
<li>Let |globalState| be the last [=list/item=] of
|globalStack|.
</li>
<li>Set |value| to a [=HID usage=] composed of
|globalState|["{{HIDCollectionInfo/usagePage}}"] in the
high order bits and |usageId| in the low order bits.
</li>
</ol>
</li>
<li>[=list/Append=] |value| to |usages|.
</li>
</ol>
</li>
<li>If |item| has the [=Usage Minimum tag=], then set
|localState|["{{HIDReportItem/usageMinimum}}"] to |value|.
</li>
<li>If |item| has the [=Usage Maximum tag=], then set
|localState|["{{HIDReportItem/usageMaximum}}"] to |value|.
</li>
<li>If |item| has the [=String Index tag=], then
[=list/append=] |value| to |stringIndices|.
</li>
<li>If |item| has the [=String Minimum tag=], then set
|localState|[`"stringMinimum"`] to |value|.
</li>
<li>If |item| has the [=String Maximum tag=], then set
|localState|[`"stringMaximum"`] to |value|.
</li>
</ol>
</li>
</ul>
</li>
</ol>
<p>
To <dfn data-lt="creating a HID report item">create a HID report
item</dfn> with |item|, |localState|, |globalState|, |usages|, and
|stringIndices|, run the following steps:
</p>
<ol>
<li>Let |reportItem:HIDReportItem| be a [=new=] {{HIDReportItem}}
dictionary.
</li>
<li>Let |value:unsigned long| be the [=item data field=] interpreted as
an `unsigned long`.
</li>
<li>Let |bitfield| be a [=list=] of `boolean` values representing each
of the bits of |value|, starting from the low-order bits.
</li>
<li>Set |reportItem|["{{HIDReportItem/isConstant}}"] to |bitfield|[0].
</li>
<li>Set |reportItem|["{{HIDReportItem/isArray}}"] to !|bitfield|[1].
</li>
<li>Set |reportItem|["{{HIDReportItem/isAbsolute}}"] to !|bitfield|[2].
</li>
<li>Set |reportItem|["{{HIDReportItem/wrap}}"] to |bitfield|[3].
</li>
<li>Set |reportItem|["{{HIDReportItem/isLinear}}"] to !|bitfield|[4].
</li>
<li>Set |reportItem|["{{HIDReportItem/hasPreferredState}}"] to
|bitfield|[5].
</li>
<li>Set |reportItem|["{{HIDReportItem/hasNull}}"] to |bitfield|[6].
</li>
<li>Set |reportItem|["{{HIDReportItem/isVolatile}}"] to |bitfield|[7].
</li>
<li>Set |reportItem|["{{HIDReportItem/isBufferedBytes}}"] to
|bitfield|[8].
</li>
<li>Set |reportItem|["{{HIDReportItem/usages}}"] to |usages|.
</li>
<li>Set |reportItem|["{{HIDReportItem/reportSize}}"] to
|globalState|["{{HIDReportItem/reportSize}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/reportCount}}"] to
|globalState|["{{HIDReportItem/reportCount}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitExponent}}"] to
|globalState|["{{HIDReportItem/unitExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitSystem}}"] to
|globalState|["{{HIDReportItem/unitSystem}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitFactorLengthExponent}}"] to
|globalState|["{{HIDReportItem/unitFactorLengthExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitFactorMassExponent}}"] to
|globalState|["{{HIDReportItem/unitFactorMassExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitFactorTimeExponent}}"] to
|globalState|["{{HIDReportItem/unitFactorTimeExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitFactorTemperatureExponent}}"]
to |globalState|["{{HIDReportItem/unitFactorTemperatureExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/unitFactorCurrentExponent}}"] to
|globalState|["{{HIDReportItem/unitFactorCurrentExponent}}"].
</li>
<li>Set
|reportItem|["{{HIDReportItem/unitFactorLuminousIntensityExponent}}"]
to
|globalState|["{{HIDReportItem/unitFactorLuminousIntensityExponent}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/logicalMinimum}}"] to
|globalState|["{{HIDReportItem/logicalMinimum}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/logicalMaximum}}"] to
|globalState|["{{HIDReportItem/logicalMaximum}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/physicalMinimum}}"] to
|globalState|["{{HIDReportItem/physicalMinimum}}"].
</li>
<li>Set |reportItem|["{{HIDReportItem/physicalMaximum}}"] to
|globalState|["{{HIDReportItem/physicalMaximum}}"].
</li>
<li>
<p>
If "{{HIDReportItem/usageMinimum}}" is in |localState|, then set
|reportItem|["{{HIDReportItem/usageMinimum}}"] to
|localState|["{{HIDReportItem/usageMinimum}}"].
</p>
</li>
<li>
<p>
If "{{HIDReportItem/usageMaximum}}" is in |localState|, then set
|reportItem|["{{HIDReportItem/usageMaximum}}"] to
|localState|["{{HIDReportItem/usageMaximum}}"].
</p>
</li>
<li>Set |reportItem|["{{HIDReportItem/isRange}}"] to `true` if
|reportItem|["{{HIDReportItem/usageMinimum}}"] is less than
|reportItem|["{{HIDReportItem/usageMaximum}}"], or `false` otherwise.
</li>
<li>If `"stringMinimum"` is in |localState| and `"stringMaximum"` is in
|localState|:
<ol>
<li>[=list/Append=] the [=list/item=]s of [=the range=] from
|localState|[`"stringMinimum"`] to |localState|[`"stringMaximum"`]
to |stringIndices|.
</li>
</ol>
</li>
<li>[=list/For each=] |stringIndex| of |stringIndices|:
<ol>
<li>Let |string| be the result of reading the string descriptor at
index |stringIndex|.
</li>
<li>[=list/Append=] |string| to
|reportItem|["{{HIDReportItem/strings}}"].
</li>
</ol>
</li>
<li>Return |reportItem|.
</li>
</ol>
<p>
When a [=HID interface=] becomes unavailable on the system, run the
following steps:
</p>
<ol>
<li>Let |device:HIDDevice| be the {{HIDDevice}} in
|hid|.{{HID/[[devices]]}} representing the interface.
</li>
<li>Let |hid:HID| be the result of calling |device|'s [=relevant global
object=]'s {{Navigator}} object's {{Navigator/hid}} getter if |device|'s
[=relevant global object=] is a {{Window}} object, otherwise be the
result of calling |device|'s [=relevant global object=]'s {{WorkerNavigator}}
object's {{WorkerNavigator/hid}} getter.
</li>
<li>[=list/Remove=] |device| from |hid|.{{HID/[[devices]]}}.
</li>
<li>If the user has allowed the site to access |device| as the result
of a previous call to {{HID/requestDevice()}}, then [=queue a global
task=] on the [=relevant global object=] of |hid| using the [=HID
device task source=] to [=fire an event=] named {{disconnect}} at |hid|
using {{HIDConnectionEvent}} with its {{HIDConnectionEvent/device}}
attribute initialized to |device|.
</li>
</ol>
<section>
<h3>
<dfn>getDevices()</dfn> method
</h3>
<p>
The {{HID/getDevices()}} method steps are:
</p>
<ol>
<li>Let |promise:Promise| be [=a new promise=].
</li>
<li>Let |document:Document| be `null`.</li>
<li>If [=this=]'s [=relevant global object=] is a {{DedicatedWorkerGlobalScope}}
or {{Window/window}} object, set |document| to [=this=]'s
[=relevant global object=]'s [=associated Document=].
</li>
<li>If [=this=]'s [=relevant global object=] is a {{ServiceWorkerGlobalScope}}
object and the associated [=service worker client=] is
[=service worker client/window client=], set |document| to the
[=associated Document=] of the associated [=service worker client=]'s
[=global object=].
</li>
<li>If |document| is `null` or |document| is not [=allowed to use=]
the [=policy-controlled feature=] named `"hid"`, [=reject=]
|promise| with a "{{SecurityError}}" {{DOMException}} and return |promise|.
</li>
<li>Run the following steps [=in parallel=]:
<ol>
<li>Let |devices:sequence<HIDDevice>| be an empty