redirect.c 85 KB

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