Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

usb.h 30KB

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