redirect.c 85 KB

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