usb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * QEMU USB API
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "block.h"
  25. #include "qdev.h"
  26. #include "qemu-queue.h"
  27. /* Constants related to the USB / PCI interaction */
  28. #define USB_SBRN 0x60 /* Serial Bus Release Number Register */
  29. #define USB_RELEASE_1 0x10 /* USB 1.0 */
  30. #define USB_RELEASE_2 0x20 /* USB 2.0 */
  31. #define USB_RELEASE_3 0x30 /* USB 3.0 */
  32. #define USB_TOKEN_SETUP 0x2d
  33. #define USB_TOKEN_IN 0x69 /* device -> host */
  34. #define USB_TOKEN_OUT 0xe1 /* host -> device */
  35. /* specific usb messages, also sent in the 'pid' parameter */
  36. #define USB_MSG_ATTACH 0x100
  37. #define USB_MSG_DETACH 0x101
  38. #define USB_MSG_RESET 0x102
  39. #define USB_RET_NODEV (-1)
  40. #define USB_RET_NAK (-2)
  41. #define USB_RET_STALL (-3)
  42. #define USB_RET_BABBLE (-4)
  43. #define USB_RET_ASYNC (-5)
  44. #define USB_SPEED_LOW 0
  45. #define USB_SPEED_FULL 1
  46. #define USB_SPEED_HIGH 2
  47. #define USB_SPEED_SUPER 3
  48. #define USB_SPEED_MASK_LOW (1 << USB_SPEED_LOW)
  49. #define USB_SPEED_MASK_FULL (1 << USB_SPEED_FULL)
  50. #define USB_SPEED_MASK_HIGH (1 << USB_SPEED_HIGH)
  51. #define USB_SPEED_MASK_SUPER (1 << USB_SPEED_SUPER)
  52. #define USB_STATE_NOTATTACHED 0
  53. #define USB_STATE_ATTACHED 1
  54. //#define USB_STATE_POWERED 2
  55. #define USB_STATE_DEFAULT 3
  56. //#define USB_STATE_ADDRESS 4
  57. //#define USB_STATE_CONFIGURED 5
  58. #define USB_STATE_SUSPENDED 6
  59. #define USB_CLASS_AUDIO 1
  60. #define USB_CLASS_COMM 2
  61. #define USB_CLASS_HID 3
  62. #define USB_CLASS_PHYSICAL 5
  63. #define USB_CLASS_STILL_IMAGE 6
  64. #define USB_CLASS_PRINTER 7
  65. #define USB_CLASS_MASS_STORAGE 8
  66. #define USB_CLASS_HUB 9
  67. #define USB_CLASS_CDC_DATA 0x0a
  68. #define USB_CLASS_CSCID 0x0b
  69. #define USB_CLASS_CONTENT_SEC 0x0d
  70. #define USB_CLASS_APP_SPEC 0xfe
  71. #define USB_CLASS_VENDOR_SPEC 0xff
  72. #define USB_DIR_OUT 0
  73. #define USB_DIR_IN 0x80
  74. #define USB_TYPE_MASK (0x03 << 5)
  75. #define USB_TYPE_STANDARD (0x00 << 5)
  76. #define USB_TYPE_CLASS (0x01 << 5)
  77. #define USB_TYPE_VENDOR (0x02 << 5)
  78. #define USB_TYPE_RESERVED (0x03 << 5)
  79. #define USB_RECIP_MASK 0x1f
  80. #define USB_RECIP_DEVICE 0x00
  81. #define USB_RECIP_INTERFACE 0x01
  82. #define USB_RECIP_ENDPOINT 0x02
  83. #define USB_RECIP_OTHER 0x03
  84. #define DeviceRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
  85. #define DeviceOutRequest ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_DEVICE)<<8)
  86. #define InterfaceRequest \
  87. ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
  88. #define InterfaceOutRequest \
  89. ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_INTERFACE)<<8)
  90. #define EndpointRequest ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
  91. #define EndpointOutRequest \
  92. ((USB_DIR_OUT|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT)<<8)
  93. #define ClassInterfaceRequest \
  94. ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8)
  95. #define ClassInterfaceOutRequest \
  96. ((USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE)<<8)
  97. #define USB_REQ_GET_STATUS 0x00
  98. #define USB_REQ_CLEAR_FEATURE 0x01
  99. #define USB_REQ_SET_FEATURE 0x03
  100. #define USB_REQ_SET_ADDRESS 0x05
  101. #define USB_REQ_GET_DESCRIPTOR 0x06
  102. #define USB_REQ_SET_DESCRIPTOR 0x07
  103. #define USB_REQ_GET_CONFIGURATION 0x08
  104. #define USB_REQ_SET_CONFIGURATION 0x09
  105. #define USB_REQ_GET_INTERFACE 0x0A
  106. #define USB_REQ_SET_INTERFACE 0x0B
  107. #define USB_REQ_SYNCH_FRAME 0x0C
  108. #define USB_DEVICE_SELF_POWERED 0
  109. #define USB_DEVICE_REMOTE_WAKEUP 1
  110. #define USB_DT_DEVICE 0x01
  111. #define USB_DT_CONFIG 0x02
  112. #define USB_DT_STRING 0x03
  113. #define USB_DT_INTERFACE 0x04
  114. #define USB_DT_ENDPOINT 0x05
  115. #define USB_DT_DEVICE_QUALIFIER 0x06
  116. #define USB_DT_OTHER_SPEED_CONFIG 0x07
  117. #define USB_DT_DEBUG 0x0A
  118. #define USB_DT_INTERFACE_ASSOC 0x0B
  119. #define USB_ENDPOINT_XFER_CONTROL 0
  120. #define USB_ENDPOINT_XFER_ISOC 1
  121. #define USB_ENDPOINT_XFER_BULK 2
  122. #define USB_ENDPOINT_XFER_INT 3
  123. typedef struct USBBus USBBus;
  124. typedef struct USBBusOps USBBusOps;
  125. typedef struct USBPort USBPort;
  126. typedef struct USBDevice USBDevice;
  127. typedef struct USBDeviceInfo USBDeviceInfo;
  128. typedef struct USBPacket USBPacket;
  129. typedef struct USBDesc USBDesc;
  130. typedef struct USBDescID USBDescID;
  131. typedef struct USBDescDevice USBDescDevice;
  132. typedef struct USBDescConfig USBDescConfig;
  133. typedef struct USBDescIfaceAssoc USBDescIfaceAssoc;
  134. typedef struct USBDescIface USBDescIface;
  135. typedef struct USBDescEndpoint USBDescEndpoint;
  136. typedef struct USBDescOther USBDescOther;
  137. typedef struct USBDescString USBDescString;
  138. struct USBDescString {
  139. uint8_t index;
  140. char *str;
  141. QLIST_ENTRY(USBDescString) next;
  142. };
  143. /* definition of a USB device */
  144. struct USBDevice {
  145. DeviceState qdev;
  146. USBDeviceInfo *info;
  147. USBPort *port;
  148. char *port_path;
  149. void *opaque;
  150. /* Actual connected speed */
  151. int speed;
  152. /* Supported speeds, not in info because it may be variable (hostdevs) */
  153. int speedmask;
  154. uint8_t addr;
  155. char product_desc[32];
  156. int auto_attach;
  157. int attached;
  158. int32_t state;
  159. uint8_t setup_buf[8];
  160. uint8_t data_buf[4096];
  161. int32_t remote_wakeup;
  162. int32_t setup_state;
  163. int32_t setup_len;
  164. int32_t setup_index;
  165. QLIST_HEAD(, USBDescString) strings;
  166. const USBDescDevice *device;
  167. const USBDescConfig *config;
  168. };
  169. struct USBDeviceInfo {
  170. DeviceInfo qdev;
  171. int (*init)(USBDevice *dev);
  172. /*
  173. * Process USB packet.
  174. * Called by the HC (Host Controller).
  175. *
  176. * Returns length of the transaction
  177. * or one of the USB_RET_XXX codes.
  178. */
  179. int (*handle_packet)(USBDevice *dev, USBPacket *p);
  180. /*
  181. * Called when a packet is canceled.
  182. */
  183. void (*cancel_packet)(USBDevice *dev, USBPacket *p);
  184. /*
  185. * Called when device is destroyed.
  186. */
  187. void (*handle_destroy)(USBDevice *dev);
  188. /*
  189. * Attach the device
  190. */
  191. void (*handle_attach)(USBDevice *dev);
  192. /*
  193. * Reset the device
  194. */
  195. void (*handle_reset)(USBDevice *dev);
  196. /*
  197. * Process control request.
  198. * Called from handle_packet().
  199. *
  200. * Returns length or one of the USB_RET_ codes.
  201. */
  202. int (*handle_control)(USBDevice *dev, USBPacket *p, int request, int value,
  203. int index, int length, uint8_t *data);
  204. /*
  205. * Process data transfers (both BULK and ISOC).
  206. * Called from handle_packet().
  207. *
  208. * Returns length or one of the USB_RET_ codes.
  209. */
  210. int (*handle_data)(USBDevice *dev, USBPacket *p);
  211. const char *product_desc;
  212. const USBDesc *usb_desc;
  213. /* handle legacy -usbdevice command line options */
  214. const char *usbdevice_name;
  215. USBDevice *(*usbdevice_init)(const char *params);
  216. };
  217. typedef struct USBPortOps {
  218. void (*attach)(USBPort *port);
  219. void (*detach)(USBPort *port);
  220. /*
  221. * This gets called when a device downstream from the device attached to
  222. * the port (iow attached through a hub) gets detached.
  223. */
  224. void (*child_detach)(USBPort *port, USBDevice *child);
  225. void (*wakeup)(USBPort *port);
  226. /*
  227. * Note that port->dev will be different then the device from which
  228. * the packet originated when a hub is involved, if you want the orginating
  229. * device use p->owner
  230. */
  231. void (*complete)(USBPort *port, USBPacket *p);
  232. } USBPortOps;
  233. /* USB port on which a device can be connected */
  234. struct USBPort {
  235. USBDevice *dev;
  236. int speedmask;
  237. char path[16];
  238. USBPortOps *ops;
  239. void *opaque;
  240. int index; /* internal port index, may be used with the opaque */
  241. QTAILQ_ENTRY(USBPort) next;
  242. };
  243. typedef void USBCallback(USBPacket * packet, void *opaque);
  244. /* Structure used to hold information about an active USB packet. */
  245. struct USBPacket {
  246. /* Data fields for use by the driver. */
  247. int pid;
  248. uint8_t devaddr;
  249. uint8_t devep;
  250. QEMUIOVector iov;
  251. int result; /* transfer length or USB_RET_* status code */
  252. /* Internal use by the USB layer. */
  253. USBDevice *owner;
  254. };
  255. void usb_packet_init(USBPacket *p);
  256. void usb_packet_setup(USBPacket *p, int pid, uint8_t addr, uint8_t ep);
  257. void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len);
  258. int usb_packet_map(USBPacket *p, QEMUSGList *sgl);
  259. void usb_packet_unmap(USBPacket *p);
  260. void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes);
  261. void usb_packet_skip(USBPacket *p, size_t bytes);
  262. void usb_packet_cleanup(USBPacket *p);
  263. int usb_handle_packet(USBDevice *dev, USBPacket *p);
  264. void usb_packet_complete(USBDevice *dev, USBPacket *p);
  265. void usb_cancel_packet(USBPacket * p);
  266. void usb_attach(USBPort *port);
  267. void usb_detach(USBPort *port);
  268. void usb_reset(USBPort *port);
  269. void usb_wakeup(USBDevice *dev);
  270. int usb_generic_handle_packet(USBDevice *s, USBPacket *p);
  271. void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p);
  272. int set_usb_string(uint8_t *buf, const char *str);
  273. void usb_send_msg(USBDevice *dev, int msg);
  274. /* usb-linux.c */
  275. USBDevice *usb_host_device_open(const char *devname);
  276. int usb_host_device_close(const char *devname);
  277. void usb_host_info(Monitor *mon);
  278. /* usb-bt.c */
  279. USBDevice *usb_bt_init(HCIInfo *hci);
  280. /* usb ports of the VM */
  281. #define VM_USB_HUB_SIZE 8
  282. /* usb-musb.c */
  283. enum musb_irq_source_e {
  284. musb_irq_suspend = 0,
  285. musb_irq_resume,
  286. musb_irq_rst_babble,
  287. musb_irq_sof,
  288. musb_irq_connect,
  289. musb_irq_disconnect,
  290. musb_irq_vbus_request,
  291. musb_irq_vbus_error,
  292. musb_irq_rx,
  293. musb_irq_tx,
  294. musb_set_vbus,
  295. musb_set_session,
  296. /* Add new interrupts here */
  297. musb_irq_max, /* total number of interrupts defined */
  298. };
  299. typedef struct MUSBState MUSBState;
  300. MUSBState *musb_init(DeviceState *parent_device, int gpio_base);
  301. void musb_reset(MUSBState *s);
  302. uint32_t musb_core_intr_get(MUSBState *s);
  303. void musb_core_intr_clear(MUSBState *s, uint32_t mask);
  304. void musb_set_size(MUSBState *s, int epnum, int size, int is_tx);
  305. /* usb-bus.c */
  306. struct USBBus {
  307. BusState qbus;
  308. USBBusOps *ops;
  309. int busnr;
  310. int nfree;
  311. int nused;
  312. QTAILQ_HEAD(, USBPort) free;
  313. QTAILQ_HEAD(, USBPort) used;
  314. QTAILQ_ENTRY(USBBus) next;
  315. };
  316. struct USBBusOps {
  317. int (*register_companion)(USBBus *bus, USBPort *ports[],
  318. uint32_t portcount, uint32_t firstport);
  319. };
  320. void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host);
  321. USBBus *usb_bus_find(int busnr);
  322. void usb_qdev_register(USBDeviceInfo *info);
  323. void usb_qdev_register_many(USBDeviceInfo *info);
  324. USBDevice *usb_create(USBBus *bus, const char *name);
  325. USBDevice *usb_create_simple(USBBus *bus, const char *name);
  326. USBDevice *usbdevice_create(const char *cmdline);
  327. void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
  328. USBPortOps *ops, int speedmask);
  329. int usb_register_companion(const char *masterbus, USBPort *ports[],
  330. uint32_t portcount, uint32_t firstport,
  331. void *opaque, USBPortOps *ops, int speedmask);
  332. void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr);
  333. void usb_unregister_port(USBBus *bus, USBPort *port);
  334. int usb_claim_port(USBDevice *dev);
  335. void usb_release_port(USBDevice *dev);
  336. int usb_device_attach(USBDevice *dev);
  337. int usb_device_detach(USBDevice *dev);
  338. int usb_device_delete_addr(int busnr, int addr);
  339. static inline USBBus *usb_bus_from_device(USBDevice *d)
  340. {
  341. return DO_UPCAST(USBBus, qbus, d->qdev.parent_bus);
  342. }