vhci_hcd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. usb_put_dev(vdev->udev);
  445. vdev->udev = usb_get_dev(urb->dev);
  446. spin_lock(&vdev->ud.lock);
  447. vdev->ud.status = VDEV_ST_USED;
  448. spin_unlock(&vdev->ud.lock);
  449. if (urb->status == -EINPROGRESS) {
  450. /* This request is successfully completed. */
  451. /* If not -EINPROGRESS, possibly unlinked. */
  452. urb->status = 0;
  453. }
  454. goto no_need_xmit;
  455. case USB_REQ_GET_DESCRIPTOR:
  456. if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
  457. usbip_dbg_vhci_hc(
  458. "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
  459. usb_put_dev(vdev->udev);
  460. vdev->udev = usb_get_dev(urb->dev);
  461. goto out;
  462. default:
  463. /* NOT REACHED */
  464. dev_err(dev,
  465. "invalid request to devnum 0 bRequest %u, wValue %u\n",
  466. ctrlreq->bRequest,
  467. ctrlreq->wValue);
  468. ret = -EINVAL;
  469. goto no_need_xmit;
  470. }
  471. }
  472. out:
  473. vhci_tx_urb(urb);
  474. spin_unlock(&the_controller->lock);
  475. return 0;
  476. no_need_xmit:
  477. usb_hcd_unlink_urb_from_ep(hcd, urb);
  478. no_need_unlink:
  479. spin_unlock(&the_controller->lock);
  480. usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
  481. return ret;
  482. }
  483. /*
  484. * vhci_rx gives back the urb after receiving the reply of the urb. If an
  485. * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
  486. * back its urb. For the driver unlinking the urb, the content of the urb is
  487. * not important, but the calling to its completion handler is important; the
  488. * completion of unlinking is notified by the completion handler.
  489. *
  490. *
  491. * CLIENT SIDE
  492. *
  493. * - When vhci_hcd receives RET_SUBMIT,
  494. *
  495. * - case 1a). the urb of the pdu is not unlinking.
  496. * - normal case
  497. * => just give back the urb
  498. *
  499. * - case 1b). the urb of the pdu is unlinking.
  500. * - usbip.ko will return a reply of the unlinking request.
  501. * => give back the urb now and go to case 2b).
  502. *
  503. * - When vhci_hcd receives RET_UNLINK,
  504. *
  505. * - case 2a). a submit request is still pending in vhci_hcd.
  506. * - urb was really pending in usbip.ko and urb_unlink_urb() was
  507. * completed there.
  508. * => free a pending submit request
  509. * => notify unlink completeness by giving back the urb
  510. *
  511. * - case 2b). a submit request is *not* pending in vhci_hcd.
  512. * - urb was already given back to the core driver.
  513. * => do not give back the urb
  514. *
  515. *
  516. * SERVER SIDE
  517. *
  518. * - When usbip receives CMD_UNLINK,
  519. *
  520. * - case 3a). the urb of the unlink request is now in submission.
  521. * => do usb_unlink_urb().
  522. * => after the unlink is completed, send RET_UNLINK.
  523. *
  524. * - case 3b). the urb of the unlink request is not in submission.
  525. * - may be already completed or never be received
  526. * => send RET_UNLINK
  527. *
  528. */
  529. static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  530. {
  531. struct vhci_priv *priv;
  532. struct vhci_device *vdev;
  533. pr_info("dequeue a urb %p\n", urb);
  534. spin_lock(&the_controller->lock);
  535. priv = urb->hcpriv;
  536. if (!priv) {
  537. /* URB was never linked! or will be soon given back by
  538. * vhci_rx. */
  539. spin_unlock(&the_controller->lock);
  540. return 0;
  541. }
  542. {
  543. int ret = 0;
  544. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  545. if (ret) {
  546. spin_unlock(&the_controller->lock);
  547. return ret;
  548. }
  549. }
  550. /* send unlink request here? */
  551. vdev = priv->vdev;
  552. if (!vdev->ud.tcp_socket) {
  553. /* tcp connection is closed */
  554. spin_lock(&vdev->priv_lock);
  555. pr_info("device %p seems to be disconnected\n", vdev);
  556. list_del(&priv->list);
  557. kfree(priv);
  558. urb->hcpriv = NULL;
  559. spin_unlock(&vdev->priv_lock);
  560. /*
  561. * If tcp connection is alive, we have sent CMD_UNLINK.
  562. * vhci_rx will receive RET_UNLINK and give back the URB.
  563. * Otherwise, we give back it here.
  564. */
  565. pr_info("gives back urb %p\n", urb);
  566. usb_hcd_unlink_urb_from_ep(hcd, urb);
  567. spin_unlock(&the_controller->lock);
  568. usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
  569. urb->status);
  570. spin_lock(&the_controller->lock);
  571. } else {
  572. /* tcp connection is alive */
  573. struct vhci_unlink *unlink;
  574. spin_lock(&vdev->priv_lock);
  575. /* setup CMD_UNLINK pdu */
  576. unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
  577. if (!unlink) {
  578. spin_unlock(&vdev->priv_lock);
  579. spin_unlock(&the_controller->lock);
  580. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  581. return -ENOMEM;
  582. }
  583. unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
  584. if (unlink->seqnum == 0xffff)
  585. pr_info("seqnum max\n");
  586. unlink->unlink_seqnum = priv->seqnum;
  587. pr_info("device %p seems to be still connected\n", vdev);
  588. /* send cmd_unlink and try to cancel the pending URB in the
  589. * peer */
  590. list_add_tail(&unlink->list, &vdev->unlink_tx);
  591. wake_up(&vdev->waitq_tx);
  592. spin_unlock(&vdev->priv_lock);
  593. }
  594. spin_unlock(&the_controller->lock);
  595. usbip_dbg_vhci_hc("leave\n");
  596. return 0;
  597. }
  598. static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
  599. {
  600. struct vhci_unlink *unlink, *tmp;
  601. spin_lock(&the_controller->lock);
  602. spin_lock(&vdev->priv_lock);
  603. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
  604. pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
  605. list_del(&unlink->list);
  606. kfree(unlink);
  607. }
  608. while (!list_empty(&vdev->unlink_rx)) {
  609. struct urb *urb;
  610. unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
  611. list);
  612. /* give back URB of unanswered unlink request */
  613. pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
  614. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  615. if (!urb) {
  616. pr_info("the urb (seqnum %lu) was already given back\n",
  617. unlink->unlink_seqnum);
  618. list_del(&unlink->list);
  619. kfree(unlink);
  620. continue;
  621. }
  622. urb->status = -ENODEV;
  623. usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
  624. list_del(&unlink->list);
  625. spin_unlock(&vdev->priv_lock);
  626. spin_unlock(&the_controller->lock);
  627. usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
  628. urb->status);
  629. spin_lock(&the_controller->lock);
  630. spin_lock(&vdev->priv_lock);
  631. kfree(unlink);
  632. }
  633. spin_unlock(&vdev->priv_lock);
  634. spin_unlock(&the_controller->lock);
  635. }
  636. /*
  637. * The important thing is that only one context begins cleanup.
  638. * This is why error handling and cleanup become simple.
  639. * We do not want to consider race condition as possible.
  640. */
  641. static void vhci_shutdown_connection(struct usbip_device *ud)
  642. {
  643. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  644. /* need this? see stub_dev.c */
  645. if (ud->tcp_socket) {
  646. pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
  647. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  648. }
  649. /* kill threads related to this sdev */
  650. if (vdev->ud.tcp_rx) {
  651. kthread_stop_put(vdev->ud.tcp_rx);
  652. vdev->ud.tcp_rx = NULL;
  653. }
  654. if (vdev->ud.tcp_tx) {
  655. kthread_stop_put(vdev->ud.tcp_tx);
  656. vdev->ud.tcp_tx = NULL;
  657. }
  658. pr_info("stop threads\n");
  659. /* active connection is closed */
  660. if (vdev->ud.tcp_socket) {
  661. sockfd_put(vdev->ud.tcp_socket);
  662. vdev->ud.tcp_socket = NULL;
  663. }
  664. pr_info("release socket\n");
  665. vhci_device_unlink_cleanup(vdev);
  666. /*
  667. * rh_port_disconnect() is a trigger of ...
  668. * usb_disable_device():
  669. * disable all the endpoints for a USB device.
  670. * usb_disable_endpoint():
  671. * disable endpoints. pending urbs are unlinked(dequeued).
  672. *
  673. * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
  674. * detached device should release used urbs in a cleanup function (i.e.
  675. * xxx_disconnect()). Therefore, vhci_hcd does not need to release
  676. * pushed urbs and their private data in this function.
  677. *
  678. * NOTE: vhci_dequeue() must be considered carefully. When shutting down
  679. * a connection, vhci_shutdown_connection() expects vhci_dequeue()
  680. * gives back pushed urbs and frees their private data by request of
  681. * the cleanup function of a USB driver. When unlinking a urb with an
  682. * active connection, vhci_dequeue() does not give back the urb which
  683. * is actually given back by vhci_rx after receiving its return pdu.
  684. *
  685. */
  686. rh_port_disconnect(vdev->rhport);
  687. pr_info("disconnect device\n");
  688. }
  689. static void vhci_device_reset(struct usbip_device *ud)
  690. {
  691. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  692. spin_lock(&ud->lock);
  693. vdev->speed = 0;
  694. vdev->devid = 0;
  695. usb_put_dev(vdev->udev);
  696. vdev->udev = NULL;
  697. if (ud->tcp_socket) {
  698. sockfd_put(ud->tcp_socket);
  699. ud->tcp_socket = NULL;
  700. }
  701. ud->status = VDEV_ST_NULL;
  702. spin_unlock(&ud->lock);
  703. }
  704. static void vhci_device_unusable(struct usbip_device *ud)
  705. {
  706. spin_lock(&ud->lock);
  707. ud->status = VDEV_ST_ERROR;
  708. spin_unlock(&ud->lock);
  709. }
  710. static void vhci_device_init(struct vhci_device *vdev)
  711. {
  712. memset(vdev, 0, sizeof(*vdev));
  713. vdev->ud.side = USBIP_VHCI;
  714. vdev->ud.status = VDEV_ST_NULL;
  715. spin_lock_init(&vdev->ud.lock);
  716. INIT_LIST_HEAD(&vdev->priv_rx);
  717. INIT_LIST_HEAD(&vdev->priv_tx);
  718. INIT_LIST_HEAD(&vdev->unlink_tx);
  719. INIT_LIST_HEAD(&vdev->unlink_rx);
  720. spin_lock_init(&vdev->priv_lock);
  721. init_waitqueue_head(&vdev->waitq_tx);
  722. vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
  723. vdev->ud.eh_ops.reset = vhci_device_reset;
  724. vdev->ud.eh_ops.unusable = vhci_device_unusable;
  725. usbip_start_eh(&vdev->ud);
  726. }
  727. static int vhci_start(struct usb_hcd *hcd)
  728. {
  729. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  730. int rhport;
  731. int err = 0;
  732. usbip_dbg_vhci_hc("enter vhci_start\n");
  733. /* initialize private data of usb_hcd */
  734. for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
  735. struct vhci_device *vdev = &vhci->vdev[rhport];
  736. vhci_device_init(vdev);
  737. vdev->rhport = rhport;
  738. }
  739. atomic_set(&vhci->seqnum, 0);
  740. spin_lock_init(&vhci->lock);
  741. hcd->power_budget = 0; /* no limit */
  742. hcd->uses_new_polling = 1;
  743. /* vhci_hcd is now ready to be controlled through sysfs */
  744. err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
  745. if (err) {
  746. pr_err("create sysfs files\n");
  747. return err;
  748. }
  749. return 0;
  750. }
  751. static void vhci_stop(struct usb_hcd *hcd)
  752. {
  753. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  754. int rhport = 0;
  755. usbip_dbg_vhci_hc("stop VHCI controller\n");
  756. /* 1. remove the userland interface of vhci_hcd */
  757. sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
  758. /* 2. shutdown all the ports of vhci_hcd */
  759. for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
  760. struct vhci_device *vdev = &vhci->vdev[rhport];
  761. usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
  762. usbip_stop_eh(&vdev->ud);
  763. }
  764. }
  765. static int vhci_get_frame_number(struct usb_hcd *hcd)
  766. {
  767. pr_err("Not yet implemented\n");
  768. return 0;
  769. }
  770. #ifdef CONFIG_PM
  771. /* FIXME: suspend/resume */
  772. static int vhci_bus_suspend(struct usb_hcd *hcd)
  773. {
  774. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  775. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  776. spin_lock(&vhci->lock);
  777. hcd->state = HC_STATE_SUSPENDED;
  778. spin_unlock(&vhci->lock);
  779. return 0;
  780. }
  781. static int vhci_bus_resume(struct usb_hcd *hcd)
  782. {
  783. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  784. int rc = 0;
  785. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  786. spin_lock(&vhci->lock);
  787. if (!HCD_HW_ACCESSIBLE(hcd))
  788. rc = -ESHUTDOWN;
  789. else
  790. hcd->state = HC_STATE_RUNNING;
  791. spin_unlock(&vhci->lock);
  792. return rc;
  793. }
  794. #else
  795. #define vhci_bus_suspend NULL
  796. #define vhci_bus_resume NULL
  797. #endif
  798. static struct hc_driver vhci_hc_driver = {
  799. .description = driver_name,
  800. .product_desc = driver_desc,
  801. .hcd_priv_size = sizeof(struct vhci_hcd),
  802. .flags = HCD_USB2,
  803. .start = vhci_start,
  804. .stop = vhci_stop,
  805. .urb_enqueue = vhci_urb_enqueue,
  806. .urb_dequeue = vhci_urb_dequeue,
  807. .get_frame_number = vhci_get_frame_number,
  808. .hub_status_data = vhci_hub_status,
  809. .hub_control = vhci_hub_control,
  810. .bus_suspend = vhci_bus_suspend,
  811. .bus_resume = vhci_bus_resume,
  812. };
  813. static int vhci_hcd_probe(struct platform_device *pdev)
  814. {
  815. struct usb_hcd *hcd;
  816. int ret;
  817. usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
  818. /*
  819. * Allocate and initialize hcd.
  820. * Our private data is also allocated automatically.
  821. */
  822. hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  823. if (!hcd) {
  824. pr_err("create hcd failed\n");
  825. return -ENOMEM;
  826. }
  827. hcd->has_tt = 1;
  828. /* this is private data for vhci_hcd */
  829. the_controller = hcd_to_vhci(hcd);
  830. /*
  831. * Finish generic HCD structure initialization and register.
  832. * Call the driver's reset() and start() routines.
  833. */
  834. ret = usb_add_hcd(hcd, 0, 0);
  835. if (ret != 0) {
  836. pr_err("usb_add_hcd failed %d\n", ret);
  837. usb_put_hcd(hcd);
  838. the_controller = NULL;
  839. return ret;
  840. }
  841. usbip_dbg_vhci_hc("bye\n");
  842. return 0;
  843. }
  844. static int vhci_hcd_remove(struct platform_device *pdev)
  845. {
  846. struct usb_hcd *hcd;
  847. hcd = platform_get_drvdata(pdev);
  848. if (!hcd)
  849. return 0;
  850. /*
  851. * Disconnects the root hub,
  852. * then reverses the effects of usb_add_hcd(),
  853. * invoking the HCD's stop() methods.
  854. */
  855. usb_remove_hcd(hcd);
  856. usb_put_hcd(hcd);
  857. the_controller = NULL;
  858. return 0;
  859. }
  860. #ifdef CONFIG_PM
  861. /* what should happen for USB/IP under suspend/resume? */
  862. static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  863. {
  864. struct usb_hcd *hcd;
  865. int rhport = 0;
  866. int connected = 0;
  867. int ret = 0;
  868. hcd = platform_get_drvdata(pdev);
  869. spin_lock(&the_controller->lock);
  870. for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
  871. if (the_controller->port_status[rhport] &
  872. USB_PORT_STAT_CONNECTION)
  873. connected += 1;
  874. spin_unlock(&the_controller->lock);
  875. if (connected > 0) {
  876. dev_info(&pdev->dev,
  877. "We have %d active connection%s. Do not suspend.\n",
  878. connected, (connected == 1 ? "" : "s"));
  879. ret = -EBUSY;
  880. } else {
  881. dev_info(&pdev->dev, "suspend vhci_hcd");
  882. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  883. }
  884. return ret;
  885. }
  886. static int vhci_hcd_resume(struct platform_device *pdev)
  887. {
  888. struct usb_hcd *hcd;
  889. dev_dbg(&pdev->dev, "%s\n", __func__);
  890. hcd = platform_get_drvdata(pdev);
  891. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  892. usb_hcd_poll_rh_status(hcd);
  893. return 0;
  894. }
  895. #else
  896. #define vhci_hcd_suspend NULL
  897. #define vhci_hcd_resume NULL
  898. #endif
  899. static struct platform_driver vhci_driver = {
  900. .probe = vhci_hcd_probe,
  901. .remove = vhci_hcd_remove,
  902. .suspend = vhci_hcd_suspend,
  903. .resume = vhci_hcd_resume,
  904. .driver = {
  905. .name = driver_name,
  906. },
  907. };
  908. /*
  909. * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
  910. * We need to add this virtual device as a platform device arbitrarily:
  911. * 1. platform_device_register()
  912. */
  913. static void the_pdev_release(struct device *dev)
  914. {
  915. }
  916. static struct platform_device the_pdev = {
  917. /* should be the same name as driver_name */
  918. .name = driver_name,
  919. .id = -1,
  920. .dev = {
  921. .release = the_pdev_release,
  922. },
  923. };
  924. static int __init vhci_hcd_init(void)
  925. {
  926. int ret;
  927. if (usb_disabled())
  928. return -ENODEV;
  929. ret = platform_driver_register(&vhci_driver);
  930. if (ret)
  931. goto err_driver_register;
  932. ret = platform_device_register(&the_pdev);
  933. if (ret)
  934. goto err_platform_device_register;
  935. pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
  936. return ret;
  937. err_platform_device_register:
  938. platform_driver_unregister(&vhci_driver);
  939. err_driver_register:
  940. return ret;
  941. }
  942. static void __exit vhci_hcd_exit(void)
  943. {
  944. platform_device_unregister(&the_pdev);
  945. platform_driver_unregister(&vhci_driver);
  946. }
  947. module_init(vhci_hcd_init);
  948. module_exit(vhci_hcd_exit);
  949. MODULE_AUTHOR(DRIVER_AUTHOR);
  950. MODULE_DESCRIPTION(DRIVER_DESC);
  951. MODULE_LICENSE("GPL");
  952. MODULE_VERSION(USBIP_VERSION);