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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. /** Interval (in microframes) */
  313. unsigned int interval;
  314. /** Endpoint is open */
  315. int open;
  316. /** Current failure state (if any) */
  317. int rc;
  318. /** Host controller operations */
  319. struct usb_endpoint_host_operations *host;
  320. /** Host controller private data */
  321. void *priv;
  322. /** Driver operations */
  323. struct usb_endpoint_driver_operations *driver;
  324. };
  325. /** USB endpoint host controller operations */
  326. struct usb_endpoint_host_operations {
  327. /** Open endpoint
  328. *
  329. * @v ep USB endpoint
  330. * @ret rc Return status code
  331. */
  332. int ( * open ) ( struct usb_endpoint *ep );
  333. /** Close endpoint
  334. *
  335. * @v ep USB endpoint
  336. */
  337. void ( * close ) ( struct usb_endpoint *ep );
  338. /**
  339. * Reset endpoint
  340. *
  341. * @v ep USB endpoint
  342. * @ret rc Return status code
  343. */
  344. int ( * reset ) ( struct usb_endpoint *ep );
  345. /** Update MTU
  346. *
  347. * @v ep USB endpoint
  348. * @ret rc Return status code
  349. */
  350. int ( * mtu ) ( struct usb_endpoint *ep );
  351. /** Enqueue message transfer
  352. *
  353. * @v ep USB endpoint
  354. * @v packet Setup packet
  355. * @v iobuf I/O buffer (if any)
  356. * @ret rc Return status code
  357. */
  358. int ( * message ) ( struct usb_endpoint *ep,
  359. struct usb_setup_packet *setup,
  360. struct io_buffer *iobuf );
  361. /** Enqueue stream transfer
  362. *
  363. * @v ep USB endpoint
  364. * @v iobuf I/O buffer
  365. * @v terminate Terminate using a short packet
  366. * @ret rc Return status code
  367. */
  368. int ( * stream ) ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  369. int terminate );
  370. };
  371. /** USB endpoint driver operations */
  372. struct usb_endpoint_driver_operations {
  373. /** Complete transfer
  374. *
  375. * @v ep USB endpoint
  376. * @v iobuf I/O buffer
  377. * @v rc Completion status code
  378. */
  379. void ( * complete ) ( struct usb_endpoint *ep,
  380. struct io_buffer *iobuf, int rc );
  381. };
  382. /** Control endpoint address */
  383. #define USB_EP0_ADDRESS 0x00
  384. /** Control endpoint attributes */
  385. #define USB_EP0_ATTRIBUTES 0x00
  386. /** Calculate default MTU based on device speed
  387. *
  388. * @v speed Device speed
  389. * @ret mtu Default MTU
  390. */
  391. #define USB_EP0_DEFAULT_MTU(speed) \
  392. ( ( (speed) >= USB_SPEED_SUPER ) ? 512 : \
  393. ( ( (speed) >= USB_SPEED_FULL ) ? 64 : 8 ) )
  394. /** Control endpoint maximum burst size */
  395. #define USB_EP0_BURST 0
  396. /** Control endpoint interval */
  397. #define USB_EP0_INTERVAL 0
  398. /** Maximum endpoint number */
  399. #define USB_ENDPOINT_MAX 0x0f
  400. /** Endpoint direction is in */
  401. #define USB_ENDPOINT_IN 0x80
  402. /** Construct endpoint index from endpoint address */
  403. #define USB_ENDPOINT_IDX(address) \
  404. ( ( (address) & USB_ENDPOINT_MAX ) | \
  405. ( ( (address) & USB_ENDPOINT_IN ) >> 3 ) )
  406. /**
  407. * Initialise USB endpoint
  408. *
  409. * @v ep USB endpoint
  410. * @v usb USB device
  411. * @v driver Driver operations
  412. */
  413. static inline __attribute__ (( always_inline )) void
  414. usb_endpoint_init ( struct usb_endpoint *ep, struct usb_device *usb,
  415. struct usb_endpoint_driver_operations *driver ) {
  416. ep->usb = usb;
  417. ep->driver = driver;
  418. }
  419. /**
  420. * Describe USB endpoint
  421. *
  422. * @v ep USB endpoint
  423. * @v address Endpoint address
  424. * @v attributes Attributes
  425. * @v mtu Maximum packet size
  426. * @v burst Maximum burst size
  427. * @v interval Interval (in microframes)
  428. */
  429. static inline __attribute__ (( always_inline )) void
  430. usb_endpoint_describe ( struct usb_endpoint *ep, unsigned int address,
  431. unsigned int attributes, size_t mtu,
  432. unsigned int burst, unsigned int interval ) {
  433. ep->address = address;
  434. ep->attributes = attributes;
  435. ep->mtu = mtu;
  436. ep->burst = burst;
  437. ep->interval = interval;
  438. }
  439. /**
  440. * Set USB endpoint host controller private data
  441. *
  442. * @v ep USB endpoint
  443. * @v priv Host controller private data
  444. */
  445. static inline __attribute__ (( always_inline )) void
  446. usb_endpoint_set_hostdata ( struct usb_endpoint *ep, void *priv ) {
  447. ep->priv = priv;
  448. }
  449. /**
  450. * Get USB endpoint host controller private data
  451. *
  452. * @v ep USB endpoint
  453. * @ret priv Host controller private data
  454. */
  455. static inline __attribute__ (( always_inline )) void *
  456. usb_endpoint_get_hostdata ( struct usb_endpoint *ep ) {
  457. return ep->priv;
  458. }
  459. extern int
  460. usb_endpoint_described ( struct usb_endpoint *ep,
  461. struct usb_configuration_descriptor *config,
  462. struct usb_interface_descriptor *interface,
  463. unsigned int type, unsigned int index );
  464. extern int usb_endpoint_open ( struct usb_endpoint *ep );
  465. extern void usb_endpoint_close ( struct usb_endpoint *ep );
  466. extern int usb_message ( struct usb_endpoint *ep, unsigned int request,
  467. unsigned int value, unsigned int index,
  468. struct io_buffer *iobuf );
  469. extern int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  470. int terminate );
  471. extern void usb_complete_err ( struct usb_endpoint *ep,
  472. struct io_buffer *iobuf, int rc );
  473. /**
  474. * A USB function
  475. *
  476. * A USB function represents an association of interfaces within a USB
  477. * device.
  478. */
  479. struct usb_function {
  480. /** Name */
  481. const char *name;
  482. /** USB device */
  483. struct usb_device *usb;
  484. /** Class */
  485. struct usb_class class;
  486. /** Number of interfaces */
  487. unsigned int count;
  488. /** Generic device */
  489. struct device dev;
  490. /** List of functions within this USB device */
  491. struct list_head list;
  492. /** Driver */
  493. struct usb_driver *driver;
  494. /** Driver private data */
  495. void *priv;
  496. /** List of interface numbers
  497. *
  498. * This must be the last field within the structure.
  499. */
  500. uint8_t interface[0];
  501. };
  502. /**
  503. * Set USB function driver private data
  504. *
  505. * @v func USB function
  506. * @v priv Driver private data
  507. */
  508. static inline __attribute__ (( always_inline )) void
  509. usb_func_set_drvdata ( struct usb_function *func, void *priv ) {
  510. func->priv = priv;
  511. }
  512. /**
  513. * Get USB function driver private data
  514. *
  515. * @v function USB function
  516. * @ret priv Driver private data
  517. */
  518. static inline __attribute__ (( always_inline )) void *
  519. usb_func_get_drvdata ( struct usb_function *func ) {
  520. return func->priv;
  521. }
  522. /** A USB device */
  523. struct usb_device {
  524. /** Name */
  525. char name[32];
  526. /** USB port */
  527. struct usb_port *port;
  528. /** List of devices on this bus */
  529. struct list_head list;
  530. /** Device address, if assigned */
  531. unsigned int address;
  532. /** Device descriptor */
  533. struct usb_device_descriptor device;
  534. /** List of functions */
  535. struct list_head functions;
  536. /** Host controller operations */
  537. struct usb_device_host_operations *host;
  538. /** Host controller private data */
  539. void *priv;
  540. /** Endpoint list */
  541. struct usb_endpoint *ep[32];
  542. /** Control endpoint */
  543. struct usb_endpoint control;
  544. /** Completed control transfers */
  545. struct list_head complete;
  546. };
  547. /** USB device host controller operations */
  548. struct usb_device_host_operations {
  549. /** Open device
  550. *
  551. * @v usb USB device
  552. * @ret rc Return status code
  553. */
  554. int ( * open ) ( struct usb_device *usb );
  555. /** Close device
  556. *
  557. * @v usb USB device
  558. */
  559. void ( * close ) ( struct usb_device *usb );
  560. /** Assign device address
  561. *
  562. * @v usb USB device
  563. * @ret rc Return status code
  564. */
  565. int ( * address ) ( struct usb_device *usb );
  566. };
  567. /**
  568. * Set USB device host controller private data
  569. *
  570. * @v usb USB device
  571. * @v priv Host controller private data
  572. */
  573. static inline __attribute__ (( always_inline )) void
  574. usb_set_hostdata ( struct usb_device *usb, void *priv ) {
  575. usb->priv = priv;
  576. }
  577. /**
  578. * Get USB device host controller private data
  579. *
  580. * @v usb USB device
  581. * @ret priv Host controller private data
  582. */
  583. static inline __attribute__ (( always_inline )) void *
  584. usb_get_hostdata ( struct usb_device *usb ) {
  585. return usb->priv;
  586. }
  587. /**
  588. * Get USB endpoint
  589. *
  590. * @v usb USB device
  591. * @v address Endpoint address
  592. * @ret ep USB endpoint, or NULL if not opened
  593. */
  594. static inline struct usb_endpoint * usb_endpoint ( struct usb_device *usb,
  595. unsigned int address ) {
  596. return usb->ep[ USB_ENDPOINT_IDX ( address ) ];
  597. }
  598. /** A USB port */
  599. struct usb_port {
  600. /** USB hub */
  601. struct usb_hub *hub;
  602. /** Port address */
  603. unsigned int address;
  604. /** Port protocol */
  605. unsigned int protocol;
  606. /** Port speed */
  607. unsigned int speed;
  608. /** Currently attached device (if any) */
  609. struct usb_device *usb;
  610. /** List of changed ports */
  611. struct list_head list;
  612. };
  613. /** A USB hub */
  614. struct usb_hub {
  615. /** Name */
  616. const char *name;
  617. /** USB bus */
  618. struct usb_bus *bus;
  619. /** Underlying USB device, if any */
  620. struct usb_device *usb;
  621. /** Hub protocol */
  622. unsigned int protocol;
  623. /** Number of ports */
  624. unsigned int ports;
  625. /** List of hubs */
  626. struct list_head list;
  627. /** Driver operations */
  628. struct usb_hub_driver_operations *driver;
  629. /** Driver private data */
  630. void *priv;
  631. /** Port list
  632. *
  633. * This must be the last field within the structure.
  634. */
  635. struct usb_port port[0];
  636. };
  637. /** USB hub operations */
  638. struct usb_hub_driver_operations {
  639. /** Open hub
  640. *
  641. * @v hub USB hub
  642. * @ret rc Return status code
  643. */
  644. int ( * open ) ( struct usb_hub *hub );
  645. /** Close hub
  646. *
  647. * @v hub USB hub
  648. */
  649. void ( * close ) ( struct usb_hub *hub );
  650. /** Enable port
  651. *
  652. * @v hub USB hub
  653. * @v port USB port
  654. * @ret rc Return status code
  655. */
  656. int ( * enable ) ( struct usb_hub *hub, struct usb_port *port );
  657. /** Disable port
  658. *
  659. * @v hub USB hub
  660. * @v port USB port
  661. * @ret rc Return status code
  662. */
  663. int ( * disable ) ( struct usb_hub *hub, struct usb_port *port );
  664. /** Update port speed
  665. *
  666. * @v hub USB hub
  667. * @v port USB port
  668. * @ret rc Return status code
  669. */
  670. int ( * speed ) ( struct usb_hub *hub, struct usb_port *port );
  671. };
  672. /**
  673. * Set USB hub driver private data
  674. *
  675. * @v hub USB hub
  676. * @v priv Driver private data
  677. */
  678. static inline __attribute__ (( always_inline )) void
  679. usb_hub_set_drvdata ( struct usb_hub *hub, void *priv ) {
  680. hub->priv = priv;
  681. }
  682. /**
  683. * Get USB hub driver private data
  684. *
  685. * @v hub USB hub
  686. * @ret priv Driver private data
  687. */
  688. static inline __attribute__ (( always_inline )) void *
  689. usb_hub_get_drvdata ( struct usb_hub *hub ) {
  690. return hub->priv;
  691. }
  692. /**
  693. * Get USB port
  694. *
  695. * @v hub USB hub
  696. * @v address Port address
  697. * @ret port USB port
  698. */
  699. static inline __attribute__ (( always_inline )) struct usb_port *
  700. usb_port ( struct usb_hub *hub, unsigned int address ) {
  701. return &hub->port[ address - 1 ];
  702. }
  703. /** A USB bus */
  704. struct usb_bus {
  705. /** Name */
  706. const char *name;
  707. /** Underlying hardware device */
  708. struct device *dev;
  709. /** Host controller operations set */
  710. struct usb_host_operations *op;
  711. /** Root hub */
  712. struct usb_hub *hub;
  713. /** List of devices */
  714. struct list_head devices;
  715. /** List of hubs */
  716. struct list_head hubs;
  717. /** List of changed ports */
  718. struct list_head changed;
  719. /** Process */
  720. struct process process;
  721. /** Host controller operations */
  722. struct usb_bus_host_operations *host;
  723. /** Host controller private data */
  724. void *priv;
  725. };
  726. /** USB bus host controller operations */
  727. struct usb_bus_host_operations {
  728. /** Open bus
  729. *
  730. * @v bus USB bus
  731. * @ret rc Return status code
  732. */
  733. int ( * open ) ( struct usb_bus *bus );
  734. /** Close bus
  735. *
  736. * @v bus USB bus
  737. */
  738. void ( * close ) ( struct usb_bus *bus );
  739. /** Poll bus
  740. *
  741. * @v bus USB bus
  742. */
  743. void ( * poll ) ( struct usb_bus *bus );
  744. };
  745. /** USB host controller operations */
  746. struct usb_host_operations {
  747. /** Endpoint operations */
  748. struct usb_endpoint_host_operations endpoint;
  749. /** Device operations */
  750. struct usb_device_host_operations device;
  751. /** Bus operations */
  752. struct usb_bus_host_operations bus;
  753. /** Root hub operations */
  754. struct usb_hub_driver_operations hub;
  755. };
  756. /**
  757. * Set USB bus host controller private data
  758. *
  759. * @v bus USB bus
  760. * @v priv Host controller private data
  761. */
  762. static inline __attribute__ (( always_inline )) void
  763. usb_bus_set_hostdata ( struct usb_bus *bus, void *priv ) {
  764. bus->priv = priv;
  765. }
  766. /**
  767. * Get USB bus host controller private data
  768. *
  769. * @v bus USB bus
  770. * @ret priv Host controller private data
  771. */
  772. static inline __attribute__ (( always_inline )) void *
  773. usb_bus_get_hostdata ( struct usb_bus *bus ) {
  774. return bus->priv;
  775. }
  776. /**
  777. * Poll USB bus
  778. *
  779. * @v bus USB bus
  780. */
  781. static inline __attribute__ (( always_inline )) void
  782. usb_poll ( struct usb_bus *bus ) {
  783. bus->host->poll ( bus );
  784. }
  785. /**
  786. * Complete transfer (without error)
  787. *
  788. * @v ep USB endpoint
  789. * @v iobuf I/O buffer
  790. */
  791. static inline __attribute__ (( always_inline )) void
  792. usb_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
  793. usb_complete_err ( ep, iobuf, 0 );
  794. }
  795. extern int usb_control ( struct usb_device *usb, unsigned int request,
  796. unsigned int value, unsigned int index, void *data,
  797. size_t len );
  798. extern int usb_get_string_descriptor ( struct usb_device *usb,
  799. unsigned int index,
  800. unsigned int language,
  801. char *buf, size_t len );
  802. /**
  803. * Get status
  804. *
  805. * @v usb USB device
  806. * @v type Request type
  807. * @v index Target index
  808. * @v data Status to fill in
  809. * @v len Length of status descriptor
  810. * @ret rc Return status code
  811. */
  812. static inline __attribute__ (( always_inline )) int
  813. usb_get_status ( struct usb_device *usb, unsigned int type, unsigned int index,
  814. void *data, size_t len ) {
  815. return usb_control ( usb, ( USB_GET_STATUS | type ), 0, index,
  816. data, len );
  817. }
  818. /**
  819. * Clear feature
  820. *
  821. * @v usb USB device
  822. * @v type Request type
  823. * @v feature Feature selector
  824. * @v index Target index
  825. * @ret rc Return status code
  826. */
  827. static inline __attribute__ (( always_inline )) int
  828. usb_clear_feature ( struct usb_device *usb, unsigned int type,
  829. unsigned int feature, unsigned int index ) {
  830. return usb_control ( usb, ( USB_CLEAR_FEATURE | type ),
  831. feature, index, NULL, 0 );
  832. }
  833. /**
  834. * Set feature
  835. *
  836. * @v usb USB device
  837. * @v type Request type
  838. * @v feature Feature selector
  839. * @v index Target index
  840. * @ret rc Return status code
  841. */
  842. static inline __attribute__ (( always_inline )) int
  843. usb_set_feature ( struct usb_device *usb, unsigned int type,
  844. unsigned int feature, unsigned int index ) {
  845. return usb_control ( usb, ( USB_SET_FEATURE | type ),
  846. feature, index, NULL, 0 );
  847. }
  848. /**
  849. * Get USB descriptor
  850. *
  851. * @v usb USB device
  852. * @v type Request type
  853. * @v desc Descriptor type
  854. * @v index Descriptor index
  855. * @v language Language ID (for string descriptors)
  856. * @v data Descriptor to fill in
  857. * @v len Maximum length of descriptor
  858. * @ret rc Return status code
  859. */
  860. static inline __attribute__ (( always_inline )) int
  861. usb_get_descriptor ( struct usb_device *usb, unsigned int type,
  862. unsigned int desc, unsigned int index,
  863. unsigned int language, struct usb_descriptor_header *data,
  864. size_t len ) {
  865. return usb_control ( usb, ( USB_GET_DESCRIPTOR | type ),
  866. ( ( desc << 8 ) | index ), language, data, len );
  867. }
  868. /**
  869. * Get first part of USB device descriptor (up to and including MTU)
  870. *
  871. * @v usb USB device
  872. * @v data Device descriptor to (partially) fill in
  873. * @ret rc Return status code
  874. */
  875. static inline __attribute__ (( always_inline )) int
  876. usb_get_mtu ( struct usb_device *usb, struct usb_device_descriptor *data ) {
  877. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  878. &data->header,
  879. ( offsetof ( typeof ( *data ), mtu ) +
  880. sizeof ( data->mtu ) ) );
  881. }
  882. /**
  883. * Get USB device descriptor
  884. *
  885. * @v usb USB device
  886. * @v data Device descriptor to fill in
  887. * @ret rc Return status code
  888. */
  889. static inline __attribute__ (( always_inline )) int
  890. usb_get_device_descriptor ( struct usb_device *usb,
  891. struct usb_device_descriptor *data ) {
  892. return usb_get_descriptor ( usb, 0, USB_DEVICE_DESCRIPTOR, 0, 0,
  893. &data->header, sizeof ( *data ) );
  894. }
  895. /**
  896. * Get USB configuration descriptor
  897. *
  898. * @v usb USB device
  899. * @v index Configuration index
  900. * @v data Configuration descriptor to fill in
  901. * @ret rc Return status code
  902. */
  903. static inline __attribute (( always_inline )) int
  904. usb_get_config_descriptor ( struct usb_device *usb, unsigned int index,
  905. struct usb_configuration_descriptor *data,
  906. size_t len ) {
  907. return usb_get_descriptor ( usb, 0, USB_CONFIGURATION_DESCRIPTOR, index,
  908. 0, &data->header, len );
  909. }
  910. /**
  911. * Set USB configuration
  912. *
  913. * @v usb USB device
  914. * @v index Configuration index
  915. * @ret rc Return status code
  916. */
  917. static inline __attribute__ (( always_inline )) int
  918. usb_set_configuration ( struct usb_device *usb, unsigned int index ) {
  919. return usb_control ( usb, USB_SET_CONFIGURATION, index, 0, NULL, 0 );
  920. }
  921. /**
  922. * Set USB interface alternate setting
  923. *
  924. * @v usb USB device
  925. * @v interface Interface number
  926. * @v alternate Alternate setting
  927. * @ret rc Return status code
  928. */
  929. static inline __attribute__ (( always_inline )) int
  930. usb_set_interface ( struct usb_device *usb, unsigned int interface,
  931. unsigned int alternate ) {
  932. return usb_control ( usb, USB_SET_INTERFACE, alternate, interface,
  933. NULL, 0 );
  934. }
  935. extern struct usb_interface_descriptor *
  936. usb_interface_descriptor ( struct usb_configuration_descriptor *config,
  937. unsigned int interface, unsigned int alternate );
  938. extern struct usb_endpoint_descriptor *
  939. usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
  940. struct usb_interface_descriptor *interface,
  941. unsigned int type, unsigned int index );
  942. extern struct usb_endpoint_companion_descriptor *
  943. usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
  944. struct usb_endpoint_descriptor *desc );
  945. extern struct usb_hub * alloc_usb_hub ( struct usb_bus *bus,
  946. struct usb_device *usb,
  947. unsigned int ports,
  948. struct usb_hub_driver_operations *op );
  949. extern int register_usb_hub ( struct usb_hub *hub );
  950. extern void unregister_usb_hub ( struct usb_hub *hub );
  951. extern void free_usb_hub ( struct usb_hub *hub );
  952. extern void usb_port_changed ( struct usb_port *port );
  953. extern struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
  954. struct usb_host_operations *op );
  955. extern int register_usb_bus ( struct usb_bus *bus );
  956. extern void unregister_usb_bus ( struct usb_bus *bus );
  957. extern void free_usb_bus ( struct usb_bus *bus );
  958. extern unsigned int usb_route_string ( struct usb_device *usb );
  959. extern unsigned int usb_depth ( struct usb_device *usb );
  960. extern struct usb_port * usb_root_hub_port ( struct usb_device *usb );
  961. /** Maximum time to wait for a control transaction to complete
  962. *
  963. * This is a policy decision.
  964. */
  965. #define USB_CONTROL_MAX_WAIT_MS 100
  966. /** Time to wait for ports to stabilise
  967. *
  968. * This is a policy decision.
  969. */
  970. #define USB_PORT_DELAY_MS 100
  971. /** A USB device ID */
  972. struct usb_device_id {
  973. /** Name */
  974. const char *name;
  975. /** Vendor ID */
  976. uint16_t vendor;
  977. /** Product ID */
  978. uint16_t product;
  979. /** Class */
  980. struct usb_class class;
  981. };
  982. /** Match-anything ID */
  983. #define USB_ANY_ID 0xffff
  984. /** A USB driver */
  985. struct usb_driver {
  986. /** USB ID table */
  987. struct usb_device_id *ids;
  988. /** Number of entries in ID table */
  989. unsigned int id_count;
  990. /**
  991. * Probe device
  992. *
  993. * @v func USB function
  994. * @v config Configuration descriptor
  995. * @ret rc Return status code
  996. */
  997. int ( * probe ) ( struct usb_function *func,
  998. struct usb_configuration_descriptor *config );
  999. /**
  1000. * Remove device
  1001. *
  1002. * @v func USB function
  1003. */
  1004. void ( * remove ) ( struct usb_function *func );
  1005. };
  1006. /** USB driver table */
  1007. #define USB_DRIVERS __table ( struct usb_driver, "usb_drivers" )
  1008. /** Declare a USB driver */
  1009. #define __usb_driver __table_entry ( USB_DRIVERS, 01 )
  1010. #endif /* _IPXE_USB_H */