vhci_hcd.c 38 KB

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