redirect.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. /*
  2. * USB redirector usb-guest
  3. *
  4. * Copyright (c) 2011-2012 Red Hat, Inc.
  5. *
  6. * Red Hat Authors:
  7. * Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "qemu-common.h"
  29. #include "qemu/units.h"
  30. #include "qapi/error.h"
  31. #include "qemu/timer.h"
  32. #include "sysemu/runstate.h"
  33. #include "sysemu/sysemu.h"
  34. #include "qapi/qmp/qerror.h"
  35. #include "qemu/error-report.h"
  36. #include "qemu/iov.h"
  37. #include "qemu/module.h"
  38. #include "chardev/char-fe.h"
  39. #include <usbredirparser.h>
  40. #include <usbredirfilter.h>
  41. #include "hw/qdev-properties.h"
  42. #include "hw/usb.h"
  43. #include "migration/qemu-file-types.h"
  44. #include "migration/vmstate.h"
  45. /* ERROR is defined below. Remove any previous definition. */
  46. #undef ERROR
  47. #define MAX_ENDPOINTS 32
  48. #define NO_INTERFACE_INFO 255 /* Valid interface_count always <= 32 */
  49. #define EP2I(ep_address) (((ep_address & 0x80) >> 3) | (ep_address & 0x0f))
  50. #define I2EP(i) (((i & 0x10) << 3) | (i & 0x0f))
  51. #define USBEP2I(usb_ep) (((usb_ep)->pid == USB_TOKEN_IN) ? \
  52. ((usb_ep)->nr | 0x10) : ((usb_ep)->nr))
  53. #define I2USBEP(d, i) (usb_ep_get(&(d)->dev, \
  54. ((i) & 0x10) ? USB_TOKEN_IN : USB_TOKEN_OUT, \
  55. (i) & 0x0f))
  56. #ifndef USBREDIR_VERSION /* This is not defined in older usbredir versions */
  57. #define USBREDIR_VERSION 0
  58. #endif
  59. typedef struct USBRedirDevice USBRedirDevice;
  60. /* Struct to hold buffered packets */
  61. struct buf_packet {
  62. uint8_t *data;
  63. void *free_on_destroy;
  64. uint16_t len;
  65. uint16_t offset;
  66. uint8_t status;
  67. QTAILQ_ENTRY(buf_packet)next;
  68. };
  69. struct endp_data {
  70. USBRedirDevice *dev;
  71. uint8_t type;
  72. uint8_t interval;
  73. uint8_t interface; /* bInterfaceNumber this ep belongs to */
  74. uint16_t max_packet_size; /* In bytes, not wMaxPacketSize format !! */
  75. uint32_t max_streams;
  76. uint8_t iso_started;
  77. uint8_t iso_error; /* For reporting iso errors to the HC */
  78. uint8_t interrupt_started;
  79. uint8_t interrupt_error;
  80. uint8_t bulk_receiving_enabled;
  81. uint8_t bulk_receiving_started;
  82. uint8_t bufpq_prefilled;
  83. uint8_t bufpq_dropping_packets;
  84. QTAILQ_HEAD(, buf_packet) bufpq;
  85. int32_t bufpq_size;
  86. int32_t bufpq_target_size;
  87. USBPacket *pending_async_packet;
  88. };
  89. struct PacketIdQueueEntry {
  90. uint64_t id;
  91. QTAILQ_ENTRY(PacketIdQueueEntry)next;
  92. };
  93. struct PacketIdQueue {
  94. USBRedirDevice *dev;
  95. const char *name;
  96. QTAILQ_HEAD(, PacketIdQueueEntry) head;
  97. int size;
  98. };
  99. struct USBRedirDevice {
  100. USBDevice dev;
  101. /* Properties */
  102. CharBackend cs;
  103. bool enable_streams;
  104. uint8_t debug;
  105. int32_t bootindex;
  106. char *filter_str;
  107. /* Data passed from chardev the fd_read cb to the usbredirparser read cb */
  108. const uint8_t *read_buf;
  109. int read_buf_size;
  110. /* Active chardev-watch-tag */
  111. guint watch;
  112. /* For async handling of close / reject */
  113. QEMUBH *chardev_close_bh;
  114. QEMUBH *device_reject_bh;
  115. /* To delay the usb attach in case of quick chardev close + open */
  116. QEMUTimer *attach_timer;
  117. int64_t next_attach_time;
  118. struct usbredirparser *parser;
  119. struct endp_data endpoint[MAX_ENDPOINTS];
  120. struct PacketIdQueue cancelled;
  121. struct PacketIdQueue already_in_flight;
  122. void (*buffered_bulk_in_complete)(USBRedirDevice *, USBPacket *, uint8_t);
  123. /* Data for device filtering */
  124. struct usb_redir_device_connect_header device_info;
  125. struct usb_redir_interface_info_header interface_info;
  126. struct usbredirfilter_rule *filter_rules;
  127. int filter_rules_count;
  128. int compatible_speedmask;
  129. VMChangeStateEntry *vmstate;
  130. };
  131. #define TYPE_USB_REDIR "usb-redir"
  132. #define USB_REDIRECT(obj) OBJECT_CHECK(USBRedirDevice, (obj), TYPE_USB_REDIR)
  133. static void usbredir_hello(void *priv, struct usb_redir_hello_header *h);
  134. static void usbredir_device_connect(void *priv,
  135. struct usb_redir_device_connect_header *device_connect);
  136. static void usbredir_device_disconnect(void *priv);
  137. static void usbredir_interface_info(void *priv,
  138. struct usb_redir_interface_info_header *interface_info);
  139. static void usbredir_ep_info(void *priv,
  140. struct usb_redir_ep_info_header *ep_info);
  141. static void usbredir_configuration_status(void *priv, uint64_t id,
  142. struct usb_redir_configuration_status_header *configuration_status);
  143. static void usbredir_alt_setting_status(void *priv, uint64_t id,
  144. struct usb_redir_alt_setting_status_header *alt_setting_status);
  145. static void usbredir_iso_stream_status(void *priv, uint64_t id,
  146. struct usb_redir_iso_stream_status_header *iso_stream_status);
  147. static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
  148. struct usb_redir_interrupt_receiving_status_header
  149. *interrupt_receiving_status);
  150. static void usbredir_bulk_streams_status(void *priv, uint64_t id,
  151. struct usb_redir_bulk_streams_status_header *bulk_streams_status);
  152. static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
  153. struct usb_redir_bulk_receiving_status_header *bulk_receiving_status);
  154. static void usbredir_control_packet(void *priv, uint64_t id,
  155. struct usb_redir_control_packet_header *control_packet,
  156. uint8_t *data, int data_len);
  157. static void usbredir_bulk_packet(void *priv, uint64_t id,
  158. struct usb_redir_bulk_packet_header *bulk_packet,
  159. uint8_t *data, int data_len);
  160. static void usbredir_iso_packet(void *priv, uint64_t id,
  161. struct usb_redir_iso_packet_header *iso_packet,
  162. uint8_t *data, int data_len);
  163. static void usbredir_interrupt_packet(void *priv, uint64_t id,
  164. struct usb_redir_interrupt_packet_header *interrupt_header,
  165. uint8_t *data, int data_len);
  166. static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
  167. struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
  168. uint8_t *data, int data_len);
  169. static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
  170. int status);
  171. #define VERSION "qemu usb-redir guest " QEMU_VERSION
  172. /*
  173. * Logging stuff
  174. */
  175. #define ERROR(...) \
  176. do { \
  177. if (dev->debug >= usbredirparser_error) { \
  178. error_report("usb-redir error: " __VA_ARGS__); \
  179. } \
  180. } while (0)
  181. #define WARNING(...) \
  182. do { \
  183. if (dev->debug >= usbredirparser_warning) { \
  184. warn_report("" __VA_ARGS__); \
  185. } \
  186. } while (0)
  187. #define INFO(...) \
  188. do { \
  189. if (dev->debug >= usbredirparser_info) { \
  190. error_report("usb-redir: " __VA_ARGS__); \
  191. } \
  192. } while (0)
  193. #define DPRINTF(...) \
  194. do { \
  195. if (dev->debug >= usbredirparser_debug) { \
  196. error_report("usb-redir: " __VA_ARGS__); \
  197. } \
  198. } while (0)
  199. #define DPRINTF2(...) \
  200. do { \
  201. if (dev->debug >= usbredirparser_debug_data) { \
  202. error_report("usb-redir: " __VA_ARGS__); \
  203. } \
  204. } while (0)
  205. static void usbredir_log(void *priv, int level, const char *msg)
  206. {
  207. USBRedirDevice *dev = priv;
  208. if (dev->debug < level) {
  209. return;
  210. }
  211. error_report("%s", msg);
  212. }
  213. static void usbredir_log_data(USBRedirDevice *dev, const char *desc,
  214. const uint8_t *data, int len)
  215. {
  216. if (dev->debug < usbredirparser_debug_data) {
  217. return;
  218. }
  219. qemu_hexdump((char *)data, stderr, desc, len);
  220. }
  221. /*
  222. * usbredirparser io functions
  223. */
  224. static int usbredir_read(void *priv, uint8_t *data, int count)
  225. {
  226. USBRedirDevice *dev = priv;
  227. if (dev->read_buf_size < count) {
  228. count = dev->read_buf_size;
  229. }
  230. memcpy(data, dev->read_buf, count);
  231. dev->read_buf_size -= count;
  232. if (dev->read_buf_size) {
  233. dev->read_buf += count;
  234. } else {
  235. dev->read_buf = NULL;
  236. }
  237. return count;
  238. }
  239. static gboolean usbredir_write_unblocked(GIOChannel *chan, GIOCondition cond,
  240. void *opaque)
  241. {
  242. USBRedirDevice *dev = opaque;
  243. dev->watch = 0;
  244. usbredirparser_do_write(dev->parser);
  245. return FALSE;
  246. }
  247. static int usbredir_write(void *priv, uint8_t *data, int count)
  248. {
  249. USBRedirDevice *dev = priv;
  250. int r;
  251. if (!qemu_chr_fe_backend_open(&dev->cs)) {
  252. return 0;
  253. }
  254. /* Don't send new data to the chardev until our state is fully synced */
  255. if (!runstate_check(RUN_STATE_RUNNING)) {
  256. return 0;
  257. }
  258. r = qemu_chr_fe_write(&dev->cs, data, count);
  259. if (r < count) {
  260. if (!dev->watch) {
  261. dev->watch = qemu_chr_fe_add_watch(&dev->cs, G_IO_OUT | G_IO_HUP,
  262. usbredir_write_unblocked, dev);
  263. }
  264. if (r < 0) {
  265. r = 0;
  266. }
  267. }
  268. return r;
  269. }
  270. /*
  271. * Cancelled and buffered packets helpers
  272. */
  273. static void packet_id_queue_init(struct PacketIdQueue *q,
  274. USBRedirDevice *dev, const char *name)
  275. {
  276. q->dev = dev;
  277. q->name = name;
  278. QTAILQ_INIT(&q->head);
  279. q->size = 0;
  280. }
  281. static void packet_id_queue_add(struct PacketIdQueue *q, uint64_t id)
  282. {
  283. USBRedirDevice *dev = q->dev;
  284. struct PacketIdQueueEntry *e;
  285. DPRINTF("adding packet id %"PRIu64" to %s queue\n", id, q->name);
  286. e = g_new0(struct PacketIdQueueEntry, 1);
  287. e->id = id;
  288. QTAILQ_INSERT_TAIL(&q->head, e, next);
  289. q->size++;
  290. }
  291. static int packet_id_queue_remove(struct PacketIdQueue *q, uint64_t id)
  292. {
  293. USBRedirDevice *dev = q->dev;
  294. struct PacketIdQueueEntry *e;
  295. QTAILQ_FOREACH(e, &q->head, next) {
  296. if (e->id == id) {
  297. DPRINTF("removing packet id %"PRIu64" from %s queue\n",
  298. id, q->name);
  299. QTAILQ_REMOVE(&q->head, e, next);
  300. q->size--;
  301. g_free(e);
  302. return 1;
  303. }
  304. }
  305. return 0;
  306. }
  307. static void packet_id_queue_empty(struct PacketIdQueue *q)
  308. {
  309. USBRedirDevice *dev = q->dev;
  310. struct PacketIdQueueEntry *e, *next_e;
  311. DPRINTF("removing %d packet-ids from %s queue\n", q->size, q->name);
  312. QTAILQ_FOREACH_SAFE(e, &q->head, next, next_e) {
  313. QTAILQ_REMOVE(&q->head, e, next);
  314. g_free(e);
  315. }
  316. q->size = 0;
  317. }
  318. static void usbredir_cancel_packet(USBDevice *udev, USBPacket *p)
  319. {
  320. USBRedirDevice *dev = USB_REDIRECT(udev);
  321. int i = USBEP2I(p->ep);
  322. if (p->combined) {
  323. usb_combined_packet_cancel(udev, p);
  324. return;
  325. }
  326. if (dev->endpoint[i].pending_async_packet) {
  327. assert(dev->endpoint[i].pending_async_packet == p);
  328. dev->endpoint[i].pending_async_packet = NULL;
  329. return;
  330. }
  331. packet_id_queue_add(&dev->cancelled, p->id);
  332. usbredirparser_send_cancel_data_packet(dev->parser, p->id);
  333. usbredirparser_do_write(dev->parser);
  334. }
  335. static int usbredir_is_cancelled(USBRedirDevice *dev, uint64_t id)
  336. {
  337. if (!dev->dev.attached) {
  338. return 1; /* Treat everything as cancelled after a disconnect */
  339. }
  340. return packet_id_queue_remove(&dev->cancelled, id);
  341. }
  342. static void usbredir_fill_already_in_flight_from_ep(USBRedirDevice *dev,
  343. struct USBEndpoint *ep)
  344. {
  345. static USBPacket *p;
  346. /* async handled packets for bulk receiving eps do not count as inflight */
  347. if (dev->endpoint[USBEP2I(ep)].bulk_receiving_started) {
  348. return;
  349. }
  350. QTAILQ_FOREACH(p, &ep->queue, queue) {
  351. /* Skip combined packets, except for the first */
  352. if (p->combined && p != p->combined->first) {
  353. continue;
  354. }
  355. if (p->state == USB_PACKET_ASYNC) {
  356. packet_id_queue_add(&dev->already_in_flight, p->id);
  357. }
  358. }
  359. }
  360. static void usbredir_fill_already_in_flight(USBRedirDevice *dev)
  361. {
  362. int ep;
  363. struct USBDevice *udev = &dev->dev;
  364. usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_ctl);
  365. for (ep = 0; ep < USB_MAX_ENDPOINTS; ep++) {
  366. usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_in[ep]);
  367. usbredir_fill_already_in_flight_from_ep(dev, &udev->ep_out[ep]);
  368. }
  369. }
  370. static int usbredir_already_in_flight(USBRedirDevice *dev, uint64_t id)
  371. {
  372. return packet_id_queue_remove(&dev->already_in_flight, id);
  373. }
  374. static USBPacket *usbredir_find_packet_by_id(USBRedirDevice *dev,
  375. uint8_t ep, uint64_t id)
  376. {
  377. USBPacket *p;
  378. if (usbredir_is_cancelled(dev, id)) {
  379. return NULL;
  380. }
  381. p = usb_ep_find_packet_by_id(&dev->dev,
  382. (ep & USB_DIR_IN) ? USB_TOKEN_IN : USB_TOKEN_OUT,
  383. ep & 0x0f, id);
  384. if (p == NULL) {
  385. ERROR("could not find packet with id %"PRIu64"\n", id);
  386. }
  387. return p;
  388. }
  389. static int bufp_alloc(USBRedirDevice *dev, uint8_t *data, uint16_t len,
  390. uint8_t status, uint8_t ep, void *free_on_destroy)
  391. {
  392. struct buf_packet *bufp;
  393. if (!dev->endpoint[EP2I(ep)].bufpq_dropping_packets &&
  394. dev->endpoint[EP2I(ep)].bufpq_size >
  395. 2 * dev->endpoint[EP2I(ep)].bufpq_target_size) {
  396. DPRINTF("bufpq overflow, dropping packets ep %02X\n", ep);
  397. dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 1;
  398. }
  399. /* Since we're interupting the stream anyways, drop enough packets to get
  400. back to our target buffer size */
  401. if (dev->endpoint[EP2I(ep)].bufpq_dropping_packets) {
  402. if (dev->endpoint[EP2I(ep)].bufpq_size >
  403. dev->endpoint[EP2I(ep)].bufpq_target_size) {
  404. free(data);
  405. return -1;
  406. }
  407. dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
  408. }
  409. bufp = g_new(struct buf_packet, 1);
  410. bufp->data = data;
  411. bufp->len = len;
  412. bufp->offset = 0;
  413. bufp->status = status;
  414. bufp->free_on_destroy = free_on_destroy;
  415. QTAILQ_INSERT_TAIL(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
  416. dev->endpoint[EP2I(ep)].bufpq_size++;
  417. return 0;
  418. }
  419. static void bufp_free(USBRedirDevice *dev, struct buf_packet *bufp,
  420. uint8_t ep)
  421. {
  422. QTAILQ_REMOVE(&dev->endpoint[EP2I(ep)].bufpq, bufp, next);
  423. dev->endpoint[EP2I(ep)].bufpq_size--;
  424. free(bufp->free_on_destroy);
  425. g_free(bufp);
  426. }
  427. static void usbredir_free_bufpq(USBRedirDevice *dev, uint8_t ep)
  428. {
  429. struct buf_packet *buf, *buf_next;
  430. QTAILQ_FOREACH_SAFE(buf, &dev->endpoint[EP2I(ep)].bufpq, next, buf_next) {
  431. bufp_free(dev, buf, ep);
  432. }
  433. }
  434. /*
  435. * USBDevice callbacks
  436. */
  437. static void usbredir_handle_reset(USBDevice *udev)
  438. {
  439. USBRedirDevice *dev = USB_REDIRECT(udev);
  440. DPRINTF("reset device\n");
  441. usbredirparser_send_reset(dev->parser);
  442. usbredirparser_do_write(dev->parser);
  443. }
  444. static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
  445. uint8_t ep)
  446. {
  447. int status, len;
  448. if (!dev->endpoint[EP2I(ep)].iso_started &&
  449. !dev->endpoint[EP2I(ep)].iso_error) {
  450. struct usb_redir_start_iso_stream_header start_iso = {
  451. .endpoint = ep,
  452. };
  453. int pkts_per_sec;
  454. if (dev->dev.speed == USB_SPEED_HIGH) {
  455. pkts_per_sec = 8000 / dev->endpoint[EP2I(ep)].interval;
  456. } else {
  457. pkts_per_sec = 1000 / dev->endpoint[EP2I(ep)].interval;
  458. }
  459. /* Testing has shown that we need circa 60 ms buffer */
  460. dev->endpoint[EP2I(ep)].bufpq_target_size = (pkts_per_sec * 60) / 1000;
  461. /* Aim for approx 100 interrupts / second on the client to
  462. balance latency and interrupt load */
  463. start_iso.pkts_per_urb = pkts_per_sec / 100;
  464. if (start_iso.pkts_per_urb < 1) {
  465. start_iso.pkts_per_urb = 1;
  466. } else if (start_iso.pkts_per_urb > 32) {
  467. start_iso.pkts_per_urb = 32;
  468. }
  469. start_iso.no_urbs = DIV_ROUND_UP(
  470. dev->endpoint[EP2I(ep)].bufpq_target_size,
  471. start_iso.pkts_per_urb);
  472. /* Output endpoints pre-fill only 1/2 of the packets, keeping the rest
  473. as overflow buffer. Also see the usbredir protocol documentation */
  474. if (!(ep & USB_DIR_IN)) {
  475. start_iso.no_urbs *= 2;
  476. }
  477. if (start_iso.no_urbs > 16) {
  478. start_iso.no_urbs = 16;
  479. }
  480. /* No id, we look at the ep when receiving a status back */
  481. usbredirparser_send_start_iso_stream(dev->parser, 0, &start_iso);
  482. usbredirparser_do_write(dev->parser);
  483. DPRINTF("iso stream started pkts/sec %d pkts/urb %d urbs %d ep %02X\n",
  484. pkts_per_sec, start_iso.pkts_per_urb, start_iso.no_urbs, ep);
  485. dev->endpoint[EP2I(ep)].iso_started = 1;
  486. dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
  487. dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
  488. }
  489. if (ep & USB_DIR_IN) {
  490. struct buf_packet *isop;
  491. if (dev->endpoint[EP2I(ep)].iso_started &&
  492. !dev->endpoint[EP2I(ep)].bufpq_prefilled) {
  493. if (dev->endpoint[EP2I(ep)].bufpq_size <
  494. dev->endpoint[EP2I(ep)].bufpq_target_size) {
  495. return;
  496. }
  497. dev->endpoint[EP2I(ep)].bufpq_prefilled = 1;
  498. }
  499. isop = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq);
  500. if (isop == NULL) {
  501. DPRINTF("iso-token-in ep %02X, no isop, iso_error: %d\n",
  502. ep, dev->endpoint[EP2I(ep)].iso_error);
  503. /* Re-fill the buffer */
  504. dev->endpoint[EP2I(ep)].bufpq_prefilled = 0;
  505. /* Check iso_error for stream errors, otherwise its an underrun */
  506. status = dev->endpoint[EP2I(ep)].iso_error;
  507. dev->endpoint[EP2I(ep)].iso_error = 0;
  508. p->status = status ? USB_RET_IOERROR : USB_RET_SUCCESS;
  509. return;
  510. }
  511. DPRINTF2("iso-token-in ep %02X status %d len %d queue-size: %d\n", ep,
  512. isop->status, isop->len, dev->endpoint[EP2I(ep)].bufpq_size);
  513. status = isop->status;
  514. len = isop->len;
  515. if (len > p->iov.size) {
  516. ERROR("received iso data is larger then packet ep %02X (%d > %d)\n",
  517. ep, len, (int)p->iov.size);
  518. len = p->iov.size;
  519. status = usb_redir_babble;
  520. }
  521. usb_packet_copy(p, isop->data, len);
  522. bufp_free(dev, isop, ep);
  523. usbredir_handle_status(dev, p, status);
  524. } else {
  525. /* If the stream was not started because of a pending error don't
  526. send the packet to the usb-host */
  527. if (dev->endpoint[EP2I(ep)].iso_started) {
  528. struct usb_redir_iso_packet_header iso_packet = {
  529. .endpoint = ep,
  530. .length = p->iov.size
  531. };
  532. uint8_t buf[p->iov.size];
  533. /* No id, we look at the ep when receiving a status back */
  534. usb_packet_copy(p, buf, p->iov.size);
  535. usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
  536. buf, p->iov.size);
  537. usbredirparser_do_write(dev->parser);
  538. }
  539. status = dev->endpoint[EP2I(ep)].iso_error;
  540. dev->endpoint[EP2I(ep)].iso_error = 0;
  541. DPRINTF2("iso-token-out ep %02X status %d len %zd\n", ep, status,
  542. p->iov.size);
  543. usbredir_handle_status(dev, p, status);
  544. }
  545. }
  546. static void usbredir_stop_iso_stream(USBRedirDevice *dev, uint8_t ep)
  547. {
  548. struct usb_redir_stop_iso_stream_header stop_iso_stream = {
  549. .endpoint = ep
  550. };
  551. if (dev->endpoint[EP2I(ep)].iso_started) {
  552. usbredirparser_send_stop_iso_stream(dev->parser, 0, &stop_iso_stream);
  553. DPRINTF("iso stream stopped ep %02X\n", ep);
  554. dev->endpoint[EP2I(ep)].iso_started = 0;
  555. }
  556. dev->endpoint[EP2I(ep)].iso_error = 0;
  557. usbredir_free_bufpq(dev, ep);
  558. }
  559. /*
  560. * The usb-host may poll the endpoint faster then our guest, resulting in lots
  561. * of smaller bulkp-s. The below buffered_bulk_in_complete* functions combine
  562. * data from multiple bulkp-s into a single packet, avoiding bufpq overflows.
  563. */
  564. static void usbredir_buffered_bulk_add_data_to_packet(USBRedirDevice *dev,
  565. struct buf_packet *bulkp, int count, USBPacket *p, uint8_t ep)
  566. {
  567. usb_packet_copy(p, bulkp->data + bulkp->offset, count);
  568. bulkp->offset += count;
  569. if (bulkp->offset == bulkp->len) {
  570. /* Store status in the last packet with data from this bulkp */
  571. usbredir_handle_status(dev, p, bulkp->status);
  572. bufp_free(dev, bulkp, ep);
  573. }
  574. }
  575. static void usbredir_buffered_bulk_in_complete_raw(USBRedirDevice *dev,
  576. USBPacket *p, uint8_t ep)
  577. {
  578. struct buf_packet *bulkp;
  579. int count;
  580. while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
  581. p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
  582. count = bulkp->len - bulkp->offset;
  583. if (count > (p->iov.size - p->actual_length)) {
  584. count = p->iov.size - p->actual_length;
  585. }
  586. usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
  587. }
  588. }
  589. static void usbredir_buffered_bulk_in_complete_ftdi(USBRedirDevice *dev,
  590. USBPacket *p, uint8_t ep)
  591. {
  592. const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
  593. uint8_t header[2] = { 0, 0 };
  594. struct buf_packet *bulkp;
  595. int count;
  596. while ((bulkp = QTAILQ_FIRST(&dev->endpoint[EP2I(ep)].bufpq)) &&
  597. p->actual_length < p->iov.size && p->status == USB_RET_SUCCESS) {
  598. if (bulkp->len < 2) {
  599. WARNING("malformed ftdi bulk in packet\n");
  600. bufp_free(dev, bulkp, ep);
  601. continue;
  602. }
  603. if ((p->actual_length % maxp) == 0) {
  604. usb_packet_copy(p, bulkp->data, 2);
  605. memcpy(header, bulkp->data, 2);
  606. } else {
  607. if (bulkp->data[0] != header[0] || bulkp->data[1] != header[1]) {
  608. break; /* Different header, add to next packet */
  609. }
  610. }
  611. if (bulkp->offset == 0) {
  612. bulkp->offset = 2; /* Skip header */
  613. }
  614. count = bulkp->len - bulkp->offset;
  615. /* Must repeat the header at maxp interval */
  616. if (count > (maxp - (p->actual_length % maxp))) {
  617. count = maxp - (p->actual_length % maxp);
  618. }
  619. usbredir_buffered_bulk_add_data_to_packet(dev, bulkp, count, p, ep);
  620. }
  621. }
  622. static void usbredir_buffered_bulk_in_complete(USBRedirDevice *dev,
  623. USBPacket *p, uint8_t ep)
  624. {
  625. p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
  626. dev->buffered_bulk_in_complete(dev, p, ep);
  627. DPRINTF("bulk-token-in ep %02X status %d len %d id %"PRIu64"\n",
  628. ep, p->status, p->actual_length, p->id);
  629. }
  630. static void usbredir_handle_buffered_bulk_in_data(USBRedirDevice *dev,
  631. USBPacket *p, uint8_t ep)
  632. {
  633. /* Input bulk endpoint, buffered packet input */
  634. if (!dev->endpoint[EP2I(ep)].bulk_receiving_started) {
  635. int bpt;
  636. struct usb_redir_start_bulk_receiving_header start = {
  637. .endpoint = ep,
  638. .stream_id = 0,
  639. .no_transfers = 5,
  640. };
  641. /* Round bytes_per_transfer up to a multiple of max_packet_size */
  642. bpt = 512 + dev->endpoint[EP2I(ep)].max_packet_size - 1;
  643. bpt /= dev->endpoint[EP2I(ep)].max_packet_size;
  644. bpt *= dev->endpoint[EP2I(ep)].max_packet_size;
  645. start.bytes_per_transfer = bpt;
  646. /* No id, we look at the ep when receiving a status back */
  647. usbredirparser_send_start_bulk_receiving(dev->parser, 0, &start);
  648. usbredirparser_do_write(dev->parser);
  649. DPRINTF("bulk receiving started bytes/transfer %u count %d ep %02X\n",
  650. start.bytes_per_transfer, start.no_transfers, ep);
  651. dev->endpoint[EP2I(ep)].bulk_receiving_started = 1;
  652. /* We don't really want to drop bulk packets ever, but
  653. having some upper limit to how much we buffer is good. */
  654. dev->endpoint[EP2I(ep)].bufpq_target_size = 5000;
  655. dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
  656. }
  657. if (QTAILQ_EMPTY(&dev->endpoint[EP2I(ep)].bufpq)) {
  658. DPRINTF("bulk-token-in ep %02X, no bulkp\n", ep);
  659. assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
  660. dev->endpoint[EP2I(ep)].pending_async_packet = p;
  661. p->status = USB_RET_ASYNC;
  662. return;
  663. }
  664. usbredir_buffered_bulk_in_complete(dev, p, ep);
  665. }
  666. static void usbredir_stop_bulk_receiving(USBRedirDevice *dev, uint8_t ep)
  667. {
  668. struct usb_redir_stop_bulk_receiving_header stop_bulk = {
  669. .endpoint = ep,
  670. .stream_id = 0,
  671. };
  672. if (dev->endpoint[EP2I(ep)].bulk_receiving_started) {
  673. usbredirparser_send_stop_bulk_receiving(dev->parser, 0, &stop_bulk);
  674. DPRINTF("bulk receiving stopped ep %02X\n", ep);
  675. dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
  676. }
  677. usbredir_free_bufpq(dev, ep);
  678. }
  679. static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
  680. uint8_t ep)
  681. {
  682. struct usb_redir_bulk_packet_header bulk_packet;
  683. size_t size = usb_packet_size(p);
  684. const int maxp = dev->endpoint[EP2I(ep)].max_packet_size;
  685. if (usbredir_already_in_flight(dev, p->id)) {
  686. p->status = USB_RET_ASYNC;
  687. return;
  688. }
  689. if (dev->endpoint[EP2I(ep)].bulk_receiving_enabled) {
  690. if (size != 0 && (size % maxp) == 0) {
  691. usbredir_handle_buffered_bulk_in_data(dev, p, ep);
  692. return;
  693. }
  694. WARNING("bulk recv invalid size %zd ep %02x, disabling\n", size, ep);
  695. assert(dev->endpoint[EP2I(ep)].pending_async_packet == NULL);
  696. usbredir_stop_bulk_receiving(dev, ep);
  697. dev->endpoint[EP2I(ep)].bulk_receiving_enabled = 0;
  698. }
  699. DPRINTF("bulk-out ep %02X stream %u len %zd id %"PRIu64"\n",
  700. ep, p->stream, size, p->id);
  701. bulk_packet.endpoint = ep;
  702. bulk_packet.length = size;
  703. bulk_packet.stream_id = p->stream;
  704. bulk_packet.length_high = size >> 16;
  705. assert(bulk_packet.length_high == 0 ||
  706. usbredirparser_peer_has_cap(dev->parser,
  707. usb_redir_cap_32bits_bulk_length));
  708. if (ep & USB_DIR_IN || size == 0) {
  709. usbredirparser_send_bulk_packet(dev->parser, p->id,
  710. &bulk_packet, NULL, 0);
  711. } else {
  712. uint8_t buf[size];
  713. usb_packet_copy(p, buf, size);
  714. usbredir_log_data(dev, "bulk data out:", buf, size);
  715. usbredirparser_send_bulk_packet(dev->parser, p->id,
  716. &bulk_packet, buf, size);
  717. }
  718. usbredirparser_do_write(dev->parser);
  719. p->status = USB_RET_ASYNC;
  720. }
  721. static void usbredir_handle_interrupt_in_data(USBRedirDevice *dev,
  722. USBPacket *p, uint8_t ep)
  723. {
  724. /* Input interrupt endpoint, buffered packet input */
  725. struct buf_packet *intp, *intp_to_free;
  726. int status, len, sum;
  727. if (!dev->endpoint[EP2I(ep)].interrupt_started &&
  728. !dev->endpoint[EP2I(ep)].interrupt_error) {
  729. struct usb_redir_start_interrupt_receiving_header start_int = {
  730. .endpoint = ep,
  731. };
  732. /* No id, we look at the ep when receiving a status back */
  733. usbredirparser_send_start_interrupt_receiving(dev->parser, 0,
  734. &start_int);
  735. usbredirparser_do_write(dev->parser);
  736. DPRINTF("interrupt recv started ep %02X\n", ep);
  737. dev->endpoint[EP2I(ep)].interrupt_started = 1;
  738. /* We don't really want to drop interrupt packets ever, but
  739. having some upper limit to how much we buffer is good. */
  740. dev->endpoint[EP2I(ep)].bufpq_target_size = 1000;
  741. dev->endpoint[EP2I(ep)].bufpq_dropping_packets = 0;
  742. }
  743. /* check for completed interrupt message (with all fragments) */
  744. sum = 0;
  745. QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
  746. sum += intp->len;
  747. if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
  748. sum >= p->iov.size)
  749. break;
  750. }
  751. if (intp == NULL) {
  752. DPRINTF2("interrupt-token-in ep %02X, no intp, buffered %d\n", ep, sum);
  753. /* Check interrupt_error for stream errors */
  754. status = dev->endpoint[EP2I(ep)].interrupt_error;
  755. dev->endpoint[EP2I(ep)].interrupt_error = 0;
  756. if (status) {
  757. usbredir_handle_status(dev, p, status);
  758. } else {
  759. p->status = USB_RET_NAK;
  760. }
  761. return;
  762. }
  763. /* copy of completed interrupt message */
  764. sum = 0;
  765. status = usb_redir_success;
  766. intp_to_free = NULL;
  767. QTAILQ_FOREACH(intp, &dev->endpoint[EP2I(ep)].bufpq, next) {
  768. if (intp_to_free) {
  769. bufp_free(dev, intp_to_free, ep);
  770. }
  771. DPRINTF("interrupt-token-in ep %02X fragment status %d len %d\n", ep,
  772. intp->status, intp->len);
  773. sum += intp->len;
  774. len = intp->len;
  775. if (status == usb_redir_success) {
  776. status = intp->status;
  777. }
  778. if (sum > p->iov.size) {
  779. ERROR("received int data is larger then packet ep %02X\n", ep);
  780. len -= (sum - p->iov.size);
  781. sum = p->iov.size;
  782. status = usb_redir_babble;
  783. }
  784. usb_packet_copy(p, intp->data, len);
  785. intp_to_free = intp;
  786. if (intp->len < dev->endpoint[EP2I(ep)].max_packet_size ||
  787. sum >= p->iov.size)
  788. break;
  789. }
  790. if (intp_to_free) {
  791. bufp_free(dev, intp_to_free, ep);
  792. }
  793. DPRINTF("interrupt-token-in ep %02X summary status %d len %d\n", ep,
  794. status, sum);
  795. usbredir_handle_status(dev, p, status);
  796. }
  797. /*
  798. * Handle interrupt out data, the usbredir protocol expects us to do this
  799. * async, so that it can report back a completion status. But guests will
  800. * expect immediate completion for an interrupt endpoint, and handling this
  801. * async causes migration issues. So we report success directly, counting
  802. * on the fact that output interrupt packets normally always succeed.
  803. */
  804. static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
  805. USBPacket *p, uint8_t ep)
  806. {
  807. struct usb_redir_interrupt_packet_header interrupt_packet;
  808. uint8_t buf[p->iov.size];
  809. DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
  810. p->iov.size, p->id);
  811. interrupt_packet.endpoint = ep;
  812. interrupt_packet.length = p->iov.size;
  813. usb_packet_copy(p, buf, p->iov.size);
  814. usbredir_log_data(dev, "interrupt data out:", buf, p->iov.size);
  815. usbredirparser_send_interrupt_packet(dev->parser, p->id,
  816. &interrupt_packet, buf, p->iov.size);
  817. usbredirparser_do_write(dev->parser);
  818. }
  819. static void usbredir_stop_interrupt_receiving(USBRedirDevice *dev,
  820. uint8_t ep)
  821. {
  822. struct usb_redir_stop_interrupt_receiving_header stop_interrupt_recv = {
  823. .endpoint = ep
  824. };
  825. if (dev->endpoint[EP2I(ep)].interrupt_started) {
  826. usbredirparser_send_stop_interrupt_receiving(dev->parser, 0,
  827. &stop_interrupt_recv);
  828. DPRINTF("interrupt recv stopped ep %02X\n", ep);
  829. dev->endpoint[EP2I(ep)].interrupt_started = 0;
  830. }
  831. dev->endpoint[EP2I(ep)].interrupt_error = 0;
  832. usbredir_free_bufpq(dev, ep);
  833. }
  834. static void usbredir_handle_data(USBDevice *udev, USBPacket *p)
  835. {
  836. USBRedirDevice *dev = USB_REDIRECT(udev);
  837. uint8_t ep;
  838. ep = p->ep->nr;
  839. if (p->pid == USB_TOKEN_IN) {
  840. ep |= USB_DIR_IN;
  841. }
  842. switch (dev->endpoint[EP2I(ep)].type) {
  843. case USB_ENDPOINT_XFER_CONTROL:
  844. ERROR("handle_data called for control transfer on ep %02X\n", ep);
  845. p->status = USB_RET_NAK;
  846. break;
  847. case USB_ENDPOINT_XFER_BULK:
  848. if (p->state == USB_PACKET_SETUP && p->pid == USB_TOKEN_IN &&
  849. p->ep->pipeline) {
  850. p->status = USB_RET_ADD_TO_QUEUE;
  851. break;
  852. }
  853. usbredir_handle_bulk_data(dev, p, ep);
  854. break;
  855. case USB_ENDPOINT_XFER_ISOC:
  856. usbredir_handle_iso_data(dev, p, ep);
  857. break;
  858. case USB_ENDPOINT_XFER_INT:
  859. if (ep & USB_DIR_IN) {
  860. usbredir_handle_interrupt_in_data(dev, p, ep);
  861. } else {
  862. usbredir_handle_interrupt_out_data(dev, p, ep);
  863. }
  864. break;
  865. default:
  866. ERROR("handle_data ep %02X has unknown type %d\n", ep,
  867. dev->endpoint[EP2I(ep)].type);
  868. p->status = USB_RET_NAK;
  869. }
  870. }
  871. static void usbredir_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
  872. {
  873. if (ep->pid == USB_TOKEN_IN && ep->pipeline) {
  874. usb_ep_combine_input_packets(ep);
  875. }
  876. }
  877. static void usbredir_stop_ep(USBRedirDevice *dev, int i)
  878. {
  879. uint8_t ep = I2EP(i);
  880. switch (dev->endpoint[i].type) {
  881. case USB_ENDPOINT_XFER_BULK:
  882. if (ep & USB_DIR_IN) {
  883. usbredir_stop_bulk_receiving(dev, ep);
  884. }
  885. break;
  886. case USB_ENDPOINT_XFER_ISOC:
  887. usbredir_stop_iso_stream(dev, ep);
  888. break;
  889. case USB_ENDPOINT_XFER_INT:
  890. if (ep & USB_DIR_IN) {
  891. usbredir_stop_interrupt_receiving(dev, ep);
  892. }
  893. break;
  894. }
  895. usbredir_free_bufpq(dev, ep);
  896. }
  897. static void usbredir_ep_stopped(USBDevice *udev, USBEndpoint *uep)
  898. {
  899. USBRedirDevice *dev = USB_REDIRECT(udev);
  900. usbredir_stop_ep(dev, USBEP2I(uep));
  901. usbredirparser_do_write(dev->parser);
  902. }
  903. static void usbredir_set_config(USBRedirDevice *dev, USBPacket *p,
  904. int config)
  905. {
  906. struct usb_redir_set_configuration_header set_config;
  907. int i;
  908. DPRINTF("set config %d id %"PRIu64"\n", config, p->id);
  909. for (i = 0; i < MAX_ENDPOINTS; i++) {
  910. usbredir_stop_ep(dev, i);
  911. }
  912. set_config.configuration = config;
  913. usbredirparser_send_set_configuration(dev->parser, p->id, &set_config);
  914. usbredirparser_do_write(dev->parser);
  915. p->status = USB_RET_ASYNC;
  916. }
  917. static void usbredir_get_config(USBRedirDevice *dev, USBPacket *p)
  918. {
  919. DPRINTF("get config id %"PRIu64"\n", p->id);
  920. usbredirparser_send_get_configuration(dev->parser, p->id);
  921. usbredirparser_do_write(dev->parser);
  922. p->status = USB_RET_ASYNC;
  923. }
  924. static void usbredir_set_interface(USBRedirDevice *dev, USBPacket *p,
  925. int interface, int alt)
  926. {
  927. struct usb_redir_set_alt_setting_header set_alt;
  928. int i;
  929. DPRINTF("set interface %d alt %d id %"PRIu64"\n", interface, alt, p->id);
  930. for (i = 0; i < MAX_ENDPOINTS; i++) {
  931. if (dev->endpoint[i].interface == interface) {
  932. usbredir_stop_ep(dev, i);
  933. }
  934. }
  935. set_alt.interface = interface;
  936. set_alt.alt = alt;
  937. usbredirparser_send_set_alt_setting(dev->parser, p->id, &set_alt);
  938. usbredirparser_do_write(dev->parser);
  939. p->status = USB_RET_ASYNC;
  940. }
  941. static void usbredir_get_interface(USBRedirDevice *dev, USBPacket *p,
  942. int interface)
  943. {
  944. struct usb_redir_get_alt_setting_header get_alt;
  945. DPRINTF("get interface %d id %"PRIu64"\n", interface, p->id);
  946. get_alt.interface = interface;
  947. usbredirparser_send_get_alt_setting(dev->parser, p->id, &get_alt);
  948. usbredirparser_do_write(dev->parser);
  949. p->status = USB_RET_ASYNC;
  950. }
  951. static void usbredir_handle_control(USBDevice *udev, USBPacket *p,
  952. int request, int value, int index, int length, uint8_t *data)
  953. {
  954. USBRedirDevice *dev = USB_REDIRECT(udev);
  955. struct usb_redir_control_packet_header control_packet;
  956. if (usbredir_already_in_flight(dev, p->id)) {
  957. p->status = USB_RET_ASYNC;
  958. return;
  959. }
  960. /* Special cases for certain standard device requests */
  961. switch (request) {
  962. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  963. DPRINTF("set address %d\n", value);
  964. dev->dev.addr = value;
  965. return;
  966. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  967. usbredir_set_config(dev, p, value & 0xff);
  968. return;
  969. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  970. usbredir_get_config(dev, p);
  971. return;
  972. case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
  973. usbredir_set_interface(dev, p, index, value);
  974. return;
  975. case InterfaceRequest | USB_REQ_GET_INTERFACE:
  976. usbredir_get_interface(dev, p, index);
  977. return;
  978. }
  979. /* Normal ctrl requests, note request is (bRequestType << 8) | bRequest */
  980. DPRINTF(
  981. "ctrl-out type 0x%x req 0x%x val 0x%x index %d len %d id %"PRIu64"\n",
  982. request >> 8, request & 0xff, value, index, length, p->id);
  983. control_packet.request = request & 0xFF;
  984. control_packet.requesttype = request >> 8;
  985. control_packet.endpoint = control_packet.requesttype & USB_DIR_IN;
  986. control_packet.value = value;
  987. control_packet.index = index;
  988. control_packet.length = length;
  989. if (control_packet.requesttype & USB_DIR_IN) {
  990. usbredirparser_send_control_packet(dev->parser, p->id,
  991. &control_packet, NULL, 0);
  992. } else {
  993. usbredir_log_data(dev, "ctrl data out:", data, length);
  994. usbredirparser_send_control_packet(dev->parser, p->id,
  995. &control_packet, data, length);
  996. }
  997. usbredirparser_do_write(dev->parser);
  998. p->status = USB_RET_ASYNC;
  999. }
  1000. static int usbredir_alloc_streams(USBDevice *udev, USBEndpoint **eps,
  1001. int nr_eps, int streams)
  1002. {
  1003. USBRedirDevice *dev = USB_REDIRECT(udev);
  1004. #if USBREDIR_VERSION >= 0x000700
  1005. struct usb_redir_alloc_bulk_streams_header alloc_streams;
  1006. int i;
  1007. if (!usbredirparser_peer_has_cap(dev->parser,
  1008. usb_redir_cap_bulk_streams)) {
  1009. ERROR("peer does not support streams\n");
  1010. goto reject;
  1011. }
  1012. if (streams == 0) {
  1013. ERROR("request to allocate 0 streams\n");
  1014. return -1;
  1015. }
  1016. alloc_streams.no_streams = streams;
  1017. alloc_streams.endpoints = 0;
  1018. for (i = 0; i < nr_eps; i++) {
  1019. alloc_streams.endpoints |= 1 << USBEP2I(eps[i]);
  1020. }
  1021. usbredirparser_send_alloc_bulk_streams(dev->parser, 0, &alloc_streams);
  1022. usbredirparser_do_write(dev->parser);
  1023. return 0;
  1024. #else
  1025. ERROR("usbredir_alloc_streams not implemented\n");
  1026. goto reject;
  1027. #endif
  1028. reject:
  1029. ERROR("streams are not available, disconnecting\n");
  1030. qemu_bh_schedule(dev->device_reject_bh);
  1031. return -1;
  1032. }
  1033. static void usbredir_free_streams(USBDevice *udev, USBEndpoint **eps,
  1034. int nr_eps)
  1035. {
  1036. #if USBREDIR_VERSION >= 0x000700
  1037. USBRedirDevice *dev = USB_REDIRECT(udev);
  1038. struct usb_redir_free_bulk_streams_header free_streams;
  1039. int i;
  1040. if (!usbredirparser_peer_has_cap(dev->parser,
  1041. usb_redir_cap_bulk_streams)) {
  1042. return;
  1043. }
  1044. free_streams.endpoints = 0;
  1045. for (i = 0; i < nr_eps; i++) {
  1046. free_streams.endpoints |= 1 << USBEP2I(eps[i]);
  1047. }
  1048. usbredirparser_send_free_bulk_streams(dev->parser, 0, &free_streams);
  1049. usbredirparser_do_write(dev->parser);
  1050. #endif
  1051. }
  1052. /*
  1053. * Close events can be triggered by usbredirparser_do_write which gets called
  1054. * from within the USBDevice data / control packet callbacks and doing a
  1055. * usb_detach from within these callbacks is not a good idea.
  1056. *
  1057. * So we use a bh handler to take care of close events.
  1058. */
  1059. static void usbredir_chardev_close_bh(void *opaque)
  1060. {
  1061. USBRedirDevice *dev = opaque;
  1062. qemu_bh_cancel(dev->device_reject_bh);
  1063. usbredir_device_disconnect(dev);
  1064. if (dev->parser) {
  1065. DPRINTF("destroying usbredirparser\n");
  1066. usbredirparser_destroy(dev->parser);
  1067. dev->parser = NULL;
  1068. }
  1069. if (dev->watch) {
  1070. g_source_remove(dev->watch);
  1071. dev->watch = 0;
  1072. }
  1073. }
  1074. static void usbredir_create_parser(USBRedirDevice *dev)
  1075. {
  1076. uint32_t caps[USB_REDIR_CAPS_SIZE] = { 0, };
  1077. int flags = 0;
  1078. DPRINTF("creating usbredirparser\n");
  1079. dev->parser = qemu_oom_check(usbredirparser_create());
  1080. dev->parser->priv = dev;
  1081. dev->parser->log_func = usbredir_log;
  1082. dev->parser->read_func = usbredir_read;
  1083. dev->parser->write_func = usbredir_write;
  1084. dev->parser->hello_func = usbredir_hello;
  1085. dev->parser->device_connect_func = usbredir_device_connect;
  1086. dev->parser->device_disconnect_func = usbredir_device_disconnect;
  1087. dev->parser->interface_info_func = usbredir_interface_info;
  1088. dev->parser->ep_info_func = usbredir_ep_info;
  1089. dev->parser->configuration_status_func = usbredir_configuration_status;
  1090. dev->parser->alt_setting_status_func = usbredir_alt_setting_status;
  1091. dev->parser->iso_stream_status_func = usbredir_iso_stream_status;
  1092. dev->parser->interrupt_receiving_status_func =
  1093. usbredir_interrupt_receiving_status;
  1094. dev->parser->bulk_streams_status_func = usbredir_bulk_streams_status;
  1095. dev->parser->bulk_receiving_status_func = usbredir_bulk_receiving_status;
  1096. dev->parser->control_packet_func = usbredir_control_packet;
  1097. dev->parser->bulk_packet_func = usbredir_bulk_packet;
  1098. dev->parser->iso_packet_func = usbredir_iso_packet;
  1099. dev->parser->interrupt_packet_func = usbredir_interrupt_packet;
  1100. dev->parser->buffered_bulk_packet_func = usbredir_buffered_bulk_packet;
  1101. dev->read_buf = NULL;
  1102. dev->read_buf_size = 0;
  1103. usbredirparser_caps_set_cap(caps, usb_redir_cap_connect_device_version);
  1104. usbredirparser_caps_set_cap(caps, usb_redir_cap_filter);
  1105. usbredirparser_caps_set_cap(caps, usb_redir_cap_ep_info_max_packet_size);
  1106. usbredirparser_caps_set_cap(caps, usb_redir_cap_64bits_ids);
  1107. usbredirparser_caps_set_cap(caps, usb_redir_cap_32bits_bulk_length);
  1108. usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_receiving);
  1109. #if USBREDIR_VERSION >= 0x000700
  1110. if (dev->enable_streams) {
  1111. usbredirparser_caps_set_cap(caps, usb_redir_cap_bulk_streams);
  1112. }
  1113. #endif
  1114. if (runstate_check(RUN_STATE_INMIGRATE)) {
  1115. flags |= usbredirparser_fl_no_hello;
  1116. }
  1117. usbredirparser_init(dev->parser, VERSION, caps, USB_REDIR_CAPS_SIZE,
  1118. flags);
  1119. usbredirparser_do_write(dev->parser);
  1120. }
  1121. static void usbredir_reject_device(USBRedirDevice *dev)
  1122. {
  1123. usbredir_device_disconnect(dev);
  1124. if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter)) {
  1125. usbredirparser_send_filter_reject(dev->parser);
  1126. usbredirparser_do_write(dev->parser);
  1127. }
  1128. }
  1129. /*
  1130. * We may need to reject the device when the hcd calls alloc_streams, doing
  1131. * an usb_detach from within a hcd call is not a good idea, hence this bh.
  1132. */
  1133. static void usbredir_device_reject_bh(void *opaque)
  1134. {
  1135. USBRedirDevice *dev = opaque;
  1136. usbredir_reject_device(dev);
  1137. }
  1138. static void usbredir_do_attach(void *opaque)
  1139. {
  1140. USBRedirDevice *dev = opaque;
  1141. Error *local_err = NULL;
  1142. /* In order to work properly with XHCI controllers we need these caps */
  1143. if ((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER) && !(
  1144. usbredirparser_peer_has_cap(dev->parser,
  1145. usb_redir_cap_ep_info_max_packet_size) &&
  1146. usbredirparser_peer_has_cap(dev->parser,
  1147. usb_redir_cap_32bits_bulk_length) &&
  1148. usbredirparser_peer_has_cap(dev->parser,
  1149. usb_redir_cap_64bits_ids))) {
  1150. ERROR("usb-redir-host lacks capabilities needed for use with XHCI\n");
  1151. usbredir_reject_device(dev);
  1152. return;
  1153. }
  1154. usb_device_attach(&dev->dev, &local_err);
  1155. if (local_err) {
  1156. error_report_err(local_err);
  1157. WARNING("rejecting device due to speed mismatch\n");
  1158. usbredir_reject_device(dev);
  1159. }
  1160. }
  1161. /*
  1162. * chardev callbacks
  1163. */
  1164. static int usbredir_chardev_can_read(void *opaque)
  1165. {
  1166. USBRedirDevice *dev = opaque;
  1167. if (!dev->parser) {
  1168. WARNING("chardev_can_read called on non open chardev!\n");
  1169. return 0;
  1170. }
  1171. /* Don't read new data from the chardev until our state is fully synced */
  1172. if (!runstate_check(RUN_STATE_RUNNING)) {
  1173. return 0;
  1174. }
  1175. /* usbredir_parser_do_read will consume *all* data we give it */
  1176. return 1 * MiB;
  1177. }
  1178. static void usbredir_chardev_read(void *opaque, const uint8_t *buf, int size)
  1179. {
  1180. USBRedirDevice *dev = opaque;
  1181. /* No recursion allowed! */
  1182. assert(dev->read_buf == NULL);
  1183. dev->read_buf = buf;
  1184. dev->read_buf_size = size;
  1185. usbredirparser_do_read(dev->parser);
  1186. /* Send any acks, etc. which may be queued now */
  1187. usbredirparser_do_write(dev->parser);
  1188. }
  1189. static void usbredir_chardev_event(void *opaque, int event)
  1190. {
  1191. USBRedirDevice *dev = opaque;
  1192. switch (event) {
  1193. case CHR_EVENT_OPENED:
  1194. DPRINTF("chardev open\n");
  1195. /* Make sure any pending closes are handled (no-op if none pending) */
  1196. usbredir_chardev_close_bh(dev);
  1197. qemu_bh_cancel(dev->chardev_close_bh);
  1198. usbredir_create_parser(dev);
  1199. break;
  1200. case CHR_EVENT_CLOSED:
  1201. DPRINTF("chardev close\n");
  1202. qemu_bh_schedule(dev->chardev_close_bh);
  1203. break;
  1204. }
  1205. }
  1206. /*
  1207. * init + destroy
  1208. */
  1209. static void usbredir_vm_state_change(void *priv, int running, RunState state)
  1210. {
  1211. USBRedirDevice *dev = priv;
  1212. if (state == RUN_STATE_RUNNING && dev->parser != NULL) {
  1213. usbredirparser_do_write(dev->parser); /* Flush any pending writes */
  1214. }
  1215. }
  1216. static void usbredir_init_endpoints(USBRedirDevice *dev)
  1217. {
  1218. int i;
  1219. usb_ep_init(&dev->dev);
  1220. memset(dev->endpoint, 0, sizeof(dev->endpoint));
  1221. for (i = 0; i < MAX_ENDPOINTS; i++) {
  1222. dev->endpoint[i].dev = dev;
  1223. QTAILQ_INIT(&dev->endpoint[i].bufpq);
  1224. }
  1225. }
  1226. static void usbredir_realize(USBDevice *udev, Error **errp)
  1227. {
  1228. USBRedirDevice *dev = USB_REDIRECT(udev);
  1229. int i;
  1230. if (!qemu_chr_fe_backend_connected(&dev->cs)) {
  1231. error_setg(errp, QERR_MISSING_PARAMETER, "chardev");
  1232. return;
  1233. }
  1234. if (dev->filter_str) {
  1235. i = usbredirfilter_string_to_rules(dev->filter_str, ":", "|",
  1236. &dev->filter_rules,
  1237. &dev->filter_rules_count);
  1238. if (i) {
  1239. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "filter",
  1240. "a usb device filter string");
  1241. return;
  1242. }
  1243. }
  1244. dev->chardev_close_bh = qemu_bh_new(usbredir_chardev_close_bh, dev);
  1245. dev->device_reject_bh = qemu_bh_new(usbredir_device_reject_bh, dev);
  1246. dev->attach_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, usbredir_do_attach, dev);
  1247. packet_id_queue_init(&dev->cancelled, dev, "cancelled");
  1248. packet_id_queue_init(&dev->already_in_flight, dev, "already-in-flight");
  1249. usbredir_init_endpoints(dev);
  1250. /* We'll do the attach once we receive the speed from the usb-host */
  1251. udev->auto_attach = 0;
  1252. /* Will be cleared during setup when we find conflicts */
  1253. dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
  1254. /* Let the backend know we are ready */
  1255. qemu_chr_fe_set_handlers(&dev->cs, usbredir_chardev_can_read,
  1256. usbredir_chardev_read, usbredir_chardev_event,
  1257. NULL, dev, NULL, true);
  1258. dev->vmstate =
  1259. qemu_add_vm_change_state_handler(usbredir_vm_state_change, dev);
  1260. }
  1261. static void usbredir_cleanup_device_queues(USBRedirDevice *dev)
  1262. {
  1263. int i;
  1264. packet_id_queue_empty(&dev->cancelled);
  1265. packet_id_queue_empty(&dev->already_in_flight);
  1266. for (i = 0; i < MAX_ENDPOINTS; i++) {
  1267. usbredir_free_bufpq(dev, I2EP(i));
  1268. }
  1269. }
  1270. static void usbredir_unrealize(USBDevice *udev, Error **errp)
  1271. {
  1272. USBRedirDevice *dev = USB_REDIRECT(udev);
  1273. qemu_chr_fe_deinit(&dev->cs, true);
  1274. /* Note must be done after qemu_chr_close, as that causes a close event */
  1275. qemu_bh_delete(dev->chardev_close_bh);
  1276. qemu_bh_delete(dev->device_reject_bh);
  1277. timer_del(dev->attach_timer);
  1278. timer_free(dev->attach_timer);
  1279. usbredir_cleanup_device_queues(dev);
  1280. if (dev->parser) {
  1281. usbredirparser_destroy(dev->parser);
  1282. }
  1283. if (dev->watch) {
  1284. g_source_remove(dev->watch);
  1285. }
  1286. free(dev->filter_rules);
  1287. qemu_del_vm_change_state_handler(dev->vmstate);
  1288. }
  1289. static int usbredir_check_filter(USBRedirDevice *dev)
  1290. {
  1291. if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
  1292. ERROR("No interface info for device\n");
  1293. goto error;
  1294. }
  1295. if (dev->filter_rules) {
  1296. if (!usbredirparser_peer_has_cap(dev->parser,
  1297. usb_redir_cap_connect_device_version)) {
  1298. ERROR("Device filter specified and peer does not have the "
  1299. "connect_device_version capability\n");
  1300. goto error;
  1301. }
  1302. if (usbredirfilter_check(
  1303. dev->filter_rules,
  1304. dev->filter_rules_count,
  1305. dev->device_info.device_class,
  1306. dev->device_info.device_subclass,
  1307. dev->device_info.device_protocol,
  1308. dev->interface_info.interface_class,
  1309. dev->interface_info.interface_subclass,
  1310. dev->interface_info.interface_protocol,
  1311. dev->interface_info.interface_count,
  1312. dev->device_info.vendor_id,
  1313. dev->device_info.product_id,
  1314. dev->device_info.device_version_bcd,
  1315. 0) != 0) {
  1316. goto error;
  1317. }
  1318. }
  1319. return 0;
  1320. error:
  1321. usbredir_reject_device(dev);
  1322. return -1;
  1323. }
  1324. static void usbredir_check_bulk_receiving(USBRedirDevice *dev)
  1325. {
  1326. int i, j, quirks;
  1327. if (!usbredirparser_peer_has_cap(dev->parser,
  1328. usb_redir_cap_bulk_receiving)) {
  1329. return;
  1330. }
  1331. for (i = EP2I(USB_DIR_IN); i < MAX_ENDPOINTS; i++) {
  1332. dev->endpoint[i].bulk_receiving_enabled = 0;
  1333. }
  1334. if (dev->interface_info.interface_count == NO_INTERFACE_INFO) {
  1335. return;
  1336. }
  1337. for (i = 0; i < dev->interface_info.interface_count; i++) {
  1338. quirks = usb_get_quirks(dev->device_info.vendor_id,
  1339. dev->device_info.product_id,
  1340. dev->interface_info.interface_class[i],
  1341. dev->interface_info.interface_subclass[i],
  1342. dev->interface_info.interface_protocol[i]);
  1343. if (!(quirks & USB_QUIRK_BUFFER_BULK_IN)) {
  1344. continue;
  1345. }
  1346. if (quirks & USB_QUIRK_IS_FTDI) {
  1347. dev->buffered_bulk_in_complete =
  1348. usbredir_buffered_bulk_in_complete_ftdi;
  1349. } else {
  1350. dev->buffered_bulk_in_complete =
  1351. usbredir_buffered_bulk_in_complete_raw;
  1352. }
  1353. for (j = EP2I(USB_DIR_IN); j < MAX_ENDPOINTS; j++) {
  1354. if (dev->endpoint[j].interface ==
  1355. dev->interface_info.interface[i] &&
  1356. dev->endpoint[j].type == USB_ENDPOINT_XFER_BULK &&
  1357. dev->endpoint[j].max_packet_size != 0) {
  1358. dev->endpoint[j].bulk_receiving_enabled = 1;
  1359. /*
  1360. * With buffering pipelining is not necessary. Also packet
  1361. * combining and bulk in buffering don't play nice together!
  1362. */
  1363. I2USBEP(dev, j)->pipeline = false;
  1364. break; /* Only buffer for the first ep of each intf */
  1365. }
  1366. }
  1367. }
  1368. }
  1369. /*
  1370. * usbredirparser packet complete callbacks
  1371. */
  1372. static void usbredir_handle_status(USBRedirDevice *dev, USBPacket *p,
  1373. int status)
  1374. {
  1375. switch (status) {
  1376. case usb_redir_success:
  1377. p->status = USB_RET_SUCCESS; /* Clear previous ASYNC status */
  1378. break;
  1379. case usb_redir_stall:
  1380. p->status = USB_RET_STALL;
  1381. break;
  1382. case usb_redir_cancelled:
  1383. /*
  1384. * When the usbredir-host unredirects a device, it will report a status
  1385. * of cancelled for all pending packets, followed by a disconnect msg.
  1386. */
  1387. p->status = USB_RET_IOERROR;
  1388. break;
  1389. case usb_redir_inval:
  1390. WARNING("got invalid param error from usb-host?\n");
  1391. p->status = USB_RET_IOERROR;
  1392. break;
  1393. case usb_redir_babble:
  1394. p->status = USB_RET_BABBLE;
  1395. break;
  1396. case usb_redir_ioerror:
  1397. case usb_redir_timeout:
  1398. default:
  1399. p->status = USB_RET_IOERROR;
  1400. }
  1401. }
  1402. static void usbredir_hello(void *priv, struct usb_redir_hello_header *h)
  1403. {
  1404. USBRedirDevice *dev = priv;
  1405. /* Try to send the filter info now that we've the usb-host's caps */
  1406. if (usbredirparser_peer_has_cap(dev->parser, usb_redir_cap_filter) &&
  1407. dev->filter_rules) {
  1408. usbredirparser_send_filter_filter(dev->parser, dev->filter_rules,
  1409. dev->filter_rules_count);
  1410. usbredirparser_do_write(dev->parser);
  1411. }
  1412. }
  1413. static void usbredir_device_connect(void *priv,
  1414. struct usb_redir_device_connect_header *device_connect)
  1415. {
  1416. USBRedirDevice *dev = priv;
  1417. const char *speed;
  1418. if (timer_pending(dev->attach_timer) || dev->dev.attached) {
  1419. ERROR("Received device connect while already connected\n");
  1420. return;
  1421. }
  1422. switch (device_connect->speed) {
  1423. case usb_redir_speed_low:
  1424. speed = "low speed";
  1425. dev->dev.speed = USB_SPEED_LOW;
  1426. dev->compatible_speedmask &= ~USB_SPEED_MASK_FULL;
  1427. dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
  1428. break;
  1429. case usb_redir_speed_full:
  1430. speed = "full speed";
  1431. dev->dev.speed = USB_SPEED_FULL;
  1432. dev->compatible_speedmask &= ~USB_SPEED_MASK_HIGH;
  1433. break;
  1434. case usb_redir_speed_high:
  1435. speed = "high speed";
  1436. dev->dev.speed = USB_SPEED_HIGH;
  1437. break;
  1438. case usb_redir_speed_super:
  1439. speed = "super speed";
  1440. dev->dev.speed = USB_SPEED_SUPER;
  1441. break;
  1442. default:
  1443. speed = "unknown speed";
  1444. dev->dev.speed = USB_SPEED_FULL;
  1445. }
  1446. if (usbredirparser_peer_has_cap(dev->parser,
  1447. usb_redir_cap_connect_device_version)) {
  1448. INFO("attaching %s device %04x:%04x version %d.%d class %02x\n",
  1449. speed, device_connect->vendor_id, device_connect->product_id,
  1450. ((device_connect->device_version_bcd & 0xf000) >> 12) * 10 +
  1451. ((device_connect->device_version_bcd & 0x0f00) >> 8),
  1452. ((device_connect->device_version_bcd & 0x00f0) >> 4) * 10 +
  1453. ((device_connect->device_version_bcd & 0x000f) >> 0),
  1454. device_connect->device_class);
  1455. } else {
  1456. INFO("attaching %s device %04x:%04x class %02x\n", speed,
  1457. device_connect->vendor_id, device_connect->product_id,
  1458. device_connect->device_class);
  1459. }
  1460. dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
  1461. dev->device_info = *device_connect;
  1462. if (usbredir_check_filter(dev)) {
  1463. WARNING("Device %04x:%04x rejected by device filter, not attaching\n",
  1464. device_connect->vendor_id, device_connect->product_id);
  1465. return;
  1466. }
  1467. usbredir_check_bulk_receiving(dev);
  1468. timer_mod(dev->attach_timer, dev->next_attach_time);
  1469. }
  1470. static void usbredir_device_disconnect(void *priv)
  1471. {
  1472. USBRedirDevice *dev = priv;
  1473. /* Stop any pending attaches */
  1474. timer_del(dev->attach_timer);
  1475. if (dev->dev.attached) {
  1476. DPRINTF("detaching device\n");
  1477. usb_device_detach(&dev->dev);
  1478. /*
  1479. * Delay next usb device attach to give the guest a chance to see
  1480. * see the detach / attach in case of quick close / open succession
  1481. */
  1482. dev->next_attach_time = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 200;
  1483. }
  1484. /* Reset state so that the next dev connected starts with a clean slate */
  1485. usbredir_cleanup_device_queues(dev);
  1486. usbredir_init_endpoints(dev);
  1487. dev->interface_info.interface_count = NO_INTERFACE_INFO;
  1488. dev->dev.addr = 0;
  1489. dev->dev.speed = 0;
  1490. dev->compatible_speedmask = USB_SPEED_MASK_FULL | USB_SPEED_MASK_HIGH;
  1491. }
  1492. static void usbredir_interface_info(void *priv,
  1493. struct usb_redir_interface_info_header *interface_info)
  1494. {
  1495. USBRedirDevice *dev = priv;
  1496. dev->interface_info = *interface_info;
  1497. /*
  1498. * If we receive interface info after the device has already been
  1499. * connected (ie on a set_config), re-check interface dependent things.
  1500. */
  1501. if (timer_pending(dev->attach_timer) || dev->dev.attached) {
  1502. usbredir_check_bulk_receiving(dev);
  1503. if (usbredir_check_filter(dev)) {
  1504. ERROR("Device no longer matches filter after interface info "
  1505. "change, disconnecting!\n");
  1506. }
  1507. }
  1508. }
  1509. static void usbredir_mark_speed_incompatible(USBRedirDevice *dev, int speed)
  1510. {
  1511. dev->compatible_speedmask &= ~(1 << speed);
  1512. dev->dev.speedmask = (1 << dev->dev.speed) | dev->compatible_speedmask;
  1513. }
  1514. static void usbredir_set_pipeline(USBRedirDevice *dev, struct USBEndpoint *uep)
  1515. {
  1516. if (uep->type != USB_ENDPOINT_XFER_BULK) {
  1517. return;
  1518. }
  1519. if (uep->pid == USB_TOKEN_OUT) {
  1520. uep->pipeline = true;
  1521. }
  1522. if (uep->pid == USB_TOKEN_IN && uep->max_packet_size != 0 &&
  1523. usbredirparser_peer_has_cap(dev->parser,
  1524. usb_redir_cap_32bits_bulk_length)) {
  1525. uep->pipeline = true;
  1526. }
  1527. }
  1528. static void usbredir_setup_usb_eps(USBRedirDevice *dev)
  1529. {
  1530. struct USBEndpoint *usb_ep;
  1531. int i;
  1532. for (i = 0; i < MAX_ENDPOINTS; i++) {
  1533. usb_ep = I2USBEP(dev, i);
  1534. usb_ep->type = dev->endpoint[i].type;
  1535. usb_ep->ifnum = dev->endpoint[i].interface;
  1536. usb_ep->max_packet_size = dev->endpoint[i].max_packet_size;
  1537. usb_ep->max_streams = dev->endpoint[i].max_streams;
  1538. usbredir_set_pipeline(dev, usb_ep);
  1539. }
  1540. }
  1541. static void usbredir_ep_info(void *priv,
  1542. struct usb_redir_ep_info_header *ep_info)
  1543. {
  1544. USBRedirDevice *dev = priv;
  1545. int i;
  1546. assert(dev != NULL);
  1547. for (i = 0; i < MAX_ENDPOINTS; i++) {
  1548. dev->endpoint[i].type = ep_info->type[i];
  1549. dev->endpoint[i].interval = ep_info->interval[i];
  1550. dev->endpoint[i].interface = ep_info->interface[i];
  1551. if (usbredirparser_peer_has_cap(dev->parser,
  1552. usb_redir_cap_ep_info_max_packet_size)) {
  1553. dev->endpoint[i].max_packet_size = ep_info->max_packet_size[i];
  1554. }
  1555. #if USBREDIR_VERSION >= 0x000700
  1556. if (usbredirparser_peer_has_cap(dev->parser,
  1557. usb_redir_cap_bulk_streams)) {
  1558. dev->endpoint[i].max_streams = ep_info->max_streams[i];
  1559. }
  1560. #endif
  1561. switch (dev->endpoint[i].type) {
  1562. case usb_redir_type_invalid:
  1563. break;
  1564. case usb_redir_type_iso:
  1565. usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
  1566. usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
  1567. /* Fall through */
  1568. case usb_redir_type_interrupt:
  1569. if (!usbredirparser_peer_has_cap(dev->parser,
  1570. usb_redir_cap_ep_info_max_packet_size) ||
  1571. ep_info->max_packet_size[i] > 64) {
  1572. usbredir_mark_speed_incompatible(dev, USB_SPEED_FULL);
  1573. }
  1574. if (!usbredirparser_peer_has_cap(dev->parser,
  1575. usb_redir_cap_ep_info_max_packet_size) ||
  1576. ep_info->max_packet_size[i] > 1024) {
  1577. usbredir_mark_speed_incompatible(dev, USB_SPEED_HIGH);
  1578. }
  1579. if (dev->endpoint[i].interval == 0) {
  1580. ERROR("Received 0 interval for isoc or irq endpoint\n");
  1581. usbredir_reject_device(dev);
  1582. return;
  1583. }
  1584. /* Fall through */
  1585. case usb_redir_type_control:
  1586. case usb_redir_type_bulk:
  1587. DPRINTF("ep: %02X type: %d interface: %d\n", I2EP(i),
  1588. dev->endpoint[i].type, dev->endpoint[i].interface);
  1589. break;
  1590. default:
  1591. ERROR("Received invalid endpoint type\n");
  1592. usbredir_reject_device(dev);
  1593. return;
  1594. }
  1595. }
  1596. /* The new ep info may have caused a speed incompatibility, recheck */
  1597. if (dev->dev.attached &&
  1598. !(dev->dev.port->speedmask & dev->dev.speedmask)) {
  1599. ERROR("Device no longer matches speed after endpoint info change, "
  1600. "disconnecting!\n");
  1601. usbredir_reject_device(dev);
  1602. return;
  1603. }
  1604. usbredir_setup_usb_eps(dev);
  1605. usbredir_check_bulk_receiving(dev);
  1606. }
  1607. static void usbredir_configuration_status(void *priv, uint64_t id,
  1608. struct usb_redir_configuration_status_header *config_status)
  1609. {
  1610. USBRedirDevice *dev = priv;
  1611. USBPacket *p;
  1612. DPRINTF("set config status %d config %d id %"PRIu64"\n",
  1613. config_status->status, config_status->configuration, id);
  1614. p = usbredir_find_packet_by_id(dev, 0, id);
  1615. if (p) {
  1616. if (dev->dev.setup_buf[0] & USB_DIR_IN) {
  1617. dev->dev.data_buf[0] = config_status->configuration;
  1618. p->actual_length = 1;
  1619. }
  1620. usbredir_handle_status(dev, p, config_status->status);
  1621. usb_generic_async_ctrl_complete(&dev->dev, p);
  1622. }
  1623. }
  1624. static void usbredir_alt_setting_status(void *priv, uint64_t id,
  1625. struct usb_redir_alt_setting_status_header *alt_setting_status)
  1626. {
  1627. USBRedirDevice *dev = priv;
  1628. USBPacket *p;
  1629. DPRINTF("alt status %d intf %d alt %d id: %"PRIu64"\n",
  1630. alt_setting_status->status, alt_setting_status->interface,
  1631. alt_setting_status->alt, id);
  1632. p = usbredir_find_packet_by_id(dev, 0, id);
  1633. if (p) {
  1634. if (dev->dev.setup_buf[0] & USB_DIR_IN) {
  1635. dev->dev.data_buf[0] = alt_setting_status->alt;
  1636. p->actual_length = 1;
  1637. }
  1638. usbredir_handle_status(dev, p, alt_setting_status->status);
  1639. usb_generic_async_ctrl_complete(&dev->dev, p);
  1640. }
  1641. }
  1642. static void usbredir_iso_stream_status(void *priv, uint64_t id,
  1643. struct usb_redir_iso_stream_status_header *iso_stream_status)
  1644. {
  1645. USBRedirDevice *dev = priv;
  1646. uint8_t ep = iso_stream_status->endpoint;
  1647. DPRINTF("iso status %d ep %02X id %"PRIu64"\n", iso_stream_status->status,
  1648. ep, id);
  1649. if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].iso_started) {
  1650. return;
  1651. }
  1652. dev->endpoint[EP2I(ep)].iso_error = iso_stream_status->status;
  1653. if (iso_stream_status->status == usb_redir_stall) {
  1654. DPRINTF("iso stream stopped by peer ep %02X\n", ep);
  1655. dev->endpoint[EP2I(ep)].iso_started = 0;
  1656. }
  1657. }
  1658. static void usbredir_interrupt_receiving_status(void *priv, uint64_t id,
  1659. struct usb_redir_interrupt_receiving_status_header
  1660. *interrupt_receiving_status)
  1661. {
  1662. USBRedirDevice *dev = priv;
  1663. uint8_t ep = interrupt_receiving_status->endpoint;
  1664. DPRINTF("interrupt recv status %d ep %02X id %"PRIu64"\n",
  1665. interrupt_receiving_status->status, ep, id);
  1666. if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].interrupt_started) {
  1667. return;
  1668. }
  1669. dev->endpoint[EP2I(ep)].interrupt_error =
  1670. interrupt_receiving_status->status;
  1671. if (interrupt_receiving_status->status == usb_redir_stall) {
  1672. DPRINTF("interrupt receiving stopped by peer ep %02X\n", ep);
  1673. dev->endpoint[EP2I(ep)].interrupt_started = 0;
  1674. }
  1675. }
  1676. static void usbredir_bulk_streams_status(void *priv, uint64_t id,
  1677. struct usb_redir_bulk_streams_status_header *bulk_streams_status)
  1678. {
  1679. #if USBREDIR_VERSION >= 0x000700
  1680. USBRedirDevice *dev = priv;
  1681. if (bulk_streams_status->status == usb_redir_success) {
  1682. DPRINTF("bulk streams status %d eps %08x\n",
  1683. bulk_streams_status->status, bulk_streams_status->endpoints);
  1684. } else {
  1685. ERROR("bulk streams %s failed status %d eps %08x\n",
  1686. (bulk_streams_status->no_streams == 0) ? "free" : "alloc",
  1687. bulk_streams_status->status, bulk_streams_status->endpoints);
  1688. ERROR("usb-redir-host does not provide streams, disconnecting\n");
  1689. usbredir_reject_device(dev);
  1690. }
  1691. #endif
  1692. }
  1693. static void usbredir_bulk_receiving_status(void *priv, uint64_t id,
  1694. struct usb_redir_bulk_receiving_status_header *bulk_receiving_status)
  1695. {
  1696. USBRedirDevice *dev = priv;
  1697. uint8_t ep = bulk_receiving_status->endpoint;
  1698. DPRINTF("bulk recv status %d ep %02X id %"PRIu64"\n",
  1699. bulk_receiving_status->status, ep, id);
  1700. if (!dev->dev.attached || !dev->endpoint[EP2I(ep)].bulk_receiving_started) {
  1701. return;
  1702. }
  1703. if (bulk_receiving_status->status == usb_redir_stall) {
  1704. DPRINTF("bulk receiving stopped by peer ep %02X\n", ep);
  1705. dev->endpoint[EP2I(ep)].bulk_receiving_started = 0;
  1706. }
  1707. }
  1708. static void usbredir_control_packet(void *priv, uint64_t id,
  1709. struct usb_redir_control_packet_header *control_packet,
  1710. uint8_t *data, int data_len)
  1711. {
  1712. USBRedirDevice *dev = priv;
  1713. USBPacket *p;
  1714. int len = control_packet->length;
  1715. DPRINTF("ctrl-in status %d len %d id %"PRIu64"\n", control_packet->status,
  1716. len, id);
  1717. /* Fix up USB-3 ep0 maxpacket size to allow superspeed connected devices
  1718. * to work redirected to a not superspeed capable hcd */
  1719. if (dev->dev.speed == USB_SPEED_SUPER &&
  1720. !((dev->dev.port->speedmask & USB_SPEED_MASK_SUPER)) &&
  1721. control_packet->requesttype == 0x80 &&
  1722. control_packet->request == 6 &&
  1723. control_packet->value == 0x100 && control_packet->index == 0 &&
  1724. data_len >= 18 && data[7] == 9) {
  1725. data[7] = 64;
  1726. }
  1727. p = usbredir_find_packet_by_id(dev, 0, id);
  1728. if (p) {
  1729. usbredir_handle_status(dev, p, control_packet->status);
  1730. if (data_len > 0) {
  1731. usbredir_log_data(dev, "ctrl data in:", data, data_len);
  1732. if (data_len > sizeof(dev->dev.data_buf)) {
  1733. ERROR("ctrl buffer too small (%d > %zu)\n",
  1734. data_len, sizeof(dev->dev.data_buf));
  1735. p->status = USB_RET_STALL;
  1736. data_len = len = sizeof(dev->dev.data_buf);
  1737. }
  1738. memcpy(dev->dev.data_buf, data, data_len);
  1739. }
  1740. p->actual_length = len;
  1741. usb_generic_async_ctrl_complete(&dev->dev, p);
  1742. }
  1743. free(data);
  1744. }
  1745. static void usbredir_bulk_packet(void *priv, uint64_t id,
  1746. struct usb_redir_bulk_packet_header *bulk_packet,
  1747. uint8_t *data, int data_len)
  1748. {
  1749. USBRedirDevice *dev = priv;
  1750. uint8_t ep = bulk_packet->endpoint;
  1751. int len = (bulk_packet->length_high << 16) | bulk_packet->length;
  1752. USBPacket *p;
  1753. DPRINTF("bulk-in status %d ep %02X stream %u len %d id %"PRIu64"\n",
  1754. bulk_packet->status, ep, bulk_packet->stream_id, len, id);
  1755. p = usbredir_find_packet_by_id(dev, ep, id);
  1756. if (p) {
  1757. size_t size = usb_packet_size(p);
  1758. usbredir_handle_status(dev, p, bulk_packet->status);
  1759. if (data_len > 0) {
  1760. usbredir_log_data(dev, "bulk data in:", data, data_len);
  1761. if (data_len > size) {
  1762. ERROR("bulk got more data then requested (%d > %zd)\n",
  1763. data_len, p->iov.size);
  1764. p->status = USB_RET_BABBLE;
  1765. data_len = len = size;
  1766. }
  1767. usb_packet_copy(p, data, data_len);
  1768. }
  1769. p->actual_length = len;
  1770. if (p->pid == USB_TOKEN_IN && p->ep->pipeline) {
  1771. usb_combined_input_packet_complete(&dev->dev, p);
  1772. } else {
  1773. usb_packet_complete(&dev->dev, p);
  1774. }
  1775. }
  1776. free(data);
  1777. }
  1778. static void usbredir_iso_packet(void *priv, uint64_t id,
  1779. struct usb_redir_iso_packet_header *iso_packet,
  1780. uint8_t *data, int data_len)
  1781. {
  1782. USBRedirDevice *dev = priv;
  1783. uint8_t ep = iso_packet->endpoint;
  1784. DPRINTF2("iso-in status %d ep %02X len %d id %"PRIu64"\n",
  1785. iso_packet->status, ep, data_len, id);
  1786. if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_ISOC) {
  1787. ERROR("received iso packet for non iso endpoint %02X\n", ep);
  1788. free(data);
  1789. return;
  1790. }
  1791. if (dev->endpoint[EP2I(ep)].iso_started == 0) {
  1792. DPRINTF("received iso packet for non started stream ep %02X\n", ep);
  1793. free(data);
  1794. return;
  1795. }
  1796. /* bufp_alloc also adds the packet to the ep queue */
  1797. bufp_alloc(dev, data, data_len, iso_packet->status, ep, data);
  1798. }
  1799. static void usbredir_interrupt_packet(void *priv, uint64_t id,
  1800. struct usb_redir_interrupt_packet_header *interrupt_packet,
  1801. uint8_t *data, int data_len)
  1802. {
  1803. USBRedirDevice *dev = priv;
  1804. uint8_t ep = interrupt_packet->endpoint;
  1805. DPRINTF("interrupt-in status %d ep %02X len %d id %"PRIu64"\n",
  1806. interrupt_packet->status, ep, data_len, id);
  1807. if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_INT) {
  1808. ERROR("received int packet for non interrupt endpoint %02X\n", ep);
  1809. free(data);
  1810. return;
  1811. }
  1812. if (ep & USB_DIR_IN) {
  1813. if (dev->endpoint[EP2I(ep)].interrupt_started == 0) {
  1814. DPRINTF("received int packet while not started ep %02X\n", ep);
  1815. free(data);
  1816. return;
  1817. }
  1818. /* bufp_alloc also adds the packet to the ep queue */
  1819. bufp_alloc(dev, data, data_len, interrupt_packet->status, ep, data);
  1820. /* insufficient data solved with USB_RET_NAK */
  1821. usb_wakeup(usb_ep_get(&dev->dev, USB_TOKEN_IN, ep & 0x0f), 0);
  1822. } else {
  1823. /*
  1824. * We report output interrupt packets as completed directly upon
  1825. * submission, so all we can do here if one failed is warn.
  1826. */
  1827. if (interrupt_packet->status) {
  1828. WARNING("interrupt output failed status %d ep %02X id %"PRIu64"\n",
  1829. interrupt_packet->status, ep, id);
  1830. }
  1831. }
  1832. }
  1833. static void usbredir_buffered_bulk_packet(void *priv, uint64_t id,
  1834. struct usb_redir_buffered_bulk_packet_header *buffered_bulk_packet,
  1835. uint8_t *data, int data_len)
  1836. {
  1837. USBRedirDevice *dev = priv;
  1838. uint8_t status, ep = buffered_bulk_packet->endpoint;
  1839. void *free_on_destroy;
  1840. int i, len;
  1841. DPRINTF("buffered-bulk-in status %d ep %02X len %d id %"PRIu64"\n",
  1842. buffered_bulk_packet->status, ep, data_len, id);
  1843. if (dev->endpoint[EP2I(ep)].type != USB_ENDPOINT_XFER_BULK) {
  1844. ERROR("received buffered-bulk packet for non bulk ep %02X\n", ep);
  1845. free(data);
  1846. return;
  1847. }
  1848. if (dev->endpoint[EP2I(ep)].bulk_receiving_started == 0) {
  1849. DPRINTF("received buffered-bulk packet on not started ep %02X\n", ep);
  1850. free(data);
  1851. return;
  1852. }
  1853. /* Data must be in maxp chunks for buffered_bulk_add_*_data_to_packet */
  1854. len = dev->endpoint[EP2I(ep)].max_packet_size;
  1855. status = usb_redir_success;
  1856. free_on_destroy = NULL;
  1857. for (i = 0; i < data_len; i += len) {
  1858. int r;
  1859. if (len >= (data_len - i)) {
  1860. len = data_len - i;
  1861. status = buffered_bulk_packet->status;
  1862. free_on_destroy = data;
  1863. }
  1864. /* bufp_alloc also adds the packet to the ep queue */
  1865. r = bufp_alloc(dev, data + i, len, status, ep, free_on_destroy);
  1866. if (r) {
  1867. break;
  1868. }
  1869. }
  1870. if (dev->endpoint[EP2I(ep)].pending_async_packet) {
  1871. USBPacket *p = dev->endpoint[EP2I(ep)].pending_async_packet;
  1872. dev->endpoint[EP2I(ep)].pending_async_packet = NULL;
  1873. usbredir_buffered_bulk_in_complete(dev, p, ep);
  1874. usb_packet_complete(&dev->dev, p);
  1875. }
  1876. }
  1877. /*
  1878. * Migration code
  1879. */
  1880. static int usbredir_pre_save(void *priv)
  1881. {
  1882. USBRedirDevice *dev = priv;
  1883. usbredir_fill_already_in_flight(dev);
  1884. return 0;
  1885. }
  1886. static int usbredir_post_load(void *priv, int version_id)
  1887. {
  1888. USBRedirDevice *dev = priv;
  1889. if (dev == NULL || dev->parser == NULL) {
  1890. return 0;
  1891. }
  1892. switch (dev->device_info.speed) {
  1893. case usb_redir_speed_low:
  1894. dev->dev.speed = USB_SPEED_LOW;
  1895. break;
  1896. case usb_redir_speed_full:
  1897. dev->dev.speed = USB_SPEED_FULL;
  1898. break;
  1899. case usb_redir_speed_high:
  1900. dev->dev.speed = USB_SPEED_HIGH;
  1901. break;
  1902. case usb_redir_speed_super:
  1903. dev->dev.speed = USB_SPEED_SUPER;
  1904. break;
  1905. default:
  1906. dev->dev.speed = USB_SPEED_FULL;
  1907. }
  1908. dev->dev.speedmask = (1 << dev->dev.speed);
  1909. usbredir_setup_usb_eps(dev);
  1910. usbredir_check_bulk_receiving(dev);
  1911. return 0;
  1912. }
  1913. /* For usbredirparser migration */
  1914. static int usbredir_put_parser(QEMUFile *f, void *priv, size_t unused,
  1915. const VMStateField *field, QJSON *vmdesc)
  1916. {
  1917. USBRedirDevice *dev = priv;
  1918. uint8_t *data;
  1919. int len;
  1920. if (dev->parser == NULL) {
  1921. qemu_put_be32(f, 0);
  1922. return 0;
  1923. }
  1924. usbredirparser_serialize(dev->parser, &data, &len);
  1925. qemu_oom_check(data);
  1926. qemu_put_be32(f, len);
  1927. qemu_put_buffer(f, data, len);
  1928. free(data);
  1929. return 0;
  1930. }
  1931. static int usbredir_get_parser(QEMUFile *f, void *priv, size_t unused,
  1932. const VMStateField *field)
  1933. {
  1934. USBRedirDevice *dev = priv;
  1935. uint8_t *data;
  1936. int len, ret;
  1937. len = qemu_get_be32(f);
  1938. if (len == 0) {
  1939. return 0;
  1940. }
  1941. /*
  1942. * If our chardev is not open already at this point the usbredir connection
  1943. * has been broken (non seamless migration, or restore from disk).
  1944. *
  1945. * In this case create a temporary parser to receive the migration data,
  1946. * and schedule the close_bh to report the device as disconnected to the
  1947. * guest and to destroy the parser again.
  1948. */
  1949. if (dev->parser == NULL) {
  1950. WARNING("usb-redir connection broken during migration\n");
  1951. usbredir_create_parser(dev);
  1952. qemu_bh_schedule(dev->chardev_close_bh);
  1953. }
  1954. data = g_malloc(len);
  1955. qemu_get_buffer(f, data, len);
  1956. ret = usbredirparser_unserialize(dev->parser, data, len);
  1957. g_free(data);
  1958. return ret;
  1959. }
  1960. static const VMStateInfo usbredir_parser_vmstate_info = {
  1961. .name = "usb-redir-parser",
  1962. .put = usbredir_put_parser,
  1963. .get = usbredir_get_parser,
  1964. };
  1965. /* For buffered packets (iso/irq) queue migration */
  1966. static int usbredir_put_bufpq(QEMUFile *f, void *priv, size_t unused,
  1967. const VMStateField *field, QJSON *vmdesc)
  1968. {
  1969. struct endp_data *endp = priv;
  1970. USBRedirDevice *dev = endp->dev;
  1971. struct buf_packet *bufp;
  1972. int len, i = 0;
  1973. qemu_put_be32(f, endp->bufpq_size);
  1974. QTAILQ_FOREACH(bufp, &endp->bufpq, next) {
  1975. len = bufp->len - bufp->offset;
  1976. DPRINTF("put_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
  1977. len, bufp->status);
  1978. qemu_put_be32(f, len);
  1979. qemu_put_be32(f, bufp->status);
  1980. qemu_put_buffer(f, bufp->data + bufp->offset, len);
  1981. i++;
  1982. }
  1983. assert(i == endp->bufpq_size);
  1984. return 0;
  1985. }
  1986. static int usbredir_get_bufpq(QEMUFile *f, void *priv, size_t unused,
  1987. const VMStateField *field)
  1988. {
  1989. struct endp_data *endp = priv;
  1990. USBRedirDevice *dev = endp->dev;
  1991. struct buf_packet *bufp;
  1992. int i;
  1993. endp->bufpq_size = qemu_get_be32(f);
  1994. for (i = 0; i < endp->bufpq_size; i++) {
  1995. bufp = g_new(struct buf_packet, 1);
  1996. bufp->len = qemu_get_be32(f);
  1997. bufp->status = qemu_get_be32(f);
  1998. bufp->offset = 0;
  1999. bufp->data = qemu_oom_check(malloc(bufp->len)); /* regular malloc! */
  2000. bufp->free_on_destroy = bufp->data;
  2001. qemu_get_buffer(f, bufp->data, bufp->len);
  2002. QTAILQ_INSERT_TAIL(&endp->bufpq, bufp, next);
  2003. DPRINTF("get_bufpq %d/%d len %d status %d\n", i + 1, endp->bufpq_size,
  2004. bufp->len, bufp->status);
  2005. }
  2006. return 0;
  2007. }
  2008. static const VMStateInfo usbredir_ep_bufpq_vmstate_info = {
  2009. .name = "usb-redir-bufpq",
  2010. .put = usbredir_put_bufpq,
  2011. .get = usbredir_get_bufpq,
  2012. };
  2013. /* For endp_data migration */
  2014. static bool usbredir_bulk_receiving_needed(void *priv)
  2015. {
  2016. struct endp_data *endp = priv;
  2017. return endp->bulk_receiving_started;
  2018. }
  2019. static const VMStateDescription usbredir_bulk_receiving_vmstate = {
  2020. .name = "usb-redir-ep/bulk-receiving",
  2021. .version_id = 1,
  2022. .minimum_version_id = 1,
  2023. .needed = usbredir_bulk_receiving_needed,
  2024. .fields = (VMStateField[]) {
  2025. VMSTATE_UINT8(bulk_receiving_started, struct endp_data),
  2026. VMSTATE_END_OF_LIST()
  2027. }
  2028. };
  2029. static bool usbredir_stream_needed(void *priv)
  2030. {
  2031. struct endp_data *endp = priv;
  2032. return endp->max_streams;
  2033. }
  2034. static const VMStateDescription usbredir_stream_vmstate = {
  2035. .name = "usb-redir-ep/stream-state",
  2036. .version_id = 1,
  2037. .minimum_version_id = 1,
  2038. .needed = usbredir_stream_needed,
  2039. .fields = (VMStateField[]) {
  2040. VMSTATE_UINT32(max_streams, struct endp_data),
  2041. VMSTATE_END_OF_LIST()
  2042. }
  2043. };
  2044. static const VMStateDescription usbredir_ep_vmstate = {
  2045. .name = "usb-redir-ep",
  2046. .version_id = 1,
  2047. .minimum_version_id = 1,
  2048. .fields = (VMStateField[]) {
  2049. VMSTATE_UINT8(type, struct endp_data),
  2050. VMSTATE_UINT8(interval, struct endp_data),
  2051. VMSTATE_UINT8(interface, struct endp_data),
  2052. VMSTATE_UINT16(max_packet_size, struct endp_data),
  2053. VMSTATE_UINT8(iso_started, struct endp_data),
  2054. VMSTATE_UINT8(iso_error, struct endp_data),
  2055. VMSTATE_UINT8(interrupt_started, struct endp_data),
  2056. VMSTATE_UINT8(interrupt_error, struct endp_data),
  2057. VMSTATE_UINT8(bufpq_prefilled, struct endp_data),
  2058. VMSTATE_UINT8(bufpq_dropping_packets, struct endp_data),
  2059. {
  2060. .name = "bufpq",
  2061. .version_id = 0,
  2062. .field_exists = NULL,
  2063. .size = 0,
  2064. .info = &usbredir_ep_bufpq_vmstate_info,
  2065. .flags = VMS_SINGLE,
  2066. .offset = 0,
  2067. },
  2068. VMSTATE_INT32(bufpq_target_size, struct endp_data),
  2069. VMSTATE_END_OF_LIST()
  2070. },
  2071. .subsections = (const VMStateDescription*[]) {
  2072. &usbredir_bulk_receiving_vmstate,
  2073. &usbredir_stream_vmstate,
  2074. NULL
  2075. }
  2076. };
  2077. /* For PacketIdQueue migration */
  2078. static int usbredir_put_packet_id_q(QEMUFile *f, void *priv, size_t unused,
  2079. const VMStateField *field, QJSON *vmdesc)
  2080. {
  2081. struct PacketIdQueue *q = priv;
  2082. USBRedirDevice *dev = q->dev;
  2083. struct PacketIdQueueEntry *e;
  2084. int remain = q->size;
  2085. DPRINTF("put_packet_id_q %s size %d\n", q->name, q->size);
  2086. qemu_put_be32(f, q->size);
  2087. QTAILQ_FOREACH(e, &q->head, next) {
  2088. qemu_put_be64(f, e->id);
  2089. remain--;
  2090. }
  2091. assert(remain == 0);
  2092. return 0;
  2093. }
  2094. static int usbredir_get_packet_id_q(QEMUFile *f, void *priv, size_t unused,
  2095. const VMStateField *field)
  2096. {
  2097. struct PacketIdQueue *q = priv;
  2098. USBRedirDevice *dev = q->dev;
  2099. int i, size;
  2100. uint64_t id;
  2101. size = qemu_get_be32(f);
  2102. DPRINTF("get_packet_id_q %s size %d\n", q->name, size);
  2103. for (i = 0; i < size; i++) {
  2104. id = qemu_get_be64(f);
  2105. packet_id_queue_add(q, id);
  2106. }
  2107. assert(q->size == size);
  2108. return 0;
  2109. }
  2110. static const VMStateInfo usbredir_ep_packet_id_q_vmstate_info = {
  2111. .name = "usb-redir-packet-id-q",
  2112. .put = usbredir_put_packet_id_q,
  2113. .get = usbredir_get_packet_id_q,
  2114. };
  2115. static const VMStateDescription usbredir_ep_packet_id_queue_vmstate = {
  2116. .name = "usb-redir-packet-id-queue",
  2117. .version_id = 1,
  2118. .minimum_version_id = 1,
  2119. .fields = (VMStateField[]) {
  2120. {
  2121. .name = "queue",
  2122. .version_id = 0,
  2123. .field_exists = NULL,
  2124. .size = 0,
  2125. .info = &usbredir_ep_packet_id_q_vmstate_info,
  2126. .flags = VMS_SINGLE,
  2127. .offset = 0,
  2128. },
  2129. VMSTATE_END_OF_LIST()
  2130. }
  2131. };
  2132. /* For usb_redir_device_connect_header migration */
  2133. static const VMStateDescription usbredir_device_info_vmstate = {
  2134. .name = "usb-redir-device-info",
  2135. .version_id = 1,
  2136. .minimum_version_id = 1,
  2137. .fields = (VMStateField[]) {
  2138. VMSTATE_UINT8(speed, struct usb_redir_device_connect_header),
  2139. VMSTATE_UINT8(device_class, struct usb_redir_device_connect_header),
  2140. VMSTATE_UINT8(device_subclass, struct usb_redir_device_connect_header),
  2141. VMSTATE_UINT8(device_protocol, struct usb_redir_device_connect_header),
  2142. VMSTATE_UINT16(vendor_id, struct usb_redir_device_connect_header),
  2143. VMSTATE_UINT16(product_id, struct usb_redir_device_connect_header),
  2144. VMSTATE_UINT16(device_version_bcd,
  2145. struct usb_redir_device_connect_header),
  2146. VMSTATE_END_OF_LIST()
  2147. }
  2148. };
  2149. /* For usb_redir_interface_info_header migration */
  2150. static const VMStateDescription usbredir_interface_info_vmstate = {
  2151. .name = "usb-redir-interface-info",
  2152. .version_id = 1,
  2153. .minimum_version_id = 1,
  2154. .fields = (VMStateField[]) {
  2155. VMSTATE_UINT32(interface_count,
  2156. struct usb_redir_interface_info_header),
  2157. VMSTATE_UINT8_ARRAY(interface,
  2158. struct usb_redir_interface_info_header, 32),
  2159. VMSTATE_UINT8_ARRAY(interface_class,
  2160. struct usb_redir_interface_info_header, 32),
  2161. VMSTATE_UINT8_ARRAY(interface_subclass,
  2162. struct usb_redir_interface_info_header, 32),
  2163. VMSTATE_UINT8_ARRAY(interface_protocol,
  2164. struct usb_redir_interface_info_header, 32),
  2165. VMSTATE_END_OF_LIST()
  2166. }
  2167. };
  2168. /* And finally the USBRedirDevice vmstate itself */
  2169. static const VMStateDescription usbredir_vmstate = {
  2170. .name = "usb-redir",
  2171. .version_id = 1,
  2172. .minimum_version_id = 1,
  2173. .pre_save = usbredir_pre_save,
  2174. .post_load = usbredir_post_load,
  2175. .fields = (VMStateField[]) {
  2176. VMSTATE_USB_DEVICE(dev, USBRedirDevice),
  2177. VMSTATE_TIMER_PTR(attach_timer, USBRedirDevice),
  2178. {
  2179. .name = "parser",
  2180. .version_id = 0,
  2181. .field_exists = NULL,
  2182. .size = 0,
  2183. .info = &usbredir_parser_vmstate_info,
  2184. .flags = VMS_SINGLE,
  2185. .offset = 0,
  2186. },
  2187. VMSTATE_STRUCT_ARRAY(endpoint, USBRedirDevice, MAX_ENDPOINTS, 1,
  2188. usbredir_ep_vmstate, struct endp_data),
  2189. VMSTATE_STRUCT(cancelled, USBRedirDevice, 1,
  2190. usbredir_ep_packet_id_queue_vmstate,
  2191. struct PacketIdQueue),
  2192. VMSTATE_STRUCT(already_in_flight, USBRedirDevice, 1,
  2193. usbredir_ep_packet_id_queue_vmstate,
  2194. struct PacketIdQueue),
  2195. VMSTATE_STRUCT(device_info, USBRedirDevice, 1,
  2196. usbredir_device_info_vmstate,
  2197. struct usb_redir_device_connect_header),
  2198. VMSTATE_STRUCT(interface_info, USBRedirDevice, 1,
  2199. usbredir_interface_info_vmstate,
  2200. struct usb_redir_interface_info_header),
  2201. VMSTATE_END_OF_LIST()
  2202. }
  2203. };
  2204. static Property usbredir_properties[] = {
  2205. DEFINE_PROP_CHR("chardev", USBRedirDevice, cs),
  2206. DEFINE_PROP_UINT8("debug", USBRedirDevice, debug, usbredirparser_warning),
  2207. DEFINE_PROP_STRING("filter", USBRedirDevice, filter_str),
  2208. DEFINE_PROP_BOOL("streams", USBRedirDevice, enable_streams, true),
  2209. DEFINE_PROP_END_OF_LIST(),
  2210. };
  2211. static void usbredir_class_initfn(ObjectClass *klass, void *data)
  2212. {
  2213. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  2214. DeviceClass *dc = DEVICE_CLASS(klass);
  2215. uc->realize = usbredir_realize;
  2216. uc->product_desc = "USB Redirection Device";
  2217. uc->unrealize = usbredir_unrealize;
  2218. uc->cancel_packet = usbredir_cancel_packet;
  2219. uc->handle_reset = usbredir_handle_reset;
  2220. uc->handle_data = usbredir_handle_data;
  2221. uc->handle_control = usbredir_handle_control;
  2222. uc->flush_ep_queue = usbredir_flush_ep_queue;
  2223. uc->ep_stopped = usbredir_ep_stopped;
  2224. uc->alloc_streams = usbredir_alloc_streams;
  2225. uc->free_streams = usbredir_free_streams;
  2226. dc->vmsd = &usbredir_vmstate;
  2227. dc->props = usbredir_properties;
  2228. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  2229. }
  2230. static void usbredir_instance_init(Object *obj)
  2231. {
  2232. USBDevice *udev = USB_DEVICE(obj);
  2233. USBRedirDevice *dev = USB_REDIRECT(udev);
  2234. device_add_bootindex_property(obj, &dev->bootindex,
  2235. "bootindex", NULL,
  2236. &udev->qdev, NULL);
  2237. }
  2238. static const TypeInfo usbredir_dev_info = {
  2239. .name = TYPE_USB_REDIR,
  2240. .parent = TYPE_USB_DEVICE,
  2241. .instance_size = sizeof(USBRedirDevice),
  2242. .class_init = usbredir_class_initfn,
  2243. .instance_init = usbredir_instance_init,
  2244. };
  2245. static void usbredir_register_types(void)
  2246. {
  2247. type_register_static(&usbredir_dev_info);
  2248. }
  2249. type_init(usbredir_register_types)