2
0

xen-usb.c 32 KB

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