redirect.c 77 KB

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