vhci_hcd.c 38 KB

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