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

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