2
0

core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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/osdep.h"
  27. #include "hw/usb.h"
  28. #include "qemu/iov.h"
  29. #include "trace.h"
  30. void usb_pick_speed(USBPort *port)
  31. {
  32. static const int speeds[] = {
  33. USB_SPEED_SUPER,
  34. USB_SPEED_HIGH,
  35. USB_SPEED_FULL,
  36. USB_SPEED_LOW,
  37. };
  38. USBDevice *udev = port->dev;
  39. int i;
  40. for (i = 0; i < ARRAY_SIZE(speeds); i++) {
  41. if ((udev->speedmask & (1 << speeds[i])) &&
  42. (port->speedmask & (1 << speeds[i]))) {
  43. udev->speed = speeds[i];
  44. return;
  45. }
  46. }
  47. }
  48. void usb_attach(USBPort *port)
  49. {
  50. USBDevice *dev = port->dev;
  51. assert(dev != NULL);
  52. assert(dev->attached);
  53. assert(dev->state == USB_STATE_NOTATTACHED);
  54. usb_pick_speed(port);
  55. port->ops->attach(port);
  56. dev->state = USB_STATE_ATTACHED;
  57. usb_device_handle_attach(dev);
  58. }
  59. void usb_detach(USBPort *port)
  60. {
  61. USBDevice *dev = port->dev;
  62. assert(dev != NULL);
  63. assert(dev->state != USB_STATE_NOTATTACHED);
  64. port->ops->detach(port);
  65. dev->state = USB_STATE_NOTATTACHED;
  66. }
  67. void usb_port_reset(USBPort *port)
  68. {
  69. USBDevice *dev = port->dev;
  70. assert(dev != NULL);
  71. usb_detach(port);
  72. usb_attach(port);
  73. usb_device_reset(dev);
  74. }
  75. void usb_device_reset(USBDevice *dev)
  76. {
  77. if (dev == NULL || !dev->attached) {
  78. return;
  79. }
  80. usb_device_handle_reset(dev);
  81. dev->remote_wakeup = 0;
  82. dev->addr = 0;
  83. dev->state = USB_STATE_DEFAULT;
  84. }
  85. void usb_wakeup(USBEndpoint *ep, unsigned int stream)
  86. {
  87. USBDevice *dev = ep->dev;
  88. USBBus *bus = usb_bus_from_device(dev);
  89. if (!qdev_hotplug) {
  90. /*
  91. * This is machine init cold plug. No need to wakeup anyone,
  92. * all devices will be reset anyway. And trying to wakeup can
  93. * cause problems due to hitting uninitialized devices.
  94. */
  95. return;
  96. }
  97. if (dev->remote_wakeup && dev->port && dev->port->ops->wakeup) {
  98. dev->port->ops->wakeup(dev->port);
  99. }
  100. if (bus->ops->wakeup_endpoint) {
  101. bus->ops->wakeup_endpoint(bus, ep, stream);
  102. }
  103. }
  104. /**********************/
  105. /* generic USB device helpers (you are not forced to use them when
  106. writing your USB device driver, but they help handling the
  107. protocol)
  108. */
  109. #define SETUP_STATE_IDLE 0
  110. #define SETUP_STATE_SETUP 1
  111. #define SETUP_STATE_DATA 2
  112. #define SETUP_STATE_ACK 3
  113. #define SETUP_STATE_PARAM 4
  114. static void do_token_setup(USBDevice *s, USBPacket *p)
  115. {
  116. int request, value, index;
  117. if (p->iov.size != 8) {
  118. p->status = USB_RET_STALL;
  119. return;
  120. }
  121. usb_packet_copy(p, s->setup_buf, p->iov.size);
  122. s->setup_index = 0;
  123. p->actual_length = 0;
  124. s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
  125. if (s->setup_len > sizeof(s->data_buf)) {
  126. fprintf(stderr,
  127. "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
  128. s->setup_len, sizeof(s->data_buf));
  129. p->status = USB_RET_STALL;
  130. return;
  131. }
  132. request = (s->setup_buf[0] << 8) | s->setup_buf[1];
  133. value = (s->setup_buf[3] << 8) | s->setup_buf[2];
  134. index = (s->setup_buf[5] << 8) | s->setup_buf[4];
  135. if (s->setup_buf[0] & USB_DIR_IN) {
  136. usb_device_handle_control(s, p, request, value, index,
  137. s->setup_len, s->data_buf);
  138. if (p->status == USB_RET_ASYNC) {
  139. s->setup_state = SETUP_STATE_SETUP;
  140. }
  141. if (p->status != USB_RET_SUCCESS) {
  142. return;
  143. }
  144. if (p->actual_length < s->setup_len) {
  145. s->setup_len = p->actual_length;
  146. }
  147. s->setup_state = SETUP_STATE_DATA;
  148. } else {
  149. if (s->setup_len == 0)
  150. s->setup_state = SETUP_STATE_ACK;
  151. else
  152. s->setup_state = SETUP_STATE_DATA;
  153. }
  154. p->actual_length = 8;
  155. }
  156. static void do_token_in(USBDevice *s, USBPacket *p)
  157. {
  158. int request, value, index;
  159. assert(p->ep->nr == 0);
  160. request = (s->setup_buf[0] << 8) | s->setup_buf[1];
  161. value = (s->setup_buf[3] << 8) | s->setup_buf[2];
  162. index = (s->setup_buf[5] << 8) | s->setup_buf[4];
  163. switch(s->setup_state) {
  164. case SETUP_STATE_ACK:
  165. if (!(s->setup_buf[0] & USB_DIR_IN)) {
  166. usb_device_handle_control(s, p, request, value, index,
  167. s->setup_len, s->data_buf);
  168. if (p->status == USB_RET_ASYNC) {
  169. return;
  170. }
  171. s->setup_state = SETUP_STATE_IDLE;
  172. p->actual_length = 0;
  173. }
  174. break;
  175. case SETUP_STATE_DATA:
  176. if (s->setup_buf[0] & USB_DIR_IN) {
  177. int len = s->setup_len - s->setup_index;
  178. if (len > p->iov.size) {
  179. len = p->iov.size;
  180. }
  181. usb_packet_copy(p, s->data_buf + s->setup_index, len);
  182. s->setup_index += len;
  183. if (s->setup_index >= s->setup_len) {
  184. s->setup_state = SETUP_STATE_ACK;
  185. }
  186. return;
  187. }
  188. s->setup_state = SETUP_STATE_IDLE;
  189. p->status = USB_RET_STALL;
  190. break;
  191. default:
  192. p->status = USB_RET_STALL;
  193. }
  194. }
  195. static void do_token_out(USBDevice *s, USBPacket *p)
  196. {
  197. assert(p->ep->nr == 0);
  198. switch(s->setup_state) {
  199. case SETUP_STATE_ACK:
  200. if (s->setup_buf[0] & USB_DIR_IN) {
  201. s->setup_state = SETUP_STATE_IDLE;
  202. /* transfer OK */
  203. } else {
  204. /* ignore additional output */
  205. }
  206. break;
  207. case SETUP_STATE_DATA:
  208. if (!(s->setup_buf[0] & USB_DIR_IN)) {
  209. int len = s->setup_len - s->setup_index;
  210. if (len > p->iov.size) {
  211. len = p->iov.size;
  212. }
  213. usb_packet_copy(p, s->data_buf + s->setup_index, len);
  214. s->setup_index += len;
  215. if (s->setup_index >= s->setup_len) {
  216. s->setup_state = SETUP_STATE_ACK;
  217. }
  218. return;
  219. }
  220. s->setup_state = SETUP_STATE_IDLE;
  221. p->status = USB_RET_STALL;
  222. break;
  223. default:
  224. p->status = USB_RET_STALL;
  225. }
  226. }
  227. static void do_parameter(USBDevice *s, USBPacket *p)
  228. {
  229. int i, request, value, index;
  230. for (i = 0; i < 8; i++) {
  231. s->setup_buf[i] = p->parameter >> (i*8);
  232. }
  233. s->setup_state = SETUP_STATE_PARAM;
  234. s->setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
  235. s->setup_index = 0;
  236. request = (s->setup_buf[0] << 8) | s->setup_buf[1];
  237. value = (s->setup_buf[3] << 8) | s->setup_buf[2];
  238. index = (s->setup_buf[5] << 8) | s->setup_buf[4];
  239. if (s->setup_len > sizeof(s->data_buf)) {
  240. fprintf(stderr,
  241. "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
  242. s->setup_len, sizeof(s->data_buf));
  243. p->status = USB_RET_STALL;
  244. return;
  245. }
  246. if (p->pid == USB_TOKEN_OUT) {
  247. usb_packet_copy(p, s->data_buf, s->setup_len);
  248. }
  249. usb_device_handle_control(s, p, request, value, index,
  250. s->setup_len, s->data_buf);
  251. if (p->status == USB_RET_ASYNC) {
  252. return;
  253. }
  254. if (p->actual_length < s->setup_len) {
  255. s->setup_len = p->actual_length;
  256. }
  257. if (p->pid == USB_TOKEN_IN) {
  258. p->actual_length = 0;
  259. usb_packet_copy(p, s->data_buf, s->setup_len);
  260. }
  261. }
  262. /* ctrl complete function for devices which use usb_generic_handle_packet and
  263. may return USB_RET_ASYNC from their handle_control callback. Device code
  264. which does this *must* call this function instead of the normal
  265. usb_packet_complete to complete their async control packets. */
  266. void usb_generic_async_ctrl_complete(USBDevice *s, USBPacket *p)
  267. {
  268. if (p->status < 0) {
  269. s->setup_state = SETUP_STATE_IDLE;
  270. }
  271. switch (s->setup_state) {
  272. case SETUP_STATE_SETUP:
  273. if (p->actual_length < s->setup_len) {
  274. s->setup_len = p->actual_length;
  275. }
  276. s->setup_state = SETUP_STATE_DATA;
  277. p->actual_length = 8;
  278. break;
  279. case SETUP_STATE_ACK:
  280. s->setup_state = SETUP_STATE_IDLE;
  281. p->actual_length = 0;
  282. break;
  283. case SETUP_STATE_PARAM:
  284. if (p->actual_length < s->setup_len) {
  285. s->setup_len = p->actual_length;
  286. }
  287. if (p->pid == USB_TOKEN_IN) {
  288. p->actual_length = 0;
  289. usb_packet_copy(p, s->data_buf, s->setup_len);
  290. }
  291. break;
  292. default:
  293. break;
  294. }
  295. usb_packet_complete(s, p);
  296. }
  297. USBDevice *usb_find_device(USBPort *port, uint8_t addr)
  298. {
  299. USBDevice *dev = port->dev;
  300. if (dev == NULL || !dev->attached || dev->state != USB_STATE_DEFAULT) {
  301. return NULL;
  302. }
  303. if (dev->addr == addr) {
  304. return dev;
  305. }
  306. return usb_device_find_device(dev, addr);
  307. }
  308. static void usb_process_one(USBPacket *p)
  309. {
  310. USBDevice *dev = p->ep->dev;
  311. /*
  312. * Handlers expect status to be initialized to USB_RET_SUCCESS, but it
  313. * can be USB_RET_NAK here from a previous usb_process_one() call,
  314. * or USB_RET_ASYNC from going through usb_queue_one().
  315. */
  316. p->status = USB_RET_SUCCESS;
  317. if (p->ep->nr == 0) {
  318. /* control pipe */
  319. if (p->parameter) {
  320. do_parameter(dev, p);
  321. return;
  322. }
  323. switch (p->pid) {
  324. case USB_TOKEN_SETUP:
  325. do_token_setup(dev, p);
  326. break;
  327. case USB_TOKEN_IN:
  328. do_token_in(dev, p);
  329. break;
  330. case USB_TOKEN_OUT:
  331. do_token_out(dev, p);
  332. break;
  333. default:
  334. p->status = USB_RET_STALL;
  335. }
  336. } else {
  337. /* data pipe */
  338. usb_device_handle_data(dev, p);
  339. }
  340. }
  341. static void usb_queue_one(USBPacket *p)
  342. {
  343. usb_packet_set_state(p, USB_PACKET_QUEUED);
  344. QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
  345. p->status = USB_RET_ASYNC;
  346. }
  347. /* Hand over a packet to a device for processing. p->status ==
  348. USB_RET_ASYNC indicates the processing isn't finished yet, the
  349. driver will call usb_packet_complete() when done processing it. */
  350. void usb_handle_packet(USBDevice *dev, USBPacket *p)
  351. {
  352. if (dev == NULL) {
  353. p->status = USB_RET_NODEV;
  354. return;
  355. }
  356. assert(dev == p->ep->dev);
  357. assert(dev->state == USB_STATE_DEFAULT);
  358. usb_packet_check_state(p, USB_PACKET_SETUP);
  359. assert(p->ep != NULL);
  360. /* Submitting a new packet clears halt */
  361. if (p->ep->halted) {
  362. assert(QTAILQ_EMPTY(&p->ep->queue));
  363. p->ep->halted = false;
  364. }
  365. if (QTAILQ_EMPTY(&p->ep->queue) || p->ep->pipeline || p->stream) {
  366. usb_process_one(p);
  367. if (p->status == USB_RET_ASYNC) {
  368. /* hcd drivers cannot handle async for isoc */
  369. assert(p->ep->type != USB_ENDPOINT_XFER_ISOC);
  370. /* using async for interrupt packets breaks migration */
  371. assert(p->ep->type != USB_ENDPOINT_XFER_INT ||
  372. (dev->flags & (1 << USB_DEV_FLAG_IS_HOST)));
  373. usb_packet_set_state(p, USB_PACKET_ASYNC);
  374. QTAILQ_INSERT_TAIL(&p->ep->queue, p, queue);
  375. } else if (p->status == USB_RET_ADD_TO_QUEUE) {
  376. usb_queue_one(p);
  377. } else {
  378. /*
  379. * When pipelining is enabled usb-devices must always return async,
  380. * otherwise packets can complete out of order!
  381. */
  382. assert(p->stream || !p->ep->pipeline ||
  383. QTAILQ_EMPTY(&p->ep->queue));
  384. if (p->status != USB_RET_NAK) {
  385. usb_packet_set_state(p, USB_PACKET_COMPLETE);
  386. }
  387. }
  388. } else {
  389. usb_queue_one(p);
  390. }
  391. }
  392. void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
  393. {
  394. USBEndpoint *ep = p->ep;
  395. assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
  396. assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
  397. if (p->status != USB_RET_SUCCESS ||
  398. (p->short_not_ok && (p->actual_length < p->iov.size))) {
  399. ep->halted = true;
  400. }
  401. usb_packet_set_state(p, USB_PACKET_COMPLETE);
  402. QTAILQ_REMOVE(&ep->queue, p, queue);
  403. dev->port->ops->complete(dev->port, p);
  404. }
  405. /* Notify the controller that an async packet is complete. This should only
  406. be called for packets previously deferred by returning USB_RET_ASYNC from
  407. handle_packet. */
  408. void usb_packet_complete(USBDevice *dev, USBPacket *p)
  409. {
  410. USBEndpoint *ep = p->ep;
  411. usb_packet_check_state(p, USB_PACKET_ASYNC);
  412. usb_packet_complete_one(dev, p);
  413. while (!QTAILQ_EMPTY(&ep->queue)) {
  414. p = QTAILQ_FIRST(&ep->queue);
  415. if (ep->halted) {
  416. /* Empty the queue on a halt */
  417. p->status = USB_RET_REMOVE_FROM_QUEUE;
  418. dev->port->ops->complete(dev->port, p);
  419. continue;
  420. }
  421. if (p->state == USB_PACKET_ASYNC) {
  422. break;
  423. }
  424. usb_packet_check_state(p, USB_PACKET_QUEUED);
  425. usb_process_one(p);
  426. if (p->status == USB_RET_ASYNC) {
  427. usb_packet_set_state(p, USB_PACKET_ASYNC);
  428. break;
  429. }
  430. usb_packet_complete_one(ep->dev, p);
  431. }
  432. }
  433. /* Cancel an active packet. The packed must have been deferred by
  434. returning USB_RET_ASYNC from handle_packet, and not yet
  435. completed. */
  436. void usb_cancel_packet(USBPacket * p)
  437. {
  438. bool callback = (p->state == USB_PACKET_ASYNC);
  439. assert(usb_packet_is_inflight(p));
  440. usb_packet_set_state(p, USB_PACKET_CANCELED);
  441. QTAILQ_REMOVE(&p->ep->queue, p, queue);
  442. if (callback) {
  443. usb_device_cancel_packet(p->ep->dev, p);
  444. }
  445. }
  446. void usb_packet_init(USBPacket *p)
  447. {
  448. qemu_iovec_init(&p->iov, 1);
  449. }
  450. static const char *usb_packet_state_name(USBPacketState state)
  451. {
  452. static const char *name[] = {
  453. [USB_PACKET_UNDEFINED] = "undef",
  454. [USB_PACKET_SETUP] = "setup",
  455. [USB_PACKET_QUEUED] = "queued",
  456. [USB_PACKET_ASYNC] = "async",
  457. [USB_PACKET_COMPLETE] = "complete",
  458. [USB_PACKET_CANCELED] = "canceled",
  459. };
  460. if (state < ARRAY_SIZE(name)) {
  461. return name[state];
  462. }
  463. return "INVALID";
  464. }
  465. void usb_packet_check_state(USBPacket *p, USBPacketState expected)
  466. {
  467. USBDevice *dev;
  468. USBBus *bus;
  469. if (p->state == expected) {
  470. return;
  471. }
  472. dev = p->ep->dev;
  473. bus = usb_bus_from_device(dev);
  474. trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
  475. usb_packet_state_name(p->state),
  476. usb_packet_state_name(expected));
  477. assert(!"usb packet state check failed");
  478. }
  479. void usb_packet_set_state(USBPacket *p, USBPacketState state)
  480. {
  481. if (p->ep) {
  482. USBDevice *dev = p->ep->dev;
  483. USBBus *bus = usb_bus_from_device(dev);
  484. trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
  485. usb_packet_state_name(p->state),
  486. usb_packet_state_name(state));
  487. } else {
  488. trace_usb_packet_state_change(-1, "", -1, p,
  489. usb_packet_state_name(p->state),
  490. usb_packet_state_name(state));
  491. }
  492. p->state = state;
  493. }
  494. void usb_packet_setup(USBPacket *p, int pid,
  495. USBEndpoint *ep, unsigned int stream,
  496. uint64_t id, bool short_not_ok, bool int_req)
  497. {
  498. assert(!usb_packet_is_inflight(p));
  499. assert(p->iov.iov != NULL);
  500. p->id = id;
  501. p->pid = pid;
  502. p->ep = ep;
  503. p->stream = stream;
  504. p->status = USB_RET_SUCCESS;
  505. p->actual_length = 0;
  506. p->parameter = 0;
  507. p->short_not_ok = short_not_ok;
  508. p->int_req = int_req;
  509. p->combined = NULL;
  510. qemu_iovec_reset(&p->iov);
  511. usb_packet_set_state(p, USB_PACKET_SETUP);
  512. }
  513. void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
  514. {
  515. qemu_iovec_add(&p->iov, ptr, len);
  516. }
  517. void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
  518. {
  519. QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
  520. assert(p->actual_length >= 0);
  521. assert(p->actual_length + bytes <= iov->size);
  522. switch (p->pid) {
  523. case USB_TOKEN_SETUP:
  524. case USB_TOKEN_OUT:
  525. iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
  526. break;
  527. case USB_TOKEN_IN:
  528. iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
  529. break;
  530. default:
  531. fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
  532. abort();
  533. }
  534. p->actual_length += bytes;
  535. }
  536. void usb_packet_skip(USBPacket *p, size_t bytes)
  537. {
  538. QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
  539. assert(p->actual_length >= 0);
  540. assert(p->actual_length + bytes <= iov->size);
  541. if (p->pid == USB_TOKEN_IN) {
  542. iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
  543. }
  544. p->actual_length += bytes;
  545. }
  546. size_t usb_packet_size(USBPacket *p)
  547. {
  548. return p->combined ? p->combined->iov.size : p->iov.size;
  549. }
  550. void usb_packet_cleanup(USBPacket *p)
  551. {
  552. assert(!usb_packet_is_inflight(p));
  553. qemu_iovec_destroy(&p->iov);
  554. }
  555. void usb_ep_reset(USBDevice *dev)
  556. {
  557. int ep;
  558. dev->ep_ctl.nr = 0;
  559. dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
  560. dev->ep_ctl.ifnum = 0;
  561. dev->ep_ctl.max_packet_size = 64;
  562. dev->ep_ctl.max_streams = 0;
  563. dev->ep_ctl.dev = dev;
  564. dev->ep_ctl.pipeline = false;
  565. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  566. dev->ep_in[ep].nr = ep + 1;
  567. dev->ep_out[ep].nr = ep + 1;
  568. dev->ep_in[ep].pid = USB_TOKEN_IN;
  569. dev->ep_out[ep].pid = USB_TOKEN_OUT;
  570. dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
  571. dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
  572. dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
  573. dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
  574. dev->ep_in[ep].max_packet_size = 0;
  575. dev->ep_out[ep].max_packet_size = 0;
  576. dev->ep_in[ep].max_streams = 0;
  577. dev->ep_out[ep].max_streams = 0;
  578. dev->ep_in[ep].dev = dev;
  579. dev->ep_out[ep].dev = dev;
  580. dev->ep_in[ep].pipeline = false;
  581. dev->ep_out[ep].pipeline = false;
  582. }
  583. }
  584. void usb_ep_init(USBDevice *dev)
  585. {
  586. int ep;
  587. usb_ep_reset(dev);
  588. QTAILQ_INIT(&dev->ep_ctl.queue);
  589. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  590. QTAILQ_INIT(&dev->ep_in[ep].queue);
  591. QTAILQ_INIT(&dev->ep_out[ep].queue);
  592. }
  593. }
  594. void usb_ep_dump(USBDevice *dev)
  595. {
  596. static const char *tname[] = {
  597. [USB_ENDPOINT_XFER_CONTROL] = "control",
  598. [USB_ENDPOINT_XFER_ISOC] = "isoc",
  599. [USB_ENDPOINT_XFER_BULK] = "bulk",
  600. [USB_ENDPOINT_XFER_INT] = "int",
  601. };
  602. int ifnum, ep, first;
  603. fprintf(stderr, "Device \"%s\", config %d\n",
  604. dev->product_desc, dev->configuration);
  605. for (ifnum = 0; ifnum < 16; ifnum++) {
  606. first = 1;
  607. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  608. if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
  609. dev->ep_in[ep].ifnum == ifnum) {
  610. if (first) {
  611. first = 0;
  612. fprintf(stderr, " Interface %d, alternative %d\n",
  613. ifnum, dev->altsetting[ifnum]);
  614. }
  615. fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
  616. tname[dev->ep_in[ep].type],
  617. dev->ep_in[ep].max_packet_size);
  618. }
  619. if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
  620. dev->ep_out[ep].ifnum == ifnum) {
  621. if (first) {
  622. first = 0;
  623. fprintf(stderr, " Interface %d, alternative %d\n",
  624. ifnum, dev->altsetting[ifnum]);
  625. }
  626. fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
  627. tname[dev->ep_out[ep].type],
  628. dev->ep_out[ep].max_packet_size);
  629. }
  630. }
  631. }
  632. fprintf(stderr, "--\n");
  633. }
  634. struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
  635. {
  636. struct USBEndpoint *eps;
  637. assert(dev != NULL);
  638. if (ep == 0) {
  639. return &dev->ep_ctl;
  640. }
  641. assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
  642. assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
  643. eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
  644. return eps + ep - 1;
  645. }
  646. uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
  647. {
  648. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  649. return uep->type;
  650. }
  651. void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
  652. {
  653. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  654. uep->type = type;
  655. }
  656. void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
  657. {
  658. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  659. uep->ifnum = ifnum;
  660. }
  661. void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
  662. uint16_t raw)
  663. {
  664. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  665. int size, microframes;
  666. size = raw & 0x7ff;
  667. switch ((raw >> 11) & 3) {
  668. case 1:
  669. microframes = 2;
  670. break;
  671. case 2:
  672. microframes = 3;
  673. break;
  674. default:
  675. microframes = 1;
  676. break;
  677. }
  678. uep->max_packet_size = size * microframes;
  679. }
  680. void usb_ep_set_max_streams(USBDevice *dev, int pid, int ep, uint8_t raw)
  681. {
  682. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  683. int MaxStreams;
  684. MaxStreams = raw & 0x1f;
  685. if (MaxStreams) {
  686. uep->max_streams = 1 << MaxStreams;
  687. } else {
  688. uep->max_streams = 0;
  689. }
  690. }
  691. void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
  692. {
  693. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  694. uep->halted = halted;
  695. }
  696. USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
  697. uint64_t id)
  698. {
  699. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  700. USBPacket *p;
  701. QTAILQ_FOREACH(p, &uep->queue, queue) {
  702. if (p->id == id) {
  703. return p;
  704. }
  705. }
  706. return NULL;
  707. }