vhci_hcd.c 28 KB

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