You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb.h 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. #ifndef _IPXE_USB_H
  2. #define _IPXE_USB_H
  3. /** @file
  4. *
  5. * Universal Serial Bus (USB)
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <byteswap.h>
  10. #include <ipxe/list.h>
  11. #include <ipxe/device.h>
  12. #include <ipxe/process.h>
  13. #include <ipxe/iobuf.h>
  14. #include <ipxe/tables.h>
  15. /** USB protocols */
  16. enum usb_protocol {
  17. /** USB 2.0 */
  18. USB_PROTO_2_0 = 0x0200,
  19. /** USB 3.0 */
  20. USB_PROTO_3_0 = 0x0300,
  21. /** USB 3.1 */
  22. USB_PROTO_3_1 = 0x0301,
  23. };
  24. /** Define a USB speed
  25. *
  26. * @v mantissa Mantissa
  27. * @v exponent Exponent (in engineering terms: 1=k, 2=M, 3=G)
  28. * @ret speed USB speed
  29. */
  30. #define USB_SPEED( mantissa, exponent ) ( (exponent << 16) | (mantissa) )
  31. /** Extract USB speed mantissa */
  32. #define USB_SPEED_MANTISSA(speed) ( (speed) & 0xffff )
  33. /** Extract USB speed exponent */
  34. #define USB_SPEED_EXPONENT(speed) ( ( (speed) >> 16 ) & 0x3 )
  35. /** USB device speeds */
  36. enum usb_speed {
  37. /** Not connected */
  38. USB_SPEED_NONE = 0,
  39. /** Low speed (1.5Mbps) */
  40. USB_SPEED_LOW = USB_SPEED ( 1500, 1 ),
  41. /** Full speed (12Mbps) */
  42. USB_SPEED_FULL = USB_SPEED ( 12, 2 ),
  43. /** High speed (480Mbps) */
  44. USB_SPEED_HIGH = USB_SPEED ( 480, 2 ),
  45. /** Super speed (5Gbps) */
  46. USB_SPEED_SUPER = USB_SPEED ( 5, 3 ),
  47. };
  48. /** USB packet IDs */
  49. enum usb_pid {
  50. /** IN PID */
  51. USB_PID_IN = 0x69,
  52. /** OUT PID */
  53. USB_PID_OUT = 0xe1,
  54. /** SETUP PID */
  55. USB_PID_SETUP = 0x2d,
  56. };
  57. /** A USB setup data packet */
  58. struct usb_setup_packet {
  59. /** Request */
  60. uint16_t request;
  61. /** Value parameter */
  62. uint16_t value;
  63. /** Index parameter */
  64. uint16_t index;
  65. /** Length of data stage */
  66. uint16_t len;
  67. } __attribute__ (( packed ));
  68. /** Data transfer is from host to device */
  69. #define USB_DIR_OUT ( 0 << 7 )
  70. /** Data transfer is from device to host */
  71. #define USB_DIR_IN ( 1 << 7 )
  72. /** Standard request type */
  73. #define USB_TYPE_STANDARD ( 0 << 5 )
  74. /** Class-specific request type */
  75. #define USB_TYPE_CLASS ( 1 << 5 )
  76. /** Vendor-specific request type */
  77. #define USB_TYPE_VENDOR ( 2 << 5 )
  78. /** Request recipient mask */
  79. #define USB_RECIP_MASK ( 0x1f << 0 )
  80. /** Request recipient is the device */
  81. #define USB_RECIP_DEVICE ( 0 << 0 )
  82. /** Request recipient is an interface */
  83. #define USB_RECIP_INTERFACE ( 1 << 0 )
  84. /** Request recipient is an endpoint */
  85. #define USB_RECIP_ENDPOINT ( 2 << 0 )
  86. /** Construct USB request type */
  87. #define USB_REQUEST_TYPE(type) ( (type) << 8 )
  88. /** Get status */
  89. #define USB_GET_STATUS ( USB_DIR_IN | USB_REQUEST_TYPE ( 0 ) )
  90. /** Clear feature */
  91. #define USB_CLEAR_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 1 ) )
  92. /** Set feature */
  93. #define USB_SET_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 3 ) )
  94. /** Set address */
  95. #define USB_SET_ADDRESS ( USB_DIR_OUT | USB_REQUEST_TYPE ( 5 ) )
  96. /** Get descriptor */
  97. #define USB_GET_DESCRIPTOR ( USB_DIR_IN | USB_REQUEST_TYPE ( 6 ) )
  98. /** Set descriptor */
  99. #define USB_SET_DESCRIPTOR ( USB_DIR_OUT | USB_REQUEST_TYPE ( 7 ) )
  100. /** Get configuration */
  101. #define USB_GET_CONFIGURATION ( USB_DIR_IN | USB_REQUEST_TYPE ( 8 ) )
  102. /** Set configuration */
  103. #define USB_SET_CONFIGURATION ( USB_DIR_OUT | USB_REQUEST_TYPE ( 9 ) )
  104. /** Get interface */
  105. #define USB_GET_INTERFACE \
  106. ( USB_DIR_IN | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 10 ) )
  107. /** Set interface */
  108. #define USB_SET_INTERFACE \
  109. ( USB_DIR_OUT | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 11 ) )
  110. /** Endpoint halt feature */
  111. #define USB_ENDPOINT_HALT 0
  112. /** A USB class code tuple */
  113. struct usb_class {
  114. /** Class code */
  115. uint8_t class;
  116. /** Subclass code */
  117. uint8_t subclass;
  118. /** Protocol code */
  119. uint8_t protocol;
  120. } __attribute__ (( packed ));
  121. /** Class code for USB hubs */
  122. #define USB_CLASS_HUB 9
  123. /** A USB descriptor header */
  124. struct usb_descriptor_header {
  125. /** Length of descriptor */
  126. uint8_t len;
  127. /** Descriptor type */
  128. uint8_t type;
  129. } __attribute__ (( packed ));
  130. /** A USB device descriptor */
  131. struct usb_device_descriptor {
  132. /** Descriptor header */
  133. struct usb_descriptor_header header;
  134. /** USB specification release number in BCD */
  135. uint16_t protocol;
  136. /** Device class */
  137. struct usb_class class;
  138. /** Maximum packet size for endpoint zero */
  139. uint8_t mtu;
  140. /** Vendor ID */
  141. uint16_t vendor;
  142. /** Product ID */
  143. uint16_t product;
  144. /** Device release number in BCD */
  145. uint16_t release;
  146. /** Manufacturer string */
  147. uint8_t manufacturer;
  148. /** Product string */
  149. uint8_t name;
  150. /** Serial number string */
  151. uint8_t serial;
  152. /** Number of possible configurations */
  153. uint8_t configurations;
  154. } __attribute__ (( packed ));
  155. /** A USB device descriptor */
  156. #define USB_DEVICE_DESCRIPTOR 1
  157. /** A USB configuration descriptor */
  158. struct usb_configuration_descriptor {
  159. /** Descriptor header */
  160. struct usb_descriptor_header header;
  161. /** Total length */
  162. uint16_t len;
  163. /** Number of interfaces */
  164. uint8_t interfaces;
  165. /** Configuration value */
  166. uint8_t config;
  167. /** Configuration string */
  168. uint8_t name;
  169. /** Attributes */
  170. uint8_t attributes;
  171. /** Maximum power consumption */
  172. uint8_t power;
  173. } __attribute__ (( packed ));
  174. /** A USB configuration descriptor */
  175. #define USB_CONFIGURATION_DESCRIPTOR 2
  176. /** A USB string descriptor */
  177. struct usb_string_descriptor {
  178. /** Descriptor header */
  179. struct usb_descriptor_header header;
  180. /** String */
  181. char string[0];
  182. } __attribute__ (( packed ));
  183. /** A USB string descriptor */
  184. #define USB_STRING_DESCRIPTOR 3
  185. /** Language ID for English */
  186. #define USB_LANG_ENGLISH 0x0409
  187. /** A USB interface descriptor */
  188. struct usb_interface_descriptor {
  189. /** Descriptor header */
  190. struct usb_descriptor_header header;
  191. /** Interface number */
  192. uint8_t interface;
  193. /** Alternate setting */
  194. uint8_t alternate;
  195. /** Number of endpoints */
  196. uint8_t endpoints;
  197. /** Interface class */
  198. struct usb_class class;
  199. /** Interface name */
  200. uint8_t name;
  201. } __attribute__ (( packed ));
  202. /** A USB interface descriptor */
  203. #define USB_INTERFACE_DESCRIPTOR 4
  204. /** A USB endpoint descriptor */
  205. struct usb_endpoint_descriptor {
  206. /** Descriptor header */
  207. struct usb_descriptor_header header;
  208. /** Endpoint address */
  209. uint8_t endpoint;
  210. /** Attributes */
  211. uint8_t attributes;
  212. /** Maximum packet size and burst size */
  213. uint16_t sizes;
  214. /** Polling interval */
  215. uint8_t interval;
  216. } __attribute__ (( packed ));
  217. /** A USB endpoint descriptor */
  218. #define USB_ENDPOINT_DESCRIPTOR 5
  219. /** Endpoint attribute transfer type mask */
  220. #define USB_ENDPOINT_ATTR_TYPE_MASK 0x03
  221. /** Endpoint periodic type */
  222. #define USB_ENDPOINT_ATTR_PERIODIC 0x01
  223. /** Control endpoint transfer type */
  224. #define USB_ENDPOINT_ATTR_CONTROL 0x00
  225. /** Bulk endpoint transfer type */
  226. #define USB_ENDPOINT_ATTR_BULK 0x02
  227. /** Interrupt endpoint transfer type */
  228. #define USB_ENDPOINT_ATTR_INTERRUPT 0x03
  229. /** Bulk OUT endpoint (internal) type */
  230. #define USB_BULK_OUT ( USB_ENDPOINT_ATTR_BULK | USB_DIR_OUT )
  231. /** Bulk IN endpoint (internal) type */
  232. #define USB_BULK_IN ( USB_ENDPOINT_ATTR_BULK | USB_DIR_IN )
  233. /** Interrupt IN endpoint (internal) type */
  234. #define USB_INTERRUPT_IN ( USB_ENDPOINT_ATTR_INTERRUPT | USB_DIR_IN )
  235. /** Interrupt OUT endpoint (internal) type */
  236. #define USB_INTERRUPT_OUT ( USB_ENDPOINT_ATTR_INTERRUPT | USB_DIR_OUT )
  237. /** USB endpoint MTU */
  238. #define USB_ENDPOINT_MTU(sizes) ( ( (sizes) >> 0 ) & 0x07ff )
  239. /** USB endpoint maximum burst size */
  240. #define USB_ENDPOINT_BURST(sizes) ( ( (sizes) >> 11 ) & 0x0003 )
  241. /** A USB endpoint companion descriptor */
  242. struct usb_endpoint_companion_descriptor {
  243. /** Descriptor header */
  244. struct usb_descriptor_header header;
  245. /** Maximum burst size */
  246. uint8_t burst;
  247. /** Extended attributes */
  248. uint8_t extended;
  249. /** Number of bytes per service interval */
  250. uint16_t periodic;
  251. } __attribute__ (( packed ));
  252. /** A USB endpoint companion descriptor */
  253. #define USB_ENDPOINT_COMPANION_DESCRIPTOR 48
  254. /** A USB interface association descriptor */
  255. struct usb_interface_association_descriptor {
  256. /** Descriptor header */
  257. struct usb_descriptor_header header;
  258. /** First interface number */
  259. uint8_t first;
  260. /** Interface count */
  261. uint8_t count;
  262. /** Association class */
  263. struct usb_class class;
  264. /** Association name */
  265. uint8_t name;
  266. } __attribute__ (( packed ));
  267. /** A USB interface association descriptor */
  268. #define USB_INTERFACE_ASSOCIATION_DESCRIPTOR 11
  269. /** A class-specific interface descriptor */
  270. #define USB_CS_INTERFACE_DESCRIPTOR 36
  271. /** A class-specific endpoint descriptor */
  272. #define USB_CS_ENDPOINT_DESCRIPTOR 37
  273. /**
  274. * Get next USB descriptor
  275. *
  276. * @v desc USB descriptor header
  277. * @ret next Next USB descriptor header
  278. */
  279. static inline __attribute__ (( always_inline )) struct usb_descriptor_header *
  280. usb_next_descriptor ( struct usb_descriptor_header *desc ) {
  281. return ( ( ( void * ) desc ) + desc->len );
  282. }
  283. /**
  284. * Check that descriptor lies within a configuration descriptor
  285. *
  286. * @v config Configuration descriptor
  287. * @v desc Descriptor header
  288. * @v is_within Descriptor is within the configuration descriptor
  289. */
  290. static inline __attribute__ (( always_inline )) int
  291. usb_is_within_config ( struct usb_configuration_descriptor *config,
  292. struct usb_descriptor_header *desc ) {
  293. struct usb_descriptor_header *end =
  294. ( ( ( void * ) config ) + le16_to_cpu ( config->len ) );
  295. /* Check that descriptor starts within the configuration
  296. * descriptor, and that the length does not exceed the
  297. * configuration descriptor. This relies on the fact that
  298. * usb_next_descriptor() needs to access only the first byte
  299. * of the descriptor in order to determine the length.
  300. */
  301. return ( ( desc < end ) && ( usb_next_descriptor ( desc ) <= end ) );
  302. }
  303. /** Iterate over all configuration descriptors */
  304. #define for_each_config_descriptor( desc, config ) \
  305. for ( desc = container_of ( &(config)->header, \
  306. typeof ( *desc ), header ) ; \
  307. usb_is_within_config ( (config), &desc->header ) ; \
  308. desc = container_of ( usb_next_descriptor ( &desc->header ), \
  309. typeof ( *desc ), header ) )
  310. /** Iterate over all configuration descriptors within an interface descriptor */
  311. #define for_each_interface_descriptor( desc, config, interface ) \
  312. for ( desc = container_of ( usb_next_descriptor ( &(interface)-> \
  313. header ), \
  314. typeof ( *desc ), header ) ; \
  315. ( usb_is_within_config ( (config), &desc->header ) && \
  316. ( desc->header.type != USB_INTERFACE_DESCRIPTOR ) ) ; \
  317. desc = container_of ( usb_next_descriptor ( &desc->header ), \
  318. typeof ( *desc ), header ) )
  319. /** A USB endpoint */
  320. struct usb_endpoint {
  321. /** USB device */
  322. struct usb_device *usb;
  323. /** Endpoint address */
  324. unsigned int address;
  325. /** Attributes */
  326. unsigned int attributes;
  327. /** Maximum transfer size */
  328. size_t mtu;
  329. /** Maximum burst size */
  330. unsigned int burst;
  331. /** Interval (in microframes) */
  332. unsigned int interval;
  333. /** Endpoint is open */
  334. int open;
  335. /** Buffer fill level */
  336. unsigned int fill;
  337. /** List of halted endpoints */
  338. struct list_head halted;
  339. /** Host controller operations */
  340. struct usb_endpoint_host_operations *host;
  341. /** Host controller private data */
  342. void *priv;
  343. /** Driver operations */
  344. struct usb_endpoint_driver_operations *driver;
  345. /** Recycled I/O buffer list */
  346. struct list_head recycled;
  347. /** Refill buffer reserved header length */
  348. size_t reserve;
  349. /** Refill buffer payload length */
  350. size_t len;
  351. /** Maximum fill level */
  352. unsigned int max;
  353. };
  354. /** USB endpoint host controller operations */
  355. struct usb_endpoint_host_operations {
  356. /** Open endpoint
  357. *
  358. * @v ep USB endpoint
  359. * @ret rc Return status code
  360. */
  361. int ( * open ) ( struct usb_endpoint *ep );
  362. /** Close endpoint
  363. *
  364. * @v ep USB endpoint
  365. */
  366. void ( * close ) ( struct usb_endpoint *ep );
  367. /**
  368. * Reset endpoint
  369. *
  370. * @v ep USB endpoint
  371. * @ret rc Return status code
  372. */
  373. int ( * reset ) ( struct usb_endpoint *ep );
  374. /** Update MTU
  375. *
  376. * @v ep USB endpoint
  377. * @ret rc Return status code
  378. */
  379. int ( * mtu ) ( struct usb_endpoint *ep );
  380. /** Enqueue message transfer
  381. *
  382. * @v ep USB endpoint
  383. * @v iobuf I/O buffer
  384. * @ret rc Return status code
  385. */
  386. int ( * message ) ( struct usb_endpoint *ep,
  387. struct io_buffer *iobuf );
  388. /** Enqueue stream transfer
  389. *
  390. * @v ep USB endpoint
  391. * @v iobuf I/O buffer
  392. * @v zlp Append a zero-length packet
  393. * @ret rc Return status code
  394. */
  395. int ( * stream ) ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  396. int zlp );
  397. };
  398. /** USB endpoint driver operations */
  399. struct usb_endpoint_driver_operations {
  400. /** Complete transfer
  401. *
  402. * @v ep USB endpoint
  403. * @v iobuf I/O buffer
  404. * @v rc Completion status code
  405. */
  406. void ( * complete ) ( struct usb_endpoint *ep,
  407. struct io_buffer *iobuf, int rc );
  408. };
  409. /** Control endpoint address */
  410. #define USB_EP0_ADDRESS 0x00
  411. /** Control endpoint attributes */
  412. #define USB_EP0_ATTRIBUTES 0x00
  413. /** Calculate default MTU based on device speed
  414. *
  415. * @v speed Device speed
  416. * @ret mtu Default MTU
  417. */
  418. #define USB_EP0_DEFAULT_MTU(speed) \
  419. ( ( (speed) >= USB_SPEED_SUPER ) ? 512 : \
  420. ( ( (speed) >= USB_SPEED_FULL ) ? 64 : 8 ) )
  421. /** Control endpoint maximum burst size */
  422. #define USB_EP0_BURST 0
  423. /** Control endpoint interval */
  424. #define USB_EP0_INTERVAL 0
  425. /** Maximum endpoint number */
  426. #define USB_ENDPOINT_MAX 0x0f
  427. /** Endpoint direction is in */
  428. #define USB_ENDPOINT_IN 0x80
  429. /** Construct endpoint index from endpoint address */
  430. #define USB_ENDPOINT_IDX(address) \
  431. ( ( (address) & USB_ENDPOINT_MAX ) | \
  432. ( ( (address) & USB_ENDPOINT_IN ) >> 3 ) )
  433. /**
  434. * Initialise USB endpoint
  435. *
  436. * @v ep USB endpoint
  437. * @v usb USB device
  438. * @v driver Driver operations
  439. */
  440. static inline __attribute__ (( always_inline )) void
  441. usb_endpoint_init ( struct usb_endpoint *ep, struct usb_device *usb,
  442. struct usb_endpoint_driver_operations *driver ) {
  443. ep->usb = usb;
  444. ep->driver = driver;
  445. }
  446. /**
  447. * Describe USB endpoint
  448. *
  449. * @v ep USB endpoint
  450. * @v address Endpoint address
  451. * @v attributes Attributes
  452. * @v mtu Maximum packet size
  453. * @v burst Maximum burst size
  454. * @v interval Interval (in microframes)
  455. */
  456. static inline __attribute__ (( always_inline )) void
  457. usb_endpoint_describe ( struct usb_endpoint *ep, unsigned int address,
  458. unsigned int attributes, size_t mtu,
  459. unsigned int burst, unsigned int interval ) {
  460. ep->address = address;
  461. ep->attributes = attributes;
  462. ep->mtu = mtu;
  463. ep->burst = burst;
  464. ep->interval = interval;
  465. }
  466. /**
  467. * Set USB endpoint host controller private data
  468. *
  469. * @v ep USB endpoint
  470. * @v priv Host controller private data
  471. */
  472. static inline __attribute__ (( always_inline )) void
  473. usb_endpoint_set_hostdata ( struct usb_endpoint *ep, void *priv ) {
  474. ep->priv = priv;
  475. }
  476. /**
  477. * Get USB endpoint host controller private data
  478. *
  479. * @v ep USB endpoint
  480. * @ret priv Host controller private data
  481. */
  482. static inline __attribute__ (( always_inline )) void *
  483. usb_endpoint_get_hostdata ( struct usb_endpoint *ep ) {
  484. return ep->priv;
  485. }
  486. extern const char * usb_endpoint_name ( struct usb_endpoint *ep );
  487. extern int
  488. usb_endpoint_described ( struct usb_endpoint *ep,
  489. struct usb_configuration_descriptor *config,
  490. struct usb_interface_descriptor *interface,
  491. unsigned int type, unsigned int index );
  492. extern int usb_endpoint_open ( struct usb_endpoint *ep );
  493. extern void usb_endpoint_close ( struct usb_endpoint *ep );
  494. extern int usb_message ( struct usb_endpoint *ep, unsigned int request,
  495. unsigned int value, unsigned int index,
  496. struct io_buffer *iobuf );
  497. extern int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  498. int terminate );
  499. extern void usb_complete_err ( struct usb_endpoint *ep,
  500. struct io_buffer *iobuf, int rc );
  501. /**
  502. * Initialise USB endpoint refill
  503. *
  504. * @v ep USB endpoint
  505. * @v reserve Refill buffer reserved header length
  506. * @v len Refill buffer payload length (zero for endpoint's MTU)
  507. * @v max Maximum fill level
  508. */
  509. static inline __attribute__ (( always_inline )) void
  510. usb_refill_init ( struct usb_endpoint *ep, size_t reserve, size_t len,
  511. unsigned int max ) {
  512. INIT_LIST_HEAD ( &ep->recycled );
  513. ep->reserve = reserve;
  514. ep->len = len;
  515. ep->max = max;
  516. }
  517. /**
  518. * Recycle I/O buffer
  519. *
  520. * @v ep USB endpoint
  521. * @v iobuf I/O buffer
  522. */
  523. static inline __attribute__ (( always_inline )) void
  524. usb_recycle ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
  525. list_add_tail ( &iobuf->list, &ep->recycled );
  526. }
  527. extern int usb_prefill ( struct usb_endpoint *ep );
  528. extern int usb_refill ( struct usb_endpoint *ep );
  529. extern void usb_flush ( struct usb_endpoint *ep );
  530. /** A USB class descriptor */
  531. union usb_class_descriptor {
  532. /** Class */
  533. struct usb_class class;
  534. /** Scalar value */
  535. uint32_t scalar;
  536. };
  537. /**
  538. * A USB function descriptor
  539. *
  540. * This is an internal descriptor used to represent an association of
  541. * interfaces within a USB device.
  542. */
  543. struct usb_function_descriptor {
  544. /** Vendor ID */
  545. uint16_t vendor;
  546. /** Product ID */
  547. uint16_t product;
  548. /** Class */
  549. union usb_class_descriptor class;
  550. /** Number of interfaces */
  551. unsigned int count;
  552. };
  553. /**
  554. * A USB function
  555. *
  556. * A USB function represents an association of interfaces within a USB
  557. * device.
  558. */
  559. struct usb_function {
  560. /** Name */
  561. const char *name;
  562. /** USB device */
  563. struct usb_device *usb;
  564. /** Function descriptor */
  565. struct usb_function_descriptor desc;
  566. /** Generic device */
  567. struct device dev;
  568. /** List of functions within this USB device */
  569. struct list_head list;
  570. /** Driver */
  571. struct usb_driver *driver;
  572. /** Driver private data */
  573. void *priv;
  574. /** Driver device ID */
  575. struct usb_device_id *id;
  576. /** List of interface numbers
  577. *
  578. * This must be the last field within the structure.
  579. */
  580. uint8_t interface[0];
  581. };
  582. /**
  583. * Set USB function driver private data
  584. *
  585. * @v func USB function
  586. * @v priv Driver private data
  587. */
  588. static inline __attribute__ (( always_inline )) void
  589. usb_func_set_drvdata ( struct usb_function *func, void *priv ) {
  590. func->priv = priv;
  591. }
  592. /**
  593. * Get USB function driver private data
  594. *
  595. * @v function USB function
  596. * @ret priv Driver private data
  597. */
  598. static inline __attribute__ (( always_inline )) void *
  599. usb_func_get_drvdata ( struct usb_function *func ) {
  600. return func->priv;
  601. }
  602. /** A USB device */
  603. struct usb_device {
  604. /** Name */
  605. char name[32];
  606. /** USB port */
  607. struct usb_port *port;
  608. /** Device speed */
  609. unsigned int speed;
  610. /** List of devices on this bus */
  611. struct list_head list;
  612. /** Device address, if assigned */
  613. unsigned int address;
  614. /** Device descriptor */
  615. struct usb_device_descriptor device;
  616. /** List of functions */
  617. struct list_head functions;
  618. /** Host controller operations */
  619. struct usb_device_host_operations *host;
  620. /** Host controller private data */
  621. void *priv;
  622. /** Endpoint list */
  623. struct usb_endpoint *ep[32];
  624. /** Control endpoint */
  625. struct usb_endpoint control;
  626. /** Completed control transfers */
  627. struct list_head complete;
  628. /** Default language ID (if known) */
  629. unsigned int language;
  630. };
  631. /** USB device host controller operations */
  632. struct usb_device_host_operations {
  633. /** Open device
  634. *
  635. * @v usb USB device
  636. * @ret rc Return status code
  637. */
  638. int ( * open ) ( struct usb_device *usb );
  639. /** Close device
  640. *
  641. * @v usb USB device
  642. */
  643. void ( * close ) ( struct usb_device *usb );
  644. /** Assign device address
  645. *
  646. * @v usb USB device
  647. * @ret rc Return status code
  648. */
  649. int ( * address ) ( struct usb_device *usb );
  650. };
  651. /**
  652. * Set USB device host controller private data
  653. *
  654. * @v usb USB device
  655. * @v priv Host controller private data
  656. */
  657. static inline __attribute__ (( always_inline )) void
  658. usb_set_hostdata ( struct usb_device *usb, void *priv ) {
  659. usb->priv = priv;
  660. }
  661. /**
  662. * Get USB device host controller private data
  663. *
  664. * @v usb USB device
  665. * @ret priv Host controller private data
  666. */
  667. static inline __attribute__ (( always_inline )) void *
  668. usb_get_hostdata ( struct usb_device *usb ) {
  669. return usb->priv;
  670. }
  671. /**
  672. * Get USB endpoint
  673. *
  674. * @v usb USB device
  675. * @v address Endpoint address
  676. * @ret ep USB endpoint, or NULL if not opened
  677. */
  678. static inline struct usb_endpoint * usb_endpoint ( struct usb_device *usb,
  679. unsigned int address ) {
  680. return usb->ep[ USB_ENDPOINT_IDX ( address ) ];
  681. }
  682. /** A USB port */
  683. struct usb_port {
  684. /** USB hub */
  685. struct usb_hub *hub;
  686. /** Port address */
  687. unsigned int address;
  688. /** Port protocol */
  689. unsigned int protocol;
  690. /** Port speed */
  691. unsigned int speed;
  692. /** Port disconnection has been detected
  693. *
  694. * This should be set whenever the underlying hardware reports
  695. * a connection status change.
  696. */
  697. int disconnected;
  698. /** Port has an attached device */
  699. int attached;
  700. /** Currently attached device (if in use)
  701. *
  702. * Note that this field will be NULL if the attached device
  703. * has been freed (e.g. because there were no drivers found).
  704. */
  705. struct usb_device *usb;
  706. /** List of changed ports */
  707. struct list_head changed;
  708. };
  709. /** A USB hub */
  710. struct usb_hub {
  711. /** Name */
  712. const char *name;
  713. /** USB bus */
  714. struct usb_bus *bus;
  715. /** Underlying USB device, if any */
  716. struct usb_device *usb;
  717. /** Hub protocol */
  718. unsigned int protocol;
  719. /** Number of ports */
  720. unsigned int ports;
  721. /** List of hubs */
  722. struct list_head list;
  723. /** Host controller operations */
  724. struct usb_hub_host_operations *host;
  725. /** Driver operations */
  726. struct usb_hub_driver_operations *driver;
  727. /** Driver private data */
  728. void *priv;
  729. /** Port list
  730. *
  731. * This must be the last field within the structure.
  732. */
  733. struct usb_port port[0];
  734. };
  735. /** USB hub host controller operations */
  736. struct usb_hub_host_operations {
  737. /** Open hub
  738. *
  739. * @v hub USB hub
  740. * @ret rc Return status code
  741. */
  742. int ( * open ) ( struct usb_hub *hub );
  743. /** Close hub
  744. *
  745. * @v hub USB hub
  746. */
  747. void ( * close ) ( struct usb_hub *hub );
  748. };
  749. /** USB hub driver operations */
  750. struct usb_hub_driver_operations {
  751. /** Open hub
  752. *
  753. * @v hub USB hub
  754. * @ret rc Return status code
  755. */
  756. int ( * open ) ( struct usb_hub *hub );
  757. /** Close hub
  758. *
  759. * @v hub USB hub
  760. */
  761. void ( * close ) ( struct usb_hub *hub );
  762. /** Enable port
  763. *
  764. * @v hub USB hub
  765. * @v port USB port
  766. * @ret rc Return status code
  767. */
  768. int ( * enable ) ( struct usb_hub *hub, struct usb_port *port );
  769. /** Disable port
  770. *
  771. * @v hub USB hub
  772. * @v port USB port
  773. * @ret rc Return status code
  774. */
  775. int ( * disable ) ( struct usb_hub *hub, struct usb_port *port );
  776. /** Update port speed
  777. *
  778. * @v hub USB hub
  779. * @v port USB port
  780. * @ret rc Return status code
  781. */
  782. int ( * speed ) ( struct usb_hub *hub, struct usb_port *port );
  783. /** Clear transaction translator buffer
  784. *
  785. * @v hub USB hub
  786. * @v port USB port
  787. * @v ep USB endpoint
  788. * @ret rc Return status code
  789. */
  790. int ( * clear_tt ) ( struct usb_hub *hub, struct usb_port *port,
  791. struct usb_endpoint *ep );
  792. };
  793. /**
  794. * Set USB hub driver private data
  795. *
  796. * @v hub USB hub
  797. * @v priv Driver private data
  798. */
  799. static inline __attribute__ (( always_inline )) void
  800. usb_hub_set_drvdata ( struct usb_hub *hub, void *priv ) {
  801. hub->priv = priv;
  802. }
  803. /**
  804. * Get USB hub driver private data
  805. *
  806. * @v hub USB hub
  807. * @ret priv Driver private data
  808. */
  809. static inline __attribute__ (( always_inline )) void *
  810. usb_hub_get_drvdata ( struct usb_hub *hub ) {
  811. return hub->priv;
  812. }
  813. /**
  814. * Get USB port
  815. *
  816. * @v hub USB hub
  817. * @v address Port address
  818. * @ret port USB port
  819. */
  820. static inline __attribute__ (( always_inline )) struct usb_port *
  821. usb_port ( struct usb_hub *hub, unsigned int address ) {
  822. return &hub->port[ address - 1 ];
  823. }
  824. /** A USB bus */
  825. struct usb_bus {
  826. /** Name */
  827. const char *name;
  828. /** Underlying hardware device */
  829. struct device *dev;
  830. /** Host controller operations set */
  831. struct usb_host_operations *op;
  832. /** Largest transfer allowed on the bus */
  833. size_t mtu;
  834. /** Address in-use mask
  835. *
  836. * This is used only by buses which perform manual address
  837. * assignment. USB allows for addresses in the range [1,127].
  838. * We use a simple bitmask which restricts us to the range
  839. * [1,64]; this is unlikely to be a problem in practice. For
  840. * comparison: controllers which perform autonomous address
  841. * assignment (such as xHCI) typically allow for only 32
  842. * devices per bus anyway.
  843. */
  844. unsigned long long addresses;
  845. /** Root hub */
  846. struct usb_hub *hub;
  847. /** List of USB buses */
  848. struct list_head list;
  849. /** List of devices */
  850. struct list_head devices;
  851. /** List of hubs */
  852. struct list_head hubs;
  853. /** Host controller operations */
  854. struct usb_bus_host_operations *host;
  855. /** Host controller private data */
  856. void *priv;
  857. };
  858. /** USB bus host controller operations */
  859. struct usb_bus_host_operations {
  860. /** Open bus
  861. *
  862. * @v bus USB bus
  863. * @ret rc Return status code
  864. */
  865. int ( * open ) ( struct usb_bus *bus );
  866. /** Close bus
  867. *
  868. * @v bus USB bus
  869. */
  870. void ( * close ) ( struct usb_bus *bus );
  871. /** Poll bus
  872. *
  873. * @v bus USB bus
  874. */
  875. void ( * poll ) ( struct usb_bus *bus );
  876. };
  877. /** USB host controller operations */
  878. struct usb_host_operations {
  879. /** Endpoint operations */
  880. struct usb_endpoint_host_operations endpoint;
  881. /** Device operations */
  882. struct usb_device_host_operations device;
  883. /** Bus operations */
  884. struct usb_bus_host_operations bus;
  885. /** Hub operations */
  886. struct usb_hub_host_operations hub;
  887. /** Root hub operations */
  888. struct usb_hub_driver_operations root;
  889. };
  890. /**
  891. * Set USB bus host controller private data
  892. *
  893. * @v bus USB bus
  894. * @v priv Host controller private data
  895. */
  896. static inline __attribute__ (( always_inline )) void
  897. usb_bus_set_hostdata ( struct usb_bus *bus, void *priv ) {
  898. bus->priv = priv;
  899. }
  900. /**
  901. * Get USB bus host controller private data
  902. *
  903. * @v bus USB bus
  904. * @ret priv Host controller private data
  905. */
  906. static inline __attribute__ (( always_inline )) void *
  907. usb_bus_get_hostdata ( struct usb_bus *bus ) {
  908. return bus->priv;
  909. }
  910. /**
  911. * Poll USB bus
  912. *
  913. * @v bus USB bus
  914. */
  915. static inline __attribute__ (( always_inline )) void
  916. usb_poll ( struct usb_bus *bus ) {
  917. bus->host->poll ( bus );
  918. }
  919. /** Iterate over all USB buses */
  920. #define for_each_usb_bus( bus ) \
  921. list_for_each_entry ( (bus), &usb_buses, list )
  922. /**
  923. * Complete transfer (without error)
  924. *
  925. * @v ep USB endpoint
  926. * @v iobuf I/O buffer
  927. */
  928. static inline __attribute__ (( always_inline )) void
  929. usb_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
  930. usb_complete_err ( ep, iobuf, 0 );
  931. }
  932. extern int usb_control ( struct usb_device *usb, unsigned int request,
  933. unsigned int value, unsigned int index, void *data,
  934. size_t len );
  935. extern int usb_get_string_descriptor ( struct usb_device *usb,
  936. unsigned int index,
  937. unsigned int language,
  938. char *buf, size_t len );
  939. /**
  940. * Get status
  941. *
  942. * @v usb USB device
  943. * @v type Request type
  944. * @v index Target index
  945. * @v data Status to fill in
  946. * @v len Length of status descriptor
  947. * @ret rc Return status code
  948. */
  949. static inline __attribute__ (( always_inline )) int
  950. usb_get_status ( struct usb_device *usb, unsigned int type, unsigned int index,
  951. void *data, size_t len ) {
  952. return usb_control ( usb, ( USB_GET_STATUS | type ), 0, index,
  953. data, len );
  954. }
  955. /**
  956. * Clear feature
  957. *
  958. * @v usb USB device
  959. * @v type Request type
  960. * @v feature Feature selector
  961. * @v index Target index
  962. * @ret rc Return status code
  963. */
  964. static inline __attribute__ (( always_inline )) int
  965. usb_clear_feature ( struct usb_device *usb, unsigned int type,
  966. unsigned int feature, unsigned int index ) {
  967. return usb_control ( usb, ( USB_CLEAR_FEATURE | type ),
  968. feature, index, NULL, 0 );
  969. }
  970. /**
  971. * Set feature
  972. *
  973. * @v usb USB device
  974. * @v type Request type
  975. * @v feature Feature selector
  976. * @v index Target index
  977. * @ret rc Return status code
  978. */
  979. static inline __attribute__ (( always_inline )) int
  980. usb_set_feature ( struct usb_device *usb, unsigned int type,
  981. unsigned int feature, unsigned int index ) {
  982. return usb_control ( usb, ( USB_SET_FEATURE | type ),
  983. feature, index, NULL, 0 );
  984. }
  985. /**
  986. * Set address
  987. *
  988. * @v usb USB device
  989. * @v address Device address
  990. * @ret rc Return status code
  991. */
  992. static inline __attribute__ (( always_inline )) int
  993. usb_set_address ( struct usb_device *usb, unsigned int address ) {
  994. return usb_control ( usb, USB_SET_ADDRESS, address, 0, NULL, 0 );
  995. }
  996. /**
  997. * Get USB descriptor
  998. *
  999. * @v usb USB device
  1000. * @v type Request type
  1001. * @v desc Descriptor type
  1002. * @v index Descriptor index
  1003. * @v language Language ID (for string descriptors)
  1004. * @v data Descriptor to fill in
  1005. * @v len Maximum length of descriptor
  1006. * @ret rc Return status code
  1007. */
  1008. static inline __attribute__ (( always_inline )) int
  1009. usb_get_descriptor ( struct usb_device *usb, unsigned int type,
  1010. unsigned int desc, unsigned int index,
  1011. unsigned int language, struct usb_descriptor_header *data,
  1012. size_t len ) {
  1013. return usb_control ( usb, ( USB_GET_DESCRIPTOR | type ),
  1014. ( ( desc << 8 ) | index ), language, data, len );
  1015. }
  1016. /**
  1017. * Get first part of USB device descriptor (up to and including MTU)
  1018. *
  1019. * @v usb USB device
  1020. * @v data Device descriptor to (partially) fill in
  1021. * @ret rc Return status code
  1022. */
  1023. static inline __attribute__ (( always_inline )) int
  1024. usb_get_mtu ( struct usb_device *usb, struct usb_device_descriptor *data ) {
  1025. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  1026. &data->header,
  1027. ( offsetof ( typeof ( *data ), mtu ) +
  1028. sizeof ( data->mtu ) ) );
  1029. }
  1030. /**
  1031. * Get USB device descriptor
  1032. *
  1033. * @v usb USB device
  1034. * @v data Device descriptor to fill in
  1035. * @ret rc Return status code
  1036. */
  1037. static inline __attribute__ (( always_inline )) int
  1038. usb_get_device_descriptor ( struct usb_device *usb,
  1039. struct usb_device_descriptor *data ) {
  1040. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  1041. &data->header, sizeof ( *data ) );
  1042. }
  1043. /**
  1044. * Get USB configuration descriptor
  1045. *
  1046. * @v usb USB device
  1047. * @v index Configuration index
  1048. * @v data Configuration descriptor to fill in
  1049. * @ret rc Return status code
  1050. */
  1051. static inline __attribute__ (( always_inline )) int
  1052. usb_get_config_descriptor ( struct usb_device *usb, unsigned int index,
  1053. struct usb_configuration_descriptor *data,
  1054. size_t len ) {
  1055. return usb_get_descriptor ( usb, 0, USB_CONFIGURATION_DESCRIPTOR, index,
  1056. 0, &data->header, len );
  1057. }
  1058. /**
  1059. * Set USB configuration
  1060. *
  1061. * @v usb USB device
  1062. * @v index Configuration index
  1063. * @ret rc Return status code
  1064. */
  1065. static inline __attribute__ (( always_inline )) int
  1066. usb_set_configuration ( struct usb_device *usb, unsigned int index ) {
  1067. return usb_control ( usb, USB_SET_CONFIGURATION, index, 0, NULL, 0 );
  1068. }
  1069. /**
  1070. * Set USB interface alternate setting
  1071. *
  1072. * @v usb USB device
  1073. * @v interface Interface number
  1074. * @v alternate Alternate setting
  1075. * @ret rc Return status code
  1076. */
  1077. static inline __attribute__ (( always_inline )) int
  1078. usb_set_interface ( struct usb_device *usb, unsigned int interface,
  1079. unsigned int alternate ) {
  1080. return usb_control ( usb, USB_SET_INTERFACE, alternate, interface,
  1081. NULL, 0 );
  1082. }
  1083. extern struct list_head usb_buses;
  1084. extern struct usb_interface_descriptor *
  1085. usb_interface_descriptor ( struct usb_configuration_descriptor *config,
  1086. unsigned int interface, unsigned int alternate );
  1087. extern struct usb_endpoint_descriptor *
  1088. usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
  1089. struct usb_interface_descriptor *interface,
  1090. unsigned int type, unsigned int index );
  1091. extern struct usb_endpoint_companion_descriptor *
  1092. usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
  1093. struct usb_endpoint_descriptor *desc );
  1094. extern struct usb_hub * alloc_usb_hub ( struct usb_bus *bus,
  1095. struct usb_device *usb,
  1096. unsigned int ports,
  1097. struct usb_hub_driver_operations *op );
  1098. extern int register_usb_hub ( struct usb_hub *hub );
  1099. extern void unregister_usb_hub ( struct usb_hub *hub );
  1100. extern void free_usb_hub ( struct usb_hub *hub );
  1101. extern void usb_port_changed ( struct usb_port *port );
  1102. extern struct usb_bus * alloc_usb_bus ( struct device *dev,
  1103. unsigned int ports, size_t mtu,
  1104. struct usb_host_operations *op );
  1105. extern int register_usb_bus ( struct usb_bus *bus );
  1106. extern void unregister_usb_bus ( struct usb_bus *bus );
  1107. extern void free_usb_bus ( struct usb_bus *bus );
  1108. extern struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
  1109. unsigned int location );
  1110. extern int usb_alloc_address ( struct usb_bus *bus );
  1111. extern void usb_free_address ( struct usb_bus *bus, unsigned int address );
  1112. extern unsigned int usb_route_string ( struct usb_device *usb );
  1113. extern unsigned int usb_depth ( struct usb_device *usb );
  1114. extern struct usb_port * usb_root_hub_port ( struct usb_device *usb );
  1115. extern struct usb_port * usb_transaction_translator ( struct usb_device *usb );
  1116. /** Minimum reset time
  1117. *
  1118. * Section 7.1.7.5 of the USB2 specification states that root hub
  1119. * ports should assert reset signalling for at least 50ms.
  1120. */
  1121. #define USB_RESET_DELAY_MS 50
  1122. /** Reset recovery time
  1123. *
  1124. * Section 9.2.6.2 of the USB2 specification states that the
  1125. * "recovery" interval after a port reset is 10ms.
  1126. */
  1127. #define USB_RESET_RECOVER_DELAY_MS 10
  1128. /** Maximum time to wait for a control transaction to complete
  1129. *
  1130. * Section 9.2.6.1 of the USB2 specification states that the upper
  1131. * limit for commands to be processed is 5 seconds.
  1132. */
  1133. #define USB_CONTROL_MAX_WAIT_MS 5000
  1134. /** Set address recovery time
  1135. *
  1136. * Section 9.2.6.3 of the USB2 specification states that devices are
  1137. * allowed a 2ms recovery interval after receiving a new address.
  1138. */
  1139. #define USB_SET_ADDRESS_RECOVER_DELAY_MS 2
  1140. /** Time to wait for ports to stabilise
  1141. *
  1142. * Section 7.1.7.3 of the USB specification states that we must allow
  1143. * 100ms for devices to signal attachment, and an additional 100ms for
  1144. * connection debouncing. (This delay is parallelised across all
  1145. * ports on a hub; we do not delay separately for each port.)
  1146. */
  1147. #define USB_PORT_DELAY_MS 200
  1148. /** A USB device ID */
  1149. struct usb_device_id {
  1150. /** Name */
  1151. const char *name;
  1152. /** Vendor ID */
  1153. uint16_t vendor;
  1154. /** Product ID */
  1155. uint16_t product;
  1156. /** Arbitrary driver data */
  1157. unsigned long driver_data;
  1158. };
  1159. /** Match-anything ID */
  1160. #define USB_ANY_ID 0xffff
  1161. /** A USB class ID */
  1162. struct usb_class_id {
  1163. /** Class */
  1164. union usb_class_descriptor class;
  1165. /** Class mask */
  1166. union usb_class_descriptor mask;
  1167. };
  1168. /** Construct USB class ID
  1169. *
  1170. * @v base Base class code (or USB_ANY_ID)
  1171. * @v subclass Subclass code (or USB_ANY_ID)
  1172. * @v protocol Protocol code (or USB_ANY_ID)
  1173. */
  1174. #define USB_CLASS_ID( base, subclass, protocol ) { \
  1175. .class = { \
  1176. .class = { \
  1177. ( (base) & 0xff ), \
  1178. ( (subclass) & 0xff ), \
  1179. ( (protocol) & 0xff ), \
  1180. }, \
  1181. }, \
  1182. .mask = { \
  1183. .class = { \
  1184. ( ( (base) == USB_ANY_ID ) ? 0x00 : 0xff ), \
  1185. ( ( (subclass) == USB_ANY_ID ) ? 0x00 : 0xff ), \
  1186. ( ( (protocol) == USB_ANY_ID ) ? 0x00 : 0xff ), \
  1187. }, \
  1188. }, \
  1189. }
  1190. /** A USB driver */
  1191. struct usb_driver {
  1192. /** USB ID table */
  1193. struct usb_device_id *ids;
  1194. /** Number of entries in ID table */
  1195. unsigned int id_count;
  1196. /** Class ID */
  1197. struct usb_class_id class;
  1198. /** Driver score
  1199. *
  1200. * This is used to determine the preferred configuration for a
  1201. * USB device.
  1202. */
  1203. unsigned int score;
  1204. /**
  1205. * Probe device
  1206. *
  1207. * @v func USB function
  1208. * @v config Configuration descriptor
  1209. * @ret rc Return status code
  1210. */
  1211. int ( * probe ) ( struct usb_function *func,
  1212. struct usb_configuration_descriptor *config );
  1213. /**
  1214. * Remove device
  1215. *
  1216. * @v func USB function
  1217. */
  1218. void ( * remove ) ( struct usb_function *func );
  1219. };
  1220. /** USB driver table */
  1221. #define USB_DRIVERS __table ( struct usb_driver, "usb_drivers" )
  1222. /** Declare a USB driver */
  1223. #define __usb_driver __table_entry ( USB_DRIVERS, 01 )
  1224. /** USB driver scores */
  1225. enum usb_driver_score {
  1226. /** Fallback driver (has no effect on overall score) */
  1227. USB_SCORE_FALLBACK = 0,
  1228. /** Deprecated driver */
  1229. USB_SCORE_DEPRECATED = 1,
  1230. /** Normal driver */
  1231. USB_SCORE_NORMAL = 2,
  1232. };
  1233. extern struct usb_driver *
  1234. usb_find_driver ( struct usb_function_descriptor *desc,
  1235. struct usb_device_id **id );
  1236. #endif /* _IPXE_USB_H */