usb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * QEMU USB emulation
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * 2008 Generic packet handler rewrite by Max Krasnyansky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu-common.h"
  27. #include "usb.h"
  28. void usb_attach(USBPort *port, USBDevice *dev)
  29. {
  30. if (dev != NULL) {
  31. /* attach */
  32. if (port->dev) {
  33. usb_attach(port, NULL);
  34. }
  35. dev->port = port;
  36. port->dev = dev;
  37. port->ops->attach(port);
  38. usb_send_msg(dev, USB_MSG_ATTACH);
  39. } else {
  40. /* detach */
  41. dev = port->dev;
  42. assert(dev);
  43. port->ops->detach(port);
  44. usb_send_msg(dev, USB_MSG_DETACH);
  45. dev->port = NULL;
  46. port->dev = NULL;
  47. }
  48. }
  49. void usb_wakeup(USBDevice *dev)
  50. {
  51. if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
  52. dev->port->ops->wakeup(dev->port);
  53. }
  54. }
  55. /**********************/
  56. /* generic USB device helpers (you are not forced to use them when
  57. writing your USB device driver, but they help handling the
  58. protocol)
  59. */
  60. #define SETUP_STATE_IDLE 0
  61. #define SETUP_STATE_SETUP 1
  62. #define SETUP_STATE_DATA 2
  63. #define SETUP_STATE_ACK 3
  64. static int do_token_setup(USBDevice *s, USBPacket *p)
  65. {
  66. int request, value, index;
  67. int ret = 0;
  68. if (p->len != 8)
  69. return USB_RET_STALL;
  70. memcpy(s->setup_buf, p->data, 8);
  71. s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
  72. s->setup_index = 0;
  73. request = (s->setup_buf[0] << 8) | s->setup_buf[1];
  74. value = (s->setup_buf[3] << 8) | s->setup_buf[2];
  75. index = (s->setup_buf[5] << 8) | s->setup_buf[4];
  76. if (s->setup_buf[0] & USB_DIR_IN) {
  77. ret = s->info->handle_control(s, p, request, value, index,
  78. s->setup_len, s->data_buf);
  79. if (ret == USB_RET_ASYNC) {
  80. s->setup_state = SETUP_STATE_SETUP;
  81. return USB_RET_ASYNC;
  82. }
  83. if (ret < 0)
  84. return ret;
  85. if (ret < s->setup_len)
  86. s->setup_len = ret;
  87. s->setup_state = SETUP_STATE_DATA;
  88. } else {
  89. if (s->setup_len > sizeof(s->data_buf)) {
  90. fprintf(stderr,
  91. "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
  92. s->setup_len, sizeof(s->data_buf));
  93. return USB_RET_STALL;
  94. }
  95. if (s->setup_len == 0)
  96. s->setup_state = SETUP_STATE_ACK;
  97. else
  98. s->setup_state = SETUP_STATE_DATA;
  99. }
  100. return ret;
  101. }
  102. static int do_token_in(USBDevice *s, USBPacket *p)
  103. {
  104. int request, value, index;
  105. int ret = 0;
  106. if (p->devep != 0)
  107. return s->info->handle_data(s, p);
  108. request = (s->setup_buf[0] << 8) | s->setup_buf[1];
  109. value = (s->setup_buf[3] << 8) | s->setup_buf[2];
  110. index = (s->setup_buf[5] << 8) | s->setup_buf[4];
  111. switch(s->setup_state) {
  112. case SETUP_STATE_ACK:
  113. if (!(s->setup_buf[0] & USB_DIR_IN)) {
  114. ret = s->info->handle_control(s, p, request, value, index,
  115. s->setup_len, s->data_buf);
  116. if (ret == USB_RET_ASYNC) {
  117. return USB_RET_ASYNC;
  118. }
  119. s->setup_state = SETUP_STATE_IDLE;
  120. if (ret > 0)
  121. return 0;
  122. return ret;
  123. }
  124. /* return 0 byte */
  125. return 0;
  126. case SETUP_STATE_DATA:
  127. if (s->setup_buf[0] & USB_DIR_IN) {
  128. int len = s->setup_len - s->setup_index;
  129. if (len > p->len)
  130. len = p->len;
  131. memcpy(p->data, s->data_buf + s->setup_index, len);
  132. s->setup_index += len;
  133. if (s->setup_index >= s->setup_len)
  134. s->setup_state = SETUP_STATE_ACK;
  135. return len;
  136. }
  137. s->setup_state = SETUP_STATE_IDLE;
  138. return USB_RET_STALL;
  139. default:
  140. return USB_RET_STALL;
  141. }
  142. }
  143. static int do_token_out(USBDevice *s, USBPacket *p)
  144. {
  145. if (p->devep != 0)
  146. return s->info->handle_data(s, p);
  147. switch(s->setup_state) {
  148. case SETUP_STATE_ACK:
  149. if (s->setup_buf[0] & USB_DIR_IN) {
  150. s->setup_state = SETUP_STATE_IDLE;
  151. /* transfer OK */
  152. } else {
  153. /* ignore additional output */
  154. }
  155. return 0;
  156. case SETUP_STATE_DATA:
  157. if (!(s->setup_buf[0] & USB_DIR_IN)) {
  158. int len = s->setup_len - s->setup_index;
  159. if (len > p->len)
  160. len = p->len;
  161. memcpy(s->data_buf + s->setup_index, p->data, len);
  162. s->setup_index += len;
  163. if (s->setup_index >= s->setup_len)
  164. s->setup_state = SETUP_STATE_ACK;
  165. return len;
  166. }
  167. s->setup_state = SETUP_STATE_IDLE;
  168. return USB_RET_STALL;
  169. default:
  170. return USB_RET_STALL;
  171. }
  172. }
  173. /*
  174. * Generic packet handler.
  175. * Called by the HC (host controller).
  176. *
  177. * Returns length of the transaction or one of the USB_RET_XXX codes.
  178. */
  179. int usb_generic_handle_packet(USBDevice *s, USBPacket *p)
  180. {
  181. switch(p->pid) {
  182. case USB_MSG_ATTACH:
  183. s->state = USB_STATE_ATTACHED;
  184. if (s->info->handle_attach) {
  185. s->info->handle_attach(s);
  186. }
  187. return 0;
  188. case USB_MSG_DETACH:
  189. s->state = USB_STATE_NOTATTACHED;
  190. return 0;
  191. case USB_MSG_RESET:
  192. s->remote_wakeup = 0;
  193. s->addr = 0;
  194. s->state = USB_STATE_DEFAULT;
  195. if (s->info->handle_reset) {
  196. s->info->handle_reset(s);
  197. }
  198. return 0;
  199. }
  200. /* Rest of the PIDs must match our address */
  201. if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr)
  202. return USB_RET_NODEV;
  203. switch (p->pid) {
  204. case USB_TOKEN_SETUP:
  205. return do_token_setup(s, p);
  206. case USB_TOKEN_IN:
  207. return do_token_in(s, p);
  208. case USB_TOKEN_OUT:
  209. return do_token_out(s, p);
  210. default:
  211. return USB_RET_STALL;
  212. }
  213. }
  214. /* ctrl complete function for devices which use usb_generic_handle_packet and
  215. may return USB_RET_ASYNC from their handle_control callback. Device code
  216. which does this *must* call this function instead of the normal
  217. usb_packet_complete to complete their async control packets. */
  218. void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
  219. {
  220. if (p->len < 0) {
  221. s->setup_state = SETUP_STATE_IDLE;
  222. }
  223. switch (s->setup_state) {
  224. case SETUP_STATE_SETUP:
  225. if (p->len < s->setup_len) {
  226. s->setup_len = p->len;
  227. }
  228. s->setup_state = SETUP_STATE_DATA;
  229. p->len = 8;
  230. break;
  231. case SETUP_STATE_ACK:
  232. s->setup_state = SETUP_STATE_IDLE;
  233. p->len = 0;
  234. break;
  235. default:
  236. break;
  237. }
  238. usb_packet_complete(s, p);
  239. }
  240. /* XXX: fix overflow */
  241. int set_usb_string(uint8_t *buf, const char *str)
  242. {
  243. int len, i;
  244. uint8_t *q;
  245. q = buf;
  246. len = strlen(str);
  247. *q++ = 2 * len + 2;
  248. *q++ = 3;
  249. for(i = 0; i < len; i++) {
  250. *q++ = str[i];
  251. *q++ = 0;
  252. }
  253. return q - buf;
  254. }
  255. /* Send an internal message to a USB device. */
  256. void usb_send_msg(USBDevice *dev, int msg)
  257. {
  258. USBPacket p;
  259. int ret;
  260. memset(&p, 0, sizeof(p));
  261. p.pid = msg;
  262. ret = usb_handle_packet(dev, &p);
  263. /* This _must_ be synchronous */
  264. assert(ret != USB_RET_ASYNC);
  265. }
  266. /* Hand over a packet to a device for processing. Return value
  267. USB_RET_ASYNC indicates the processing isn't finished yet, the
  268. driver will call usb_packet_complete() when done processing it. */
  269. int usb_handle_packet(USBDevice *dev, USBPacket *p)
  270. {
  271. int ret;
  272. assert(p->owner == NULL);
  273. ret = dev->info->handle_packet(dev, p);
  274. if (ret == USB_RET_ASYNC) {
  275. if (p->owner == NULL) {
  276. p->owner = dev;
  277. } else {
  278. /* We'll end up here when usb_handle_packet is called
  279. * recursively due to a hub being in the chain. Nothing
  280. * to do. Leave p->owner pointing to the device, not the
  281. * hub. */;
  282. }
  283. }
  284. return ret;
  285. }
  286. /* Notify the controller that an async packet is complete. This should only
  287. be called for packets previously deferred by returning USB_RET_ASYNC from
  288. handle_packet. */
  289. void usb_packet_complete(USBDevice *dev, USBPacket *p)
  290. {
  291. /* Note: p->owner != dev is possible in case dev is a hub */
  292. assert(p->owner != NULL);
  293. dev->port->ops->complete(dev->port, p);
  294. p->owner = NULL;
  295. }
  296. /* Cancel an active packet. The packed must have been deferred by
  297. returning USB_RET_ASYNC from handle_packet, and not yet
  298. completed. */
  299. void usb_cancel_packet(USBPacket * p)
  300. {
  301. assert(p->owner != NULL);
  302. p->owner->info->cancel_packet(p->owner, p);
  303. p->owner = NULL;
  304. }