core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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, unsigned int stream)
  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, stream);
  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 || p->stream) {
  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 & (1 << 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->stream || !p->ep->pipeline ||
  371. QTAILQ_EMPTY(&p->ep->queue));
  372. if (p->status != USB_RET_NAK) {
  373. usb_packet_set_state(p, USB_PACKET_COMPLETE);
  374. }
  375. }
  376. } else {
  377. usb_queue_one(p);
  378. }
  379. }
  380. void usb_packet_complete_one(USBDevice *dev, USBPacket *p)
  381. {
  382. USBEndpoint *ep = p->ep;
  383. assert(p->stream || QTAILQ_FIRST(&ep->queue) == p);
  384. assert(p->status != USB_RET_ASYNC && p->status != USB_RET_NAK);
  385. if (p->status != USB_RET_SUCCESS ||
  386. (p->short_not_ok && (p->actual_length < p->iov.size))) {
  387. ep->halted = true;
  388. }
  389. usb_packet_set_state(p, USB_PACKET_COMPLETE);
  390. QTAILQ_REMOVE(&ep->queue, p, queue);
  391. dev->port->ops->complete(dev->port, p);
  392. }
  393. /* Notify the controller that an async packet is complete. This should only
  394. be called for packets previously deferred by returning USB_RET_ASYNC from
  395. handle_packet. */
  396. void usb_packet_complete(USBDevice *dev, USBPacket *p)
  397. {
  398. USBEndpoint *ep = p->ep;
  399. usb_packet_check_state(p, USB_PACKET_ASYNC);
  400. usb_packet_complete_one(dev, p);
  401. while (!QTAILQ_EMPTY(&ep->queue)) {
  402. p = QTAILQ_FIRST(&ep->queue);
  403. if (ep->halted) {
  404. /* Empty the queue on a halt */
  405. p->status = USB_RET_REMOVE_FROM_QUEUE;
  406. dev->port->ops->complete(dev->port, p);
  407. continue;
  408. }
  409. if (p->state == USB_PACKET_ASYNC) {
  410. break;
  411. }
  412. usb_packet_check_state(p, USB_PACKET_QUEUED);
  413. usb_process_one(p);
  414. if (p->status == USB_RET_ASYNC) {
  415. usb_packet_set_state(p, USB_PACKET_ASYNC);
  416. break;
  417. }
  418. usb_packet_complete_one(ep->dev, p);
  419. }
  420. }
  421. /* Cancel an active packet. The packed must have been deferred by
  422. returning USB_RET_ASYNC from handle_packet, and not yet
  423. completed. */
  424. void usb_cancel_packet(USBPacket * p)
  425. {
  426. bool callback = (p->state == USB_PACKET_ASYNC);
  427. assert(usb_packet_is_inflight(p));
  428. usb_packet_set_state(p, USB_PACKET_CANCELED);
  429. QTAILQ_REMOVE(&p->ep->queue, p, queue);
  430. if (callback) {
  431. usb_device_cancel_packet(p->ep->dev, p);
  432. }
  433. }
  434. void usb_packet_init(USBPacket *p)
  435. {
  436. qemu_iovec_init(&p->iov, 1);
  437. }
  438. static const char *usb_packet_state_name(USBPacketState state)
  439. {
  440. static const char *name[] = {
  441. [USB_PACKET_UNDEFINED] = "undef",
  442. [USB_PACKET_SETUP] = "setup",
  443. [USB_PACKET_QUEUED] = "queued",
  444. [USB_PACKET_ASYNC] = "async",
  445. [USB_PACKET_COMPLETE] = "complete",
  446. [USB_PACKET_CANCELED] = "canceled",
  447. };
  448. if (state < ARRAY_SIZE(name)) {
  449. return name[state];
  450. }
  451. return "INVALID";
  452. }
  453. void usb_packet_check_state(USBPacket *p, USBPacketState expected)
  454. {
  455. USBDevice *dev;
  456. USBBus *bus;
  457. if (p->state == expected) {
  458. return;
  459. }
  460. dev = p->ep->dev;
  461. bus = usb_bus_from_device(dev);
  462. trace_usb_packet_state_fault(bus->busnr, dev->port->path, p->ep->nr, p,
  463. usb_packet_state_name(p->state),
  464. usb_packet_state_name(expected));
  465. assert(!"usb packet state check failed");
  466. }
  467. void usb_packet_set_state(USBPacket *p, USBPacketState state)
  468. {
  469. if (p->ep) {
  470. USBDevice *dev = p->ep->dev;
  471. USBBus *bus = usb_bus_from_device(dev);
  472. trace_usb_packet_state_change(bus->busnr, dev->port->path, p->ep->nr, p,
  473. usb_packet_state_name(p->state),
  474. usb_packet_state_name(state));
  475. } else {
  476. trace_usb_packet_state_change(-1, "", -1, p,
  477. usb_packet_state_name(p->state),
  478. usb_packet_state_name(state));
  479. }
  480. p->state = state;
  481. }
  482. void usb_packet_setup(USBPacket *p, int pid,
  483. USBEndpoint *ep, unsigned int stream,
  484. uint64_t id, bool short_not_ok, bool int_req)
  485. {
  486. assert(!usb_packet_is_inflight(p));
  487. assert(p->iov.iov != NULL);
  488. p->id = id;
  489. p->pid = pid;
  490. p->ep = ep;
  491. p->stream = stream;
  492. p->status = USB_RET_SUCCESS;
  493. p->actual_length = 0;
  494. p->parameter = 0;
  495. p->short_not_ok = short_not_ok;
  496. p->int_req = int_req;
  497. p->combined = NULL;
  498. qemu_iovec_reset(&p->iov);
  499. usb_packet_set_state(p, USB_PACKET_SETUP);
  500. }
  501. void usb_packet_addbuf(USBPacket *p, void *ptr, size_t len)
  502. {
  503. qemu_iovec_add(&p->iov, ptr, len);
  504. }
  505. void usb_packet_copy(USBPacket *p, void *ptr, size_t bytes)
  506. {
  507. QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
  508. assert(p->actual_length >= 0);
  509. assert(p->actual_length + bytes <= iov->size);
  510. switch (p->pid) {
  511. case USB_TOKEN_SETUP:
  512. case USB_TOKEN_OUT:
  513. iov_to_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
  514. break;
  515. case USB_TOKEN_IN:
  516. iov_from_buf(iov->iov, iov->niov, p->actual_length, ptr, bytes);
  517. break;
  518. default:
  519. fprintf(stderr, "%s: invalid pid: %x\n", __func__, p->pid);
  520. abort();
  521. }
  522. p->actual_length += bytes;
  523. }
  524. void usb_packet_skip(USBPacket *p, size_t bytes)
  525. {
  526. QEMUIOVector *iov = p->combined ? &p->combined->iov : &p->iov;
  527. assert(p->actual_length >= 0);
  528. assert(p->actual_length + bytes <= iov->size);
  529. if (p->pid == USB_TOKEN_IN) {
  530. iov_memset(iov->iov, iov->niov, p->actual_length, 0, bytes);
  531. }
  532. p->actual_length += bytes;
  533. }
  534. size_t usb_packet_size(USBPacket *p)
  535. {
  536. return p->combined ? p->combined->iov.size : p->iov.size;
  537. }
  538. void usb_packet_cleanup(USBPacket *p)
  539. {
  540. assert(!usb_packet_is_inflight(p));
  541. qemu_iovec_destroy(&p->iov);
  542. }
  543. void usb_ep_reset(USBDevice *dev)
  544. {
  545. int ep;
  546. dev->ep_ctl.nr = 0;
  547. dev->ep_ctl.type = USB_ENDPOINT_XFER_CONTROL;
  548. dev->ep_ctl.ifnum = 0;
  549. dev->ep_ctl.max_packet_size = 64;
  550. dev->ep_ctl.max_streams = 0;
  551. dev->ep_ctl.dev = dev;
  552. dev->ep_ctl.pipeline = false;
  553. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  554. dev->ep_in[ep].nr = ep + 1;
  555. dev->ep_out[ep].nr = ep + 1;
  556. dev->ep_in[ep].pid = USB_TOKEN_IN;
  557. dev->ep_out[ep].pid = USB_TOKEN_OUT;
  558. dev->ep_in[ep].type = USB_ENDPOINT_XFER_INVALID;
  559. dev->ep_out[ep].type = USB_ENDPOINT_XFER_INVALID;
  560. dev->ep_in[ep].ifnum = USB_INTERFACE_INVALID;
  561. dev->ep_out[ep].ifnum = USB_INTERFACE_INVALID;
  562. dev->ep_in[ep].max_packet_size = 0;
  563. dev->ep_out[ep].max_packet_size = 0;
  564. dev->ep_in[ep].max_streams = 0;
  565. dev->ep_out[ep].max_streams = 0;
  566. dev->ep_in[ep].dev = dev;
  567. dev->ep_out[ep].dev = dev;
  568. dev->ep_in[ep].pipeline = false;
  569. dev->ep_out[ep].pipeline = false;
  570. }
  571. }
  572. void usb_ep_init(USBDevice *dev)
  573. {
  574. int ep;
  575. usb_ep_reset(dev);
  576. QTAILQ_INIT(&dev->ep_ctl.queue);
  577. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  578. QTAILQ_INIT(&dev->ep_in[ep].queue);
  579. QTAILQ_INIT(&dev->ep_out[ep].queue);
  580. }
  581. }
  582. void usb_ep_dump(USBDevice *dev)
  583. {
  584. static const char *tname[] = {
  585. [USB_ENDPOINT_XFER_CONTROL] = "control",
  586. [USB_ENDPOINT_XFER_ISOC] = "isoc",
  587. [USB_ENDPOINT_XFER_BULK] = "bulk",
  588. [USB_ENDPOINT_XFER_INT] = "int",
  589. };
  590. int ifnum, ep, first;
  591. fprintf(stderr, "Device \"%s\", config %d\n",
  592. dev->product_desc, dev->configuration);
  593. for (ifnum = 0; ifnum < 16; ifnum++) {
  594. first = 1;
  595. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  596. if (dev->ep_in[ep].type != USB_ENDPOINT_XFER_INVALID &&
  597. dev->ep_in[ep].ifnum == ifnum) {
  598. if (first) {
  599. first = 0;
  600. fprintf(stderr, " Interface %d, alternative %d\n",
  601. ifnum, dev->altsetting[ifnum]);
  602. }
  603. fprintf(stderr, " Endpoint %d, IN, %s, %d max\n", ep,
  604. tname[dev->ep_in[ep].type],
  605. dev->ep_in[ep].max_packet_size);
  606. }
  607. if (dev->ep_out[ep].type != USB_ENDPOINT_XFER_INVALID &&
  608. dev->ep_out[ep].ifnum == ifnum) {
  609. if (first) {
  610. first = 0;
  611. fprintf(stderr, " Interface %d, alternative %d\n",
  612. ifnum, dev->altsetting[ifnum]);
  613. }
  614. fprintf(stderr, " Endpoint %d, OUT, %s, %d max\n", ep,
  615. tname[dev->ep_out[ep].type],
  616. dev->ep_out[ep].max_packet_size);
  617. }
  618. }
  619. }
  620. fprintf(stderr, "--\n");
  621. }
  622. struct USBEndpoint *usb_ep_get(USBDevice *dev, int pid, int ep)
  623. {
  624. struct USBEndpoint *eps;
  625. if (dev == NULL) {
  626. return NULL;
  627. }
  628. eps = (pid == USB_TOKEN_IN) ? dev->ep_in : dev->ep_out;
  629. if (ep == 0) {
  630. return &dev->ep_ctl;
  631. }
  632. assert(pid == USB_TOKEN_IN || pid == USB_TOKEN_OUT);
  633. assert(ep > 0 && ep <= USB_MAX_ENDPOINTS);
  634. return eps + ep - 1;
  635. }
  636. uint8_t usb_ep_get_type(USBDevice *dev, int pid, int ep)
  637. {
  638. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  639. return uep->type;
  640. }
  641. void usb_ep_set_type(USBDevice *dev, int pid, int ep, uint8_t type)
  642. {
  643. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  644. uep->type = type;
  645. }
  646. uint8_t usb_ep_get_ifnum(USBDevice *dev, int pid, int ep)
  647. {
  648. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  649. return uep->ifnum;
  650. }
  651. void usb_ep_set_ifnum(USBDevice *dev, int pid, int ep, uint8_t ifnum)
  652. {
  653. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  654. uep->ifnum = ifnum;
  655. }
  656. void usb_ep_set_max_packet_size(USBDevice *dev, int pid, int ep,
  657. uint16_t raw)
  658. {
  659. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  660. int size, microframes;
  661. size = raw & 0x7ff;
  662. switch ((raw >> 11) & 3) {
  663. case 1:
  664. microframes = 2;
  665. break;
  666. case 2:
  667. microframes = 3;
  668. break;
  669. default:
  670. microframes = 1;
  671. break;
  672. }
  673. uep->max_packet_size = size * microframes;
  674. }
  675. int usb_ep_get_max_packet_size(USBDevice *dev, int pid, int ep)
  676. {
  677. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  678. return uep->max_packet_size;
  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. int usb_ep_get_max_streams(USBDevice *dev, int pid, int ep)
  692. {
  693. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  694. return uep->max_streams;
  695. }
  696. void usb_ep_set_pipeline(USBDevice *dev, int pid, int ep, bool enabled)
  697. {
  698. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  699. uep->pipeline = enabled;
  700. }
  701. void usb_ep_set_halted(USBDevice *dev, int pid, int ep, bool halted)
  702. {
  703. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  704. uep->halted = halted;
  705. }
  706. USBPacket *usb_ep_find_packet_by_id(USBDevice *dev, int pid, int ep,
  707. uint64_t id)
  708. {
  709. struct USBEndpoint *uep = usb_ep_get(dev, pid, ep);
  710. USBPacket *p;
  711. QTAILQ_FOREACH(p, &uep->queue, queue) {
  712. if (p->id == id) {
  713. return p;
  714. }
  715. }
  716. return NULL;
  717. }