2
0

xen-usb.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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/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. void *urb_sring;
  89. void *conn_sring;
  90. struct usbif_urb_back_ring urb_ring;
  91. struct usbif_conn_back_ring conn_ring;
  92. int num_ports;
  93. int usb_ver;
  94. bool ring_error;
  95. QTAILQ_HEAD(, usbback_req) req_free_q;
  96. QSIMPLEQ_HEAD(, usbback_hotplug) hotplug_q;
  97. struct usbback_stub ports[USBBACK_MAXPORTS];
  98. struct usbback_stub *addr_table[USB_DEV_ADDR_SIZE];
  99. QEMUBH *bh;
  100. };
  101. static struct usbback_req *usbback_get_req(struct usbback_info *usbif)
  102. {
  103. struct usbback_req *usbback_req;
  104. if (QTAILQ_EMPTY(&usbif->req_free_q)) {
  105. usbback_req = g_new0(struct usbback_req, 1);
  106. } else {
  107. usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
  108. QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
  109. }
  110. return usbback_req;
  111. }
  112. static void usbback_put_req(struct usbback_req *usbback_req)
  113. {
  114. struct usbback_info *usbif;
  115. usbif = usbback_req->usbif;
  116. memset(usbback_req, 0, sizeof(*usbback_req));
  117. QTAILQ_INSERT_HEAD(&usbif->req_free_q, usbback_req, q);
  118. }
  119. static int usbback_gnttab_map(struct usbback_req *usbback_req)
  120. {
  121. unsigned int nr_segs, i, prot;
  122. uint32_t ref[USBIF_MAX_SEGMENTS_PER_REQUEST];
  123. struct usbback_info *usbif = usbback_req->usbif;
  124. struct XenLegacyDevice *xendev = &usbif->xendev;
  125. struct usbif_request_segment *seg;
  126. void *addr;
  127. nr_segs = usbback_req->nr_buffer_segs + usbback_req->nr_extra_segs;
  128. if (!nr_segs) {
  129. return 0;
  130. }
  131. if (nr_segs > USBIF_MAX_SEGMENTS_PER_REQUEST) {
  132. xen_pv_printf(xendev, 0, "bad number of segments in request (%d)\n",
  133. nr_segs);
  134. return -EINVAL;
  135. }
  136. for (i = 0; i < nr_segs; i++) {
  137. if ((unsigned)usbback_req->req.seg[i].offset +
  138. (unsigned)usbback_req->req.seg[i].length > XC_PAGE_SIZE) {
  139. xen_pv_printf(xendev, 0, "segment crosses page boundary\n");
  140. return -EINVAL;
  141. }
  142. }
  143. if (usbback_req->nr_buffer_segs) {
  144. prot = PROT_READ;
  145. if (usbif_pipein(usbback_req->req.pipe)) {
  146. prot |= PROT_WRITE;
  147. }
  148. for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
  149. ref[i] = usbback_req->req.seg[i].gref;
  150. }
  151. usbback_req->buffer =
  152. xen_be_map_grant_refs(xendev, ref, usbback_req->nr_buffer_segs,
  153. prot);
  154. if (!usbback_req->buffer) {
  155. return -ENOMEM;
  156. }
  157. for (i = 0; i < usbback_req->nr_buffer_segs; i++) {
  158. seg = usbback_req->req.seg + i;
  159. addr = usbback_req->buffer + i * XC_PAGE_SIZE + seg->offset;
  160. qemu_iovec_add(&usbback_req->packet.iov, addr, seg->length);
  161. }
  162. }
  163. if (!usbif_pipeisoc(usbback_req->req.pipe)) {
  164. return 0;
  165. }
  166. /*
  167. * Right now isoc requests are not supported.
  168. * Prepare supporting those by doing the work needed on the guest
  169. * interface side.
  170. */
  171. if (!usbback_req->nr_extra_segs) {
  172. xen_pv_printf(xendev, 0, "iso request without descriptor segments\n");
  173. return -EINVAL;
  174. }
  175. prot = PROT_READ | PROT_WRITE;
  176. for (i = 0; i < usbback_req->nr_extra_segs; i++) {
  177. ref[i] = usbback_req->req.seg[i + usbback_req->req.nr_buffer_segs].gref;
  178. }
  179. usbback_req->isoc_buffer =
  180. xen_be_map_grant_refs(xendev, ref, usbback_req->nr_extra_segs,
  181. prot);
  182. if (!usbback_req->isoc_buffer) {
  183. return -ENOMEM;
  184. }
  185. return 0;
  186. }
  187. static int usbback_init_packet(struct usbback_req *usbback_req)
  188. {
  189. struct XenLegacyDevice *xendev = &usbback_req->usbif->xendev;
  190. USBPacket *packet = &usbback_req->packet;
  191. USBDevice *dev = usbback_req->stub->dev;
  192. USBEndpoint *ep;
  193. unsigned int pid, ep_nr;
  194. bool sok;
  195. int ret = 0;
  196. qemu_iovec_init(&packet->iov, USBIF_MAX_SEGMENTS_PER_REQUEST);
  197. pid = usbif_pipein(usbback_req->req.pipe) ? USB_TOKEN_IN : USB_TOKEN_OUT;
  198. ep_nr = usbif_pipeendpoint(usbback_req->req.pipe);
  199. sok = !!(usbback_req->req.transfer_flags & USBIF_SHORT_NOT_OK);
  200. if (usbif_pipectrl(usbback_req->req.pipe)) {
  201. ep_nr = 0;
  202. sok = false;
  203. }
  204. ep = usb_ep_get(dev, pid, ep_nr);
  205. usb_packet_setup(packet, pid, ep, 0, 1, sok, true);
  206. switch (usbif_pipetype(usbback_req->req.pipe)) {
  207. case USBIF_PIPE_TYPE_ISOC:
  208. TR_REQ(xendev, "iso transfer %s: buflen: %x, %d frames\n",
  209. (pid == USB_TOKEN_IN) ? "in" : "out",
  210. usbback_req->req.buffer_length,
  211. usbback_req->req.u.isoc.nr_frame_desc_segs);
  212. ret = -EINVAL; /* isoc not implemented yet */
  213. break;
  214. case USBIF_PIPE_TYPE_INT:
  215. TR_REQ(xendev, "int transfer %s: buflen: %x\n",
  216. (pid == USB_TOKEN_IN) ? "in" : "out",
  217. usbback_req->req.buffer_length);
  218. break;
  219. case USBIF_PIPE_TYPE_CTRL:
  220. packet->parameter = *(uint64_t *)usbback_req->req.u.ctrl;
  221. TR_REQ(xendev, "ctrl parameter: %"PRIx64", buflen: %x\n",
  222. packet->parameter,
  223. usbback_req->req.buffer_length);
  224. break;
  225. case USBIF_PIPE_TYPE_BULK:
  226. TR_REQ(xendev, "bulk transfer %s: buflen: %x\n",
  227. (pid == USB_TOKEN_IN) ? "in" : "out",
  228. usbback_req->req.buffer_length);
  229. break;
  230. default:
  231. ret = -EINVAL;
  232. break;
  233. }
  234. return ret;
  235. }
  236. static void usbback_do_response(struct usbback_req *usbback_req, int32_t status,
  237. int32_t actual_length, int32_t error_count)
  238. {
  239. struct usbback_info *usbif;
  240. struct usbif_urb_response *res;
  241. struct XenLegacyDevice *xendev;
  242. unsigned int notify;
  243. usbif = usbback_req->usbif;
  244. xendev = &usbif->xendev;
  245. TR_REQ(xendev, "id %d, status %d, length %d, errcnt %d\n",
  246. usbback_req->req.id, status, actual_length, error_count);
  247. if (usbback_req->packet.iov.iov) {
  248. qemu_iovec_destroy(&usbback_req->packet.iov);
  249. }
  250. if (usbback_req->buffer) {
  251. xen_be_unmap_grant_refs(xendev, usbback_req->buffer,
  252. usbback_req->nr_buffer_segs);
  253. usbback_req->buffer = NULL;
  254. }
  255. if (usbback_req->isoc_buffer) {
  256. xen_be_unmap_grant_refs(xendev, usbback_req->isoc_buffer,
  257. usbback_req->nr_extra_segs);
  258. usbback_req->isoc_buffer = NULL;
  259. }
  260. if (usbif->urb_sring) {
  261. res = RING_GET_RESPONSE(&usbif->urb_ring, usbif->urb_ring.rsp_prod_pvt);
  262. res->id = usbback_req->req.id;
  263. res->status = status;
  264. res->actual_length = actual_length;
  265. res->error_count = error_count;
  266. res->start_frame = 0;
  267. usbif->urb_ring.rsp_prod_pvt++;
  268. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&usbif->urb_ring, notify);
  269. if (notify) {
  270. xen_pv_send_notify(xendev);
  271. }
  272. }
  273. if (!usbback_req->cancelled)
  274. usbback_put_req(usbback_req);
  275. }
  276. static void usbback_do_response_ret(struct usbback_req *usbback_req,
  277. int32_t status)
  278. {
  279. usbback_do_response(usbback_req, status, 0, 0);
  280. }
  281. static int32_t usbback_xlat_status(int status)
  282. {
  283. switch (status) {
  284. case USB_RET_SUCCESS:
  285. return 0;
  286. case USB_RET_NODEV:
  287. return -ENODEV;
  288. case USB_RET_STALL:
  289. return -EPIPE;
  290. case USB_RET_BABBLE:
  291. return -EOVERFLOW;
  292. case USB_RET_IOERROR:
  293. return -EPROTO;
  294. }
  295. return -ESHUTDOWN;
  296. }
  297. static void usbback_packet_complete(USBPacket *packet)
  298. {
  299. struct usbback_req *usbback_req;
  300. int32_t status;
  301. usbback_req = container_of(packet, struct usbback_req, packet);
  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->packet);
  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, &local_err);
  644. if (local_err) {
  645. goto err;
  646. }
  647. usbif->ports[port - 1].dev = USB_DEVICE(qdev_device_add(opts, &local_err));
  648. if (!usbif->ports[port - 1].dev) {
  649. goto err;
  650. }
  651. qobject_unref(qdict);
  652. speed = usbif->ports[port - 1].dev->speed;
  653. switch (speed) {
  654. case USB_SPEED_LOW:
  655. speed = USBIF_SPEED_LOW;
  656. break;
  657. case USB_SPEED_FULL:
  658. speed = USBIF_SPEED_FULL;
  659. break;
  660. case USB_SPEED_HIGH:
  661. speed = (usbif->usb_ver < USB_VER_USB20) ?
  662. USBIF_SPEED_NONE : USBIF_SPEED_HIGH;
  663. break;
  664. default:
  665. speed = USBIF_SPEED_NONE;
  666. break;
  667. }
  668. if (speed == USBIF_SPEED_NONE) {
  669. xen_pv_printf(&usbif->xendev, 0, "device %s wrong speed\n", busid);
  670. object_unparent(OBJECT(usbif->ports[port - 1].dev));
  671. usbif->ports[port - 1].dev = NULL;
  672. return;
  673. }
  674. usb_device_reset(usbif->ports[port - 1].dev);
  675. usbif->ports[port - 1].speed = speed;
  676. usbif->ports[port - 1].attached = true;
  677. QTAILQ_INIT(&usbif->ports[port - 1].submit_q);
  678. usbback_hotplug_enq(usbif, port);
  679. TR_BUS(&usbif->xendev, "port %d attached\n", port);
  680. return;
  681. err:
  682. qobject_unref(qdict);
  683. xen_pv_printf(&usbif->xendev, 0, "device %s could not be opened\n", busid);
  684. }
  685. static void usbback_process_port(struct usbback_info *usbif, unsigned port)
  686. {
  687. char node[8];
  688. char *busid;
  689. snprintf(node, sizeof(node), "port/%d", port);
  690. busid = xenstore_read_be_str(&usbif->xendev, node);
  691. if (busid == NULL) {
  692. xen_pv_printf(&usbif->xendev, 0, "xenstore_read %s failed\n", node);
  693. return;
  694. }
  695. /* Remove portid, if the port is not connected. */
  696. if (strlen(busid) == 0) {
  697. usbback_portid_remove(usbif, port);
  698. } else {
  699. usbback_portid_add(usbif, port, busid);
  700. }
  701. g_free(busid);
  702. }
  703. static void usbback_disconnect(struct XenLegacyDevice *xendev)
  704. {
  705. struct usbback_info *usbif;
  706. unsigned int i;
  707. TR_BUS(xendev, "start\n");
  708. usbif = container_of(xendev, struct usbback_info, xendev);
  709. xen_pv_unbind_evtchn(xendev);
  710. if (usbif->urb_sring) {
  711. xen_be_unmap_grant_ref(xendev, usbif->urb_sring);
  712. usbif->urb_sring = NULL;
  713. }
  714. if (usbif->conn_sring) {
  715. xen_be_unmap_grant_ref(xendev, usbif->conn_sring);
  716. usbif->conn_sring = NULL;
  717. }
  718. for (i = 0; i < usbif->num_ports; i++) {
  719. if (usbif->ports[i].dev) {
  720. usbback_portid_drain(usbif, i + 1);
  721. }
  722. }
  723. TR_BUS(xendev, "finished\n");
  724. }
  725. static int usbback_connect(struct XenLegacyDevice *xendev)
  726. {
  727. struct usbback_info *usbif;
  728. struct usbif_urb_sring *urb_sring;
  729. struct usbif_conn_sring *conn_sring;
  730. int urb_ring_ref;
  731. int conn_ring_ref;
  732. unsigned int i, max_grants;
  733. TR_BUS(xendev, "start\n");
  734. /* max_grants: for each request and for the rings (request and connect). */
  735. max_grants = USBIF_MAX_SEGMENTS_PER_REQUEST * USB_URB_RING_SIZE + 2;
  736. xen_be_set_max_grant_refs(xendev, max_grants);
  737. usbif = container_of(xendev, struct usbback_info, xendev);
  738. if (xenstore_read_fe_int(xendev, "urb-ring-ref", &urb_ring_ref)) {
  739. xen_pv_printf(xendev, 0, "error reading urb-ring-ref\n");
  740. return -1;
  741. }
  742. if (xenstore_read_fe_int(xendev, "conn-ring-ref", &conn_ring_ref)) {
  743. xen_pv_printf(xendev, 0, "error reading conn-ring-ref\n");
  744. return -1;
  745. }
  746. if (xenstore_read_fe_int(xendev, "event-channel", &xendev->remote_port)) {
  747. xen_pv_printf(xendev, 0, "error reading event-channel\n");
  748. return -1;
  749. }
  750. usbif->urb_sring = xen_be_map_grant_ref(xendev, urb_ring_ref,
  751. PROT_READ | PROT_WRITE);
  752. usbif->conn_sring = xen_be_map_grant_ref(xendev, conn_ring_ref,
  753. PROT_READ | PROT_WRITE);
  754. if (!usbif->urb_sring || !usbif->conn_sring) {
  755. xen_pv_printf(xendev, 0, "error mapping rings\n");
  756. usbback_disconnect(xendev);
  757. return -1;
  758. }
  759. urb_sring = usbif->urb_sring;
  760. conn_sring = usbif->conn_sring;
  761. BACK_RING_INIT(&usbif->urb_ring, urb_sring, XC_PAGE_SIZE);
  762. BACK_RING_INIT(&usbif->conn_ring, conn_sring, XC_PAGE_SIZE);
  763. xen_be_bind_evtchn(xendev);
  764. xen_pv_printf(xendev, 1, "urb-ring-ref %d, conn-ring-ref %d, "
  765. "remote port %d, local port %d\n", urb_ring_ref,
  766. conn_ring_ref, xendev->remote_port, xendev->local_port);
  767. for (i = 1; i <= usbif->num_ports; i++) {
  768. if (usbif->ports[i - 1].dev) {
  769. usbback_hotplug_enq(usbif, i);
  770. }
  771. }
  772. return 0;
  773. }
  774. static void usbback_backend_changed(struct XenLegacyDevice *xendev,
  775. const char *node)
  776. {
  777. struct usbback_info *usbif;
  778. unsigned int i;
  779. TR_BUS(xendev, "path %s\n", node);
  780. usbif = container_of(xendev, struct usbback_info, xendev);
  781. for (i = 1; i <= usbif->num_ports; i++) {
  782. usbback_process_port(usbif, i);
  783. }
  784. }
  785. static int usbback_init(struct XenLegacyDevice *xendev)
  786. {
  787. struct usbback_info *usbif;
  788. TR_BUS(xendev, "start\n");
  789. usbif = container_of(xendev, struct usbback_info, xendev);
  790. if (xenstore_read_be_int(xendev, "num-ports", &usbif->num_ports) ||
  791. usbif->num_ports < 1 || usbif->num_ports > USBBACK_MAXPORTS) {
  792. xen_pv_printf(xendev, 0, "num-ports not readable or out of bounds\n");
  793. return -1;
  794. }
  795. if (xenstore_read_be_int(xendev, "usb-ver", &usbif->usb_ver) ||
  796. (usbif->usb_ver != USB_VER_USB11 && usbif->usb_ver != USB_VER_USB20)) {
  797. xen_pv_printf(xendev, 0, "usb-ver not readable or out of bounds\n");
  798. return -1;
  799. }
  800. usbback_backend_changed(xendev, "port");
  801. TR_BUS(xendev, "finished\n");
  802. return 0;
  803. }
  804. static void xen_bus_attach(USBPort *port)
  805. {
  806. struct usbback_info *usbif;
  807. usbif = port->opaque;
  808. TR_BUS(&usbif->xendev, "\n");
  809. usbif->ports[port->index].attached = true;
  810. usbback_hotplug_enq(usbif, port->index + 1);
  811. }
  812. static void xen_bus_detach(USBPort *port)
  813. {
  814. struct usbback_info *usbif;
  815. usbif = port->opaque;
  816. TR_BUS(&usbif->xendev, "\n");
  817. usbback_portid_detach(usbif, port->index + 1);
  818. }
  819. static void xen_bus_child_detach(USBPort *port, USBDevice *child)
  820. {
  821. struct usbback_info *usbif;
  822. usbif = port->opaque;
  823. TR_BUS(&usbif->xendev, "\n");
  824. }
  825. static void xen_bus_complete(USBPort *port, USBPacket *packet)
  826. {
  827. struct usbback_req *usbback_req;
  828. struct usbback_info *usbif;
  829. usbback_req = container_of(packet, struct usbback_req, packet);
  830. if (usbback_req->cancelled) {
  831. g_free(usbback_req);
  832. return;
  833. }
  834. usbif = usbback_req->usbif;
  835. TR_REQ(&usbif->xendev, "\n");
  836. usbback_packet_complete(packet);
  837. }
  838. static USBPortOps xen_usb_port_ops = {
  839. .attach = xen_bus_attach,
  840. .detach = xen_bus_detach,
  841. .child_detach = xen_bus_child_detach,
  842. .complete = xen_bus_complete,
  843. };
  844. static USBBusOps xen_usb_bus_ops = {
  845. };
  846. static void usbback_alloc(struct XenLegacyDevice *xendev)
  847. {
  848. struct usbback_info *usbif;
  849. USBPort *p;
  850. unsigned int i;
  851. usbif = container_of(xendev, struct usbback_info, xendev);
  852. usb_bus_new(&usbif->bus, sizeof(usbif->bus), &xen_usb_bus_ops,
  853. DEVICE(&xendev->qdev));
  854. for (i = 0; i < USBBACK_MAXPORTS; i++) {
  855. p = &(usbif->ports[i].port);
  856. usb_register_port(&usbif->bus, p, usbif, i, &xen_usb_port_ops,
  857. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL |
  858. USB_SPEED_MASK_HIGH);
  859. }
  860. QTAILQ_INIT(&usbif->req_free_q);
  861. QSIMPLEQ_INIT(&usbif->hotplug_q);
  862. usbif->bh = qemu_bh_new(usbback_bh, usbif);
  863. }
  864. static int usbback_free(struct XenLegacyDevice *xendev)
  865. {
  866. struct usbback_info *usbif;
  867. struct usbback_req *usbback_req;
  868. struct usbback_hotplug *usb_hp;
  869. unsigned int i;
  870. TR_BUS(xendev, "start\n");
  871. usbback_disconnect(xendev);
  872. usbif = container_of(xendev, struct usbback_info, xendev);
  873. for (i = 1; i <= usbif->num_ports; i++) {
  874. usbback_portid_remove(usbif, i);
  875. }
  876. while (!QTAILQ_EMPTY(&usbif->req_free_q)) {
  877. usbback_req = QTAILQ_FIRST(&usbif->req_free_q);
  878. QTAILQ_REMOVE(&usbif->req_free_q, usbback_req, q);
  879. g_free(usbback_req);
  880. }
  881. while (!QSIMPLEQ_EMPTY(&usbif->hotplug_q)) {
  882. usb_hp = QSIMPLEQ_FIRST(&usbif->hotplug_q);
  883. QSIMPLEQ_REMOVE_HEAD(&usbif->hotplug_q, q);
  884. g_free(usb_hp);
  885. }
  886. qemu_bh_delete(usbif->bh);
  887. for (i = 0; i < USBBACK_MAXPORTS; i++) {
  888. usb_unregister_port(&usbif->bus, &(usbif->ports[i].port));
  889. }
  890. usb_bus_release(&usbif->bus);
  891. TR_BUS(xendev, "finished\n");
  892. return 0;
  893. }
  894. static void usbback_event(struct XenLegacyDevice *xendev)
  895. {
  896. struct usbback_info *usbif;
  897. usbif = container_of(xendev, struct usbback_info, xendev);
  898. qemu_bh_schedule(usbif->bh);
  899. }
  900. struct XenDevOps xen_usb_ops = {
  901. .size = sizeof(struct usbback_info),
  902. .flags = DEVOPS_FLAG_NEED_GNTDEV,
  903. .init = usbback_init,
  904. .alloc = usbback_alloc,
  905. .free = usbback_free,
  906. .backend_changed = usbback_backend_changed,
  907. .initialise = usbback_connect,
  908. .disconnect = usbback_disconnect,
  909. .event = usbback_event,
  910. };
  911. #else /* USBIF_SHORT_NOT_OK */
  912. static int usbback_not_supported(void)
  913. {
  914. return -EINVAL;
  915. }
  916. struct XenDevOps xen_usb_ops = {
  917. .backend_register = usbback_not_supported,
  918. };
  919. #endif