vhci_hcd.c 30 KB

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