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 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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 );
  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. /** A USB setup data packet */
  49. struct usb_setup_packet {
  50. /** Request */
  51. uint16_t request;
  52. /** Value paramer */
  53. uint16_t value;
  54. /** Index parameter */
  55. uint16_t index;
  56. /** Length of data stage */
  57. uint16_t len;
  58. } __attribute__ (( packed ));
  59. /** Data transfer is from host to device */
  60. #define USB_DIR_OUT ( 0 << 7 )
  61. /** Data transfer is from device to host */
  62. #define USB_DIR_IN ( 1 << 7 )
  63. /** Standard request type */
  64. #define USB_TYPE_STANDARD ( 0 << 5 )
  65. /** Class-specific request type */
  66. #define USB_TYPE_CLASS ( 1 << 5 )
  67. /** Request recipient is the device */
  68. #define USB_RECIP_DEVICE ( 0 << 0 )
  69. /** Request recipient is an interface */
  70. #define USB_RECIP_INTERFACE ( 1 << 0 )
  71. /** Request recipient is an endpoint */
  72. #define USB_RECIP_ENDPOINT ( 2 << 0 )
  73. /** Construct USB request type */
  74. #define USB_REQUEST_TYPE(type) ( (type) << 8 )
  75. /** Get status */
  76. #define USB_GET_STATUS ( USB_DIR_IN | USB_REQUEST_TYPE ( 0 ) )
  77. /** Clear feature */
  78. #define USB_CLEAR_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 1 ) )
  79. /** Set feature */
  80. #define USB_SET_FEATURE ( USB_DIR_OUT | USB_REQUEST_TYPE ( 3 ) )
  81. /** Set address */
  82. #define USB_SET_ADDRESS ( USB_DIR_OUT | USB_REQUEST_TYPE ( 5 ) )
  83. /** Get descriptor */
  84. #define USB_GET_DESCRIPTOR ( USB_DIR_IN | USB_REQUEST_TYPE ( 6 ) )
  85. /** Set descriptor */
  86. #define USB_SET_DESCRIPTOR ( USB_DIR_OUT | USB_REQUEST_TYPE ( 7 ) )
  87. /** Get configuration */
  88. #define USB_GET_CONFIGURATION ( USB_DIR_IN | USB_REQUEST_TYPE ( 8 ) )
  89. /** Set configuration */
  90. #define USB_SET_CONFIGURATION ( USB_DIR_OUT | USB_REQUEST_TYPE ( 9 ) )
  91. /** Get interface */
  92. #define USB_GET_INTERFACE \
  93. ( USB_DIR_IN | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 10 ) )
  94. /** Set interface */
  95. #define USB_SET_INTERFACE \
  96. ( USB_DIR_OUT | USB_RECIP_INTERFACE | USB_REQUEST_TYPE ( 11 ) )
  97. /** Endpoint halt feature */
  98. #define USB_ENDPOINT_HALT 0
  99. /** A USB class code tuple */
  100. struct usb_class {
  101. /** Class code */
  102. uint8_t class;
  103. /** Subclass code */
  104. uint8_t subclass;
  105. /** Protocol code */
  106. uint8_t protocol;
  107. } __attribute__ (( packed ));
  108. /** Class code for USB hubs */
  109. #define USB_CLASS_HUB 9
  110. /** A USB descriptor header */
  111. struct usb_descriptor_header {
  112. /** Length of descriptor */
  113. uint8_t len;
  114. /** Descriptor type */
  115. uint8_t type;
  116. } __attribute__ (( packed ));
  117. /** A USB device descriptor */
  118. struct usb_device_descriptor {
  119. /** Descriptor header */
  120. struct usb_descriptor_header header;
  121. /** USB specification release number in BCD */
  122. uint16_t protocol;
  123. /** Device class */
  124. struct usb_class class;
  125. /** Maximum packet size for endpoint zero */
  126. uint8_t mtu;
  127. /** Vendor ID */
  128. uint16_t vendor;
  129. /** Product ID */
  130. uint16_t product;
  131. /** Device release number in BCD */
  132. uint16_t release;
  133. /** Manufacturer string */
  134. uint8_t manufacturer;
  135. /** Product string */
  136. uint8_t name;
  137. /** Serial number string */
  138. uint8_t serial;
  139. /** Number of possible configurations */
  140. uint8_t configurations;
  141. } __attribute__ (( packed ));
  142. /** A USB device descriptor */
  143. #define USB_DEVICE_DESCRIPTOR 1
  144. /** A USB configuration descriptor */
  145. struct usb_configuration_descriptor {
  146. /** Descriptor header */
  147. struct usb_descriptor_header header;
  148. /** Total length */
  149. uint16_t len;
  150. /** Number of interfaces */
  151. uint8_t interfaces;
  152. /** Configuration value */
  153. uint8_t config;
  154. /** Configuration string */
  155. uint8_t name;
  156. /** Attributes */
  157. uint8_t attributes;
  158. /** Maximum power consumption */
  159. uint8_t power;
  160. } __attribute__ (( packed ));
  161. /** A USB configuration descriptor */
  162. #define USB_CONFIGURATION_DESCRIPTOR 2
  163. /** A USB string descriptor */
  164. struct usb_string_descriptor {
  165. /** Descriptor header */
  166. struct usb_descriptor_header header;
  167. /** String */
  168. char string[0];
  169. } __attribute__ (( packed ));
  170. /** A USB string descriptor */
  171. #define USB_STRING_DESCRIPTOR 3
  172. /** A USB interface descriptor */
  173. struct usb_interface_descriptor {
  174. /** Descriptor header */
  175. struct usb_descriptor_header header;
  176. /** Interface number */
  177. uint8_t interface;
  178. /** Alternate setting */
  179. uint8_t alternate;
  180. /** Number of endpoints */
  181. uint8_t endpoints;
  182. /** Interface class */
  183. struct usb_class class;
  184. /** Interface name */
  185. uint8_t name;
  186. } __attribute__ (( packed ));
  187. /** A USB interface descriptor */
  188. #define USB_INTERFACE_DESCRIPTOR 4
  189. /** A USB endpoint descriptor */
  190. struct usb_endpoint_descriptor {
  191. /** Descriptor header */
  192. struct usb_descriptor_header header;
  193. /** Endpoint address */
  194. uint8_t endpoint;
  195. /** Attributes */
  196. uint8_t attributes;
  197. /** Maximum packet size and burst size */
  198. uint16_t sizes;
  199. /** Polling interval */
  200. uint8_t interval;
  201. } __attribute__ (( packed ));
  202. /** A USB endpoint descriptor */
  203. #define USB_ENDPOINT_DESCRIPTOR 5
  204. /** Endpoint attribute transfer type mask */
  205. #define USB_ENDPOINT_ATTR_TYPE_MASK 0x03
  206. /** Control endpoint transfer type */
  207. #define USB_ENDPOINT_ATTR_CONTROL 0x00
  208. /** Bulk endpoint transfer type */
  209. #define USB_ENDPOINT_ATTR_BULK 0x02
  210. /** Interrupt endpoint transfer type */
  211. #define USB_ENDPOINT_ATTR_INTERRUPT 0x03
  212. /** Bulk OUT endpoint (internal) type */
  213. #define USB_BULK_OUT ( USB_ENDPOINT_ATTR_BULK | USB_DIR_OUT )
  214. /** Bulk IN endpoint (internal) type */
  215. #define USB_BULK_IN ( USB_ENDPOINT_ATTR_BULK | USB_DIR_IN )
  216. /** Interrupt endpoint (internal) type */
  217. #define USB_INTERRUPT ( USB_ENDPOINT_ATTR_INTERRUPT | USB_DIR_IN )
  218. /** USB endpoint MTU */
  219. #define USB_ENDPOINT_MTU(sizes) ( ( (sizes) >> 0 ) & 0x07ff )
  220. /** USB endpoint maximum burst size */
  221. #define USB_ENDPOINT_BURST(sizes) ( ( (sizes) >> 11 ) & 0x0003 )
  222. /** A USB endpoint companion descriptor */
  223. struct usb_endpoint_companion_descriptor {
  224. /** Descriptor header */
  225. struct usb_descriptor_header header;
  226. /** Maximum burst size */
  227. uint8_t burst;
  228. /** Extended attributes */
  229. uint8_t extended;
  230. /** Number of bytes per service interval */
  231. uint16_t periodic;
  232. } __attribute__ (( packed ));
  233. /** A USB endpoint companion descriptor */
  234. #define USB_ENDPOINT_COMPANION_DESCRIPTOR 48
  235. /** A USB interface association descriptor */
  236. struct usb_interface_association_descriptor {
  237. /** Descriptor header */
  238. struct usb_descriptor_header header;
  239. /** First interface number */
  240. uint8_t first;
  241. /** Interface count */
  242. uint8_t count;
  243. /** Association class */
  244. struct usb_class class;
  245. /** Association name */
  246. uint8_t name;
  247. } __attribute__ (( packed ));
  248. /** A USB interface association descriptor */
  249. #define USB_INTERFACE_ASSOCIATION_DESCRIPTOR 11
  250. /** A class-specific interface descriptor */
  251. #define USB_CS_INTERFACE_DESCRIPTOR 36
  252. /** A class-specific endpoint descriptor */
  253. #define USB_CS_ENDPOINT_DESCRIPTOR 37
  254. /**
  255. * Get next USB descriptor
  256. *
  257. * @v desc USB descriptor header
  258. * @ret next Next USB descriptor header
  259. */
  260. static inline __attribute__ (( always_inline )) struct usb_descriptor_header *
  261. usb_next_descriptor ( struct usb_descriptor_header *desc ) {
  262. return ( ( ( void * ) desc ) + desc->len );
  263. }
  264. /**
  265. * Check that descriptor lies within a configuration descriptor
  266. *
  267. * @v config Configuration descriptor
  268. * @v desc Descriptor header
  269. * @v is_within Descriptor is within the configuration descriptor
  270. */
  271. static inline __attribute__ (( always_inline )) int
  272. usb_is_within_config ( struct usb_configuration_descriptor *config,
  273. struct usb_descriptor_header *desc ) {
  274. struct usb_descriptor_header *end =
  275. ( ( ( void * ) config ) + le16_to_cpu ( config->len ) );
  276. /* Check that descriptor starts within the configuration
  277. * descriptor, and that the length does not exceed the
  278. * configuration descriptor. This relies on the fact that
  279. * usb_next_descriptor() needs to access only the first byte
  280. * of the descriptor in order to determine the length.
  281. */
  282. return ( ( desc < end ) && ( usb_next_descriptor ( desc ) <= end ) );
  283. }
  284. /** Iterate over all configuration descriptors */
  285. #define for_each_config_descriptor( desc, config ) \
  286. for ( desc = container_of ( &(config)->header, \
  287. typeof ( *desc ), header ) ; \
  288. usb_is_within_config ( (config), &desc->header ) ; \
  289. desc = container_of ( usb_next_descriptor ( &desc->header ), \
  290. typeof ( *desc ), header ) )
  291. /** Iterate over all configuration descriptors within an interface descriptor */
  292. #define for_each_interface_descriptor( desc, config, interface ) \
  293. for ( desc = container_of ( usb_next_descriptor ( &(interface)-> \
  294. header ), \
  295. typeof ( *desc ), header ) ; \
  296. ( usb_is_within_config ( (config), &desc->header ) && \
  297. ( desc->header.type != USB_INTERFACE_DESCRIPTOR ) ) ; \
  298. desc = container_of ( usb_next_descriptor ( &desc->header ), \
  299. typeof ( *desc ), header ) )
  300. /** A USB endpoint */
  301. struct usb_endpoint {
  302. /** USB device */
  303. struct usb_device *usb;
  304. /** Endpoint address */
  305. unsigned int address;
  306. /** Attributes */
  307. unsigned int attributes;
  308. /** Maximum transfer size */
  309. size_t mtu;
  310. /** Maximum burst size */
  311. unsigned int burst;
  312. /** Endpoint is open */
  313. int open;
  314. /** Current failure state (if any) */
  315. int rc;
  316. /** Host controller operations */
  317. struct usb_endpoint_host_operations *host;
  318. /** Host controller private data */
  319. void *priv;
  320. /** Driver operations */
  321. struct usb_endpoint_driver_operations *driver;
  322. };
  323. /** USB endpoint host controller operations */
  324. struct usb_endpoint_host_operations {
  325. /** Open endpoint
  326. *
  327. * @v ep USB endpoint
  328. * @ret rc Return status code
  329. */
  330. int ( * open ) ( struct usb_endpoint *ep );
  331. /** Close endpoint
  332. *
  333. * @v ep USB endpoint
  334. */
  335. void ( * close ) ( struct usb_endpoint *ep );
  336. /**
  337. * Reset endpoint
  338. *
  339. * @v ep USB endpoint
  340. * @ret rc Return status code
  341. */
  342. int ( * reset ) ( struct usb_endpoint *ep );
  343. /** Update MTU
  344. *
  345. * @v ep USB endpoint
  346. * @ret rc Return status code
  347. */
  348. int ( * mtu ) ( struct usb_endpoint *ep );
  349. /** Enqueue message transfer
  350. *
  351. * @v ep USB endpoint
  352. * @v packet Setup packet
  353. * @v iobuf I/O buffer (if any)
  354. * @ret rc Return status code
  355. */
  356. int ( * message ) ( struct usb_endpoint *ep,
  357. struct usb_setup_packet *setup,
  358. struct io_buffer *iobuf );
  359. /** Enqueue stream transfer
  360. *
  361. * @v ep USB endpoint
  362. * @v iobuf I/O buffer
  363. * @ret rc Return status code
  364. */
  365. int ( * stream ) ( struct usb_endpoint *ep,
  366. struct io_buffer *iobuf );
  367. };
  368. /** USB endpoint driver operations */
  369. struct usb_endpoint_driver_operations {
  370. /** Complete transfer
  371. *
  372. * @v ep USB endpoint
  373. * @v iobuf I/O buffer
  374. * @v rc Completion status code
  375. */
  376. void ( * complete ) ( struct usb_endpoint *ep,
  377. struct io_buffer *iobuf, int rc );
  378. };
  379. /** Control endpoint address */
  380. #define USB_EP0_ADDRESS 0x00
  381. /** Control endpoint attributes */
  382. #define USB_EP0_ATTRIBUTES 0x00
  383. /** Calculate default MTU based on device speed
  384. *
  385. * @v speed Device speed
  386. * @ret mtu Default MTU
  387. */
  388. #define USB_EP0_DEFAULT_MTU(speed) \
  389. ( ( (speed) >= USB_SPEED_SUPER ) ? 512 : \
  390. ( ( (speed) >= USB_SPEED_FULL ) ? 64 : 8 ) )
  391. /** Control endpoint maximum burst size */
  392. #define USB_EP0_BURST 0
  393. /** Maximum endpoint number */
  394. #define USB_ENDPOINT_MAX 0x0f
  395. /** Endpoint direction is in */
  396. #define USB_ENDPOINT_IN 0x80
  397. /** Construct endpoint index from endpoint address */
  398. #define USB_ENDPOINT_IDX(address) \
  399. ( ( (address) & USB_ENDPOINT_MAX ) | \
  400. ( ( (address) & USB_ENDPOINT_IN ) >> 3 ) )
  401. /**
  402. * Initialise USB endpoint
  403. *
  404. * @v ep USB endpoint
  405. * @v usb USB device
  406. * @v driver Driver operations
  407. */
  408. static inline __attribute__ (( always_inline )) void
  409. usb_endpoint_init ( struct usb_endpoint *ep, struct usb_device *usb,
  410. struct usb_endpoint_driver_operations *driver ) {
  411. ep->usb = usb;
  412. ep->driver = driver;
  413. }
  414. /**
  415. * Describe USB endpoint
  416. *
  417. * @v ep USB endpoint
  418. * @v address Endpoint address
  419. * @v attributes Attributes
  420. * @v mtu Maximum packet size
  421. * @v burst Maximum burst size
  422. */
  423. static inline __attribute__ (( always_inline )) void
  424. usb_endpoint_describe ( struct usb_endpoint *ep, unsigned int address,
  425. unsigned int attributes, size_t mtu,
  426. unsigned int burst ) {
  427. ep->address = address;
  428. ep->attributes = attributes;
  429. ep->mtu = mtu;
  430. ep->burst = burst;
  431. }
  432. /**
  433. * Set USB endpoint host controller private data
  434. *
  435. * @v ep USB endpoint
  436. * @v priv Host controller private data
  437. */
  438. static inline __attribute__ (( always_inline )) void
  439. usb_endpoint_set_hostdata ( struct usb_endpoint *ep, void *priv ) {
  440. ep->priv = priv;
  441. }
  442. /**
  443. * Get USB endpoint host controller private data
  444. *
  445. * @v ep USB endpoint
  446. * @ret priv Host controller private data
  447. */
  448. static inline __attribute__ (( always_inline )) void *
  449. usb_endpoint_get_hostdata ( struct usb_endpoint *ep ) {
  450. return ep->priv;
  451. }
  452. extern int
  453. usb_endpoint_described ( struct usb_endpoint *ep,
  454. struct usb_configuration_descriptor *config,
  455. struct usb_interface_descriptor *interface,
  456. unsigned int type, unsigned int index );
  457. extern int usb_endpoint_open ( struct usb_endpoint *ep );
  458. extern void usb_endpoint_close ( struct usb_endpoint *ep );
  459. extern int usb_message ( struct usb_endpoint *ep, unsigned int request,
  460. unsigned int value, unsigned int index,
  461. struct io_buffer *iobuf );
  462. extern int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf );
  463. extern void usb_complete_err ( struct usb_endpoint *ep,
  464. struct io_buffer *iobuf, int rc );
  465. /**
  466. * A USB function
  467. *
  468. * A USB function represents an association of interfaces within a USB
  469. * device.
  470. */
  471. struct usb_function {
  472. /** Name */
  473. const char *name;
  474. /** USB device */
  475. struct usb_device *usb;
  476. /** Class */
  477. struct usb_class class;
  478. /** Number of interfaces */
  479. unsigned int count;
  480. /** Generic device */
  481. struct device dev;
  482. /** List of functions within this USB device */
  483. struct list_head list;
  484. /** Driver */
  485. struct usb_driver *driver;
  486. /** Driver private data */
  487. void *priv;
  488. /** List of interface numbers
  489. *
  490. * This must be the last field within the structure.
  491. */
  492. uint8_t interface[0];
  493. };
  494. /**
  495. * Set USB function driver private data
  496. *
  497. * @v func USB function
  498. * @v priv Driver private data
  499. */
  500. static inline __attribute__ (( always_inline )) void
  501. usb_func_set_drvdata ( struct usb_function *func, void *priv ) {
  502. func->priv = priv;
  503. }
  504. /**
  505. * Get USB function driver private data
  506. *
  507. * @v function USB function
  508. * @ret priv Driver private data
  509. */
  510. static inline __attribute__ (( always_inline )) void *
  511. usb_func_get_drvdata ( struct usb_function *func ) {
  512. return func->priv;
  513. }
  514. /** A USB device */
  515. struct usb_device {
  516. /** Name */
  517. char name[32];
  518. /** USB port */
  519. struct usb_port *port;
  520. /** List of devices on this bus */
  521. struct list_head list;
  522. /** Device address, if assigned */
  523. unsigned int address;
  524. /** Device descriptor */
  525. struct usb_device_descriptor device;
  526. /** List of functions */
  527. struct list_head functions;
  528. /** Host controller operations */
  529. struct usb_device_host_operations *host;
  530. /** Host controller private data */
  531. void *priv;
  532. /** Endpoint list */
  533. struct usb_endpoint *ep[32];
  534. /** Control endpoint */
  535. struct usb_endpoint control;
  536. /** Completed control transfers */
  537. struct list_head complete;
  538. };
  539. /** USB device host controller operations */
  540. struct usb_device_host_operations {
  541. /** Open device
  542. *
  543. * @v usb USB device
  544. * @ret rc Return status code
  545. */
  546. int ( * open ) ( struct usb_device *usb );
  547. /** Close device
  548. *
  549. * @v usb USB device
  550. */
  551. void ( * close ) ( struct usb_device *usb );
  552. /** Assign device address
  553. *
  554. * @v usb USB device
  555. * @ret rc Return status code
  556. */
  557. int ( * address ) ( struct usb_device *usb );
  558. };
  559. /**
  560. * Set USB device host controller private data
  561. *
  562. * @v usb USB device
  563. * @v priv Host controller private data
  564. */
  565. static inline __attribute__ (( always_inline )) void
  566. usb_set_hostdata ( struct usb_device *usb, void *priv ) {
  567. usb->priv = priv;
  568. }
  569. /**
  570. * Get USB device host controller private data
  571. *
  572. * @v usb USB device
  573. * @ret priv Host controller private data
  574. */
  575. static inline __attribute__ (( always_inline )) void *
  576. usb_get_hostdata ( struct usb_device *usb ) {
  577. return usb->priv;
  578. }
  579. /**
  580. * Get USB endpoint
  581. *
  582. * @v usb USB device
  583. * @v address Endpoint address
  584. * @ret ep USB endpoint, or NULL if not opened
  585. */
  586. static inline struct usb_endpoint * usb_endpoint ( struct usb_device *usb,
  587. unsigned int address ) {
  588. return usb->ep[ USB_ENDPOINT_IDX ( address ) ];
  589. }
  590. /** A USB port */
  591. struct usb_port {
  592. /** USB hub */
  593. struct usb_hub *hub;
  594. /** Port address */
  595. unsigned int address;
  596. /** Port protocol */
  597. unsigned int protocol;
  598. /** Port speed */
  599. unsigned int speed;
  600. /** Currently attached device (if any) */
  601. struct usb_device *usb;
  602. /** List of changed ports */
  603. struct list_head list;
  604. };
  605. /** A USB hub */
  606. struct usb_hub {
  607. /** Name */
  608. const char *name;
  609. /** USB bus */
  610. struct usb_bus *bus;
  611. /** Underlying USB device, if any */
  612. struct usb_device *usb;
  613. /** Hub protocol */
  614. unsigned int protocol;
  615. /** Number of ports */
  616. unsigned int ports;
  617. /** List of hubs */
  618. struct list_head list;
  619. /** Driver operations */
  620. struct usb_hub_driver_operations *driver;
  621. /** Driver private data */
  622. void *priv;
  623. /** Port list
  624. *
  625. * This must be the last field within the structure.
  626. */
  627. struct usb_port port[0];
  628. };
  629. /** USB hub operations */
  630. struct usb_hub_driver_operations {
  631. /** Open hub
  632. *
  633. * @v hub USB hub
  634. * @ret rc Return status code
  635. */
  636. int ( * open ) ( struct usb_hub *hub );
  637. /** Close hub
  638. *
  639. * @v hub USB hub
  640. */
  641. void ( * close ) ( struct usb_hub *hub );
  642. /** Enable port
  643. *
  644. * @v hub USB hub
  645. * @v port USB port
  646. * @ret rc Return status code
  647. */
  648. int ( * enable ) ( struct usb_hub *hub, struct usb_port *port );
  649. /** Disable port
  650. *
  651. * @v hub USB hub
  652. * @v port USB port
  653. * @ret rc Return status code
  654. */
  655. int ( * disable ) ( struct usb_hub *hub, struct usb_port *port );
  656. /** Update port speed
  657. *
  658. * @v hub USB hub
  659. * @v port USB port
  660. * @ret rc Return status code
  661. */
  662. int ( * speed ) ( struct usb_hub *hub, struct usb_port *port );
  663. };
  664. /**
  665. * Set USB hub driver private data
  666. *
  667. * @v hub USB hub
  668. * @v priv Driver private data
  669. */
  670. static inline __attribute__ (( always_inline )) void
  671. usb_hub_set_drvdata ( struct usb_hub *hub, void *priv ) {
  672. hub->priv = priv;
  673. }
  674. /**
  675. * Get USB hub driver private data
  676. *
  677. * @v hub USB hub
  678. * @ret priv Driver private data
  679. */
  680. static inline __attribute__ (( always_inline )) void *
  681. usb_hub_get_drvdata ( struct usb_hub *hub ) {
  682. return hub->priv;
  683. }
  684. /**
  685. * Get USB port
  686. *
  687. * @v hub USB hub
  688. * @v address Port address
  689. * @ret port USB port
  690. */
  691. static inline __attribute__ (( always_inline )) struct usb_port *
  692. usb_port ( struct usb_hub *hub, unsigned int address ) {
  693. return &hub->port[ address - 1 ];
  694. }
  695. /** A USB bus */
  696. struct usb_bus {
  697. /** Name */
  698. const char *name;
  699. /** Underlying hardware device */
  700. struct device *dev;
  701. /** Host controller operations set */
  702. struct usb_host_operations *op;
  703. /** Root hub */
  704. struct usb_hub *hub;
  705. /** List of devices */
  706. struct list_head devices;
  707. /** List of hubs */
  708. struct list_head hubs;
  709. /** List of changed ports */
  710. struct list_head changed;
  711. /** Process */
  712. struct process process;
  713. /** Host controller operations */
  714. struct usb_bus_host_operations *host;
  715. /** Host controller private data */
  716. void *priv;
  717. };
  718. /** USB bus host controller operations */
  719. struct usb_bus_host_operations {
  720. /** Open bus
  721. *
  722. * @v bus USB bus
  723. * @ret rc Return status code
  724. */
  725. int ( * open ) ( struct usb_bus *bus );
  726. /** Close bus
  727. *
  728. * @v bus USB bus
  729. */
  730. void ( * close ) ( struct usb_bus *bus );
  731. /** Poll bus
  732. *
  733. * @v bus USB bus
  734. */
  735. void ( * poll ) ( struct usb_bus *bus );
  736. };
  737. /** USB host controller operations */
  738. struct usb_host_operations {
  739. /** Endpoint operations */
  740. struct usb_endpoint_host_operations endpoint;
  741. /** Device operations */
  742. struct usb_device_host_operations device;
  743. /** Bus operations */
  744. struct usb_bus_host_operations bus;
  745. /** Root hub operations */
  746. struct usb_hub_driver_operations hub;
  747. };
  748. /**
  749. * Set USB bus host controller private data
  750. *
  751. * @v bus USB bus
  752. * @v priv Host controller private data
  753. */
  754. static inline __attribute__ (( always_inline )) void
  755. usb_bus_set_hostdata ( struct usb_bus *bus, void *priv ) {
  756. bus->priv = priv;
  757. }
  758. /**
  759. * Get USB bus host controller private data
  760. *
  761. * @v bus USB bus
  762. * @ret priv Host controller private data
  763. */
  764. static inline __attribute__ (( always_inline )) void *
  765. usb_bus_get_hostdata ( struct usb_bus *bus ) {
  766. return bus->priv;
  767. }
  768. /**
  769. * Poll USB bus
  770. *
  771. * @v bus USB bus
  772. */
  773. static inline __attribute__ (( always_inline )) void
  774. usb_poll ( struct usb_bus *bus ) {
  775. bus->host->poll ( bus );
  776. }
  777. /**
  778. * Complete transfer (without error)
  779. *
  780. * @v ep USB endpoint
  781. * @v iobuf I/O buffer
  782. */
  783. static inline __attribute__ (( always_inline )) void
  784. usb_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
  785. usb_complete_err ( ep, iobuf, 0 );
  786. }
  787. extern int usb_control ( struct usb_device *usb, unsigned int request,
  788. unsigned int value, unsigned int index, void *data,
  789. size_t len );
  790. extern int usb_get_string_descriptor ( struct usb_device *usb,
  791. unsigned int index,
  792. unsigned int language,
  793. char *buf, size_t len );
  794. /**
  795. * Get status
  796. *
  797. * @v usb USB device
  798. * @v type Request type
  799. * @v index Target index
  800. * @v data Status to fill in
  801. * @v len Length of status descriptor
  802. * @ret rc Return status code
  803. */
  804. static inline __attribute__ (( always_inline )) int
  805. usb_get_status ( struct usb_device *usb, unsigned int type, unsigned int index,
  806. void *data, size_t len ) {
  807. return usb_control ( usb, ( USB_GET_STATUS | type ), 0, index,
  808. data, len );
  809. }
  810. /**
  811. * Clear feature
  812. *
  813. * @v usb USB device
  814. * @v type Request type
  815. * @v feature Feature selector
  816. * @v index Target index
  817. * @ret rc Return status code
  818. */
  819. static inline __attribute__ (( always_inline )) int
  820. usb_clear_feature ( struct usb_device *usb, unsigned int type,
  821. unsigned int feature, unsigned int index ) {
  822. return usb_control ( usb, ( USB_CLEAR_FEATURE | type ),
  823. feature, index, NULL, 0 );
  824. }
  825. /**
  826. * Set feature
  827. *
  828. * @v usb USB device
  829. * @v type Request type
  830. * @v feature Feature selector
  831. * @v index Target index
  832. * @ret rc Return status code
  833. */
  834. static inline __attribute__ (( always_inline )) int
  835. usb_set_feature ( struct usb_device *usb, unsigned int type,
  836. unsigned int feature, unsigned int index ) {
  837. return usb_control ( usb, ( USB_SET_FEATURE | type ),
  838. feature, index, NULL, 0 );
  839. }
  840. /**
  841. * Get USB descriptor
  842. *
  843. * @v usb USB device
  844. * @v type Request type
  845. * @v desc Descriptor type
  846. * @v index Descriptor index
  847. * @v language Language ID (for string descriptors)
  848. * @v data Descriptor to fill in
  849. * @v len Maximum length of descriptor
  850. * @ret rc Return status code
  851. */
  852. static inline __attribute__ (( always_inline )) int
  853. usb_get_descriptor ( struct usb_device *usb, unsigned int type,
  854. unsigned int desc, unsigned int index,
  855. unsigned int language, struct usb_descriptor_header *data,
  856. size_t len ) {
  857. return usb_control ( usb, ( USB_GET_DESCRIPTOR | type ),
  858. ( ( desc << 8 ) | index ), language, data, len );
  859. }
  860. /**
  861. * Get first part of USB device descriptor (up to and including MTU)
  862. *
  863. * @v usb USB device
  864. * @v data Device descriptor to (partially) fill in
  865. * @ret rc Return status code
  866. */
  867. static inline __attribute__ (( always_inline )) int
  868. usb_get_mtu ( struct usb_device *usb, struct usb_device_descriptor *data ) {
  869. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  870. &data->header,
  871. ( offsetof ( typeof ( *data ), mtu ) +
  872. sizeof ( data->mtu ) ) );
  873. }
  874. /**
  875. * Get USB device descriptor
  876. *
  877. * @v usb USB device
  878. * @v data Device descriptor to fill in
  879. * @ret rc Return status code
  880. */
  881. static inline __attribute__ (( always_inline )) int
  882. usb_get_device_descriptor ( struct usb_device *usb,
  883. struct usb_device_descriptor *data ) {
  884. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  885. &data->header, sizeof ( *data ) );
  886. }
  887. /**
  888. * Get USB configuration descriptor
  889. *
  890. * @v usb USB device
  891. * @v index Configuration index
  892. * @v data Configuration descriptor to fill in
  893. * @ret rc Return status code
  894. */
  895. static inline __attribute (( always_inline )) int
  896. usb_get_config_descriptor ( struct usb_device *usb, unsigned int index,
  897. struct usb_configuration_descriptor *data,
  898. size_t len ) {
  899. return usb_get_descriptor ( usb, 0, USB_CONFIGURATION_DESCRIPTOR, index,
  900. 0, &data->header, len );
  901. }
  902. /**
  903. * Set USB configuration
  904. *
  905. * @v usb USB device
  906. * @v index Configuration index
  907. * @ret rc Return status code
  908. */
  909. static inline __attribute__ (( always_inline )) int
  910. usb_set_configuration ( struct usb_device *usb, unsigned int index ) {
  911. return usb_control ( usb, USB_SET_CONFIGURATION, index, 0, NULL, 0 );
  912. }
  913. /**
  914. * Set USB interface alternate setting
  915. *
  916. * @v usb USB device
  917. * @v interface Interface number
  918. * @v alternate Alternate setting
  919. * @ret rc Return status code
  920. */
  921. static inline __attribute__ (( always_inline )) int
  922. usb_set_interface ( struct usb_device *usb, unsigned int interface,
  923. unsigned int alternate ) {
  924. return usb_control ( usb, USB_SET_INTERFACE, alternate, interface,
  925. NULL, 0 );
  926. }
  927. extern struct usb_interface_descriptor *
  928. usb_interface_descriptor ( struct usb_configuration_descriptor *config,
  929. unsigned int interface, unsigned int alternate );
  930. extern struct usb_endpoint_descriptor *
  931. usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
  932. struct usb_interface_descriptor *interface,
  933. unsigned int type, unsigned int index );
  934. extern struct usb_endpoint_companion_descriptor *
  935. usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
  936. struct usb_endpoint_descriptor *desc );
  937. extern struct usb_hub * alloc_usb_hub ( struct usb_bus *bus,
  938. struct usb_device *usb,
  939. unsigned int ports,
  940. struct usb_hub_driver_operations *op );
  941. extern int register_usb_hub ( struct usb_hub *hub );
  942. extern void unregister_usb_hub ( struct usb_hub *hub );
  943. extern void free_usb_hub ( struct usb_hub *hub );
  944. extern void usb_port_changed ( struct usb_port *port );
  945. extern struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  946. struct usb_host_operations *op );
  947. extern int register_usb_bus ( struct usb_bus *bus );
  948. extern void unregister_usb_bus ( struct usb_bus *bus );
  949. extern void free_usb_bus ( struct usb_bus *bus );
  950. extern unsigned int usb_route_string ( struct usb_device *usb );
  951. extern unsigned int usb_depth ( struct usb_device *usb );
  952. extern struct usb_port * usb_root_hub_port ( struct usb_device *usb );
  953. /** Maximum time to wait for a control transaction to complete
  954. *
  955. * This is a policy decision.
  956. */
  957. #define USB_CONTROL_MAX_WAIT_MS 100
  958. /** Time to wait for ports to stabilise
  959. *
  960. * This is a policy decision.
  961. */
  962. #define USB_PORT_DELAY_MS 100
  963. /** A USB device ID */
  964. struct usb_device_id {
  965. /** Name */
  966. const char *name;
  967. /** Vendor ID */
  968. uint16_t vendor;
  969. /** Product ID */
  970. uint16_t product;
  971. /** Class */
  972. struct usb_class class;
  973. };
  974. /** Match-anything ID */
  975. #define USB_ANY_ID 0xffff
  976. /** A USB driver */
  977. struct usb_driver {
  978. /** USB ID table */
  979. struct usb_device_id *ids;
  980. /** Number of entries in ID table */
  981. unsigned int id_count;
  982. /**
  983. * Probe device
  984. *
  985. * @v func USB function
  986. * @v config Configuration descriptor
  987. * @ret rc Return status code
  988. */
  989. int ( * probe ) ( struct usb_function *func,
  990. struct usb_configuration_descriptor *config );
  991. /**
  992. * Remove device
  993. *
  994. * @v func USB function
  995. */
  996. void ( * remove ) ( struct usb_function *func );
  997. };
  998. /** USB driver table */
  999. #define USB_DRIVERS __table ( struct usb_driver, "usb_drivers" )
  1000. /** Declare a USB driver */
  1001. #define __usb_driver __table_entry ( USB_DRIVERS, 01 )
  1002. #endif /* _IPXE_USB_H */