vhci_hcd.c 39 KB

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