2
0

core.c 22 KB

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