aboutsummaryrefslogtreecommitdiff
path: root/src/include/ipxe/usb.h
blob: ab060b8f48b4908add3ea094201a24962562379b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
#ifndef _IPXE_USB_H
#define _IPXE_USB_H

/** @file
 *
 * Universal Serial Bus (USB)
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <byteswap.h>
#include <ipxe/list.h>
#include <ipxe/device.h>
#include <ipxe/process.h>
#include <ipxe/iobuf.h>
#include <ipxe/tables.h>

/** USB protocols */
enum usb_protocol {
	/** USB 2.0 */
	USB_PROTO_2_0 = 0x0200,
	/** USB 3.0 */
	USB_PROTO_3_0 = 0x0300,
	/** USB 3.1 */
	USB_PROTO_3_1 = 0x0301,
};

/** Define a USB speed
 *
 * @v mantissa		Mantissa
 * @v exponent		Exponent (in engineering terms: 1=k, 2=M, 3=G)
 * @ret speed		USB speed
 */
#define USB_SPEED( mantissa, exponent ) ( (exponent << 16) | (mantissa) )

/** Extract USB speed mantissa */
#define USB_SPEED_MANTISSA(speed) ( (speed) & 0xffff )

/** Extract USB speed exponent */
#define USB_SPEED_EXPONENT(speed) ( ( (speed) >> 16 ) & 0x3 )

/** USB device speeds */
enum usb_speed {
	/** Not connected */
	USB_SPEED_NONE = 0,
	/** Low speed (1.5Mbps) */
	USB_SPEED_LOW = USB_SPEED ( 1500, 1 ),
	/** Full speed (12Mbps) */
	USB_SPEED_FULL = USB_SPEED ( 12, 2 ),
	/** High speed (480Mbps) */
	USB_SPEED_HIGH = USB_SPEED ( 480, 2 ),
	/** Super speed (5Gbps) */
	USB_SPEED_SUPER = USB_SPEED ( 5, 3 ),
};

/** USB packet IDs */
enum usb_pid {
	/** IN PID */
	USB_PID_IN = 0x69,
	/** OUT PID */
	USB_PID_OUT = 0xe1,
	/** SETUP PID */
	USB_PID_SETUP = 0x2d,
};

/** A USB setup data packet */
struct usb_setup_packet {
	/** Request */
	uint16_t request;
	/** Value paramer */
	uint16_t value;
	/** Index parameter */
	uint16_t index;
	/** Length of data stage */
	uint16_t len;
} __attribute__ (( packed ));

/** Data transfer is from host to device */
#define USB_DIR_OUT ( 0 << 7 )

/** Data transfer is from device to host */
#define USB_DIR_IN ( 1 << 7 )

/** Standard request type */
#define USB_TYPE_STANDARD ( 0 << 5 )

/** Class-specific request type */
#define USB_TYPE_CLASS ( 1 << 5 )

/** Vendor-specific request type */
#define USB_TYPE_VENDOR ( 2 << 5 )

/** Request recipient is the device */
#define USB_RECIP_DEVICE ( 0 << 0 )

/** Request recipient is an interface */
#define USB_RECIP_INTERFACE ( 1 << 0 )

/** Request recipient is an endpoint */
#define USB_RECIP_ENDPOINT ( 2 << 0 )

/** Construct USB request type */
#define USB_REQUEST_TYPE(type) ( (type) << 8 )

/** Get status */
#define USB_GET_STATUS ( USB_DIR_IN | USB_REQUEST_TYPE ( 0 ) )

/** Clear feature */
#define USB_CLEAR_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 1 ) )

/** Set feature */
#define USB_SET_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 3 ) )

/** Set address */
#define USB_SET_ADDRESS ( USB_DIR_OUT | USB_REQUEST_TYPE ( 5 ) )

/** Get descriptor */
#define USB_GET_DESCRIPTOR ( USB_DIR_IN | USB_REQUEST_TYPE ( 6 ) )

/** Set descriptor */
#define USB_SET_DESCRIPTOR ( USB_DIR_OUT | USB_REQUEST_TYPE ( 7 ) )

/** Get configuration */
#define USB_GET_CONFIGURATION ( USB_DIR_IN | USB_REQUEST_TYPE ( 8 ) )

/** Set configuration */
#define USB_SET_CONFIGURATION ( USB_DIR_OUT | USB_REQUEST_TYPE ( 9 ) )

/** Get interface */
#define USB_GET_INTERFACE \
	( USB_DIR_IN | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 10 ) )

/** Set interface */
#define USB_SET_INTERFACE \
	( USB_DIR_OUT | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 11 ) )

/** Endpoint halt feature */
#define USB_ENDPOINT_HALT 0

/** A USB class code tuple */
struct usb_class {
	/** Class code */
	uint8_t class;
	/** Subclass code */
	uint8_t subclass;
	/** Protocol code */
	uint8_t protocol;
} __attribute__ (( packed ));

/** Class code for USB hubs */
#define USB_CLASS_HUB 9

/** A USB descriptor header */
struct usb_descriptor_header {
	/** Length of descriptor */
	uint8_t len;
	/** Descriptor type */
	uint8_t type;
} __attribute__ (( packed ));

/** A USB device descriptor */
struct usb_device_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** USB specification release number in BCD */
	uint16_t protocol;
	/** Device class */
	struct usb_class class;
	/** Maximum packet size for endpoint zero */
	uint8_t mtu;
	/** Vendor ID */
	uint16_t vendor;
	/** Product ID */
	uint16_t product;
	/** Device release number in BCD */
	uint16_t release;
	/** Manufacturer string */
	uint8_t manufacturer;
	/** Product string */
	uint8_t name;
	/** Serial number string */
	uint8_t serial;
	/** Number of possible configurations */
	uint8_t configurations;
} __attribute__ (( packed ));

/** A USB device descriptor */
#define USB_DEVICE_DESCRIPTOR 1

/** A USB configuration descriptor */
struct usb_configuration_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** Total length */
	uint16_t len;
	/** Number of interfaces */
	uint8_t interfaces;
	/** Configuration value */
	uint8_t config;
	/** Configuration string */
	uint8_t name;
	/** Attributes */
	uint8_t attributes;
	/** Maximum power consumption */
	uint8_t power;
} __attribute__ (( packed ));

/** A USB configuration descriptor */
#define USB_CONFIGURATION_DESCRIPTOR 2

/** A USB string descriptor */
struct usb_string_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** String */
	char string[0];
} __attribute__ (( packed ));

/** A USB string descriptor */
#define USB_STRING_DESCRIPTOR 3

/** A USB interface descriptor */
struct usb_interface_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** Interface number */
	uint8_t interface;
	/** Alternate setting */
	uint8_t alternate;
	/** Number of endpoints */
	uint8_t endpoints;
	/** Interface class */
	struct usb_class class;
	/** Interface name */
	uint8_t name;
} __attribute__ (( packed ));

/** A USB interface descriptor */
#define USB_INTERFACE_DESCRIPTOR 4

/** A USB endpoint descriptor */
struct usb_endpoint_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** Endpoint address */
	uint8_t endpoint;
	/** Attributes */
	uint8_t attributes;
	/** Maximum packet size and burst size */
	uint16_t sizes;
	/** Polling interval */
	uint8_t interval;
} __attribute__ (( packed ));

/** A USB endpoint descriptor */
#define USB_ENDPOINT_DESCRIPTOR 5

/** Endpoint attribute transfer type mask */
#define USB_ENDPOINT_ATTR_TYPE_MASK 0x03

/** Endpoint periodic type */
#define USB_ENDPOINT_ATTR_PERIODIC 0x01

/** Control endpoint transfer type */
#define USB_ENDPOINT_ATTR_CONTROL 0x00

/** Bulk endpoint transfer type */
#define USB_ENDPOINT_ATTR_BULK 0x02

/** Interrupt endpoint transfer type */
#define USB_ENDPOINT_ATTR_INTERRUPT 0x03

/** Bulk OUT endpoint (internal) type */
#define USB_BULK_OUT ( USB_ENDPOINT_ATTR_BULK | USB_DIR_OUT )

/** Bulk IN endpoint (internal) type */
#define USB_BULK_IN ( USB_ENDPOINT_ATTR_BULK | USB_DIR_IN )

/** Interrupt IN endpoint (internal) type */
#define USB_INTERRUPT_IN ( USB_ENDPOINT_ATTR_INTERRUPT | USB_DIR_IN )

/** Interrupt OUT endpoint (internal) type */
#define USB_INTERRUPT_OUT ( USB_ENDPOINT_ATTR_INTERRUPT | USB_DIR_OUT )

/** USB endpoint MTU */
#define USB_ENDPOINT_MTU(sizes) ( ( (sizes) >> 0 ) & 0x07ff )

/** USB endpoint maximum burst size */
#define USB_ENDPOINT_BURST(sizes) ( ( (sizes) >> 11 ) & 0x0003 )

/** A USB endpoint companion descriptor */
struct usb_endpoint_companion_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** Maximum burst size */
	uint8_t burst;
	/** Extended attributes */
	uint8_t extended;
	/** Number of bytes per service interval */
	uint16_t periodic;
} __attribute__ (( packed ));

/** A USB endpoint companion descriptor */
#define USB_ENDPOINT_COMPANION_DESCRIPTOR 48

/** A USB interface association descriptor */
struct usb_interface_association_descriptor {
	/** Descriptor header */
	struct usb_descriptor_header header;
	/** First interface number */
	uint8_t first;
	/** Interface count */
	uint8_t count;
	/** Association class */
	struct usb_class class;
	/** Association name */
	uint8_t name;
} __attribute__ (( packed ));

/** A USB interface association descriptor */
#define USB_INTERFACE_ASSOCIATION_DESCRIPTOR 11

/** A class-specific interface descriptor */
#define USB_CS_INTERFACE_DESCRIPTOR 36

/** A class-specific endpoint descriptor */
#define USB_CS_ENDPOINT_DESCRIPTOR 37

/**
 * Get next USB descriptor
 *
 * @v desc		USB descriptor header
 * @ret next		Next USB descriptor header
 */
static inline __attribute__ (( always_inline )) struct usb_descriptor_header *
usb_next_descriptor ( struct usb_descriptor_header *desc ) {

	return ( ( ( void * ) desc ) + desc->len );
}

/**
 * Check that descriptor lies within a configuration descriptor
 *
 * @v config		Configuration descriptor
 * @v desc		Descriptor header
 * @v is_within		Descriptor is within the configuration descriptor
 */
static inline __attribute__ (( always_inline )) int
usb_is_within_config ( struct usb_configuration_descriptor *config,
		       struct usb_descriptor_header *desc ) {
	struct usb_descriptor_header *end =
		( ( ( void * ) config ) + le16_to_cpu ( config->len ) );

	/* Check that descriptor starts within the configuration
	 * descriptor, and that the length does not exceed the
	 * configuration descriptor.  This relies on the fact that
	 * usb_next_descriptor() needs to access only the first byte
	 * of the descriptor in order to determine the length.
	 */
	return ( ( desc < end ) && ( usb_next_descriptor ( desc ) <= end ) );
}

/** Iterate over all configuration descriptors */
#define for_each_config_descriptor( desc, config )			   \
	for ( desc = container_of ( &(config)->header,			   \
				    typeof ( *desc ), header ) ;	   \
	      usb_is_within_config ( (config), &desc->header ) ;	   \
	      desc = container_of ( usb_next_descriptor ( &desc->header ), \
				    typeof ( *desc ), header ) )

/** Iterate over all configuration descriptors within an interface descriptor */
#define for_each_interface_descriptor( desc, config, interface )	   \
	for ( desc = container_of ( usb_next_descriptor ( &(interface)->   \
							  header ),	   \
				    typeof ( *desc ), header ) ;	   \
	      ( usb_is_within_config ( (config), &desc->header ) &&	   \
		( desc->header.type != USB_INTERFACE_DESCRIPTOR ) ) ;	   \
	      desc = container_of ( usb_next_descriptor ( &desc->header ), \
				    typeof ( *desc ), header ) )

/** A USB endpoint */
struct usb_endpoint {
	/** USB device */
	struct usb_device *usb;
	/** Endpoint address */
	unsigned int address;
	/** Attributes */
	unsigned int attributes;
	/** Maximum transfer size */
	size_t mtu;
	/** Maximum burst size */
	unsigned int burst;
	/** Interval (in microframes) */
	unsigned int interval;

	/** Endpoint is open */
	int open;
	/** Buffer fill level */
	unsigned int fill;

	/** List of halted endpoints */
	struct list_head halted;

	/** Host controller operations */
	struct usb_endpoint_host_operations *host;
	/** Host controller private data */
	void *priv;
	/** Driver operations */
	struct usb_endpoint_driver_operations *driver;

	/** Recycled I/O buffer list */
	struct list_head recycled;
	/** Refill buffer length */
	size_t len;
	/** Maximum fill level */
	unsigned int max;
};

/** USB endpoint host controller operations */
struct usb_endpoint_host_operations {
	/** Open endpoint
	 *
	 * @v ep		USB endpoint
	 * @ret rc		Return status code
	 */
	int ( * open ) ( struct usb_endpoint *ep );
	/** Close endpoint
	 *
	 * @v ep		USB endpoint
	 */
	void ( * close ) ( struct usb_endpoint *ep );
	/**
	 * Reset endpoint
	 *
	 * @v ep		USB endpoint
	 * @ret rc		Return status code
	 */
	int ( * reset ) ( struct usb_endpoint *ep );
	/** Update MTU
	 *
	 * @v ep		USB endpoint
	 * @ret rc		Return status code
	 */
	int ( * mtu ) ( struct usb_endpoint *ep );
	/** Enqueue message transfer
	 *
	 * @v ep		USB endpoint
	 * @v iobuf		I/O buffer
	 * @ret rc		Return status code
	 */
	int ( * message ) ( struct usb_endpoint *ep,
			    struct io_buffer *iobuf );
	/** Enqueue stream transfer
	 *
	 * @v ep		USB endpoint
	 * @v iobuf		I/O buffer
	 * @v terminate		Terminate using a short packet
	 * @ret rc		Return status code
	 */
	int ( * stream ) ( struct usb_endpoint *ep, struct io_buffer *iobuf,
			   int terminate );
};

/** USB endpoint driver operations */
struct usb_endpoint_driver_operations {
	/** Complete transfer
	 *
	 * @v ep		USB endpoint
	 * @v iobuf		I/O buffer
	 * @v rc		Completion status code
	 */
	void ( * complete ) ( struct usb_endpoint *ep,
			      struct io_buffer *iobuf, int rc );
};

/** Control endpoint address */
#define USB_EP0_ADDRESS 0x00

/** Control endpoint attributes */
#define USB_EP0_ATTRIBUTES 0x00

/** Calculate default MTU based on device speed
 *
 * @v speed		Device speed
 * @ret mtu		Default MTU
 */
#define USB_EP0_DEFAULT_MTU(speed)			\
	( ( (speed) >= USB_SPEED_SUPER ) ? 512 :	\
	  ( ( (speed) >= USB_SPEED_FULL ) ? 64 : 8 ) )

/** Control endpoint maximum burst size */
#define USB_EP0_BURST 0

/** Control endpoint interval */
#define USB_EP0_INTERVAL 0

/** Maximum endpoint number */
#define USB_ENDPOINT_MAX 0x0f

/** Endpoint direction is in */
#define USB_ENDPOINT_IN 0x80

/** Construct endpoint index from endpoint address */
#define USB_ENDPOINT_IDX(address)					\
	( ( (address) & USB_ENDPOINT_MAX ) |				\
	  ( ( (address) & USB_ENDPOINT_IN ) >> 3 ) )

/**
 * Initialise USB endpoint
 *
 * @v ep		USB endpoint
 * @v usb		USB device
 * @v driver		Driver operations
 */
static inline __attribute__ (( always_inline )) void
usb_endpoint_init ( struct usb_endpoint *ep, struct usb_device *usb,
		    struct usb_endpoint_driver_operations *driver ) {

	ep->usb = usb;
	ep->driver = driver;
}

/**
 * Describe USB endpoint
 *
 * @v ep		USB endpoint
 * @v address		Endpoint address
 * @v attributes	Attributes
 * @v mtu		Maximum packet size
 * @v burst		Maximum burst size
 * @v interval		Interval (in microframes)
 */
static inline __attribute__ (( always_inline )) void
usb_endpoint_describe ( struct usb_endpoint *ep, unsigned int address,
			unsigned int attributes, size_t mtu,
			unsigned int burst, unsigned int interval ) {

	ep->address = address;
	ep->attributes = attributes;
	ep->mtu = mtu;
	ep->burst = burst;
	ep->interval = interval;
}

/**
 * Set USB endpoint host controller private data
 *
 * @v ep		USB endpoint
 * @v priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void
usb_endpoint_set_hostdata ( struct usb_endpoint *ep, void *priv ) {
	ep->priv = priv;
}

/**
 * Get USB endpoint host controller private data
 *
 * @v ep		USB endpoint
 * @ret priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void *
usb_endpoint_get_hostdata ( struct usb_endpoint *ep ) {
	return ep->priv;
}

extern const char * usb_endpoint_name ( struct usb_endpoint *ep );
extern int
usb_endpoint_described ( struct usb_endpoint *ep,
			 struct usb_configuration_descriptor *config,
			 struct usb_interface_descriptor *interface,
			 unsigned int type, unsigned int index );
extern int usb_endpoint_open ( struct usb_endpoint *ep );
extern void usb_endpoint_close ( struct usb_endpoint *ep );
extern int usb_message ( struct usb_endpoint *ep, unsigned int request,
			 unsigned int value, unsigned int index,
			 struct io_buffer *iobuf );
extern int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf,
			int terminate );
extern void usb_complete_err ( struct usb_endpoint *ep,
			       struct io_buffer *iobuf, int rc );

/**
 * Initialise USB endpoint refill
 *
 * @v ep		USB endpoint
 * @v len		Refill buffer length (or zero to use endpoint's MTU)
 * @v max		Maximum fill level
 */
static inline __attribute__ (( always_inline )) void
usb_refill_init ( struct usb_endpoint *ep, size_t len, unsigned int max ) {

	INIT_LIST_HEAD ( &ep->recycled );
	ep->len = len;
	ep->max = max;
}

/**
 * Recycle I/O buffer
 *
 * @v ep		USB endpoint
 * @v iobuf		I/O buffer
 */
static inline __attribute__ (( always_inline )) void
usb_recycle ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {

	list_add_tail ( &iobuf->list, &ep->recycled );
}

extern int usb_prefill ( struct usb_endpoint *ep );
extern int usb_refill ( struct usb_endpoint *ep );
extern void usb_flush ( struct usb_endpoint *ep );

/**
 * A USB function
 *
 * A USB function represents an association of interfaces within a USB
 * device.
 */
struct usb_function {
	/** Name */
	const char *name;
	/** USB device */
	struct usb_device *usb;
	/** Class */
	struct usb_class class;
	/** Number of interfaces */
	unsigned int count;
	/** Generic device */
	struct device dev;
	/** List of functions within this USB device */
	struct list_head list;

	/** Driver */
	struct usb_driver *driver;
	/** Driver private data */
	void *priv;

	/** List of interface numbers
	 *
	 * This must be the last field within the structure.
	 */
	uint8_t interface[0];
};

/**
 * Set USB function driver private data
 *
 * @v func		USB function
 * @v priv		Driver private data
 */
static inline __attribute__ (( always_inline )) void
usb_func_set_drvdata ( struct usb_function *func, void *priv ) {
	func->priv = priv;
}

/**
 * Get USB function driver private data
 *
 * @v function		USB function
 * @ret priv		Driver private data
 */
static inline __attribute__ (( always_inline )) void *
usb_func_get_drvdata ( struct usb_function *func ) {
	return func->priv;
}

/** A USB device */
struct usb_device {
	/** Name */
	char name[32];
	/** USB port */
	struct usb_port *port;
	/** List of devices on this bus */
	struct list_head list;
	/** Device address, if assigned */
	unsigned int address;
	/** Device descriptor */
	struct usb_device_descriptor device;
	/** List of functions */
	struct list_head functions;

	/** Host controller operations */
	struct usb_device_host_operations *host;
	/** Host controller private data */
	void *priv;

	/** Endpoint list */
	struct usb_endpoint *ep[32];

	/** Control endpoint */
	struct usb_endpoint control;
	/** Completed control transfers */
	struct list_head complete;
};

/** USB device host controller operations */
struct usb_device_host_operations {
	/** Open device
	 *
	 * @v usb		USB device
	 * @ret rc		Return status code
	 */
	int ( * open ) ( struct usb_device *usb );
	/** Close device
	 *
	 * @v usb		USB device
	 */
	void ( * close ) ( struct usb_device *usb );
	/** Assign device address
	 *
	 * @v usb		USB device
	 * @ret rc		Return status code
	 */
	int ( * address ) ( struct usb_device *usb );
};

/**
 * Set USB device host controller private data
 *
 * @v usb		USB device
 * @v priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void
usb_set_hostdata ( struct usb_device *usb, void *priv ) {
	usb->priv = priv;
}

/**
 * Get USB device host controller private data
 *
 * @v usb		USB device
 * @ret priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void *
usb_get_hostdata ( struct usb_device *usb ) {
	return usb->priv;
}

/**
 * Get USB endpoint
 *
 * @v usb		USB device
 * @v address		Endpoint address
 * @ret ep		USB endpoint, or NULL if not opened
 */
static inline struct usb_endpoint * usb_endpoint ( struct usb_device *usb,
						   unsigned int address ) {

	return usb->ep[ USB_ENDPOINT_IDX ( address ) ];
}

/** A USB port */
struct usb_port {
	/** USB hub */
	struct usb_hub *hub;
	/** Port address */
	unsigned int address;
	/** Port protocol */
	unsigned int protocol;
	/** Port speed */
	unsigned int speed;
	/** Port disconnection has been detected
	 *
	 * This should be set whenever the underlying hardware reports
	 * a connection status change.
	 */
	int disconnected;
	/** Port has an attached device */
	int attached;
	/** Currently attached device (if in use)
	 *
	 * Note that this field will be NULL if the attached device
	 * has been freed (e.g. because there were no drivers found).
	 */
	struct usb_device *usb;
	/** List of changed ports */
	struct list_head changed;
};

/** A USB hub */
struct usb_hub {
	/** Name */
	const char *name;
	/** USB bus */
	struct usb_bus *bus;
	/** Underlying USB device, if any */
	struct usb_device *usb;
	/** Hub protocol */
	unsigned int protocol;
	/** Number of ports */
	unsigned int ports;

	/** List of hubs */
	struct list_head list;

	/** Host controller operations */
	struct usb_hub_host_operations *host;
	/** Driver operations */
	struct usb_hub_driver_operations *driver;
	/** Driver private data */
	void *priv;

	/** Port list
	 *
	 * This must be the last field within the structure.
	 */
	struct usb_port port[0];
};

/** USB hub host controller operations */
struct usb_hub_host_operations {
	/** Open hub
	 *
	 * @v hub		USB hub
	 * @ret rc		Return status code
	 */
	int ( * open ) ( struct usb_hub *hub );
	/** Close hub
	 *
	 * @v hub		USB hub
	 */
	void ( * close ) ( struct usb_hub *hub );
};

/** USB hub driver operations */
struct usb_hub_driver_operations {
	/** Open hub
	 *
	 * @v hub		USB hub
	 * @ret rc		Return status code
	 */
	int ( * open ) ( struct usb_hub *hub );
	/** Close hub
	 *
	 * @v hub		USB hub
	 */
	void ( * close ) ( struct usb_hub *hub );
	/** Enable port
	 *
	 * @v hub		USB hub
	 * @v port		USB port
	 * @ret rc		Return status code
	 */
	int ( * enable ) ( struct usb_hub *hub, struct usb_port *port );
	/** Disable port
	 *
	 * @v hub		USB hub
	 * @v port		USB port
	 * @ret rc		Return status code
	 */
	int ( * disable ) ( struct usb_hub *hub, struct usb_port *port );
	/** Update port speed
	 *
	 * @v hub		USB hub
	 * @v port		USB port
	 * @ret rc		Return status code
	 */
	int ( * speed ) ( struct usb_hub *hub, struct usb_port *port );
	/** Clear transaction translator buffer
	 *
	 * @v hub		USB hub
	 * @v port		USB port
	 * @v ep		USB endpoint
	 * @ret rc		Return status code
	 */
	int ( * clear_tt ) ( struct usb_hub *hub, struct usb_port *port,
			     struct usb_endpoint *ep );
};

/**
 * Set USB hub driver private data
 *
 * @v hub		USB hub
 * @v priv		Driver private data
 */
static inline __attribute__ (( always_inline )) void
usb_hub_set_drvdata ( struct usb_hub *hub, void *priv ) {
	hub->priv = priv;
}

/**
 * Get USB hub driver private data
 *
 * @v hub		USB hub
 * @ret priv		Driver private data
 */
static inline __attribute__ (( always_inline )) void *
usb_hub_get_drvdata ( struct usb_hub *hub ) {
	return hub->priv;
}

/**
 * Get USB port
 *
 * @v hub		USB hub
 * @v address		Port address
 * @ret port		USB port
 */
static inline __attribute__ (( always_inline )) struct usb_port *
usb_port ( struct usb_hub *hub, unsigned int address ) {

	return &hub->port[ address - 1 ];
}

/** A USB bus */
struct usb_bus {
	/** Name */
	const char *name;
	/** Underlying hardware device */
	struct device *dev;
	/** Host controller operations set */
	struct usb_host_operations *op;

	/** Largest transfer allowed on the bus */
	size_t mtu;
	/** Address in-use mask
	 *
	 * This is used only by buses which perform manual address
	 * assignment.  USB allows for addresses in the range [1,127].
	 * We use a simple bitmask which restricts us to the range
	 * [1,64]; this is unlikely to be a problem in practice.  For
	 * comparison: controllers which perform autonomous address
	 * assignment (such as xHCI) typically allow for only 32
	 * devices per bus anyway.
	 */
	unsigned long long addresses;

	/** Root hub */
	struct usb_hub *hub;

	/** List of USB buses */
	struct list_head list;
	/** List of devices */
	struct list_head devices;
	/** List of hubs */
	struct list_head hubs;

	/** Host controller operations */
	struct usb_bus_host_operations *host;
	/** Host controller private data */
	void *priv;
};

/** USB bus host controller operations */
struct usb_bus_host_operations {
	/** Open bus
	 *
	 * @v bus		USB bus
	 * @ret rc		Return status code
	 */
	int ( * open ) ( struct usb_bus *bus );
	/** Close bus
	 *
	 * @v bus		USB bus
	 */
	void ( * close ) ( struct usb_bus *bus );
	/** Poll bus
	 *
	 * @v bus		USB bus
	 */
	void ( * poll ) ( struct usb_bus *bus );
};

/** USB host controller operations */
struct usb_host_operations {
	/** Endpoint operations */
	struct usb_endpoint_host_operations endpoint;
	/** Device operations */
	struct usb_device_host_operations device;
	/** Bus operations */
	struct usb_bus_host_operations bus;
	/** Hub operations */
	struct usb_hub_host_operations hub;
	/** Root hub operations */
	struct usb_hub_driver_operations root;
};

/**
 * Set USB bus host controller private data
 *
 * @v bus		USB bus
 * @v priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void
usb_bus_set_hostdata ( struct usb_bus *bus, void *priv ) {
	bus->priv = priv;
}

/**
 * Get USB bus host controller private data
 *
 * @v bus		USB bus
 * @ret priv		Host controller private data
 */
static inline __attribute__ (( always_inline )) void *
usb_bus_get_hostdata ( struct usb_bus *bus ) {
	return bus->priv;
}

/**
 * Poll USB bus
 *
 * @v bus		USB bus
 */
static inline __attribute__ (( always_inline )) void
usb_poll ( struct usb_bus *bus ) {
	bus->host->poll ( bus );
}

/** Iterate over all USB buses */
#define for_each_usb_bus( bus ) \
	list_for_each_entry ( (bus), &usb_buses, list )

/**
 * Complete transfer (without error)
 *
 * @v ep		USB endpoint
 * @v iobuf		I/O buffer
 */
static inline __attribute__ (( always_inline )) void
usb_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
	usb_complete_err ( ep, iobuf, 0 );
}

extern int usb_control ( struct usb_device *usb, unsigned int request,
			 unsigned int value, unsigned int index, void *data,
			 size_t len );
extern int usb_get_string_descriptor ( struct usb_device *usb,
				       unsigned int index,
				       unsigned int language,
				       char *buf, size_t len );

/**
 * Get status
 *
 * @v usb		USB device
 * @v type		Request type
 * @v index		Target index
 * @v data		Status to fill in
 * @v len		Length of status descriptor
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_get_status ( struct usb_device *usb, unsigned int type, unsigned int index,
		 void *data, size_t len ) {

	return usb_control ( usb, ( USB_GET_STATUS | type ), 0, index,
			     data, len );
}

/**
 * Clear feature
 *
 * @v usb		USB device
 * @v type		Request type
 * @v feature		Feature selector
 * @v index		Target index
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_clear_feature ( struct usb_device *usb, unsigned int type,
		    unsigned int feature, unsigned int index ) {

	return usb_control ( usb, ( USB_CLEAR_FEATURE | type ),
			     feature, index, NULL, 0 );
}

/**
 * Set feature
 *
 * @v usb		USB device
 * @v type		Request type
 * @v feature		Feature selector
 * @v index		Target index
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_set_feature ( struct usb_device *usb, unsigned int type,
		  unsigned int feature, unsigned int index ) {

	return usb_control ( usb, ( USB_SET_FEATURE | type ),
			     feature, index, NULL, 0 );
}

/**
 * Set address
 *
 * @v usb		USB device
 * @v address		Device address
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_set_address ( struct usb_device *usb, unsigned int address ) {

	return usb_control ( usb, USB_SET_ADDRESS, address, 0, NULL, 0 );
}

/**
 * Get USB descriptor
 *
 * @v usb		USB device
 * @v type		Request type
 * @v desc		Descriptor type
 * @v index		Descriptor index
 * @v language		Language ID (for string descriptors)
 * @v data		Descriptor to fill in
 * @v len		Maximum length of descriptor
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_get_descriptor ( struct usb_device *usb, unsigned int type,
		     unsigned int desc, unsigned int index,
		     unsigned int language, struct usb_descriptor_header *data,
		     size_t len ) {

	return usb_control ( usb, ( USB_GET_DESCRIPTOR | type ),
			     ( ( desc << 8 ) | index ), language, data, len );
}

/**
 * Get first part of USB device descriptor (up to and including MTU)
 *
 * @v usb		USB device
 * @v data		Device descriptor to (partially) fill in
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_get_mtu ( struct usb_device *usb, struct usb_device_descriptor *data ) {

	return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
				    &data->header,
				    ( offsetof ( typeof ( *data ), mtu ) +
				      sizeof ( data->mtu ) ) );
}

/**
 * Get USB device descriptor
 *
 * @v usb		USB device
 * @v data		Device descriptor to fill in
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_get_device_descriptor ( struct usb_device *usb,
			    struct usb_device_descriptor *data ) {

	return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
				    &data->header, sizeof ( *data ) );
}

/**
 * Get USB configuration descriptor
 *
 * @v usb		USB device
 * @v index		Configuration index
 * @v data		Configuration descriptor to fill in
 * @ret rc		Return status code
 */
static inline __attribute (( always_inline )) int
usb_get_config_descriptor ( struct usb_device *usb, unsigned int index,
			    struct usb_configuration_descriptor *data,
			    size_t len ) {

	return usb_get_descriptor ( usb, 0, USB_CONFIGURATION_DESCRIPTOR, index,
				    0, &data->header, len );
}

/**
 * Set USB configuration
 *
 * @v usb		USB device
 * @v index		Configuration index
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_set_configuration ( struct usb_device *usb, unsigned int index ) {

	return usb_control ( usb, USB_SET_CONFIGURATION, index, 0, NULL, 0 );
}

/**
 * Set USB interface alternate setting
 *
 * @v usb		USB device
 * @v interface		Interface number
 * @v alternate		Alternate setting
 * @ret rc		Return status code
 */
static inline __attribute__ (( always_inline )) int
usb_set_interface ( struct usb_device *usb, unsigned int interface,
		    unsigned int alternate ) {

	return usb_control ( usb, USB_SET_INTERFACE, alternate, interface,
			     NULL, 0 );
}

extern struct list_head usb_buses;

extern struct usb_interface_descriptor *
usb_interface_descriptor ( struct usb_configuration_descriptor *config,
			   unsigned int interface, unsigned int alternate );
extern struct usb_endpoint_descriptor *
usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
			  struct usb_interface_descriptor *interface,
			  unsigned int type, unsigned int index );
extern struct usb_endpoint_companion_descriptor *
usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
				    struct usb_endpoint_descriptor *desc );

extern struct usb_hub * alloc_usb_hub ( struct usb_bus *bus,
					struct usb_device *usb,
					unsigned int ports,
					struct usb_hub_driver_operations *op );
extern int register_usb_hub ( struct usb_hub *hub );
extern void unregister_usb_hub ( struct usb_hub *hub );
extern void free_usb_hub ( struct usb_hub *hub );

extern void usb_port_changed ( struct usb_port *port );

extern struct usb_bus * alloc_usb_bus ( struct device *dev,
					unsigned int ports, size_t mtu,
					struct usb_host_operations *op );
extern int register_usb_bus ( struct usb_bus *bus );
extern void unregister_usb_bus ( struct usb_bus *bus );
extern void free_usb_bus ( struct usb_bus *bus );
extern struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
						   unsigned int location );

extern int usb_alloc_address ( struct usb_bus *bus );
extern void usb_free_address ( struct usb_bus *bus, unsigned int address );
extern unsigned int usb_route_string ( struct usb_device *usb );
extern unsigned int usb_depth ( struct usb_device *usb );
extern struct usb_port * usb_root_hub_port ( struct usb_device *usb );
extern struct usb_port * usb_transaction_translator ( struct usb_device *usb );

/** Minimum reset time
 *
 * Section 7.1.7.5 of the USB2 specification states that root hub
 * ports should assert reset signalling for at least 50ms.
 */
#define USB_RESET_DELAY_MS 50

/** Reset recovery time
 *
 * Section 9.2.6.2 of the USB2 specification states that the
 * "recovery" interval after a port reset is 10ms.
 */
#define USB_RESET_RECOVER_DELAY_MS 10

/** Maximum time to wait for a control transaction to complete
 *
 * Section 9.2.6.1 of the USB2 specification states that the upper
 * limit for commands to be processed is 5 seconds.
 */
#define USB_CONTROL_MAX_WAIT_MS 5000

/** Set address recovery time
 *
 * Section 9.2.6.3 of the USB2 specification states that devices are
 * allowed a 2ms recovery interval after receiving a new address.
 */
#define USB_SET_ADDRESS_RECOVER_DELAY_MS 2

/** Time to wait for ports to stabilise
 *
 * Section 7.1.7.3 of the USB specification states that we must allow
 * 100ms for devices to signal attachment, and an additional 100ms for
 * connection debouncing.  (This delay is parallelised across all
 * ports on a hub; we do not delay separately for each port.)
 */
#define USB_PORT_DELAY_MS 200

/** A USB device ID */
struct usb_device_id {
	/** Name */
	const char *name;
	/** Vendor ID */
	uint16_t vendor;
	/** Product ID */
	uint16_t product;
	/** Class */
	struct usb_class class;
};

/** Match-anything ID */
#define USB_ANY_ID 0xffff

/** A USB driver */
struct usb_driver {
	/** USB ID table */
	struct usb_device_id *ids;
	/** Number of entries in ID table */
	unsigned int id_count;
	/**
	 * Probe device
	 *
	 * @v func		USB function
	 * @v config		Configuration descriptor
	 * @ret rc		Return status code
	 */
	int ( * probe ) ( struct usb_function *func,
			  struct usb_configuration_descriptor *config );
	/**
	 * Remove device
	 *
	 * @v func		USB function
	 */
	void ( * remove ) ( struct usb_function *func );
};

/** USB driver table */
#define USB_DRIVERS __table ( struct usb_driver, "usb_drivers" )

/** Declare a USB driver */
#define __usb_driver __table_entry ( USB_DRIVERS, 01 )

#endif /* _IPXE_USB_H */