xen-usb.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. /*
  2. * xen paravirt usb device backend
  3. *
  4. * (c) Juergen Gross <jgross@suse.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; under version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Contributions after 2012-01-13 are licensed under the terms of the
  19. * GNU GPL, version 2 or (at your option) any later version.
  20. */
  21. #include "qemu/osdep.h"
  22. #include <libusb.h>
  23. #include <sys/user.h>
  24. #include "qemu/config-file.h"
  25. #include "qemu/main-loop.h"
  26. #include "qemu/option.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/usb.h"
  29. #include "hw/xen/xen-legacy-backend.h"
  30. #include "monitor/qdev.h"
  31. #include "qapi/error.h"
  32. #include "qapi/qmp/qdict.h"
  33. #include "qapi/qmp/qstring.h"
  34. #include "hw/xen/interface/io/usbif.h"
  35. /*
  36. * Check for required support of usbif.h: USBIF_SHORT_NOT_OK was the last
  37. * macro added we rely on.
  38. */
  39. #ifdef USBIF_SHORT_NOT_OK
  40. #define TR(xendev, lvl, fmt, args...) \
  41. { \
  42. struct timeval tv; \
  43. \
  44. gettimeofday(&tv, NULL); \
  45. xen_pv_printf(xendev, lvl, "%8ld.%06ld xen-usb(%s):" fmt, \
  46. tv.tv_sec, tv.tv_usec, __func__, ##args); \
  47. }
  48. #define TR_BUS(xendev, fmt, args...) TR(xendev, 2, fmt, ##args)
  49. #define TR_REQ(xendev, fmt, args...) TR(xendev, 3, fmt, ##args)
  50. #define USBBACK_MAXPORTS USBIF_PIPE_PORT_MASK
  51. #define USB_DEV_ADDR_SIZE (USBIF_PIPE_DEV_MASK + 1)
  52. /* USB wire protocol: structure describing control request parameter. */
  53. struct usbif_ctrlrequest {
  54. uint8_t bRequestType;
  55. uint8_t bRequest;
  56. uint16_t wValue;
  57. uint16_t wIndex;
  58. uint16_t wLength;
  59. };
  60. struct usbback_info;
  61. struct usbback_req;
  62. struct usbback_stub {
  63. USBDevice *dev;
  64. USBPort port;
  65. unsigned int speed;
  66. bool attached;
  67. QTAILQ_HEAD(, usbback_req) submit_q;
  68. };
  69. struct usbback_req {
  70. struct usbback_info *usbif;
  71. struct usbback_stub *stub;
  72. struct usbif_urb_request req;
  73. USBPacket packet;
  74. unsigned int nr_buffer_segs; /* # of transfer_buffer segments */
  75. unsigned int nr_extra_segs; /* # of iso_frame_desc segments */
  76. QTAILQ_ENTRY(usbback_req) q;
  77. void *buffer;
  78. void *isoc_buffer;
  79. struct libusb_transfer *xfer;
  80. bool cancelled;
  81. };
  82. struct usbback_hotplug {
  83. QSIMPLEQ_ENTRY(usbback_hotplug) q;
  84. unsigned port;
  85. };
  86. struct usbback_info {
  87. struct XenLegacyDevice xendev; /* must be first */
  88. USBBus bus;
  89. void *urb_sring;
  90. void *conn_sring;
  91. struct usbif_urb_back_ring urb_ring;
  92. struct usbif_conn_back_ring conn_ring;
  93. int num_ports;
  94. int usb_ver;
  95. bool ring_error;
  96. QTAILQ_HEAD(, usbback_req) req_free_q;
  97. QSIMPLEQ_HEAD(, usbback_hotplug) hotplug_q;
  98. struct usbback_stub ports[USBBACK_MAXPORTS];
  99. struct usbback_stub *addr_table[USB_DEV_ADDR_SIZE];
  100. QEMUBH *bh;
  101. };
  102. static struct usbback_req *usbback_get_req(struct usbback_info *usbif)
  103. {
  104. struct usbback_req *usbback_req;
  105. if (QTAILQ_EMPTY(&usbif->req_free_q)) {
  106. usbback_req = g_new0(struct usbback_req, 1);
  107. } else {
  108. usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
  109. QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
  110. }
  111. return usbback_req;
  112. }
  113. static void usbback_put_req(struct usbback_req *usbback_req)
  114. {
  115. struct usbback_info *usbif;
  116. usbif = usbback_req->usbif;
  117. memset(usbback_req, 0, sizeof(*usbback_req));
  118. QTAILQ_INSERT_HEAD(&usbif->req_free_q, usbback_req, q);
  119. }
  120. static int usbback_gnttab_map(struct usbback_req *usbback_req)
  121. {
  122. unsigned int nr_segs, i, prot;
  123. uint32_t ref[USBIF_MAX_SEGMENTS_PER_REQUEST];
  124. struct usbback_info *usbif = usbback_req->usbif;
  125. struct XenLegacyDevice *xendev = &usbif->xendev;
  126. struct usbif_request_segment *seg;
  127. void *addr;
  128. nr_segs = usbback_req->nr_buffer_segs + usbback_req->nr_extra_segs;
  129. if (!nr_segs) {
  130. return 0;
  131. }
  132. if (nr_segs > USBIF_MAX_SEGMENTS_PER_REQUEST) {
  133. xen_pv_printf(xendev, 0, "bad number of segments in request (%d)\n",
  134. nr_segs);
  135. return -EINVAL;
  136. }
  137. for (i = 0; i < nr_segs; i++) {
  138. if ((unsigned)usbback_req->req.seg[i].offset +
  139. (unsigned)usbback_req->req.seg[i].length > XC_PAGE_SIZE) {
  140. xen_pv_printf(xendev, 0, "segment crosses page boundary\n");
  141. return -EINVAL;
  142. }
  143. }
  144. if (usbback_req->nr_buffer_segs) {
  145. prot = PROT_READ;
  146. if (usbif_pipein(usbback_req->req.pipe)) {
  147. prot |= PROT_WRITE;
  148. }
  149. for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
  150. ref[i] = usbback_req->req.seg[i].gref;
  151. }
  152. usbback_req->buffer =
  153. xen_be_map_grant_refs(xendev, ref, usbback_req->nr_buffer_segs,
  154. prot);
  155. if (!usbback_req->buffer) {
  156. return -ENOMEM;
  157. }
  158. for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
  159. seg = usbback_req->req.seg + i;
  160. addr = usbback_req->buffer + i * XC_PAGE_SIZE + seg->offset;
  161. qemu_iovec_add(&usbback_req->packet.iov, addr, seg->length);
  162. }
  163. }
  164. if (!usbif_pipeisoc(usbback_req->req.pipe)) {
  165. return 0;
  166. }
  167. /*
  168. * Right now isoc requests are not supported.
  169. * Prepare supporting those by doing the work needed on the guest
  170. * interface side.
  171. */
  172. if (!usbback_req->nr_extra_segs) {
  173. xen_pv_printf(xendev, 0, "iso request without descriptor segments\n");
  174. return -EINVAL;
  175. }
  176. prot = PROT_READ | PROT_WRITE;
  177. for (i = 0; i < usbback_req->nr_extra_segs; i++) {
  178. ref[i] = usbback_req->req.seg[i + usbback_req->req.nr_buffer_segs].gref;
  179. }
  180. usbback_req->isoc_buffer =
  181. xen_be_map_grant_refs(xendev, ref, usbback_req->nr_extra_segs,
  182. prot);
  183. if (!usbback_req->isoc_buffer) {
  184. return -ENOMEM;
  185. }
  186. return 0;
  187. }
  188. static int usbback_init_packet(struct usbback_req *usbback_req)
  189. {
  190. struct XenLegacyDevice *xendev = &usbback_req->usbif->xendev;
  191. USBPacket *packet = &usbback_req->packet;
  192. USBDevice *dev = usbback_req->stub->dev;
  193. USBEndpoint *ep;
  194. unsigned int pid, ep_nr;
  195. bool sok;
  196. int ret = 0;
  197. qemu_iovec_init(&packet->iov, USBIF_MAX_SEGMENTS_PER_REQUEST);
  198. pid = usbif_pipein(usbback_req->req.pipe) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  199. ep_nr = usbif_pipeendpoint(usbback_req->req.pipe);
  200. sok = !!(usbback_req->req.transfer_flags & USBIF_SHORT_NOT_OK);
  201. if (usbif_pipectrl(usbback_req->req.pipe)) {
  202. ep_nr = 0;
  203. sok = false;
  204. }
  205. ep = usb_ep_get(dev, pid, ep_nr);
  206. usb_packet_setup(packet, pid, ep, 0, 1, sok, true);
  207. switch (usbif_pipetype(usbback_req->req.pipe)) {
  208. case USBIF_PIPE_TYPE_ISOC:
  209. TR_REQ(xendev, "iso transfer %s: buflen: %x, %d frames\n",
  210. (pid == USB_TOKEN_IN) ? "in" : "out",
  211. usbback_req->req.buffer_length,
  212. usbback_req->req.u.isoc.nr_frame_desc_segs);
  213. ret = -EINVAL; /* isoc not implemented yet */
  214. break;
  215. case USBIF_PIPE_TYPE_INT:
  216. TR_REQ(xendev, "int transfer %s: buflen: %x\n",
  217. (pid == USB_TOKEN_IN) ? "in" : "out",
  218. usbback_req->req.buffer_length);
  219. break;
  220. case USBIF_PIPE_TYPE_CTRL:
  221. packet->parameter = *(uint64_t *)usbback_req->req.u.ctrl;
  222. TR_REQ(xendev, "ctrl parameter: %"PRIx64", buflen: %x\n",
  223. packet->parameter,
  224. usbback_req->req.buffer_length);
  225. break;
  226. case USBIF_PIPE_TYPE_BULK:
  227. TR_REQ(xendev, "bulk transfer %s: buflen: %x\n",
  228. (pid == USB_TOKEN_IN) ? "in" : "out",
  229. usbback_req->req.buffer_length);
  230. break;
  231. default:
  232. ret = -EINVAL;
  233. break;
  234. }
  235. return ret;
  236. }
  237. static void usbback_do_response(struct usbback_req *usbback_req, int32_t status,
  238. int32_t actual_length, int32_t error_count)
  239. {
  240. struct usbback_info *usbif;
  241. struct usbif_urb_response *res;
  242. struct XenLegacyDevice *xendev;
  243. unsigned int notify;
  244. usbif = usbback_req->usbif;
  245. xendev = &usbif->xendev;
  246. TR_REQ(xendev, "id %d, status %d, length %d, errcnt %d\n",
  247. usbback_req->req.id, status, actual_length, error_count);
  248. if (usbback_req->packet.iov.iov) {
  249. qemu_iovec_destroy(&usbback_req->packet.iov);
  250. }
  251. if (usbback_req->buffer) {
  252. xen_be_unmap_grant_refs(xendev, usbback_req->buffer,
  253. usbback_req->nr_buffer_segs);
  254. usbback_req->buffer = NULL;
  255. }
  256. if (usbback_req->isoc_buffer) {
  257. xen_be_unmap_grant_refs(xendev, usbback_req->isoc_buffer,
  258. usbback_req->nr_extra_segs);
  259. usbback_req->isoc_buffer = NULL;
  260. }
  261. if (usbif->urb_sring) {
  262. res = RING_GET_RESPONSE(&usbif->urb_ring, usbif->urb_ring.rsp_prod_pvt);
  263. res->id = usbback_req->req.id;
  264. res->status = status;
  265. res->actual_length = actual_length;
  266. res->error_count = error_count;
  267. res->start_frame = 0;
  268. usbif->urb_ring.rsp_prod_pvt++;
  269. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&usbif->urb_ring, notify);
  270. if (notify) {
  271. xen_pv_send_notify(xendev);
  272. }
  273. }
  274. if (!usbback_req->cancelled)
  275. usbback_put_req(usbback_req);
  276. }
  277. static void usbback_do_response_ret(struct usbback_req *usbback_req,
  278. int32_t status)
  279. {
  280. usbback_do_response(usbback_req, status, 0, 0);
  281. }
  282. static int32_t usbback_xlat_status(int status)
  283. {
  284. switch (status) {
  285. case USB_RET_SUCCESS:
  286. return 0;
  287. case USB_RET_NODEV:
  288. return -ENODEV;
  289. case USB_RET_STALL:
  290. return -EPIPE;
  291. case USB_RET_BABBLE:
  292. return -EOVERFLOW;
  293. case USB_RET_IOERROR:
  294. return -EPROTO;
  295. }
  296. return -ESHUTDOWN;
  297. }
  298. static void usbback_packet_complete(struct usbback_req *usbback_req)
  299. {
  300. USBPacket *packet = &usbback_req->packet;
  301. int32_t status;
  302. QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
  303. status = usbback_xlat_status(packet->status);
  304. usbback_do_response(usbback_req, status, packet->actual_length, 0);
  305. }
  306. static void usbback_set_address(struct usbback_info *usbif,
  307. struct usbback_stub *stub,
  308. unsigned int cur_addr, unsigned int new_addr)
  309. {
  310. if (cur_addr) {
  311. usbif->addr_table[cur_addr] = NULL;
  312. }
  313. if (new_addr) {
  314. usbif->addr_table[new_addr] = stub;
  315. }
  316. }
  317. static void usbback_cancel_req(struct usbback_req *usbback_req)
  318. {
  319. if (usb_packet_is_inflight(&usbback_req->packet)) {
  320. usb_cancel_packet(&usbback_req->packet);
  321. QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
  322. usbback_req->cancelled = true;
  323. usbback_do_response_ret(usbback_req, -EPROTO);
  324. }
  325. }
  326. static void usbback_process_unlink_req(struct usbback_req *usbback_req)
  327. {
  328. struct usbback_info *usbif;
  329. struct usbback_req *unlink_req;
  330. unsigned int id, devnum;
  331. int ret;
  332. usbif = usbback_req->usbif;
  333. ret = 0;
  334. id = usbback_req->req.u.unlink.unlink_id;
  335. TR_REQ(&usbif->xendev, "unlink id %d\n", id);
  336. devnum = usbif_pipedevice(usbback_req->req.pipe);
  337. if (unlikely(devnum == 0)) {
  338. usbback_req->stub = usbif->ports +
  339. usbif_pipeportnum(usbback_req->req.pipe) - 1;
  340. if (unlikely(!usbback_req->stub)) {
  341. ret = -ENODEV;
  342. goto fail_response;
  343. }
  344. } else {
  345. if (unlikely(!usbif->addr_table[devnum])) {
  346. ret = -ENODEV;
  347. goto fail_response;
  348. }
  349. usbback_req->stub = usbif->addr_table[devnum];
  350. }
  351. QTAILQ_FOREACH(unlink_req, &usbback_req->stub->submit_q, q) {
  352. if (unlink_req->req.id == id) {
  353. usbback_cancel_req(unlink_req);
  354. break;
  355. }
  356. }
  357. fail_response:
  358. usbback_do_response_ret(usbback_req, ret);
  359. }
  360. /*
  361. * Checks whether a request can be handled at once or should be forwarded
  362. * to the usb framework.
  363. * Return value is:
  364. * 0 in case of usb framework is needed
  365. * 1 in case of local handling (no error)
  366. * The request response has been queued already if return value not 0.
  367. */
  368. static int usbback_check_and_submit(struct usbback_req *usbback_req)
  369. {
  370. struct usbback_info *usbif;
  371. unsigned int devnum;
  372. struct usbback_stub *stub;
  373. struct usbif_ctrlrequest *ctrl;
  374. int ret;
  375. uint16_t wValue;
  376. usbif = usbback_req->usbif;
  377. stub = NULL;
  378. devnum = usbif_pipedevice(usbback_req->req.pipe);
  379. ctrl = (struct usbif_ctrlrequest *)usbback_req->req.u.ctrl;
  380. wValue = le16_to_cpu(ctrl->wValue);
  381. /*
  382. * When the device is first connected or resetted, USB device has no
  383. * address. In this initial state, following requests are sent to device
  384. * address (#0),
  385. *
  386. * 1. GET_DESCRIPTOR (with Descriptor Type is "DEVICE") is sent,
  387. * and OS knows what device is connected to.
  388. *
  389. * 2. SET_ADDRESS is sent, and then device has its address.
  390. *
  391. * In the next step, SET_CONFIGURATION is sent to addressed device, and
  392. * then the device is finally ready to use.
  393. */
  394. if (unlikely(devnum == 0)) {
  395. stub = usbif->ports + usbif_pipeportnum(usbback_req->req.pipe) - 1;
  396. if (!stub->dev || !stub->attached) {
  397. ret = -ENODEV;
  398. goto do_response;
  399. }
  400. switch (ctrl->bRequest) {
  401. case USB_REQ_GET_DESCRIPTOR:
  402. /*
  403. * GET_DESCRIPTOR request to device #0.
  404. * through normal transfer.
  405. */
  406. TR_REQ(&usbif->xendev, "devnum 0 GET_DESCRIPTOR\n");
  407. usbback_req->stub = stub;
  408. return 0;
  409. case USB_REQ_SET_ADDRESS:
  410. /*
  411. * SET_ADDRESS request to device #0.
  412. * add attached device to addr_table.
  413. */
  414. TR_REQ(&usbif->xendev, "devnum 0 SET_ADDRESS\n");
  415. usbback_set_address(usbif, stub, 0, wValue);
  416. ret = 0;
  417. break;
  418. default:
  419. ret = -EINVAL;
  420. break;
  421. }
  422. goto do_response;
  423. }
  424. if (unlikely(!usbif->addr_table[devnum])) {
  425. ret = -ENODEV;
  426. goto do_response;
  427. }
  428. usbback_req->stub = usbif->addr_table[devnum];
  429. /*
  430. * Check special request
  431. */
  432. if (ctrl->bRequest != USB_REQ_SET_ADDRESS) {
  433. return 0;
  434. }
  435. /*
  436. * SET_ADDRESS request to addressed device.
  437. * change addr or remove from addr_table.
  438. */
  439. usbback_set_address(usbif, usbback_req->stub, devnum, wValue);
  440. ret = 0;
  441. do_response:
  442. usbback_do_response_ret(usbback_req, ret);
  443. return 1;
  444. }
  445. static void usbback_dispatch(struct usbback_req *usbback_req)
  446. {
  447. int ret;
  448. unsigned int devnum;
  449. struct usbback_info *usbif;
  450. usbif = usbback_req->usbif;
  451. TR_REQ(&usbif->xendev, "start req_id %d pipe %08x\n", usbback_req->req.id,
  452. usbback_req->req.pipe);
  453. /* unlink request */
  454. if (unlikely(usbif_pipeunlink(usbback_req->req.pipe))) {
  455. usbback_process_unlink_req(usbback_req);
  456. return;
  457. }
  458. if (usbif_pipectrl(usbback_req->req.pipe)) {
  459. if (usbback_check_and_submit(usbback_req)) {
  460. return;
  461. }
  462. } else {
  463. devnum = usbif_pipedevice(usbback_req->req.pipe);
  464. usbback_req->stub = usbif->addr_table[devnum];
  465. if (!usbback_req->stub || !usbback_req->stub->attached) {
  466. ret = -ENODEV;
  467. goto fail_response;
  468. }
  469. }
  470. QTAILQ_INSERT_TAIL(&usbback_req->stub->submit_q, usbback_req, q);
  471. usbback_req->nr_buffer_segs = usbback_req->req.nr_buffer_segs;
  472. usbback_req->nr_extra_segs = usbif_pipeisoc(usbback_req->req.pipe) ?
  473. usbback_req->req.u.isoc.nr_frame_desc_segs : 0;
  474. ret = usbback_init_packet(usbback_req);
  475. if (ret) {
  476. xen_pv_printf(&usbif->xendev, 0, "invalid request\n");
  477. ret = -ESHUTDOWN;
  478. goto fail_free_urb;
  479. }
  480. ret = usbback_gnttab_map(usbback_req);
  481. if (ret) {
  482. xen_pv_printf(&usbif->xendev, 0, "invalid buffer, ret=%d\n", ret);
  483. ret = -ESHUTDOWN;
  484. goto fail_free_urb;
  485. }
  486. usb_handle_packet(usbback_req->stub->dev, &usbback_req->packet);
  487. if (usbback_req->packet.status != USB_RET_ASYNC) {
  488. usbback_packet_complete(usbback_req);
  489. }
  490. return;
  491. fail_free_urb:
  492. QTAILQ_REMOVE(&usbback_req->stub->submit_q, usbback_req, q);
  493. fail_response:
  494. usbback_do_response_ret(usbback_req, ret);
  495. }
  496. static void usbback_hotplug_notify(struct usbback_info *usbif)
  497. {
  498. struct usbif_conn_back_ring *ring = &usbif->conn_ring;
  499. struct usbif_conn_request req;
  500. struct usbif_conn_response *res;
  501. struct usbback_hotplug *usb_hp;
  502. unsigned int notify;
  503. if (!usbif->conn_sring) {
  504. return;
  505. }
  506. /* Check for full ring. */
  507. if ((RING_SIZE(ring) - ring->rsp_prod_pvt - ring->req_cons) == 0) {
  508. xen_pv_send_notify(&usbif->xendev);
  509. return;
  510. }
  511. usb_hp = QSIMPLEQ_FIRST(&usbif->hotplug_q);
  512. QSIMPLEQ_REMOVE_HEAD(&usbif->hotplug_q, q);
  513. RING_COPY_REQUEST(ring, ring->req_cons, &req);
  514. ring->req_cons++;
  515. ring->sring->req_event = ring->req_cons + 1;
  516. res = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
  517. res->id = req.id;
  518. res->portnum = usb_hp->port;
  519. res->speed = usbif->ports[usb_hp->port - 1].speed;
  520. ring->rsp_prod_pvt++;
  521. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(ring, notify);
  522. if (notify) {
  523. xen_pv_send_notify(&usbif->xendev);
  524. }
  525. TR_BUS(&usbif->xendev, "hotplug port %d speed %d\n", usb_hp->port,
  526. res->speed);
  527. g_free(usb_hp);
  528. if (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
  529. qemu_bh_schedule(usbif->bh);
  530. }
  531. }
  532. static void usbback_bh(void *opaque)
  533. {
  534. struct usbback_info *usbif;
  535. struct usbif_urb_back_ring *urb_ring;
  536. struct usbback_req *usbback_req;
  537. RING_IDX rc, rp;
  538. unsigned int more_to_do;
  539. usbif = opaque;
  540. if (usbif->ring_error) {
  541. return;
  542. }
  543. if (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
  544. usbback_hotplug_notify(usbif);
  545. }
  546. urb_ring = &usbif->urb_ring;
  547. rc = urb_ring->req_cons;
  548. rp = urb_ring->sring->req_prod;
  549. xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
  550. if (RING_REQUEST_PROD_OVERFLOW(urb_ring, rp)) {
  551. rc = urb_ring->rsp_prod_pvt;
  552. xen_pv_printf(&usbif->xendev, 0, "domU provided bogus ring requests "
  553. "(%#x - %#x = %u). Halting ring processing.\n",
  554. rp, rc, rp - rc);
  555. usbif->ring_error = true;
  556. return;
  557. }
  558. while (rc != rp) {
  559. if (RING_REQUEST_CONS_OVERFLOW(urb_ring, rc)) {
  560. break;
  561. }
  562. usbback_req = usbback_get_req(usbif);
  563. RING_COPY_REQUEST(urb_ring, rc, &usbback_req->req);
  564. usbback_req->usbif = usbif;
  565. usbback_dispatch(usbback_req);
  566. urb_ring->req_cons = ++rc;
  567. }
  568. RING_FINAL_CHECK_FOR_REQUESTS(urb_ring, more_to_do);
  569. if (more_to_do) {
  570. qemu_bh_schedule(usbif->bh);
  571. }
  572. }
  573. static void usbback_hotplug_enq(struct usbback_info *usbif, unsigned port)
  574. {
  575. struct usbback_hotplug *usb_hp;
  576. usb_hp = g_new0(struct usbback_hotplug, 1);
  577. usb_hp->port = port;
  578. QSIMPLEQ_INSERT_TAIL(&usbif->hotplug_q, usb_hp, q);
  579. usbback_hotplug_notify(usbif);
  580. }
  581. static void usbback_portid_drain(struct usbback_info *usbif, unsigned port)
  582. {
  583. struct usbback_req *req, *tmp;
  584. bool sched = false;
  585. QTAILQ_FOREACH_SAFE(req, &usbif->ports[port - 1].submit_q, q, tmp) {
  586. usbback_cancel_req(req);
  587. sched = true;
  588. }
  589. if (sched) {
  590. qemu_bh_schedule(usbif->bh);
  591. }
  592. }
  593. static void usbback_portid_detach(struct usbback_info *usbif, unsigned port)
  594. {
  595. if (!usbif->ports[port - 1].attached) {
  596. return;
  597. }
  598. usbif->ports[port - 1].speed = USBIF_SPEED_NONE;
  599. usbif->ports[port - 1].attached = false;
  600. usbback_portid_drain(usbif, port);
  601. usbback_hotplug_enq(usbif, port);
  602. }
  603. static void usbback_portid_remove(struct usbback_info *usbif, unsigned port)
  604. {
  605. if (!usbif->ports[port - 1].dev) {
  606. return;
  607. }
  608. object_unparent(OBJECT(usbif->ports[port - 1].dev));
  609. usbif->ports[port - 1].dev = NULL;
  610. usbback_portid_detach(usbif, port);
  611. TR_BUS(&usbif->xendev, "port %d removed\n", port);
  612. }
  613. static void usbback_portid_add(struct usbback_info *usbif, unsigned port,
  614. char *busid)
  615. {
  616. unsigned speed;
  617. char *portname;
  618. Error *local_err = NULL;
  619. QDict *qdict;
  620. QemuOpts *opts;
  621. char *tmp;
  622. if (usbif->ports[port - 1].dev) {
  623. return;
  624. }
  625. portname = strchr(busid, '-');
  626. if (!portname) {
  627. xen_pv_printf(&usbif->xendev, 0, "device %s illegal specification\n",
  628. busid);
  629. return;
  630. }
  631. portname++;
  632. qdict = qdict_new();
  633. qdict_put_str(qdict, "driver", "usb-host");
  634. tmp = g_strdup_printf("%s.0", usbif->xendev.qdev.id);
  635. qdict_put_str(qdict, "bus", tmp);
  636. g_free(tmp);
  637. tmp = g_strdup_printf("%s-%u", usbif->xendev.qdev.id, port);
  638. qdict_put_str(qdict, "id", tmp);
  639. g_free(tmp);
  640. qdict_put_int(qdict, "port", port);
  641. qdict_put_int(qdict, "hostbus", atoi(busid));
  642. qdict_put_str(qdict, "hostport", portname);
  643. opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict,
  644. &error_abort);
  645. usbif->ports[port - 1].dev = USB_DEVICE(qdev_device_add(opts, &local_err));
  646. if (!usbif->ports[port - 1].dev) {
  647. qobject_unref(qdict);
  648. xen_pv_printf(&usbif->xendev, 0,
  649. "device %s could not be opened: %s\n",
  650. busid, error_get_pretty(local_err));
  651. error_free(local_err);
  652. return;
  653. }
  654. qobject_unref(qdict);
  655. speed = usbif->ports[port - 1].dev->speed;
  656. switch (speed) {
  657. case USB_SPEED_LOW:
  658. speed = USBIF_SPEED_LOW;
  659. break;
  660. case USB_SPEED_FULL:
  661. speed = USBIF_SPEED_FULL;
  662. break;
  663. case USB_SPEED_HIGH:
  664. speed = (usbif->usb_ver < USB_VER_USB20) ?
  665. USBIF_SPEED_NONE : USBIF_SPEED_HIGH;
  666. break;
  667. default:
  668. speed = USBIF_SPEED_NONE;
  669. break;
  670. }
  671. if (speed == USBIF_SPEED_NONE) {
  672. xen_pv_printf(&usbif->xendev, 0, "device %s wrong speed\n", busid);
  673. object_unparent(OBJECT(usbif->ports[port - 1].dev));
  674. usbif->ports[port - 1].dev = NULL;
  675. return;
  676. }
  677. usb_device_reset(usbif->ports[port - 1].dev);
  678. usbif->ports[port - 1].speed = speed;
  679. usbif->ports[port - 1].attached = true;
  680. QTAILQ_INIT(&usbif->ports[port - 1].submit_q);
  681. usbback_hotplug_enq(usbif, port);
  682. TR_BUS(&usbif->xendev, "port %d attached\n", port);
  683. }
  684. static void usbback_process_port(struct usbback_info *usbif, unsigned port)
  685. {
  686. char node[8];
  687. char *busid;
  688. snprintf(node, sizeof(node), "port/%d", port);
  689. busid = xenstore_read_be_str(&usbif->xendev, node);
  690. if (busid == NULL) {
  691. xen_pv_printf(&usbif->xendev, 0, "xenstore_read %s failed\n", node);
  692. return;
  693. }
  694. /* Remove portid, if the port is not connected. */
  695. if (strlen(busid) == 0) {
  696. usbback_portid_remove(usbif, port);
  697. } else {
  698. usbback_portid_add(usbif, port, busid);
  699. }
  700. g_free(busid);
  701. }
  702. static void usbback_disconnect(struct XenLegacyDevice *xendev)
  703. {
  704. struct usbback_info *usbif;
  705. unsigned int i;
  706. TR_BUS(xendev, "start\n");
  707. usbif = container_of(xendev, struct usbback_info, xendev);
  708. xen_pv_unbind_evtchn(xendev);
  709. if (usbif->urb_sring) {
  710. xen_be_unmap_grant_ref(xendev, usbif->urb_sring);
  711. usbif->urb_sring = NULL;
  712. }
  713. if (usbif->conn_sring) {
  714. xen_be_unmap_grant_ref(xendev, usbif->conn_sring);
  715. usbif->conn_sring = NULL;
  716. }
  717. for (i = 0; i < usbif->num_ports; i++) {
  718. if (usbif->ports[i].dev) {
  719. usbback_portid_drain(usbif, i + 1);
  720. }
  721. }
  722. TR_BUS(xendev, "finished\n");
  723. }
  724. static int usbback_connect(struct XenLegacyDevice *xendev)
  725. {
  726. struct usbback_info *usbif;
  727. struct usbif_urb_sring *urb_sring;
  728. struct usbif_conn_sring *conn_sring;
  729. int urb_ring_ref;
  730. int conn_ring_ref;
  731. unsigned int i, max_grants;
  732. TR_BUS(xendev, "start\n");
  733. /* max_grants: for each request and for the rings (request and connect). */
  734. max_grants = USBIF_MAX_SEGMENTS_PER_REQUEST * USB_URB_RING_SIZE + 2;
  735. xen_be_set_max_grant_refs(xendev, max_grants);
  736. usbif = container_of(xendev, struct usbback_info, xendev);
  737. if (xenstore_read_fe_int(xendev, "urb-ring-ref", &urb_ring_ref)) {
  738. xen_pv_printf(xendev, 0, "error reading urb-ring-ref\n");
  739. return -1;
  740. }
  741. if (xenstore_read_fe_int(xendev, "conn-ring-ref", &conn_ring_ref)) {
  742. xen_pv_printf(xendev, 0, "error reading conn-ring-ref\n");
  743. return -1;
  744. }
  745. if (xenstore_read_fe_int(xendev, "event-channel", &xendev->remote_port)) {
  746. xen_pv_printf(xendev, 0, "error reading event-channel\n");
  747. return -1;
  748. }
  749. usbif->urb_sring = xen_be_map_grant_ref(xendev, urb_ring_ref,
  750. PROT_READ | PROT_WRITE);
  751. usbif->conn_sring = xen_be_map_grant_ref(xendev, conn_ring_ref,
  752. PROT_READ | PROT_WRITE);
  753. if (!usbif->urb_sring || !usbif->conn_sring) {
  754. xen_pv_printf(xendev, 0, "error mapping rings\n");
  755. usbback_disconnect(xendev);
  756. return -1;
  757. }
  758. urb_sring = usbif->urb_sring;
  759. conn_sring = usbif->conn_sring;
  760. BACK_RING_INIT(&usbif->urb_ring, urb_sring, XC_PAGE_SIZE);
  761. BACK_RING_INIT(&usbif->conn_ring, conn_sring, XC_PAGE_SIZE);
  762. xen_be_bind_evtchn(xendev);
  763. xen_pv_printf(xendev, 1, "urb-ring-ref %d, conn-ring-ref %d, "
  764. "remote port %d, local port %d\n", urb_ring_ref,
  765. conn_ring_ref, xendev->remote_port, xendev->local_port);
  766. for (i = 1; i <= usbif->num_ports; i++) {
  767. if (usbif->ports[i - 1].dev) {
  768. usbback_hotplug_enq(usbif, i);
  769. }
  770. }
  771. return 0;
  772. }
  773. static void usbback_backend_changed(struct XenLegacyDevice *xendev,
  774. const char *node)
  775. {
  776. struct usbback_info *usbif;
  777. unsigned int i;
  778. TR_BUS(xendev, "path %s\n", node);
  779. usbif = container_of(xendev, struct usbback_info, xendev);
  780. for (i = 1; i <= usbif->num_ports; i++) {
  781. usbback_process_port(usbif, i);
  782. }
  783. }
  784. static int usbback_init(struct XenLegacyDevice *xendev)
  785. {
  786. struct usbback_info *usbif;
  787. TR_BUS(xendev, "start\n");
  788. usbif = container_of(xendev, struct usbback_info, xendev);
  789. if (xenstore_read_be_int(xendev, "num-ports", &usbif->num_ports) ||
  790. usbif->num_ports < 1 || usbif->num_ports > USBBACK_MAXPORTS) {
  791. xen_pv_printf(xendev, 0, "num-ports not readable or out of bounds\n");
  792. return -1;
  793. }
  794. if (xenstore_read_be_int(xendev, "usb-ver", &usbif->usb_ver) ||
  795. (usbif->usb_ver != USB_VER_USB11 && usbif->usb_ver != USB_VER_USB20)) {
  796. xen_pv_printf(xendev, 0, "usb-ver not readable or out of bounds\n");
  797. return -1;
  798. }
  799. usbback_backend_changed(xendev, "port");
  800. TR_BUS(xendev, "finished\n");
  801. return 0;
  802. }
  803. static void xen_bus_attach(USBPort *port)
  804. {
  805. struct usbback_info *usbif;
  806. usbif = port->opaque;
  807. TR_BUS(&usbif->xendev, "\n");
  808. usbif->ports[port->index].attached = true;
  809. usbback_hotplug_enq(usbif, port->index + 1);
  810. }
  811. static void xen_bus_detach(USBPort *port)
  812. {
  813. struct usbback_info *usbif;
  814. usbif = port->opaque;
  815. TR_BUS(&usbif->xendev, "\n");
  816. usbback_portid_detach(usbif, port->index + 1);
  817. }
  818. static void xen_bus_child_detach(USBPort *port, USBDevice *child)
  819. {
  820. struct usbback_info *usbif;
  821. usbif = port->opaque;
  822. TR_BUS(&usbif->xendev, "\n");
  823. }
  824. static void xen_bus_complete(USBPort *port, USBPacket *packet)
  825. {
  826. struct usbback_req *usbback_req;
  827. struct usbback_info *usbif;
  828. usbback_req = container_of(packet, struct usbback_req, packet);
  829. if (usbback_req->cancelled) {
  830. g_free(usbback_req);
  831. return;
  832. }
  833. usbif = usbback_req->usbif;
  834. TR_REQ(&usbif->xendev, "\n");
  835. usbback_packet_complete(usbback_req);
  836. }
  837. static USBPortOps xen_usb_port_ops = {
  838. .attach = xen_bus_attach,
  839. .detach = xen_bus_detach,
  840. .child_detach = xen_bus_child_detach,
  841. .complete = xen_bus_complete,
  842. };
  843. static USBBusOps xen_usb_bus_ops = {
  844. };
  845. static void usbback_alloc(struct XenLegacyDevice *xendev)
  846. {
  847. struct usbback_info *usbif;
  848. USBPort *p;
  849. unsigned int i;
  850. usbif = container_of(xendev, struct usbback_info, xendev);
  851. usb_bus_new(&usbif->bus, sizeof(usbif->bus), &xen_usb_bus_ops,
  852. DEVICE(&xendev->qdev));
  853. for (i = 0; i < USBBACK_MAXPORTS; i++) {
  854. p = &(usbif->ports[i].port);
  855. usb_register_port(&usbif->bus, p, usbif, i, &xen_usb_port_ops,
  856. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL |
  857. USB_SPEED_MASK_HIGH);
  858. }
  859. QTAILQ_INIT(&usbif->req_free_q);
  860. QSIMPLEQ_INIT(&usbif->hotplug_q);
  861. usbif->bh = qemu_bh_new(usbback_bh, usbif);
  862. }
  863. static int usbback_free(struct XenLegacyDevice *xendev)
  864. {
  865. struct usbback_info *usbif;
  866. struct usbback_req *usbback_req;
  867. struct usbback_hotplug *usb_hp;
  868. unsigned int i;
  869. TR_BUS(xendev, "start\n");
  870. usbback_disconnect(xendev);
  871. usbif = container_of(xendev, struct usbback_info, xendev);
  872. for (i = 1; i <= usbif->num_ports; i++) {
  873. usbback_portid_remove(usbif, i);
  874. }
  875. while (!QTAILQ_EMPTY(&usbif->req_free_q)) {
  876. usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
  877. QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
  878. g_free(usbback_req);
  879. }
  880. while (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
  881. usb_hp = QSIMPLEQ_FIRST(&usbif->hotplug_q);
  882. QSIMPLEQ_REMOVE_HEAD(&usbif->hotplug_q, q);
  883. g_free(usb_hp);
  884. }
  885. qemu_bh_delete(usbif->bh);
  886. for (i = 0; i < USBBACK_MAXPORTS; i++) {
  887. usb_unregister_port(&usbif->bus, &(usbif->ports[i].port));
  888. }
  889. usb_bus_release(&usbif->bus);
  890. TR_BUS(xendev, "finished\n");
  891. return 0;
  892. }
  893. static void usbback_event(struct XenLegacyDevice *xendev)
  894. {
  895. struct usbback_info *usbif;
  896. usbif = container_of(xendev, struct usbback_info, xendev);
  897. qemu_bh_schedule(usbif->bh);
  898. }
  899. struct XenDevOps xen_usb_ops = {
  900. .size = sizeof(struct usbback_info),
  901. .flags = DEVOPS_FLAG_NEED_GNTDEV,
  902. .init = usbback_init,
  903. .alloc = usbback_alloc,
  904. .free = usbback_free,
  905. .backend_changed = usbback_backend_changed,
  906. .initialise = usbback_connect,
  907. .disconnect = usbback_disconnect,
  908. .event = usbback_event,
  909. };
  910. #else /* USBIF_SHORT_NOT_OK */
  911. static int usbback_not_supported(void)
  912. {
  913. return -EINVAL;
  914. }
  915. struct XenDevOps xen_usb_ops = {
  916. .backend_register = usbback_not_supported,
  917. };
  918. #endif