2
0

core.c 23 KB

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