vhci_hcd.c 39 KB

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