vhci_hcd.c 28 KB

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