vhci_hcd.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. switch (wValue) {
  303. case USB_PORT_FEAT_SUSPEND:
  304. if (hcd->speed == HCD_USB3) {
  305. pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not "
  306. "supported for USB 3.0 roothub\n");
  307. goto error;
  308. }
  309. usbip_dbg_vhci_rh(
  310. " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
  311. if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
  312. /* 20msec signaling */
  313. vhci_hcd->resuming = 1;
  314. vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
  315. }
  316. break;
  317. case USB_PORT_FEAT_POWER:
  318. usbip_dbg_vhci_rh(
  319. " ClearPortFeature: USB_PORT_FEAT_POWER\n");
  320. if (hcd->speed == HCD_USB3)
  321. vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
  322. else
  323. vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
  324. break;
  325. default:
  326. usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
  327. wValue);
  328. vhci_hcd->port_status[rhport] &= ~(1 << wValue);
  329. break;
  330. }
  331. break;
  332. case GetHubDescriptor:
  333. usbip_dbg_vhci_rh(" GetHubDescriptor\n");
  334. if (hcd->speed == HCD_USB3 &&
  335. (wLength < USB_DT_SS_HUB_SIZE ||
  336. wValue != (USB_DT_SS_HUB << 8))) {
  337. pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n");
  338. goto error;
  339. }
  340. if (hcd->speed == HCD_USB3)
  341. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  342. else
  343. hub_descriptor((struct usb_hub_descriptor *) buf);
  344. break;
  345. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  346. if (hcd->speed != HCD_USB3)
  347. goto error;
  348. if ((wValue >> 8) != USB_DT_BOS)
  349. goto error;
  350. memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
  351. retval = sizeof(usb3_bos_desc);
  352. break;
  353. case GetHubStatus:
  354. usbip_dbg_vhci_rh(" GetHubStatus\n");
  355. *(__le32 *) buf = cpu_to_le32(0);
  356. break;
  357. case GetPortStatus:
  358. usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
  359. if (wIndex < 1) {
  360. pr_err("invalid port number %d\n", wIndex);
  361. retval = -EPIPE;
  362. }
  363. /* we do not care about resume. */
  364. /* whoever resets or resumes must GetPortStatus to
  365. * complete it!!
  366. */
  367. if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
  368. vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
  369. vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
  370. vhci_hcd->resuming = 0;
  371. vhci_hcd->re_timeout = 0;
  372. }
  373. if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
  374. 0 && time_after(jiffies, vhci_hcd->re_timeout)) {
  375. vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
  376. vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
  377. vhci_hcd->re_timeout = 0;
  378. if (vhci_hcd->vdev[rhport].ud.status ==
  379. VDEV_ST_NOTASSIGNED) {
  380. usbip_dbg_vhci_rh(
  381. " enable rhport %d (status %u)\n",
  382. rhport,
  383. vhci_hcd->vdev[rhport].ud.status);
  384. vhci_hcd->port_status[rhport] |=
  385. USB_PORT_STAT_ENABLE;
  386. }
  387. if (hcd->speed < HCD_USB3) {
  388. switch (vhci_hcd->vdev[rhport].speed) {
  389. case USB_SPEED_HIGH:
  390. vhci_hcd->port_status[rhport] |=
  391. USB_PORT_STAT_HIGH_SPEED;
  392. break;
  393. case USB_SPEED_LOW:
  394. vhci_hcd->port_status[rhport] |=
  395. USB_PORT_STAT_LOW_SPEED;
  396. break;
  397. default:
  398. pr_err("vhci_device speed not set\n");
  399. break;
  400. }
  401. }
  402. }
  403. ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
  404. ((__le16 *) buf)[1] =
  405. cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
  406. usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
  407. ((u16 *)buf)[1]);
  408. break;
  409. case SetHubFeature:
  410. usbip_dbg_vhci_rh(" SetHubFeature\n");
  411. retval = -EPIPE;
  412. break;
  413. case SetPortFeature:
  414. switch (wValue) {
  415. case USB_PORT_FEAT_LINK_STATE:
  416. usbip_dbg_vhci_rh(
  417. " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
  418. if (hcd->speed != HCD_USB3) {
  419. pr_err("USB_PORT_FEAT_LINK_STATE req not "
  420. "supported for USB 2.0 roothub\n");
  421. goto error;
  422. }
  423. /*
  424. * Since this is dummy we don't have an actual link so
  425. * there is nothing to do for the SET_LINK_STATE cmd
  426. */
  427. break;
  428. case USB_PORT_FEAT_U1_TIMEOUT:
  429. usbip_dbg_vhci_rh(
  430. " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
  431. case USB_PORT_FEAT_U2_TIMEOUT:
  432. usbip_dbg_vhci_rh(
  433. " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
  434. /* TODO: add suspend/resume support! */
  435. if (hcd->speed != HCD_USB3) {
  436. pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not "
  437. "supported for USB 2.0 roothub\n");
  438. goto error;
  439. }
  440. break;
  441. case USB_PORT_FEAT_SUSPEND:
  442. usbip_dbg_vhci_rh(
  443. " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
  444. /* Applicable only for USB2.0 hub */
  445. if (hcd->speed == HCD_USB3) {
  446. pr_err("USB_PORT_FEAT_SUSPEND req not "
  447. "supported for USB 3.0 roothub\n");
  448. goto error;
  449. }
  450. vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
  451. break;
  452. case USB_PORT_FEAT_POWER:
  453. usbip_dbg_vhci_rh(
  454. " SetPortFeature: USB_PORT_FEAT_POWER\n");
  455. if (hcd->speed == HCD_USB3)
  456. vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
  457. else
  458. vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
  459. break;
  460. case USB_PORT_FEAT_BH_PORT_RESET:
  461. usbip_dbg_vhci_rh(
  462. " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
  463. /* Applicable only for USB3.0 hub */
  464. if (hcd->speed != HCD_USB3) {
  465. pr_err("USB_PORT_FEAT_BH_PORT_RESET req not "
  466. "supported for USB 2.0 roothub\n");
  467. goto error;
  468. }
  469. /* FALLS THROUGH */
  470. case USB_PORT_FEAT_RESET:
  471. usbip_dbg_vhci_rh(
  472. " SetPortFeature: USB_PORT_FEAT_RESET\n");
  473. /* if it's already enabled, disable */
  474. if (hcd->speed == HCD_USB3) {
  475. vhci_hcd->port_status[rhport] = 0;
  476. vhci_hcd->port_status[rhport] =
  477. (USB_SS_PORT_STAT_POWER |
  478. USB_PORT_STAT_CONNECTION |
  479. USB_PORT_STAT_RESET);
  480. } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
  481. vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
  482. | USB_PORT_STAT_LOW_SPEED
  483. | USB_PORT_STAT_HIGH_SPEED);
  484. }
  485. /* 50msec reset signaling */
  486. vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  487. /* FALLS THROUGH */
  488. default:
  489. usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
  490. wValue);
  491. if (hcd->speed == HCD_USB3) {
  492. if ((vhci_hcd->port_status[rhport] &
  493. USB_SS_PORT_STAT_POWER) != 0) {
  494. vhci_hcd->port_status[rhport] |= (1 << wValue);
  495. }
  496. } else
  497. if ((vhci_hcd->port_status[rhport] &
  498. USB_PORT_STAT_POWER) != 0) {
  499. vhci_hcd->port_status[rhport] |= (1 << wValue);
  500. }
  501. }
  502. break;
  503. case GetPortErrorCount:
  504. usbip_dbg_vhci_rh(" GetPortErrorCount\n");
  505. if (hcd->speed != HCD_USB3) {
  506. pr_err("GetPortErrorCount req not "
  507. "supported for USB 2.0 roothub\n");
  508. goto error;
  509. }
  510. /* We'll always return 0 since this is a dummy hub */
  511. *(__le32 *) buf = cpu_to_le32(0);
  512. break;
  513. case SetHubDepth:
  514. usbip_dbg_vhci_rh(" SetHubDepth\n");
  515. if (hcd->speed != HCD_USB3) {
  516. pr_err("SetHubDepth req not supported for "
  517. "USB 2.0 roothub\n");
  518. goto error;
  519. }
  520. break;
  521. default:
  522. pr_err("default hub control req: %04x v%04x i%04x l%d\n",
  523. typeReq, wValue, wIndex, wLength);
  524. error:
  525. /* "protocol stall" on error */
  526. retval = -EPIPE;
  527. }
  528. if (usbip_dbg_flag_vhci_rh) {
  529. pr_debug("port %d\n", rhport);
  530. /* Only dump valid port status */
  531. if (rhport >= 0) {
  532. dump_port_status_diff(prev_port_status[rhport],
  533. vhci_hcd->port_status[rhport],
  534. hcd->speed == HCD_USB3);
  535. }
  536. }
  537. usbip_dbg_vhci_rh(" bye\n");
  538. spin_unlock_irqrestore(&vhci->lock, flags);
  539. if ((vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0)
  540. usb_hcd_poll_rh_status(hcd);
  541. return retval;
  542. }
  543. static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
  544. {
  545. struct vhci_priv *priv;
  546. struct vhci_hcd *vhci_hcd;
  547. unsigned long flags;
  548. if (!vdev) {
  549. pr_err("could not get virtual device");
  550. return;
  551. }
  552. vhci_hcd = vdev_to_vhci_hcd(vdev);
  553. priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
  554. if (!priv) {
  555. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  556. return;
  557. }
  558. spin_lock_irqsave(&vdev->priv_lock, flags);
  559. priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
  560. if (priv->seqnum == 0xffff)
  561. dev_info(&urb->dev->dev, "seqnum max\n");
  562. priv->vdev = vdev;
  563. priv->urb = urb;
  564. urb->hcpriv = (void *) priv;
  565. list_add_tail(&priv->list, &vdev->priv_tx);
  566. wake_up(&vdev->waitq_tx);
  567. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  568. }
  569. static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  570. {
  571. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  572. struct vhci *vhci = vhci_hcd->vhci;
  573. struct device *dev = &urb->dev->dev;
  574. u8 portnum = urb->dev->portnum;
  575. int ret = 0;
  576. struct vhci_device *vdev;
  577. unsigned long flags;
  578. if (portnum > VHCI_HC_PORTS) {
  579. pr_err("invalid port number %d\n", portnum);
  580. return -ENODEV;
  581. }
  582. vdev = &vhci_hcd->vdev[portnum-1];
  583. /* patch to usb_sg_init() is in 2.5.60 */
  584. BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
  585. spin_lock_irqsave(&vhci->lock, flags);
  586. if (urb->status != -EINPROGRESS) {
  587. dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
  588. spin_unlock_irqrestore(&vhci->lock, flags);
  589. return urb->status;
  590. }
  591. /* refuse enqueue for dead connection */
  592. spin_lock(&vdev->ud.lock);
  593. if (vdev->ud.status == VDEV_ST_NULL ||
  594. vdev->ud.status == VDEV_ST_ERROR) {
  595. dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
  596. spin_unlock(&vdev->ud.lock);
  597. spin_unlock_irqrestore(&vhci->lock, flags);
  598. return -ENODEV;
  599. }
  600. spin_unlock(&vdev->ud.lock);
  601. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  602. if (ret)
  603. goto no_need_unlink;
  604. /*
  605. * The enumeration process is as follows;
  606. *
  607. * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
  608. * to get max packet length of default pipe
  609. *
  610. * 2. Set_Address request to DevAddr(0) EndPoint(0)
  611. *
  612. */
  613. if (usb_pipedevice(urb->pipe) == 0) {
  614. __u8 type = usb_pipetype(urb->pipe);
  615. struct usb_ctrlrequest *ctrlreq =
  616. (struct usb_ctrlrequest *) urb->setup_packet;
  617. if (type != PIPE_CONTROL || !ctrlreq) {
  618. dev_err(dev, "invalid request to devnum 0\n");
  619. ret = -EINVAL;
  620. goto no_need_xmit;
  621. }
  622. switch (ctrlreq->bRequest) {
  623. case USB_REQ_SET_ADDRESS:
  624. /* set_address may come when a device is reset */
  625. dev_info(dev, "SetAddress Request (%d) to port %d\n",
  626. ctrlreq->wValue, vdev->rhport);
  627. usb_put_dev(vdev->udev);
  628. vdev->udev = usb_get_dev(urb->dev);
  629. spin_lock(&vdev->ud.lock);
  630. vdev->ud.status = VDEV_ST_USED;
  631. spin_unlock(&vdev->ud.lock);
  632. if (urb->status == -EINPROGRESS) {
  633. /* This request is successfully completed. */
  634. /* If not -EINPROGRESS, possibly unlinked. */
  635. urb->status = 0;
  636. }
  637. goto no_need_xmit;
  638. case USB_REQ_GET_DESCRIPTOR:
  639. if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
  640. usbip_dbg_vhci_hc(
  641. "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
  642. usb_put_dev(vdev->udev);
  643. vdev->udev = usb_get_dev(urb->dev);
  644. goto out;
  645. default:
  646. /* NOT REACHED */
  647. dev_err(dev,
  648. "invalid request to devnum 0 bRequest %u, wValue %u\n",
  649. ctrlreq->bRequest,
  650. ctrlreq->wValue);
  651. ret = -EINVAL;
  652. goto no_need_xmit;
  653. }
  654. }
  655. out:
  656. vhci_tx_urb(urb, vdev);
  657. spin_unlock_irqrestore(&vhci->lock, flags);
  658. return 0;
  659. no_need_xmit:
  660. usb_hcd_unlink_urb_from_ep(hcd, urb);
  661. no_need_unlink:
  662. spin_unlock_irqrestore(&vhci->lock, flags);
  663. if (!ret)
  664. usb_hcd_giveback_urb(hcd, urb, urb->status);
  665. return ret;
  666. }
  667. /*
  668. * vhci_rx gives back the urb after receiving the reply of the urb. If an
  669. * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
  670. * back its urb. For the driver unlinking the urb, the content of the urb is
  671. * not important, but the calling to its completion handler is important; the
  672. * completion of unlinking is notified by the completion handler.
  673. *
  674. *
  675. * CLIENT SIDE
  676. *
  677. * - When vhci_hcd receives RET_SUBMIT,
  678. *
  679. * - case 1a). the urb of the pdu is not unlinking.
  680. * - normal case
  681. * => just give back the urb
  682. *
  683. * - case 1b). the urb of the pdu is unlinking.
  684. * - usbip.ko will return a reply of the unlinking request.
  685. * => give back the urb now and go to case 2b).
  686. *
  687. * - When vhci_hcd receives RET_UNLINK,
  688. *
  689. * - case 2a). a submit request is still pending in vhci_hcd.
  690. * - urb was really pending in usbip.ko and urb_unlink_urb() was
  691. * completed there.
  692. * => free a pending submit request
  693. * => notify unlink completeness by giving back the urb
  694. *
  695. * - case 2b). a submit request is *not* pending in vhci_hcd.
  696. * - urb was already given back to the core driver.
  697. * => do not give back the urb
  698. *
  699. *
  700. * SERVER SIDE
  701. *
  702. * - When usbip receives CMD_UNLINK,
  703. *
  704. * - case 3a). the urb of the unlink request is now in submission.
  705. * => do usb_unlink_urb().
  706. * => after the unlink is completed, send RET_UNLINK.
  707. *
  708. * - case 3b). the urb of the unlink request is not in submission.
  709. * - may be already completed or never be received
  710. * => send RET_UNLINK
  711. *
  712. */
  713. static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  714. {
  715. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  716. struct vhci *vhci = vhci_hcd->vhci;
  717. struct vhci_priv *priv;
  718. struct vhci_device *vdev;
  719. unsigned long flags;
  720. spin_lock_irqsave(&vhci->lock, flags);
  721. priv = urb->hcpriv;
  722. if (!priv) {
  723. /* URB was never linked! or will be soon given back by
  724. * vhci_rx. */
  725. spin_unlock_irqrestore(&vhci->lock, flags);
  726. return -EIDRM;
  727. }
  728. {
  729. int ret = 0;
  730. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  731. if (ret) {
  732. spin_unlock_irqrestore(&vhci->lock, flags);
  733. return ret;
  734. }
  735. }
  736. /* send unlink request here? */
  737. vdev = priv->vdev;
  738. if (!vdev->ud.tcp_socket) {
  739. /* tcp connection is closed */
  740. spin_lock(&vdev->priv_lock);
  741. list_del(&priv->list);
  742. kfree(priv);
  743. urb->hcpriv = NULL;
  744. spin_unlock(&vdev->priv_lock);
  745. /*
  746. * If tcp connection is alive, we have sent CMD_UNLINK.
  747. * vhci_rx will receive RET_UNLINK and give back the URB.
  748. * Otherwise, we give back it here.
  749. */
  750. usb_hcd_unlink_urb_from_ep(hcd, urb);
  751. spin_unlock_irqrestore(&vhci->lock, flags);
  752. usb_hcd_giveback_urb(hcd, urb, urb->status);
  753. spin_lock_irqsave(&vhci->lock, flags);
  754. } else {
  755. /* tcp connection is alive */
  756. struct vhci_unlink *unlink;
  757. spin_lock(&vdev->priv_lock);
  758. /* setup CMD_UNLINK pdu */
  759. unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
  760. if (!unlink) {
  761. spin_unlock(&vdev->priv_lock);
  762. spin_unlock_irqrestore(&vhci->lock, flags);
  763. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  764. return -ENOMEM;
  765. }
  766. unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
  767. if (unlink->seqnum == 0xffff)
  768. pr_info("seqnum max\n");
  769. unlink->unlink_seqnum = priv->seqnum;
  770. /* send cmd_unlink and try to cancel the pending URB in the
  771. * peer */
  772. list_add_tail(&unlink->list, &vdev->unlink_tx);
  773. wake_up(&vdev->waitq_tx);
  774. spin_unlock(&vdev->priv_lock);
  775. }
  776. spin_unlock_irqrestore(&vhci->lock, flags);
  777. usbip_dbg_vhci_hc("leave\n");
  778. return 0;
  779. }
  780. static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
  781. {
  782. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  783. struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
  784. struct vhci *vhci = vhci_hcd->vhci;
  785. struct vhci_unlink *unlink, *tmp;
  786. unsigned long flags;
  787. spin_lock_irqsave(&vhci->lock, flags);
  788. spin_lock(&vdev->priv_lock);
  789. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
  790. pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
  791. list_del(&unlink->list);
  792. kfree(unlink);
  793. }
  794. while (!list_empty(&vdev->unlink_rx)) {
  795. struct urb *urb;
  796. unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
  797. list);
  798. /* give back URB of unanswered unlink request */
  799. pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
  800. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  801. if (!urb) {
  802. pr_info("the urb (seqnum %lu) was already given back\n",
  803. unlink->unlink_seqnum);
  804. list_del(&unlink->list);
  805. kfree(unlink);
  806. continue;
  807. }
  808. urb->status = -ENODEV;
  809. usb_hcd_unlink_urb_from_ep(hcd, urb);
  810. list_del(&unlink->list);
  811. spin_unlock(&vdev->priv_lock);
  812. spin_unlock_irqrestore(&vhci->lock, flags);
  813. usb_hcd_giveback_urb(hcd, urb, urb->status);
  814. spin_lock_irqsave(&vhci->lock, flags);
  815. spin_lock(&vdev->priv_lock);
  816. kfree(unlink);
  817. }
  818. spin_unlock(&vdev->priv_lock);
  819. spin_unlock_irqrestore(&vhci->lock, flags);
  820. }
  821. /*
  822. * The important thing is that only one context begins cleanup.
  823. * This is why error handling and cleanup become simple.
  824. * We do not want to consider race condition as possible.
  825. */
  826. static void vhci_shutdown_connection(struct usbip_device *ud)
  827. {
  828. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  829. /* need this? see stub_dev.c */
  830. if (ud->tcp_socket) {
  831. pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
  832. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  833. }
  834. /* kill threads related to this sdev */
  835. if (vdev->ud.tcp_rx) {
  836. kthread_stop_put(vdev->ud.tcp_rx);
  837. vdev->ud.tcp_rx = NULL;
  838. }
  839. if (vdev->ud.tcp_tx) {
  840. kthread_stop_put(vdev->ud.tcp_tx);
  841. vdev->ud.tcp_tx = NULL;
  842. }
  843. pr_info("stop threads\n");
  844. /* active connection is closed */
  845. if (vdev->ud.tcp_socket) {
  846. sockfd_put(vdev->ud.tcp_socket);
  847. vdev->ud.tcp_socket = NULL;
  848. }
  849. pr_info("release socket\n");
  850. vhci_device_unlink_cleanup(vdev);
  851. /*
  852. * rh_port_disconnect() is a trigger of ...
  853. * usb_disable_device():
  854. * disable all the endpoints for a USB device.
  855. * usb_disable_endpoint():
  856. * disable endpoints. pending urbs are unlinked(dequeued).
  857. *
  858. * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
  859. * detached device should release used urbs in a cleanup function (i.e.
  860. * xxx_disconnect()). Therefore, vhci_hcd does not need to release
  861. * pushed urbs and their private data in this function.
  862. *
  863. * NOTE: vhci_dequeue() must be considered carefully. When shutting down
  864. * a connection, vhci_shutdown_connection() expects vhci_dequeue()
  865. * gives back pushed urbs and frees their private data by request of
  866. * the cleanup function of a USB driver. When unlinking a urb with an
  867. * active connection, vhci_dequeue() does not give back the urb which
  868. * is actually given back by vhci_rx after receiving its return pdu.
  869. *
  870. */
  871. rh_port_disconnect(vdev);
  872. pr_info("disconnect device\n");
  873. }
  874. static void vhci_device_reset(struct usbip_device *ud)
  875. {
  876. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  877. unsigned long flags;
  878. spin_lock_irqsave(&ud->lock, flags);
  879. vdev->speed = 0;
  880. vdev->devid = 0;
  881. usb_put_dev(vdev->udev);
  882. vdev->udev = NULL;
  883. if (ud->tcp_socket) {
  884. sockfd_put(ud->tcp_socket);
  885. ud->tcp_socket = NULL;
  886. }
  887. ud->status = VDEV_ST_NULL;
  888. spin_unlock_irqrestore(&ud->lock, flags);
  889. }
  890. static void vhci_device_unusable(struct usbip_device *ud)
  891. {
  892. unsigned long flags;
  893. spin_lock_irqsave(&ud->lock, flags);
  894. ud->status = VDEV_ST_ERROR;
  895. spin_unlock_irqrestore(&ud->lock, flags);
  896. }
  897. static void vhci_device_init(struct vhci_device *vdev)
  898. {
  899. memset(vdev, 0, sizeof(struct vhci_device));
  900. vdev->ud.side = USBIP_VHCI;
  901. vdev->ud.status = VDEV_ST_NULL;
  902. spin_lock_init(&vdev->ud.lock);
  903. INIT_LIST_HEAD(&vdev->priv_rx);
  904. INIT_LIST_HEAD(&vdev->priv_tx);
  905. INIT_LIST_HEAD(&vdev->unlink_tx);
  906. INIT_LIST_HEAD(&vdev->unlink_rx);
  907. spin_lock_init(&vdev->priv_lock);
  908. init_waitqueue_head(&vdev->waitq_tx);
  909. vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
  910. vdev->ud.eh_ops.reset = vhci_device_reset;
  911. vdev->ud.eh_ops.unusable = vhci_device_unusable;
  912. usbip_start_eh(&vdev->ud);
  913. }
  914. static int hcd_name_to_id(const char *name)
  915. {
  916. char *c;
  917. long val;
  918. int ret;
  919. c = strchr(name, '.');
  920. if (c == NULL)
  921. return 0;
  922. ret = kstrtol(c+1, 10, &val);
  923. if (ret < 0)
  924. return ret;
  925. return val;
  926. }
  927. static int vhci_setup(struct usb_hcd *hcd)
  928. {
  929. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  930. if (usb_hcd_is_primary_hcd(hcd)) {
  931. vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
  932. vhci->vhci_hcd_hs->vhci = vhci;
  933. /*
  934. * Mark the first roothub as being USB 2.0.
  935. * The USB 3.0 roothub will be registered later by
  936. * vhci_hcd_probe()
  937. */
  938. hcd->speed = HCD_USB2;
  939. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  940. } else {
  941. vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
  942. vhci->vhci_hcd_ss->vhci = vhci;
  943. hcd->speed = HCD_USB3;
  944. hcd->self.root_hub->speed = USB_SPEED_SUPER;
  945. }
  946. return 0;
  947. }
  948. static int vhci_start(struct usb_hcd *hcd)
  949. {
  950. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  951. int id, rhport;
  952. int err;
  953. usbip_dbg_vhci_hc("enter vhci_start\n");
  954. if (usb_hcd_is_primary_hcd(hcd))
  955. spin_lock_init(&vhci_hcd->vhci->lock);
  956. /* initialize private data of usb_hcd */
  957. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  958. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  959. vhci_device_init(vdev);
  960. vdev->rhport = rhport;
  961. }
  962. atomic_set(&vhci_hcd->seqnum, 0);
  963. hcd->power_budget = 0; /* no limit */
  964. hcd->uses_new_polling = 1;
  965. #ifdef CONFIG_USB_OTG
  966. hcd->self.otg_port = 1;
  967. #endif
  968. id = hcd_name_to_id(hcd_name(hcd));
  969. if (id < 0) {
  970. pr_err("invalid vhci name %s\n", hcd_name(hcd));
  971. return -EINVAL;
  972. }
  973. /* vhci_hcd is now ready to be controlled through sysfs */
  974. if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
  975. err = vhci_init_attr_group();
  976. if (err) {
  977. pr_err("init attr group\n");
  978. return err;
  979. }
  980. err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  981. if (err) {
  982. pr_err("create sysfs files\n");
  983. vhci_finish_attr_group();
  984. return err;
  985. }
  986. pr_info("created sysfs %s\n", hcd_name(hcd));
  987. }
  988. return 0;
  989. }
  990. static void vhci_stop(struct usb_hcd *hcd)
  991. {
  992. struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
  993. int id, rhport;
  994. usbip_dbg_vhci_hc("stop VHCI controller\n");
  995. /* 1. remove the userland interface of vhci_hcd */
  996. id = hcd_name_to_id(hcd_name(hcd));
  997. if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
  998. sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  999. vhci_finish_attr_group();
  1000. }
  1001. /* 2. shutdown all the ports of vhci_hcd */
  1002. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  1003. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  1004. usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
  1005. usbip_stop_eh(&vdev->ud);
  1006. }
  1007. }
  1008. static int vhci_get_frame_number(struct usb_hcd *hcd)
  1009. {
  1010. dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
  1011. return 0;
  1012. }
  1013. #ifdef CONFIG_PM
  1014. /* FIXME: suspend/resume */
  1015. static int vhci_bus_suspend(struct usb_hcd *hcd)
  1016. {
  1017. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1018. unsigned long flags;
  1019. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1020. spin_lock_irqsave(&vhci->lock, flags);
  1021. hcd->state = HC_STATE_SUSPENDED;
  1022. spin_unlock_irqrestore(&vhci->lock, flags);
  1023. return 0;
  1024. }
  1025. static int vhci_bus_resume(struct usb_hcd *hcd)
  1026. {
  1027. struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1028. int rc = 0;
  1029. unsigned long flags;
  1030. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1031. spin_lock_irqsave(&vhci->lock, flags);
  1032. if (!HCD_HW_ACCESSIBLE(hcd))
  1033. rc = -ESHUTDOWN;
  1034. else
  1035. hcd->state = HC_STATE_RUNNING;
  1036. spin_unlock_irqrestore(&vhci->lock, flags);
  1037. return rc;
  1038. }
  1039. #else
  1040. #define vhci_bus_suspend NULL
  1041. #define vhci_bus_resume NULL
  1042. #endif
  1043. /* Change a group of bulk endpoints to support multiple stream IDs */
  1044. static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1045. struct usb_host_endpoint **eps, unsigned int num_eps,
  1046. unsigned int num_streams, gfp_t mem_flags)
  1047. {
  1048. dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
  1049. return 0;
  1050. }
  1051. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  1052. static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  1053. struct usb_host_endpoint **eps, unsigned int num_eps,
  1054. gfp_t mem_flags)
  1055. {
  1056. dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
  1057. return 0;
  1058. }
  1059. static const struct hc_driver vhci_hc_driver = {
  1060. .description = driver_name,
  1061. .product_desc = driver_desc,
  1062. .hcd_priv_size = sizeof(struct vhci_hcd),
  1063. .flags = HCD_USB3 | HCD_SHARED,
  1064. .reset = vhci_setup,
  1065. .start = vhci_start,
  1066. .stop = vhci_stop,
  1067. .urb_enqueue = vhci_urb_enqueue,
  1068. .urb_dequeue = vhci_urb_dequeue,
  1069. .get_frame_number = vhci_get_frame_number,
  1070. .hub_status_data = vhci_hub_status,
  1071. .hub_control = vhci_hub_control,
  1072. .bus_suspend = vhci_bus_suspend,
  1073. .bus_resume = vhci_bus_resume,
  1074. .alloc_streams = vhci_alloc_streams,
  1075. .free_streams = vhci_free_streams,
  1076. };
  1077. static int vhci_hcd_probe(struct platform_device *pdev)
  1078. {
  1079. struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
  1080. struct usb_hcd *hcd_hs;
  1081. struct usb_hcd *hcd_ss;
  1082. int ret;
  1083. usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
  1084. /*
  1085. * Allocate and initialize hcd.
  1086. * Our private data is also allocated automatically.
  1087. */
  1088. hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  1089. if (!hcd_hs) {
  1090. pr_err("create primary hcd failed\n");
  1091. return -ENOMEM;
  1092. }
  1093. hcd_hs->has_tt = 1;
  1094. /*
  1095. * Finish generic HCD structure initialization and register.
  1096. * Call the driver's reset() and start() routines.
  1097. */
  1098. ret = usb_add_hcd(hcd_hs, 0, 0);
  1099. if (ret != 0) {
  1100. pr_err("usb_add_hcd hs failed %d\n", ret);
  1101. goto put_usb2_hcd;
  1102. }
  1103. hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
  1104. dev_name(&pdev->dev), hcd_hs);
  1105. if (!hcd_ss) {
  1106. ret = -ENOMEM;
  1107. pr_err("create shared hcd failed\n");
  1108. goto remove_usb2_hcd;
  1109. }
  1110. ret = usb_add_hcd(hcd_ss, 0, 0);
  1111. if (ret) {
  1112. pr_err("usb_add_hcd ss failed %d\n", ret);
  1113. goto put_usb3_hcd;
  1114. }
  1115. usbip_dbg_vhci_hc("bye\n");
  1116. return 0;
  1117. put_usb3_hcd:
  1118. usb_put_hcd(hcd_ss);
  1119. remove_usb2_hcd:
  1120. usb_remove_hcd(hcd_hs);
  1121. put_usb2_hcd:
  1122. usb_put_hcd(hcd_hs);
  1123. vhci->vhci_hcd_hs = NULL;
  1124. vhci->vhci_hcd_ss = NULL;
  1125. return ret;
  1126. }
  1127. static int vhci_hcd_remove(struct platform_device *pdev)
  1128. {
  1129. struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
  1130. /*
  1131. * Disconnects the root hub,
  1132. * then reverses the effects of usb_add_hcd(),
  1133. * invoking the HCD's stop() methods.
  1134. */
  1135. usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
  1136. usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
  1137. usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
  1138. usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
  1139. vhci->vhci_hcd_hs = NULL;
  1140. vhci->vhci_hcd_ss = NULL;
  1141. return 0;
  1142. }
  1143. #ifdef CONFIG_PM
  1144. /* what should happen for USB/IP under suspend/resume? */
  1145. static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  1146. {
  1147. struct usb_hcd *hcd;
  1148. struct vhci *vhci;
  1149. int rhport;
  1150. int connected = 0;
  1151. int ret = 0;
  1152. unsigned long flags;
  1153. dev_dbg(&pdev->dev, "%s\n", __func__);
  1154. hcd = platform_get_drvdata(pdev);
  1155. if (!hcd)
  1156. return 0;
  1157. vhci = *((void **)dev_get_platdata(hcd->self.controller));
  1158. spin_lock_irqsave(&vhci->lock, flags);
  1159. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  1160. if (vhci->vhci_hcd_hs->port_status[rhport] &
  1161. USB_PORT_STAT_CONNECTION)
  1162. connected += 1;
  1163. if (vhci->vhci_hcd_ss->port_status[rhport] &
  1164. USB_PORT_STAT_CONNECTION)
  1165. connected += 1;
  1166. }
  1167. spin_unlock_irqrestore(&vhci->lock, flags);
  1168. if (connected > 0) {
  1169. dev_info(&pdev->dev,
  1170. "We have %d active connection%s. Do not suspend.\n",
  1171. connected, (connected == 1 ? "" : "s"));
  1172. ret = -EBUSY;
  1173. } else {
  1174. dev_info(&pdev->dev, "suspend vhci_hcd");
  1175. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1176. }
  1177. return ret;
  1178. }
  1179. static int vhci_hcd_resume(struct platform_device *pdev)
  1180. {
  1181. struct usb_hcd *hcd;
  1182. dev_dbg(&pdev->dev, "%s\n", __func__);
  1183. hcd = platform_get_drvdata(pdev);
  1184. if (!hcd)
  1185. return 0;
  1186. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  1187. usb_hcd_poll_rh_status(hcd);
  1188. return 0;
  1189. }
  1190. #else
  1191. #define vhci_hcd_suspend NULL
  1192. #define vhci_hcd_resume NULL
  1193. #endif
  1194. static struct platform_driver vhci_driver = {
  1195. .probe = vhci_hcd_probe,
  1196. .remove = vhci_hcd_remove,
  1197. .suspend = vhci_hcd_suspend,
  1198. .resume = vhci_hcd_resume,
  1199. .driver = {
  1200. .name = driver_name,
  1201. },
  1202. };
  1203. static void del_platform_devices(void)
  1204. {
  1205. struct platform_device *pdev;
  1206. int i;
  1207. for (i = 0; i < vhci_num_controllers; i++) {
  1208. pdev = vhcis[i].pdev;
  1209. if (pdev != NULL)
  1210. platform_device_unregister(pdev);
  1211. vhcis[i].pdev = NULL;
  1212. }
  1213. sysfs_remove_link(&platform_bus.kobj, driver_name);
  1214. }
  1215. static int __init vhci_hcd_init(void)
  1216. {
  1217. int i, ret;
  1218. if (usb_disabled())
  1219. return -ENODEV;
  1220. if (vhci_num_controllers < 1)
  1221. vhci_num_controllers = 1;
  1222. vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL);
  1223. if (vhcis == NULL)
  1224. return -ENOMEM;
  1225. for (i = 0; i < vhci_num_controllers; i++) {
  1226. vhcis[i].pdev = platform_device_alloc(driver_name, i);
  1227. if (!vhcis[i].pdev) {
  1228. i--;
  1229. while (i >= 0)
  1230. platform_device_put(vhcis[i--].pdev);
  1231. ret = -ENOMEM;
  1232. goto err_device_alloc;
  1233. }
  1234. }
  1235. for (i = 0; i < vhci_num_controllers; i++) {
  1236. void *vhci = &vhcis[i];
  1237. ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *));
  1238. if (ret)
  1239. goto err_driver_register;
  1240. }
  1241. ret = platform_driver_register(&vhci_driver);
  1242. if (ret)
  1243. goto err_driver_register;
  1244. for (i = 0; i < vhci_num_controllers; i++) {
  1245. ret = platform_device_add(vhcis[i].pdev);
  1246. if (ret < 0) {
  1247. i--;
  1248. while (i >= 0)
  1249. platform_device_del(vhcis[i--].pdev);
  1250. goto err_add_hcd;
  1251. }
  1252. }
  1253. return ret;
  1254. err_add_hcd:
  1255. platform_driver_unregister(&vhci_driver);
  1256. err_driver_register:
  1257. for (i = 0; i < vhci_num_controllers; i++)
  1258. platform_device_put(vhcis[i].pdev);
  1259. err_device_alloc:
  1260. kfree(vhcis);
  1261. return ret;
  1262. }
  1263. static void __exit vhci_hcd_exit(void)
  1264. {
  1265. del_platform_devices();
  1266. platform_driver_unregister(&vhci_driver);
  1267. kfree(vhcis);
  1268. }
  1269. module_init(vhci_hcd_init);
  1270. module_exit(vhci_hcd_exit);
  1271. MODULE_AUTHOR(DRIVER_AUTHOR);
  1272. MODULE_DESCRIPTION(DRIVER_DESC);
  1273. MODULE_LICENSE("GPL");