usbif.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * usbif.h
  4. *
  5. * USB I/O interface for Xen guest OSes.
  6. *
  7. * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
  8. * Author: Noboru Iwamatsu <[email protected]>
  9. */
  10. #ifndef __XEN_PUBLIC_IO_USBIF_H__
  11. #define __XEN_PUBLIC_IO_USBIF_H__
  12. #include "ring.h"
  13. #include "../grant_table.h"
  14. /*
  15. * Detailed Interface Description
  16. * ==============================
  17. * The pvUSB interface is using a split driver design: a frontend driver in
  18. * the guest and a backend driver in a driver domain (normally dom0) having
  19. * access to the physical USB device(s) being passed to the guest.
  20. *
  21. * The frontend and backend drivers use XenStore to initiate the connection
  22. * between them, the I/O activity is handled via two shared ring pages and an
  23. * event channel. As the interface between frontend and backend is at the USB
  24. * host connector level, multiple (up to 31) physical USB devices can be
  25. * handled by a single connection.
  26. *
  27. * The Xen pvUSB device name is "qusb", so the frontend's XenStore entries are
  28. * to be found under "device/qusb", while the backend's XenStore entries are
  29. * under "backend/<guest-dom-id>/qusb".
  30. *
  31. * When a new pvUSB connection is established, the frontend needs to setup the
  32. * two shared ring pages for communication and the event channel. The ring
  33. * pages need to be made available to the backend via the grant table
  34. * interface.
  35. *
  36. * One of the shared ring pages is used by the backend to inform the frontend
  37. * about USB device plug events (device to be added or removed). This is the
  38. * "conn-ring".
  39. *
  40. * The other ring page is used for USB I/O communication (requests and
  41. * responses). This is the "urb-ring".
  42. *
  43. * Feature and Parameter Negotiation
  44. * =================================
  45. * The two halves of a Xen pvUSB driver utilize nodes within the XenStore to
  46. * communicate capabilities and to negotiate operating parameters. This
  47. * section enumerates these nodes which reside in the respective front and
  48. * backend portions of the XenStore, following the XenBus convention.
  49. *
  50. * Any specified default value is in effect if the corresponding XenBus node
  51. * is not present in the XenStore.
  52. *
  53. * XenStore nodes in sections marked "PRIVATE" are solely for use by the
  54. * driver side whose XenBus tree contains them.
  55. *
  56. *****************************************************************************
  57. * Backend XenBus Nodes
  58. *****************************************************************************
  59. *
  60. *------------------ Backend Device Identification (PRIVATE) ------------------
  61. *
  62. * num-ports
  63. * Values: unsigned [1...31]
  64. *
  65. * Number of ports for this (virtual) USB host connector.
  66. *
  67. * usb-ver
  68. * Values: unsigned [1...2]
  69. *
  70. * USB version of this host connector: 1 = USB 1.1, 2 = USB 2.0.
  71. *
  72. * port/[1...31]
  73. * Values: string
  74. *
  75. * Physical USB device connected to the given port, e.g. "3-1.5".
  76. *
  77. *****************************************************************************
  78. * Frontend XenBus Nodes
  79. *****************************************************************************
  80. *
  81. *----------------------- Request Transport Parameters -----------------------
  82. *
  83. * event-channel
  84. * Values: unsigned
  85. *
  86. * The identifier of the Xen event channel used to signal activity
  87. * in the ring buffer.
  88. *
  89. * urb-ring-ref
  90. * Values: unsigned
  91. *
  92. * The Xen grant reference granting permission for the backend to map
  93. * the sole page in a single page sized ring buffer. This is the ring
  94. * buffer for urb requests.
  95. *
  96. * conn-ring-ref
  97. * Values: unsigned
  98. *
  99. * The Xen grant reference granting permission for the backend to map
  100. * the sole page in a single page sized ring buffer. This is the ring
  101. * buffer for connection/disconnection requests.
  102. *
  103. * protocol
  104. * Values: string (XEN_IO_PROTO_ABI_*)
  105. * Default Value: XEN_IO_PROTO_ABI_NATIVE
  106. *
  107. * The machine ABI rules governing the format of all ring request and
  108. * response structures.
  109. *
  110. * Protocol Description
  111. * ====================
  112. *
  113. *-------------------------- USB device plug events --------------------------
  114. *
  115. * USB device plug events are send via the "conn-ring" shared page. As only
  116. * events are being sent, the respective requests from the frontend to the
  117. * backend are just dummy ones.
  118. * The events sent to the frontend have the following layout:
  119. * 0 1 2 3 octet
  120. * +----------------+----------------+----------------+----------------+
  121. * | id | portnum | speed | 4
  122. * +----------------+----------------+----------------+----------------+
  123. * id - uint16_t, event id (taken from the actual frontend dummy request)
  124. * portnum - uint8_t, port number (1 ... 31)
  125. * speed - uint8_t, device XENUSB_SPEED_*, XENUSB_SPEED_NONE == unplug
  126. *
  127. * The dummy request:
  128. * 0 1 octet
  129. * +----------------+----------------+
  130. * | id | 2
  131. * +----------------+----------------+
  132. * id - uint16_t, guest supplied value (no need for being unique)
  133. *
  134. *-------------------------- USB I/O request ---------------------------------
  135. *
  136. * A single USB I/O request on the "urb-ring" has the following layout:
  137. * 0 1 2 3 octet
  138. * +----------------+----------------+----------------+----------------+
  139. * | id | nr_buffer_segs | 4
  140. * +----------------+----------------+----------------+----------------+
  141. * | pipe | 8
  142. * +----------------+----------------+----------------+----------------+
  143. * | transfer_flags | buffer_length | 12
  144. * +----------------+----------------+----------------+----------------+
  145. * | request type specific | 16
  146. * | data | 20
  147. * +----------------+----------------+----------------+----------------+
  148. * | seg[0] | 24
  149. * | data | 28
  150. * +----------------+----------------+----------------+----------------+
  151. * |/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/|
  152. * +----------------+----------------+----------------+----------------+
  153. * | seg[XENUSB_MAX_SEGMENTS_PER_REQUEST - 1] | 144
  154. * | data | 148
  155. * +----------------+----------------+----------------+----------------+
  156. * Bit field bit number 0 is always least significant bit, undefined bits must
  157. * be zero.
  158. * id - uint16_t, guest supplied value
  159. * nr_buffer_segs - uint16_t, number of segment entries in seg[] array
  160. * pipe - uint32_t, bit field with multiple information:
  161. * bits 0-4: port request to send to
  162. * bit 5: unlink request with specified id (cancel I/O) if set (see below)
  163. * bit 7: direction (1 = read from device)
  164. * bits 8-14: device number on port
  165. * bits 15-18: endpoint of device
  166. * bits 30-31: request type: 00 = isochronous, 01 = interrupt,
  167. * 10 = control, 11 = bulk
  168. * transfer_flags - uint16_t, bit field with processing flags:
  169. * bit 0: less data than specified allowed
  170. * buffer_length - uint16_t, total length of data
  171. * request type specific data - 8 bytes, see below
  172. * seg[] - array with 8 byte elements, see below
  173. *
  174. * Request type specific data for isochronous request:
  175. * 0 1 2 3 octet
  176. * +----------------+----------------+----------------+----------------+
  177. * | interval | start_frame | 4
  178. * +----------------+----------------+----------------+----------------+
  179. * | number_of_packets | nr_frame_desc_segs | 8
  180. * +----------------+----------------+----------------+----------------+
  181. * interval - uint16_t, time interval in msecs between frames
  182. * start_frame - uint16_t, start frame number
  183. * number_of_packets - uint16_t, number of packets to transfer
  184. * nr_frame_desc_segs - uint16_t number of seg[] frame descriptors elements
  185. *
  186. * Request type specific data for interrupt request:
  187. * 0 1 2 3 octet
  188. * +----------------+----------------+----------------+----------------+
  189. * | interval | 0 | 4
  190. * +----------------+----------------+----------------+----------------+
  191. * | 0 | 8
  192. * +----------------+----------------+----------------+----------------+
  193. * interval - uint16_t, time in msecs until interruption
  194. *
  195. * Request type specific data for control request:
  196. * 0 1 2 3 octet
  197. * +----------------+----------------+----------------+----------------+
  198. * | data of setup packet | 4
  199. * | | 8
  200. * +----------------+----------------+----------------+----------------+
  201. *
  202. * Request type specific data for bulk request:
  203. * 0 1 2 3 octet
  204. * +----------------+----------------+----------------+----------------+
  205. * | 0 | 4
  206. * | 0 | 8
  207. * +----------------+----------------+----------------+----------------+
  208. *
  209. * Request type specific data for unlink request:
  210. * 0 1 2 3 octet
  211. * +----------------+----------------+----------------+----------------+
  212. * | unlink_id | 0 | 4
  213. * +----------------+----------------+----------------+----------------+
  214. * | 0 | 8
  215. * +----------------+----------------+----------------+----------------+
  216. * unlink_id - uint16_t, request id of request to terminate
  217. *
  218. * seg[] array element layout:
  219. * 0 1 2 3 octet
  220. * +----------------+----------------+----------------+----------------+
  221. * | gref | 4
  222. * +----------------+----------------+----------------+----------------+
  223. * | offset | length | 8
  224. * +----------------+----------------+----------------+----------------+
  225. * gref - uint32_t, grant reference of buffer page
  226. * offset - uint16_t, offset of buffer start in page
  227. * length - uint16_t, length of buffer in page
  228. *
  229. *-------------------------- USB I/O response --------------------------------
  230. *
  231. * 0 1 2 3 octet
  232. * +----------------+----------------+----------------+----------------+
  233. * | id | start_frame | 4
  234. * +----------------+----------------+----------------+----------------+
  235. * | status | 8
  236. * +----------------+----------------+----------------+----------------+
  237. * | actual_length | 12
  238. * +----------------+----------------+----------------+----------------+
  239. * | error_count | 16
  240. * +----------------+----------------+----------------+----------------+
  241. * id - uint16_t, id of the request this response belongs to
  242. * start_frame - uint16_t, start_frame this response (iso requests only)
  243. * status - int32_t, XENUSB_STATUS_* (non-iso requests)
  244. * actual_length - uint32_t, actual size of data transferred
  245. * error_count - uint32_t, number of errors (iso requests)
  246. */
  247. enum xenusb_spec_version {
  248. XENUSB_VER_UNKNOWN = 0,
  249. XENUSB_VER_USB11,
  250. XENUSB_VER_USB20,
  251. XENUSB_VER_USB30, /* not supported yet */
  252. };
  253. /*
  254. * USB pipe in xenusb_request
  255. *
  256. * - port number: bits 0-4
  257. * (USB_MAXCHILDREN is 31)
  258. *
  259. * - operation flag: bit 5
  260. * (0 = submit urb,
  261. * 1 = unlink urb)
  262. *
  263. * - direction: bit 7
  264. * (0 = Host-to-Device [Out]
  265. * 1 = Device-to-Host [In])
  266. *
  267. * - device address: bits 8-14
  268. *
  269. * - endpoint: bits 15-18
  270. *
  271. * - pipe type: bits 30-31
  272. * (00 = isochronous, 01 = interrupt,
  273. * 10 = control, 11 = bulk)
  274. */
  275. #define XENUSB_PIPE_PORT_MASK 0x0000001f
  276. #define XENUSB_PIPE_UNLINK 0x00000020
  277. #define XENUSB_PIPE_DIR 0x00000080
  278. #define XENUSB_PIPE_DEV_MASK 0x0000007f
  279. #define XENUSB_PIPE_DEV_SHIFT 8
  280. #define XENUSB_PIPE_EP_MASK 0x0000000f
  281. #define XENUSB_PIPE_EP_SHIFT 15
  282. #define XENUSB_PIPE_TYPE_MASK 0x00000003
  283. #define XENUSB_PIPE_TYPE_SHIFT 30
  284. #define XENUSB_PIPE_TYPE_ISOC 0
  285. #define XENUSB_PIPE_TYPE_INT 1
  286. #define XENUSB_PIPE_TYPE_CTRL 2
  287. #define XENUSB_PIPE_TYPE_BULK 3
  288. #define xenusb_pipeportnum(pipe) ((pipe) & XENUSB_PIPE_PORT_MASK)
  289. #define xenusb_setportnum_pipe(pipe, portnum) ((pipe) | (portnum))
  290. #define xenusb_pipeunlink(pipe) ((pipe) & XENUSB_PIPE_UNLINK)
  291. #define xenusb_pipesubmit(pipe) (!xenusb_pipeunlink(pipe))
  292. #define xenusb_setunlink_pipe(pipe) ((pipe) | XENUSB_PIPE_UNLINK)
  293. #define xenusb_pipein(pipe) ((pipe) & XENUSB_PIPE_DIR)
  294. #define xenusb_pipeout(pipe) (!xenusb_pipein(pipe))
  295. #define xenusb_pipedevice(pipe) \
  296. (((pipe) >> XENUSB_PIPE_DEV_SHIFT) & XENUSB_PIPE_DEV_MASK)
  297. #define xenusb_pipeendpoint(pipe) \
  298. (((pipe) >> XENUSB_PIPE_EP_SHIFT) & XENUSB_PIPE_EP_MASK)
  299. #define xenusb_pipetype(pipe) \
  300. (((pipe) >> XENUSB_PIPE_TYPE_SHIFT) & XENUSB_PIPE_TYPE_MASK)
  301. #define xenusb_pipeisoc(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_ISOC)
  302. #define xenusb_pipeint(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_INT)
  303. #define xenusb_pipectrl(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_CTRL)
  304. #define xenusb_pipebulk(pipe) (xenusb_pipetype(pipe) == XENUSB_PIPE_TYPE_BULK)
  305. #define XENUSB_MAX_SEGMENTS_PER_REQUEST (16)
  306. #define XENUSB_MAX_PORTNR 31
  307. #define XENUSB_RING_SIZE 4096
  308. /*
  309. * RING for transferring urbs.
  310. */
  311. struct xenusb_request_segment {
  312. grant_ref_t gref;
  313. uint16_t offset;
  314. uint16_t length;
  315. };
  316. struct xenusb_urb_request {
  317. uint16_t id; /* request id */
  318. uint16_t nr_buffer_segs; /* number of urb->transfer_buffer segments */
  319. /* basic urb parameter */
  320. uint32_t pipe;
  321. uint16_t transfer_flags;
  322. #define XENUSB_SHORT_NOT_OK 0x0001
  323. uint16_t buffer_length;
  324. union {
  325. uint8_t ctrl[8]; /* setup_packet (Ctrl) */
  326. struct {
  327. uint16_t interval; /* maximum (1024*8) in usb core */
  328. uint16_t start_frame; /* start frame */
  329. uint16_t number_of_packets; /* number of ISO packet */
  330. uint16_t nr_frame_desc_segs; /* number of iso_frame_desc segments */
  331. } isoc;
  332. struct {
  333. uint16_t interval; /* maximum (1024*8) in usb core */
  334. uint16_t pad[3];
  335. } intr;
  336. struct {
  337. uint16_t unlink_id; /* unlink request id */
  338. uint16_t pad[3];
  339. } unlink;
  340. } u;
  341. /* urb data segments */
  342. struct xenusb_request_segment seg[XENUSB_MAX_SEGMENTS_PER_REQUEST];
  343. };
  344. struct xenusb_urb_response {
  345. uint16_t id; /* request id */
  346. uint16_t start_frame; /* start frame (ISO) */
  347. int32_t status; /* status (non-ISO) */
  348. #define XENUSB_STATUS_OK 0
  349. #define XENUSB_STATUS_NODEV (-19)
  350. #define XENUSB_STATUS_INVAL (-22)
  351. #define XENUSB_STATUS_STALL (-32)
  352. #define XENUSB_STATUS_IOERROR (-71)
  353. #define XENUSB_STATUS_BABBLE (-75)
  354. #define XENUSB_STATUS_SHUTDOWN (-108)
  355. int32_t actual_length; /* actual transfer length */
  356. int32_t error_count; /* number of ISO errors */
  357. };
  358. DEFINE_RING_TYPES(xenusb_urb, struct xenusb_urb_request, struct xenusb_urb_response);
  359. #define XENUSB_URB_RING_SIZE __CONST_RING_SIZE(xenusb_urb, XENUSB_RING_SIZE)
  360. /*
  361. * RING for notifying connect/disconnect events to frontend
  362. */
  363. struct xenusb_conn_request {
  364. uint16_t id;
  365. };
  366. struct xenusb_conn_response {
  367. uint16_t id; /* request id */
  368. uint8_t portnum; /* port number */
  369. uint8_t speed; /* usb_device_speed */
  370. #define XENUSB_SPEED_NONE 0
  371. #define XENUSB_SPEED_LOW 1
  372. #define XENUSB_SPEED_FULL 2
  373. #define XENUSB_SPEED_HIGH 3
  374. };
  375. DEFINE_RING_TYPES(xenusb_conn, struct xenusb_conn_request, struct xenusb_conn_response);
  376. #define XENUSB_CONN_RING_SIZE __CONST_RING_SIZE(xenusb_conn, XENUSB_RING_SIZE)
  377. #endif /* __XEN_PUBLIC_IO_USBIF_H__ */