vhci_hcd.c 38 KB

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