vhci_hcd.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. * Copyright (C) 2015-2016 Nobuo Iwata
  5. */
  6. #include <linux/init.h>
  7. #include <linux/file.h>
  8. #include <linux/kernel.h>
  9. #include <linux/kthread.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/string_choices.h>
  14. #include "usbip_common.h"
  15. #include "vhci.h"
  16. #define DRIVER_AUTHOR "Takahiro Hirofuchi"
  17. #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
  18. /*
  19. * TODO
  20. * - update root hub emulation
  21. * - move the emulation code to userland ?
  22. * porting to other operating systems
  23. * minimize kernel code
  24. * - add suspend/resume code
  25. * - clean up everything
  26. */
  27. /* See usb gadget dummy hcd */
  28. static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
  29. static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  30. u16 wIndex, char *buff, u16 wLength);
  31. static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  32. gfp_t mem_flags);
  33. static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
  34. static int vhci_start(struct usb_hcd *vhci_hcd);
  35. static void vhci_stop(struct usb_hcd *hcd);
  36. static int vhci_get_frame_number(struct usb_hcd *hcd);
  37. static const char driver_name[] = "vhci_hcd";
  38. static const char driver_desc[] = "USB/IP Virtual Host Controller";
  39. int vhci_num_controllers = VHCI_NR_HCS;
  40. struct vhci *vhcis;
  41. static const char * const bit_desc[] = {
  42. "CONNECTION", /*0*/
  43. "ENABLE", /*1*/
  44. "SUSPEND", /*2*/
  45. "OVER_CURRENT", /*3*/
  46. "RESET", /*4*/
  47. "L1", /*5*/
  48. "R6", /*6*/
  49. "R7", /*7*/
  50. "POWER", /*8*/
  51. "LOWSPEED", /*9*/
  52. "HIGHSPEED", /*10*/
  53. "PORT_TEST", /*11*/
  54. "INDICATOR", /*12*/
  55. "R13", /*13*/
  56. "R14", /*14*/
  57. "R15", /*15*/
  58. "C_CONNECTION", /*16*/
  59. "C_ENABLE", /*17*/
  60. "C_SUSPEND", /*18*/
  61. "C_OVER_CURRENT", /*19*/
  62. "C_RESET", /*20*/
  63. "C_L1", /*21*/
  64. "R22", /*22*/
  65. "R23", /*23*/
  66. "R24", /*24*/
  67. "R25", /*25*/
  68. "R26", /*26*/
  69. "R27", /*27*/
  70. "R28", /*28*/
  71. "R29", /*29*/
  72. "R30", /*30*/
  73. "R31", /*31*/
  74. };
  75. static const char * const bit_desc_ss[] = {
  76. "CONNECTION", /*0*/
  77. "ENABLE", /*1*/
  78. "SUSPEND", /*2*/
  79. "OVER_CURRENT", /*3*/
  80. "RESET", /*4*/
  81. "L1", /*5*/
  82. "R6", /*6*/
  83. "R7", /*7*/
  84. "R8", /*8*/
  85. "POWER", /*9*/
  86. "HIGHSPEED", /*10*/
  87. "PORT_TEST", /*11*/
  88. "INDICATOR", /*12*/
  89. "R13", /*13*/
  90. "R14", /*14*/
  91. "R15", /*15*/
  92. "C_CONNECTION", /*16*/
  93. "C_ENABLE", /*17*/
  94. "C_SUSPEND", /*18*/
  95. "C_OVER_CURRENT", /*19*/
  96. "C_RESET", /*20*/
  97. "C_BH_RESET", /*21*/
  98. "C_LINK_STATE", /*22*/
  99. "C_CONFIG_ERROR", /*23*/
  100. "R24", /*24*/
  101. "R25", /*25*/
  102. "R26", /*26*/
  103. "R27", /*27*/
  104. "R28", /*28*/
  105. "R29", /*29*/
  106. "R30", /*30*/
  107. "R31", /*31*/
  108. };
  109. static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3)
  110. {
  111. int i = 0;
  112. u32 bit = 1;
  113. const char * const *desc = bit_desc;
  114. if (usb3)
  115. desc = bit_desc_ss;
  116. pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
  117. while (bit) {
  118. u32 prev = prev_status & bit;
  119. u32 new = new_status & bit;
  120. char change;
  121. if (!prev && new)
  122. change = '+';
  123. else if (prev && !new)
  124. change = '-';
  125. else
  126. change = ' ';
  127. if (prev || new) {
  128. pr_debug(" %c%s\n", change, desc[i]);
  129. if (bit == 1) /* USB_PORT_STAT_CONNECTION */
  130. pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS");
  131. }
  132. bit <<= 1;
  133. i++;
  134. }
  135. pr_debug("\n");
  136. }
  137. void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
  138. {
  139. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  140. struct vhci *vhci = vhci_hcd->vhci;
  141. int rhport = vdev->rhport;
  142. u32 status;
  143. unsigned long flags;
  144. usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
  145. spin_lock_irqsave(&vhci->lock, flags);
  146. status = vhci_hcd->port_status[rhport];
  147. status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
  148. switch (speed) {
  149. case USB_SPEED_HIGH:
  150. status |= USB_PORT_STAT_HIGH_SPEED;
  151. break;
  152. case USB_SPEED_LOW:
  153. status |= USB_PORT_STAT_LOW_SPEED;
  154. break;
  155. default:
  156. break;
  157. }
  158. vhci_hcd->port_status[rhport] = status;
  159. spin_unlock_irqrestore(&vhci->lock, flags);
  160. usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
  161. }
  162. static void rh_port_disconnect(struct vhci_device *vdev)
  163. {
  164. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  165. struct vhci *vhci = vhci_hcd->vhci;
  166. int rhport = vdev->rhport;
  167. u32 status;
  168. unsigned long flags;
  169. usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
  170. spin_lock_irqsave(&vhci->lock, flags);
  171. status = vhci_hcd->port_status[rhport];
  172. status &= ~USB_PORT_STAT_CONNECTION;
  173. status |= (1 << USB_PORT_FEAT_C_CONNECTION);
  174. vhci_hcd->port_status[rhport] = status;
  175. spin_unlock_irqrestore(&vhci->lock, flags);
  176. usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
  177. }
  178. #define PORT_C_MASK \
  179. ((USB_PORT_STAT_C_CONNECTION \
  180. | USB_PORT_STAT_C_ENABLE \
  181. | USB_PORT_STAT_C_SUSPEND \
  182. | USB_PORT_STAT_C_OVERCURRENT \
  183. | USB_PORT_STAT_C_RESET) << 16)
  184. /*
  185. * Returns 0 if the status hasn't changed, or the number of bytes in buf.
  186. * Ports are 0-indexed from the HCD point of view,
  187. * and 1-indexed from the USB core pointer of view.
  188. *
  189. * @buf: a bitmap to show which port status has been changed.
  190. * bit 0: reserved
  191. * bit 1: the status of port 0 has been changed.
  192. * bit 2: the status of port 1 has been changed.
  193. * ...
  194. */
  195. static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
  196. {
  197. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  198. struct vhci *vhci = vhci_hcd->vhci;
  199. int retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
  200. int rhport;
  201. int changed = 0;
  202. unsigned long flags;
  203. memset(buf, 0, retval);
  204. spin_lock_irqsave(&vhci->lock, flags);
  205. if (!HCD_HW_ACCESSIBLE(hcd)) {
  206. usbip_dbg_vhci_rh("hw accessible flag not on?\n");
  207. goto done;
  208. }
  209. /* check pseudo status register for each port */
  210. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  211. if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) {
  212. /* The status of a port has been changed, */
  213. usbip_dbg_vhci_rh("port %d status changed\n", rhport);
  214. buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
  215. changed = 1;
  216. }
  217. }
  218. if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
  219. usb_hcd_resume_root_hub(hcd);
  220. done:
  221. spin_unlock_irqrestore(&vhci->lock, flags);
  222. return changed ? retval : 0;
  223. }
  224. /* usb 3.0 root hub device descriptor */
  225. static struct {
  226. struct usb_bos_descriptor bos;
  227. struct usb_ss_cap_descriptor ss_cap;
  228. } __packed usb3_bos_desc = {
  229. .bos = {
  230. .bLength = USB_DT_BOS_SIZE,
  231. .bDescriptorType = USB_DT_BOS,
  232. .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
  233. .bNumDeviceCaps = 1,
  234. },
  235. .ss_cap = {
  236. .bLength = USB_DT_USB_SS_CAP_SIZE,
  237. .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
  238. .bDevCapabilityType = USB_SS_CAP_TYPE,
  239. .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
  240. .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
  241. },
  242. };
  243. static inline void
  244. ss_hub_descriptor(struct usb_hub_descriptor *desc)
  245. {
  246. memset(desc, 0, sizeof *desc);
  247. desc->bDescriptorType = USB_DT_SS_HUB;
  248. desc->bDescLength = 12;
  249. desc->wHubCharacteristics = cpu_to_le16(
  250. HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
  251. desc->bNbrPorts = VHCI_HC_PORTS;
  252. desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
  253. desc->u.ss.DeviceRemovable = 0xffff;
  254. }
  255. static inline void hub_descriptor(struct usb_hub_descriptor *desc)
  256. {
  257. int width;
  258. memset(desc, 0, sizeof(*desc));
  259. desc->bDescriptorType = USB_DT_HUB;
  260. desc->wHubCharacteristics = cpu_to_le16(
  261. HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
  262. desc->bNbrPorts = VHCI_HC_PORTS;
  263. BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
  264. width = desc->bNbrPorts / 8 + 1;
  265. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
  266. memset(&desc->u.hs.DeviceRemovable[0], 0, width);
  267. memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
  268. }
  269. static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  270. u16 wIndex, char *buf, u16 wLength)
  271. {
  272. struct vhci_hcd *vhci_hcd;
  273. struct vhci *vhci;
  274. int retval = 0;
  275. int rhport = -1;
  276. unsigned long flags;
  277. bool invalid_rhport = false;
  278. u32 prev_port_status[VHCI_HC_PORTS];
  279. if (!HCD_HW_ACCESSIBLE(hcd))
  280. return -ETIMEDOUT;
  281. /*
  282. * NOTE:
  283. * wIndex (bits 0-7) shows the port number and begins from 1?
  284. */
  285. wIndex = ((__u8)(wIndex & 0x00ff));
  286. usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
  287. wIndex);
  288. /*
  289. * wIndex can be 0 for some request types (typeReq). rhport is
  290. * in valid range when wIndex >= 1 and < VHCI_HC_PORTS.
  291. *
  292. * Reference port_status[] only with valid rhport when
  293. * invalid_rhport is false.
  294. */
  295. if (wIndex < 1 || wIndex > VHCI_HC_PORTS) {
  296. invalid_rhport = true;
  297. if (wIndex > VHCI_HC_PORTS)
  298. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  299. } else {
  300. rhport = wIndex - 1;
  301. }
  302. vhci_hcd = hcd_to_vhci_hcd(hcd);
  303. vhci = vhci_hcd->vhci;
  304. spin_lock_irqsave(&vhci->lock, flags);
  305. /* store old status and compare now and old later */
  306. if (usbip_dbg_flag_vhci_rh) {
  307. if (!invalid_rhport)
  308. memcpy(prev_port_status, vhci_hcd->port_status,
  309. sizeof(prev_port_status));
  310. }
  311. switch (typeReq) {
  312. case ClearHubFeature:
  313. usbip_dbg_vhci_rh(" ClearHubFeature\n");
  314. break;
  315. case ClearPortFeature:
  316. if (invalid_rhport) {
  317. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  318. goto error;
  319. }
  320. switch (wValue) {
  321. case USB_PORT_FEAT_SUSPEND:
  322. if (hcd->speed >= HCD_USB3) {
  323. dev_err(hcd_dev(hcd),
  324. "ClearPortFeature: USB_PORT_FEAT_SUSPEND req not supported for USB 3.0 roothub\n");
  325. goto error;
  326. }
  327. usbip_dbg_vhci_rh(
  328. " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
  329. if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
  330. /* 20msec signaling */
  331. vhci_hcd->resuming = 1;
  332. vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
  333. }
  334. break;
  335. case USB_PORT_FEAT_POWER:
  336. usbip_dbg_vhci_rh(
  337. " ClearPortFeature: USB_PORT_FEAT_POWER\n");
  338. if (hcd->speed >= HCD_USB3)
  339. vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
  340. else
  341. vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
  342. break;
  343. default:
  344. usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
  345. wValue);
  346. if (wValue >= 32)
  347. goto error;
  348. vhci_hcd->port_status[rhport] &= ~(1 << wValue);
  349. break;
  350. }
  351. break;
  352. case GetHubDescriptor:
  353. usbip_dbg_vhci_rh(" GetHubDescriptor\n");
  354. if (hcd->speed >= HCD_USB3 &&
  355. (wLength < USB_DT_SS_HUB_SIZE ||
  356. wValue != (USB_DT_SS_HUB << 8))) {
  357. dev_err(hcd_dev(hcd),
  358. "Wrong hub descriptor type for USB 3.0 roothub.\n");
  359. goto error;
  360. }
  361. if (hcd->speed >= HCD_USB3)
  362. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  363. else
  364. hub_descriptor((struct usb_hub_descriptor *) buf);
  365. break;
  366. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  367. if (hcd->speed < HCD_USB3)
  368. goto error;
  369. if ((wValue >> 8) != USB_DT_BOS)
  370. goto error;
  371. memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
  372. retval = sizeof(usb3_bos_desc);
  373. break;
  374. case GetHubStatus:
  375. usbip_dbg_vhci_rh(" GetHubStatus\n");
  376. *(__le32 *) buf = cpu_to_le32(0);
  377. break;
  378. case GetPortStatus:
  379. usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
  380. if (invalid_rhport) {
  381. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  382. retval = -EPIPE;
  383. goto error;
  384. }
  385. /* we do not care about resume. */
  386. /* whoever resets or resumes must GetPortStatus to
  387. * complete it!!
  388. */
  389. if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
  390. vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
  391. vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
  392. vhci_hcd->resuming = 0;
  393. vhci_hcd->re_timeout = 0;
  394. }
  395. if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
  396. 0 && time_after(jiffies, vhci_hcd->re_timeout)) {
  397. vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
  398. vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
  399. vhci_hcd->re_timeout = 0;
  400. /*
  401. * A few drivers do usb reset during probe when
  402. * the device could be in VDEV_ST_USED state
  403. */
  404. if (vhci_hcd->vdev[rhport].ud.status ==
  405. VDEV_ST_NOTASSIGNED ||
  406. vhci_hcd->vdev[rhport].ud.status ==
  407. VDEV_ST_USED) {
  408. usbip_dbg_vhci_rh(
  409. " enable rhport %d (status %u)\n",
  410. rhport,
  411. vhci_hcd->vdev[rhport].ud.status);
  412. vhci_hcd->port_status[rhport] |=
  413. USB_PORT_STAT_ENABLE;
  414. }
  415. if (hcd->speed < HCD_USB3) {
  416. switch (vhci_hcd->vdev[rhport].speed) {
  417. case USB_SPEED_HIGH:
  418. vhci_hcd->port_status[rhport] |=
  419. USB_PORT_STAT_HIGH_SPEED;
  420. break;
  421. case USB_SPEED_LOW:
  422. vhci_hcd->port_status[rhport] |=
  423. USB_PORT_STAT_LOW_SPEED;
  424. break;
  425. default:
  426. dev_err(hcd_dev(hcd), "vhci_device speed not set\n");
  427. break;
  428. }
  429. }
  430. }
  431. ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
  432. ((__le16 *) buf)[1] =
  433. cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
  434. usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
  435. ((u16 *)buf)[1]);
  436. break;
  437. case SetHubFeature:
  438. usbip_dbg_vhci_rh(" SetHubFeature\n");
  439. retval = -EPIPE;
  440. break;
  441. case SetPortFeature:
  442. switch (wValue) {
  443. case USB_PORT_FEAT_LINK_STATE:
  444. usbip_dbg_vhci_rh(
  445. " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
  446. if (hcd->speed < HCD_USB3) {
  447. dev_err(hcd_dev(hcd),
  448. "USB_PORT_FEAT_LINK_STATE req not supported for USB 2.0 roothub\n");
  449. goto error;
  450. }
  451. /*
  452. * Since this is dummy we don't have an actual link so
  453. * there is nothing to do for the SET_LINK_STATE cmd
  454. */
  455. break;
  456. case USB_PORT_FEAT_U1_TIMEOUT:
  457. usbip_dbg_vhci_rh(
  458. " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
  459. fallthrough;
  460. case USB_PORT_FEAT_U2_TIMEOUT:
  461. usbip_dbg_vhci_rh(
  462. " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
  463. /* TODO: add suspend/resume support! */
  464. if (hcd->speed < HCD_USB3) {
  465. dev_err(hcd_dev(hcd),
  466. "USB_PORT_FEAT_U1/2_TIMEOUT req not supported for USB 2.0 roothub\n");
  467. goto error;
  468. }
  469. break;
  470. case USB_PORT_FEAT_SUSPEND:
  471. usbip_dbg_vhci_rh(
  472. " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
  473. /* Applicable only for USB2.0 hub */
  474. if (hcd->speed >= HCD_USB3) {
  475. dev_err(hcd_dev(hcd),
  476. "USB_PORT_FEAT_SUSPEND req not supported for USB 3.0 roothub\n");
  477. goto error;
  478. }
  479. if (invalid_rhport) {
  480. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  481. goto error;
  482. }
  483. vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
  484. break;
  485. case USB_PORT_FEAT_POWER:
  486. usbip_dbg_vhci_rh(
  487. " SetPortFeature: USB_PORT_FEAT_POWER\n");
  488. if (invalid_rhport) {
  489. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  490. goto error;
  491. }
  492. if (hcd->speed >= HCD_USB3)
  493. vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
  494. else
  495. vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
  496. break;
  497. case USB_PORT_FEAT_BH_PORT_RESET:
  498. usbip_dbg_vhci_rh(
  499. " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
  500. if (invalid_rhport) {
  501. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  502. goto error;
  503. }
  504. /* Applicable only for USB3.0 hub */
  505. if (hcd->speed < HCD_USB3) {
  506. dev_err(hcd_dev(hcd),
  507. "USB_PORT_FEAT_BH_PORT_RESET req not supported for USB 2.0 roothub\n");
  508. goto error;
  509. }
  510. fallthrough;
  511. case USB_PORT_FEAT_RESET:
  512. usbip_dbg_vhci_rh(
  513. " SetPortFeature: USB_PORT_FEAT_RESET\n");
  514. if (invalid_rhport) {
  515. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  516. goto error;
  517. }
  518. /* if it's already enabled, disable */
  519. if (hcd->speed >= HCD_USB3) {
  520. vhci_hcd->port_status[rhport] = 0;
  521. vhci_hcd->port_status[rhport] =
  522. (USB_SS_PORT_STAT_POWER |
  523. USB_PORT_STAT_CONNECTION |
  524. USB_PORT_STAT_RESET);
  525. } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
  526. vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
  527. | USB_PORT_STAT_LOW_SPEED
  528. | USB_PORT_STAT_HIGH_SPEED);
  529. }
  530. /* 50msec reset signaling */
  531. vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  532. fallthrough;
  533. default:
  534. usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
  535. wValue);
  536. if (invalid_rhport) {
  537. dev_err(hcd_dev(hcd), "invalid port number %d\n", wIndex);
  538. goto error;
  539. }
  540. if (wValue >= 32)
  541. goto error;
  542. if (hcd->speed >= HCD_USB3) {
  543. if ((vhci_hcd->port_status[rhport] &
  544. USB_SS_PORT_STAT_POWER) != 0) {
  545. vhci_hcd->port_status[rhport] |= (1 << wValue);
  546. }
  547. } else
  548. if ((vhci_hcd->port_status[rhport] &
  549. USB_PORT_STAT_POWER) != 0) {
  550. vhci_hcd->port_status[rhport] |= (1 << wValue);
  551. }
  552. }
  553. break;
  554. case GetPortErrorCount:
  555. usbip_dbg_vhci_rh(" GetPortErrorCount\n");
  556. if (hcd->speed < HCD_USB3) {
  557. dev_err(hcd_dev(hcd),
  558. "GetPortErrorCount req not supported for USB 2.0 roothub\n");
  559. goto error;
  560. }
  561. /* We'll always return 0 since this is a dummy hub */
  562. *(__le32 *) buf = cpu_to_le32(0);
  563. break;
  564. case SetHubDepth:
  565. usbip_dbg_vhci_rh(" SetHubDepth\n");
  566. if (hcd->speed < HCD_USB3) {
  567. dev_err(hcd_dev(hcd),
  568. "SetHubDepth req not supported for USB 2.0 roothub\n");
  569. goto error;
  570. }
  571. break;
  572. default:
  573. dev_err(hcd_dev(hcd),
  574. "default hub control req: %04x v%04x i%04x l%d\n",
  575. typeReq, wValue, wIndex, wLength);
  576. error:
  577. /* "protocol stall" on error */
  578. retval = -EPIPE;
  579. }
  580. if (usbip_dbg_flag_vhci_rh) {
  581. dev_dbg(hcd_dev(hcd), "%s port %d\n", __func__, rhport);
  582. /* Only dump valid port status */
  583. if (!invalid_rhport) {
  584. dump_port_status_diff(prev_port_status[rhport],
  585. vhci_hcd->port_status[rhport],
  586. hcd->speed >= HCD_USB3);
  587. }
  588. }
  589. usbip_dbg_vhci_rh(" bye\n");
  590. spin_unlock_irqrestore(&vhci->lock, flags);
  591. if (!invalid_rhport &&
  592. (vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) {
  593. usb_hcd_poll_rh_status(hcd);
  594. }
  595. return retval;
  596. }
  597. static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
  598. {
  599. struct vhci_priv *priv;
  600. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  601. unsigned long flags;
  602. priv = kzalloc_obj(struct vhci_priv, GFP_ATOMIC);
  603. if (!priv) {
  604. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  605. return;
  606. }
  607. spin_lock_irqsave(&vdev->priv_lock, flags);
  608. priv->seqnum = (u32)atomic_inc_return(&vhci_hcd->seqnum);
  609. if (priv->seqnum == 0xffff)
  610. dev_info(&urb->dev->dev, "seqnum max\n");
  611. priv->vdev = vdev;
  612. priv->urb = urb;
  613. urb->hcpriv = (void *) priv;
  614. list_add_tail(&priv->list, &vdev->priv_tx);
  615. wake_up(&vdev->waitq_tx);
  616. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  617. }
  618. static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  619. {
  620. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  621. struct vhci *vhci = vhci_hcd->vhci;
  622. struct device *dev = &urb->dev->dev;
  623. u8 portnum = urb->dev->portnum;
  624. int ret = 0;
  625. struct vhci_device *vdev;
  626. unsigned long flags;
  627. if (portnum > VHCI_HC_PORTS) {
  628. dev_err(hcd_dev(hcd), "invalid port number %d\n", portnum);
  629. return -ENODEV;
  630. }
  631. vdev = &vhci_hcd->vdev[portnum-1];
  632. if (!urb->transfer_buffer && !urb->num_sgs &&
  633. urb->transfer_buffer_length) {
  634. dev_dbg(dev, "Null URB transfer buffer\n");
  635. return -EINVAL;
  636. }
  637. spin_lock_irqsave(&vhci->lock, flags);
  638. if (urb->status != -EINPROGRESS) {
  639. dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
  640. spin_unlock_irqrestore(&vhci->lock, flags);
  641. return urb->status;
  642. }
  643. /* refuse enqueue for dead connection */
  644. spin_lock(&vdev->ud.lock);
  645. if (vdev->ud.status == VDEV_ST_NULL ||
  646. vdev->ud.status == VDEV_ST_ERROR) {
  647. dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
  648. spin_unlock(&vdev->ud.lock);
  649. spin_unlock_irqrestore(&vhci->lock, flags);
  650. return -ENODEV;
  651. }
  652. spin_unlock(&vdev->ud.lock);
  653. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  654. if (ret)
  655. goto no_need_unlink;
  656. /*
  657. * The enumeration process is as follows;
  658. *
  659. * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
  660. * to get max packet length of default pipe
  661. *
  662. * 2. Set_Address request to DevAddr(0) EndPoint(0)
  663. *
  664. */
  665. if (usb_pipedevice(urb->pipe) == 0) {
  666. struct usb_device *old;
  667. __u8 type = usb_pipetype(urb->pipe);
  668. struct usb_ctrlrequest *ctrlreq =
  669. (struct usb_ctrlrequest *) urb->setup_packet;
  670. if (type != PIPE_CONTROL || !ctrlreq) {
  671. dev_err(dev, "invalid request to devnum 0\n");
  672. ret = -EINVAL;
  673. goto no_need_xmit;
  674. }
  675. old = vdev->udev;
  676. switch (ctrlreq->bRequest) {
  677. case USB_REQ_SET_ADDRESS:
  678. /* set_address may come when a device is reset */
  679. dev_info(dev, "SetAddress Request (%d) to port %d\n",
  680. ctrlreq->wValue, vdev->rhport);
  681. vdev->udev = usb_get_dev(urb->dev);
  682. /*
  683. * NOTE: A similar operation has been done via
  684. * USB_REQ_GET_DESCRIPTOR handler below, which is
  685. * supposed to always precede USB_REQ_SET_ADDRESS.
  686. *
  687. * It's not entirely clear if operating on a different
  688. * usb_device instance here is a real possibility,
  689. * otherwise this call and vdev->udev assignment above
  690. * should be dropped.
  691. */
  692. dev_pm_syscore_device(&vdev->udev->dev, true);
  693. usb_put_dev(old);
  694. spin_lock(&vdev->ud.lock);
  695. vdev->ud.status = VDEV_ST_USED;
  696. spin_unlock(&vdev->ud.lock);
  697. if (urb->status == -EINPROGRESS) {
  698. /* This request is successfully completed. */
  699. /* If not -EINPROGRESS, possibly unlinked. */
  700. urb->status = 0;
  701. }
  702. goto no_need_xmit;
  703. case USB_REQ_GET_DESCRIPTOR:
  704. if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
  705. usbip_dbg_vhci_hc(
  706. "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
  707. vdev->udev = usb_get_dev(urb->dev);
  708. /*
  709. * Set syscore PM flag for the virtually attached
  710. * devices to ensure they will not enter suspend on
  711. * the client side.
  712. *
  713. * Note this doesn't have any impact on the physical
  714. * devices attached to the host system on the server
  715. * side, hence there is no need to undo the operation
  716. * on disconnect.
  717. */
  718. dev_pm_syscore_device(&vdev->udev->dev, true);
  719. usb_put_dev(old);
  720. goto out;
  721. default:
  722. /* NOT REACHED */
  723. dev_err(dev,
  724. "invalid request to devnum 0 bRequest %u, wValue %u\n",
  725. ctrlreq->bRequest,
  726. ctrlreq->wValue);
  727. ret = -EINVAL;
  728. goto no_need_xmit;
  729. }
  730. }
  731. out:
  732. vhci_tx_urb(urb, vdev);
  733. spin_unlock_irqrestore(&vhci->lock, flags);
  734. return 0;
  735. no_need_xmit:
  736. usb_hcd_unlink_urb_from_ep(hcd, urb);
  737. no_need_unlink:
  738. if (!ret) {
  739. /* usb_hcd_giveback_urb() should be called with
  740. * irqs disabled
  741. */
  742. spin_unlock(&vhci->lock);
  743. usb_hcd_giveback_urb(hcd, urb, urb->status);
  744. spin_lock(&vhci->lock);
  745. }
  746. spin_unlock_irqrestore(&vhci->lock, flags);
  747. return ret;
  748. }
  749. /*
  750. * vhci_rx gives back the urb after receiving the reply of the urb. If an
  751. * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
  752. * back its urb. For the driver unlinking the urb, the content of the urb is
  753. * not important, but the calling to its completion handler is important; the
  754. * completion of unlinking is notified by the completion handler.
  755. *
  756. *
  757. * CLIENT SIDE
  758. *
  759. * - When vhci_hcd receives RET_SUBMIT,
  760. *
  761. * - case 1a). the urb of the pdu is not unlinking.
  762. * - normal case
  763. * => just give back the urb
  764. *
  765. * - case 1b). the urb of the pdu is unlinking.
  766. * - usbip.ko will return a reply of the unlinking request.
  767. * => give back the urb now and go to case 2b).
  768. *
  769. * - When vhci_hcd receives RET_UNLINK,
  770. *
  771. * - case 2a). a submit request is still pending in vhci_hcd.
  772. * - urb was really pending in usbip.ko and urb_unlink_urb() was
  773. * completed there.
  774. * => free a pending submit request
  775. * => notify unlink completeness by giving back the urb
  776. *
  777. * - case 2b). a submit request is *not* pending in vhci_hcd.
  778. * - urb was already given back to the core driver.
  779. * => do not give back the urb
  780. *
  781. *
  782. * SERVER SIDE
  783. *
  784. * - When usbip receives CMD_UNLINK,
  785. *
  786. * - case 3a). the urb of the unlink request is now in submission.
  787. * => do usb_unlink_urb().
  788. * => after the unlink is completed, send RET_UNLINK.
  789. *
  790. * - case 3b). the urb of the unlink request is not in submission.
  791. * - may be already completed or never be received
  792. * => send RET_UNLINK
  793. *
  794. */
  795. static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  796. {
  797. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  798. struct vhci *vhci = vhci_hcd->vhci;
  799. struct vhci_priv *priv;
  800. struct vhci_device *vdev;
  801. unsigned long flags;
  802. spin_lock_irqsave(&vhci->lock, flags);
  803. priv = urb->hcpriv;
  804. if (!priv) {
  805. /* URB was never linked! or will be soon given back by
  806. * vhci_rx. */
  807. spin_unlock_irqrestore(&vhci->lock, flags);
  808. return -EIDRM;
  809. }
  810. {
  811. int ret = 0;
  812. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  813. if (ret) {
  814. spin_unlock_irqrestore(&vhci->lock, flags);
  815. return ret;
  816. }
  817. }
  818. /* send unlink request here? */
  819. vdev = priv->vdev;
  820. if (!vdev->ud.tcp_socket) {
  821. /* tcp connection is closed */
  822. spin_lock(&vdev->priv_lock);
  823. list_del(&priv->list);
  824. kfree(priv);
  825. urb->hcpriv = NULL;
  826. spin_unlock(&vdev->priv_lock);
  827. /*
  828. * If tcp connection is alive, we have sent CMD_UNLINK.
  829. * vhci_rx will receive RET_UNLINK and give back the URB.
  830. * Otherwise, we give back it here.
  831. */
  832. usb_hcd_unlink_urb_from_ep(hcd, urb);
  833. spin_unlock_irqrestore(&vhci->lock, flags);
  834. usb_hcd_giveback_urb(hcd, urb, urb->status);
  835. spin_lock_irqsave(&vhci->lock, flags);
  836. } else {
  837. /* tcp connection is alive */
  838. struct vhci_unlink *unlink;
  839. spin_lock(&vdev->priv_lock);
  840. /* setup CMD_UNLINK pdu */
  841. unlink = kzalloc_obj(struct vhci_unlink, GFP_ATOMIC);
  842. if (!unlink) {
  843. spin_unlock(&vdev->priv_lock);
  844. spin_unlock_irqrestore(&vhci->lock, flags);
  845. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  846. return -ENOMEM;
  847. }
  848. unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
  849. if (unlink->seqnum == 0xffff)
  850. dev_info(hcd_dev(hcd), "seqnum max\n");
  851. unlink->unlink_seqnum = priv->seqnum;
  852. /* send cmd_unlink and try to cancel the pending URB in the
  853. * peer */
  854. list_add_tail(&unlink->list, &vdev->unlink_tx);
  855. wake_up(&vdev->waitq_tx);
  856. spin_unlock(&vdev->priv_lock);
  857. }
  858. spin_unlock_irqrestore(&vhci->lock, flags);
  859. usbip_dbg_vhci_hc("leave\n");
  860. return 0;
  861. }
  862. static void vhci_cleanup_unlink_list(struct vhci_device *vdev,
  863. struct list_head *unlink_list)
  864. {
  865. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  866. struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
  867. struct vhci *vhci = vhci_hcd->vhci;
  868. struct vhci_unlink *unlink, *tmp;
  869. unsigned long flags;
  870. spin_lock_irqsave(&vhci->lock, flags);
  871. spin_lock(&vdev->priv_lock);
  872. list_for_each_entry_safe(unlink, tmp, unlink_list, list) {
  873. struct urb *urb;
  874. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  875. if (!urb) {
  876. list_del(&unlink->list);
  877. kfree(unlink);
  878. continue;
  879. }
  880. urb->status = -ENODEV;
  881. usb_hcd_unlink_urb_from_ep(hcd, urb);
  882. list_del(&unlink->list);
  883. spin_unlock(&vdev->priv_lock);
  884. spin_unlock_irqrestore(&vhci->lock, flags);
  885. usb_hcd_giveback_urb(hcd, urb, urb->status);
  886. spin_lock_irqsave(&vhci->lock, flags);
  887. spin_lock(&vdev->priv_lock);
  888. kfree(unlink);
  889. }
  890. spin_unlock(&vdev->priv_lock);
  891. spin_unlock_irqrestore(&vhci->lock, flags);
  892. }
  893. static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
  894. {
  895. /* give back URB of unsent unlink request */
  896. vhci_cleanup_unlink_list(vdev, &vdev->unlink_tx);
  897. /* give back URB of unanswered unlink request */
  898. vhci_cleanup_unlink_list(vdev, &vdev->unlink_rx);
  899. }
  900. /*
  901. * The important thing is that only one context begins cleanup.
  902. * This is why error handling and cleanup become simple.
  903. * We do not want to consider race condition as possible.
  904. */
  905. static void vhci_shutdown_connection(struct usbip_device *ud)
  906. {
  907. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  908. struct usb_hcd *hcd = vhci_hcd_to_hcd(vdev_to_vhci_hcd(vdev));
  909. /* need this? see stub_dev.c */
  910. if (ud->tcp_socket) {
  911. dev_dbg(hcd_dev(hcd), "shutdown tcp_socket %d\n", ud->sockfd);
  912. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  913. }
  914. /* kill threads related to this sdev */
  915. if (vdev->ud.tcp_rx) {
  916. kthread_stop_put(vdev->ud.tcp_rx);
  917. vdev->ud.tcp_rx = NULL;
  918. }
  919. if (vdev->ud.tcp_tx) {
  920. kthread_stop_put(vdev->ud.tcp_tx);
  921. vdev->ud.tcp_tx = NULL;
  922. }
  923. dev_info(hcd_dev(hcd), "stop threads\n");
  924. /* active connection is closed */
  925. if (vdev->ud.tcp_socket) {
  926. sockfd_put(vdev->ud.tcp_socket);
  927. vdev->ud.tcp_socket = NULL;
  928. vdev->ud.sockfd = -1;
  929. }
  930. dev_info(hcd_dev(hcd), "release socket\n");
  931. vhci_device_unlink_cleanup(vdev);
  932. /*
  933. * rh_port_disconnect() is a trigger of ...
  934. * usb_disable_device():
  935. * disable all the endpoints for a USB device.
  936. * usb_disable_endpoint():
  937. * disable endpoints. pending urbs are unlinked(dequeued).
  938. *
  939. * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
  940. * detached device should release used urbs in a cleanup function (i.e.
  941. * xxx_disconnect()). Therefore, vhci_hcd does not need to release
  942. * pushed urbs and their private data in this function.
  943. *
  944. * NOTE: vhci_dequeue() must be considered carefully. When shutting down
  945. * a connection, vhci_shutdown_connection() expects vhci_dequeue()
  946. * gives back pushed urbs and frees their private data by request of
  947. * the cleanup function of a USB driver. When unlinking a urb with an
  948. * active connection, vhci_dequeue() does not give back the urb which
  949. * is actually given back by vhci_rx after receiving its return pdu.
  950. *
  951. */
  952. rh_port_disconnect(vdev);
  953. dev_info(hcd_dev(hcd), "disconnect device\n");
  954. }
  955. static void vhci_device_reset(struct usbip_device *ud)
  956. {
  957. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  958. struct usb_device *old = vdev->udev;
  959. unsigned long flags;
  960. spin_lock_irqsave(&ud->lock, flags);
  961. vdev->speed = 0;
  962. vdev->devid = 0;
  963. vdev->udev = NULL;
  964. usb_put_dev(old);
  965. if (ud->tcp_socket) {
  966. sockfd_put(ud->tcp_socket);
  967. ud->tcp_socket = NULL;
  968. ud->sockfd = -1;
  969. }
  970. ud->status = VDEV_ST_NULL;
  971. spin_unlock_irqrestore(&ud->lock, flags);
  972. }
  973. static void vhci_device_unusable(struct usbip_device *ud)
  974. {
  975. unsigned long flags;
  976. spin_lock_irqsave(&ud->lock, flags);
  977. ud->status = VDEV_ST_ERROR;
  978. spin_unlock_irqrestore(&ud->lock, flags);
  979. }
  980. static void vhci_device_init(struct vhci_device *vdev)
  981. {
  982. memset(vdev, 0, sizeof(struct vhci_device));
  983. vdev->ud.side = USBIP_VHCI;
  984. vdev->ud.status = VDEV_ST_NULL;
  985. spin_lock_init(&vdev->ud.lock);
  986. mutex_init(&vdev->ud.sysfs_lock);
  987. INIT_LIST_HEAD(&vdev->priv_rx);
  988. INIT_LIST_HEAD(&vdev->priv_tx);
  989. INIT_LIST_HEAD(&vdev->unlink_tx);
  990. INIT_LIST_HEAD(&vdev->unlink_rx);
  991. spin_lock_init(&vdev->priv_lock);
  992. init_waitqueue_head(&vdev->waitq_tx);
  993. vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
  994. vdev->ud.eh_ops.reset = vhci_device_reset;
  995. vdev->ud.eh_ops.unusable = vhci_device_unusable;
  996. usbip_start_eh(&vdev->ud);
  997. }
  998. static int hcd_name_to_id(const char *name)
  999. {
  1000. char *c;
  1001. long val;
  1002. int ret;
  1003. c = strchr(name, '.');
  1004. if (c == NULL)
  1005. return 0;
  1006. ret = kstrtol(c+1, 10, &val);
  1007. if (ret < 0)
  1008. return ret;
  1009. return val;
  1010. }
  1011. static int vhci_setup(struct usb_hcd *hcd)
  1012. {
  1013. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1014. if (usb_hcd_is_primary_hcd(hcd)) {
  1015. vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
  1016. vhci->vhci_hcd_hs->vhci = vhci;
  1017. /*
  1018. * Mark the first roothub as being USB 2.0.
  1019. * The USB 3.0 roothub will be registered later by
  1020. * vhci_hcd_probe()
  1021. */
  1022. hcd->speed = HCD_USB2;
  1023. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  1024. } else {
  1025. vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
  1026. vhci->vhci_hcd_ss->vhci = vhci;
  1027. hcd->speed = HCD_USB31;
  1028. hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
  1029. }
  1030. /* accept arbitrarily long scatter-gather lists */
  1031. hcd->self.sg_tablesize = ~0;
  1032. hcd->self.no_sg_constraint = 1;
  1033. return 0;
  1034. }
  1035. static int vhci_start(struct usb_hcd *hcd)
  1036. {
  1037. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  1038. int id, rhport;
  1039. int err;
  1040. usbip_dbg_vhci_hc("enter vhci_start\n");
  1041. if (usb_hcd_is_primary_hcd(hcd))
  1042. spin_lock_init(&vhci_hcd->vhci->lock);
  1043. /* initialize private data of usb_hcd */
  1044. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  1045. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  1046. vhci_device_init(vdev);
  1047. vdev->rhport = rhport;
  1048. }
  1049. atomic_set(&vhci_hcd->seqnum, 0);
  1050. hcd->power_budget = 0; /* no limit */
  1051. hcd->uses_new_polling = 1;
  1052. #ifdef CONFIG_USB_OTG
  1053. hcd->self.otg_port = 1;
  1054. #endif
  1055. id = hcd_name_to_id(hcd_name(hcd));
  1056. if (id < 0) {
  1057. dev_err(hcd_dev(hcd), "invalid vhci name %s\n", hcd_name(hcd));
  1058. return -EINVAL;
  1059. }
  1060. /* vhci_hcd is now ready to be controlled through sysfs */
  1061. if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
  1062. err = vhci_init_attr_group();
  1063. if (err) {
  1064. dev_err(hcd_dev(hcd), "init attr group failed, err = %d\n", err);
  1065. return err;
  1066. }
  1067. err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  1068. if (err) {
  1069. dev_err(hcd_dev(hcd), "create sysfs files failed, err = %d\n", err);
  1070. vhci_finish_attr_group();
  1071. return err;
  1072. }
  1073. dev_info(hcd_dev(hcd), "created sysfs %s\n", hcd_name(hcd));
  1074. }
  1075. return 0;
  1076. }
  1077. static void vhci_stop(struct usb_hcd *hcd)
  1078. {
  1079. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  1080. int id, rhport;
  1081. usbip_dbg_vhci_hc("stop VHCI controller\n");
  1082. /* 1. remove the userland interface of vhci_hcd */
  1083. id = hcd_name_to_id(hcd_name(hcd));
  1084. if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
  1085. sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  1086. vhci_finish_attr_group();
  1087. }
  1088. /* 2. shutdown all the ports of vhci_hcd */
  1089. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  1090. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  1091. usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
  1092. usbip_stop_eh(&vdev->ud);
  1093. }
  1094. }
  1095. static int vhci_get_frame_number(struct usb_hcd *hcd)
  1096. {
  1097. dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
  1098. return 0;
  1099. }
  1100. #ifdef CONFIG_PM
  1101. /* FIXME: suspend/resume */
  1102. static int vhci_bus_suspend(struct usb_hcd *hcd)
  1103. {
  1104. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1105. unsigned long flags;
  1106. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1107. spin_lock_irqsave(&vhci->lock, flags);
  1108. hcd->state = HC_STATE_SUSPENDED;
  1109. spin_unlock_irqrestore(&vhci->lock, flags);
  1110. return 0;
  1111. }
  1112. static int vhci_bus_resume(struct usb_hcd *hcd)
  1113. {
  1114. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1115. int rc = 0;
  1116. unsigned long flags;
  1117. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1118. spin_lock_irqsave(&vhci->lock, flags);
  1119. if (!HCD_HW_ACCESSIBLE(hcd))
  1120. rc = -ESHUTDOWN;
  1121. else
  1122. hcd->state = HC_STATE_RUNNING;
  1123. spin_unlock_irqrestore(&vhci->lock, flags);
  1124. return rc;
  1125. }
  1126. #else
  1127. #define vhci_bus_suspend NULL
  1128. #define vhci_bus_resume NULL
  1129. #endif
  1130. /* Change a group of bulk endpoints to support multiple stream IDs */
  1131. static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1132. struct usb_host_endpoint **eps, unsigned int num_eps,
  1133. unsigned int num_streams, gfp_t mem_flags)
  1134. {
  1135. dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
  1136. return 0;
  1137. }
  1138. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  1139. static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1140. struct usb_host_endpoint **eps, unsigned int num_eps,
  1141. gfp_t mem_flags)
  1142. {
  1143. dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
  1144. return 0;
  1145. }
  1146. static const struct hc_driver vhci_hc_driver = {
  1147. .description = driver_name,
  1148. .product_desc = driver_desc,
  1149. .hcd_priv_size = sizeof(struct vhci_hcd),
  1150. .flags = HCD_USB31 | HCD_SHARED,
  1151. .reset = vhci_setup,
  1152. .start = vhci_start,
  1153. .stop = vhci_stop,
  1154. .urb_enqueue = vhci_urb_enqueue,
  1155. .urb_dequeue = vhci_urb_dequeue,
  1156. .get_frame_number = vhci_get_frame_number,
  1157. .hub_status_data = vhci_hub_status,
  1158. .hub_control = vhci_hub_control,
  1159. .bus_suspend = vhci_bus_suspend,
  1160. .bus_resume = vhci_bus_resume,
  1161. .alloc_streams = vhci_alloc_streams,
  1162. .free_streams = vhci_free_streams,
  1163. };
  1164. static int vhci_hcd_probe(struct platform_device *pdev)
  1165. {
  1166. struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
  1167. struct usb_hcd *hcd_hs;
  1168. struct usb_hcd *hcd_ss;
  1169. int ret;
  1170. usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
  1171. /*
  1172. * Allocate and initialize hcd.
  1173. * Our private data is also allocated automatically.
  1174. */
  1175. hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  1176. if (!hcd_hs)
  1177. return dev_err_probe(&pdev->dev, -ENOMEM, "create primary hcd failed\n");
  1178. hcd_hs->has_tt = 1;
  1179. /*
  1180. * Finish generic HCD structure initialization and register.
  1181. * Call the driver's reset() and start() routines.
  1182. */
  1183. ret = usb_add_hcd(hcd_hs, 0, 0);
  1184. if (ret) {
  1185. dev_err_probe(&pdev->dev, ret, "usb_add_hcd hs failed\n");
  1186. goto put_usb2_hcd;
  1187. }
  1188. hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
  1189. dev_name(&pdev->dev), hcd_hs);
  1190. if (!hcd_ss) {
  1191. ret = dev_err_probe(&pdev->dev, -ENOMEM, "create shared hcd failed\n");
  1192. goto remove_usb2_hcd;
  1193. }
  1194. ret = usb_add_hcd(hcd_ss, 0, 0);
  1195. if (ret) {
  1196. dev_err_probe(&pdev->dev, ret, "usb_add_hcd ss failed\n");
  1197. goto put_usb3_hcd;
  1198. }
  1199. usbip_dbg_vhci_hc("bye\n");
  1200. return 0;
  1201. put_usb3_hcd:
  1202. usb_put_hcd(hcd_ss);
  1203. remove_usb2_hcd:
  1204. usb_remove_hcd(hcd_hs);
  1205. put_usb2_hcd:
  1206. usb_put_hcd(hcd_hs);
  1207. vhci->vhci_hcd_hs = NULL;
  1208. vhci->vhci_hcd_ss = NULL;
  1209. return ret;
  1210. }
  1211. static void vhci_hcd_remove(struct platform_device *pdev)
  1212. {
  1213. struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
  1214. /*
  1215. * Disconnects the root hub,
  1216. * then reverses the effects of usb_add_hcd(),
  1217. * invoking the HCD's stop() methods.
  1218. */
  1219. usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
  1220. usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
  1221. usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
  1222. usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
  1223. vhci->vhci_hcd_hs = NULL;
  1224. vhci->vhci_hcd_ss = NULL;
  1225. }
  1226. #ifdef CONFIG_PM
  1227. /* what should happen for USB/IP under suspend/resume? */
  1228. static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  1229. {
  1230. struct usb_hcd *hcd;
  1231. struct vhci *vhci;
  1232. int rhport;
  1233. int connected = 0;
  1234. int ret = 0;
  1235. unsigned long flags;
  1236. dev_dbg(&pdev->dev, "%s\n", __func__);
  1237. hcd = platform_get_drvdata(pdev);
  1238. if (!hcd)
  1239. return 0;
  1240. vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1241. spin_lock_irqsave(&vhci->lock, flags);
  1242. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  1243. if (vhci->vhci_hcd_hs->port_status[rhport] &
  1244. USB_PORT_STAT_CONNECTION)
  1245. connected += 1;
  1246. if (vhci->vhci_hcd_ss->port_status[rhport] &
  1247. USB_PORT_STAT_CONNECTION)
  1248. connected += 1;
  1249. }
  1250. spin_unlock_irqrestore(&vhci->lock, flags);
  1251. if (connected > 0) {
  1252. dev_info(&pdev->dev,
  1253. "We have %d active connection%s. Do not suspend.\n",
  1254. connected, str_plural(connected));
  1255. ret = -EBUSY;
  1256. } else {
  1257. dev_info(&pdev->dev, "suspend vhci_hcd");
  1258. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1259. }
  1260. return ret;
  1261. }
  1262. static int vhci_hcd_resume(struct platform_device *pdev)
  1263. {
  1264. struct usb_hcd *hcd;
  1265. dev_dbg(&pdev->dev, "%s\n", __func__);
  1266. hcd = platform_get_drvdata(pdev);
  1267. if (!hcd)
  1268. return 0;
  1269. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1270. usb_hcd_poll_rh_status(hcd);
  1271. return 0;
  1272. }
  1273. #else
  1274. #define vhci_hcd_suspend NULL
  1275. #define vhci_hcd_resume NULL
  1276. #endif
  1277. static struct platform_driver vhci_driver = {
  1278. .probe = vhci_hcd_probe,
  1279. .remove = vhci_hcd_remove,
  1280. .suspend = vhci_hcd_suspend,
  1281. .resume = vhci_hcd_resume,
  1282. .driver = {
  1283. .name = driver_name,
  1284. },
  1285. };
  1286. static void del_platform_devices(void)
  1287. {
  1288. int i;
  1289. for (i = 0; i < vhci_num_controllers; i++) {
  1290. platform_device_unregister(vhcis[i].pdev);
  1291. vhcis[i].pdev = NULL;
  1292. }
  1293. sysfs_remove_link(&platform_bus.kobj, driver_name);
  1294. }
  1295. static int __init vhci_hcd_init(void)
  1296. {
  1297. int i, ret;
  1298. if (usb_disabled())
  1299. return -ENODEV;
  1300. if (vhci_num_controllers < 1)
  1301. vhci_num_controllers = 1;
  1302. vhcis = kzalloc_objs(struct vhci, vhci_num_controllers);
  1303. if (vhcis == NULL)
  1304. return -ENOMEM;
  1305. ret = platform_driver_register(&vhci_driver);
  1306. if (ret)
  1307. goto err_driver_register;
  1308. for (i = 0; i < vhci_num_controllers; i++) {
  1309. void *vhci = &vhcis[i];
  1310. struct platform_device_info pdevinfo = {
  1311. .name = driver_name,
  1312. .id = i,
  1313. .data = &vhci,
  1314. .size_data = sizeof(void *),
  1315. };
  1316. vhcis[i].pdev = platform_device_register_full(&pdevinfo);
  1317. ret = PTR_ERR_OR_ZERO(vhcis[i].pdev);
  1318. if (ret < 0) {
  1319. while (i--)
  1320. platform_device_unregister(vhcis[i].pdev);
  1321. goto err_add_hcd;
  1322. }
  1323. }
  1324. return 0;
  1325. err_add_hcd:
  1326. platform_driver_unregister(&vhci_driver);
  1327. err_driver_register:
  1328. kfree(vhcis);
  1329. return ret;
  1330. }
  1331. static void __exit vhci_hcd_exit(void)
  1332. {
  1333. del_platform_devices();
  1334. platform_driver_unregister(&vhci_driver);
  1335. kfree(vhcis);
  1336. }
  1337. module_init(vhci_hcd_init);
  1338. module_exit(vhci_hcd_exit);
  1339. MODULE_AUTHOR(DRIVER_AUTHOR);
  1340. MODULE_DESCRIPTION(DRIVER_DESC);
  1341. MODULE_LICENSE("GPL");