vhci_hcd.c 30 KB

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