vhci_hcd.c 37 KB

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