vhci_hcd.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. int width;
  199. memset(desc, 0, sizeof(*desc));
  200. desc->bDescriptorType = USB_DT_HUB;
  201. desc->wHubCharacteristics = cpu_to_le16(
  202. HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
  203. desc->bNbrPorts = VHCI_HC_PORTS;
  204. BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
  205. width = desc->bNbrPorts / 8 + 1;
  206. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
  207. memset(&desc->u.hs.DeviceRemovable[0], 0, width);
  208. memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
  209. }
  210. static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  211. u16 wIndex, char *buf, u16 wLength)
  212. {
  213. struct vhci_hcd *dum;
  214. int retval = 0;
  215. int rhport;
  216. unsigned long flags;
  217. u32 prev_port_status[VHCI_HC_PORTS];
  218. if (!HCD_HW_ACCESSIBLE(hcd))
  219. return -ETIMEDOUT;
  220. /*
  221. * NOTE:
  222. * wIndex shows the port number and begins from 1.
  223. */
  224. usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
  225. wIndex);
  226. if (wIndex > VHCI_HC_PORTS)
  227. pr_err("invalid port number %d\n", wIndex);
  228. rhport = ((__u8)(wIndex & 0x00ff)) - 1;
  229. dum = hcd_to_vhci(hcd);
  230. spin_lock_irqsave(&dum->lock, flags);
  231. /* store old status and compare now and old later */
  232. if (usbip_dbg_flag_vhci_rh) {
  233. memcpy(prev_port_status, dum->port_status,
  234. sizeof(prev_port_status));
  235. }
  236. switch (typeReq) {
  237. case ClearHubFeature:
  238. usbip_dbg_vhci_rh(" ClearHubFeature\n");
  239. break;
  240. case ClearPortFeature:
  241. switch (wValue) {
  242. case USB_PORT_FEAT_SUSPEND:
  243. if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
  244. /* 20msec signaling */
  245. dum->resuming = 1;
  246. dum->re_timeout =
  247. jiffies + msecs_to_jiffies(20);
  248. }
  249. break;
  250. case USB_PORT_FEAT_POWER:
  251. usbip_dbg_vhci_rh(
  252. " ClearPortFeature: USB_PORT_FEAT_POWER\n");
  253. dum->port_status[rhport] &= ~USB_PORT_STAT_POWER;
  254. dum->resuming = 0;
  255. break;
  256. case USB_PORT_FEAT_C_RESET:
  257. usbip_dbg_vhci_rh(
  258. " ClearPortFeature: USB_PORT_FEAT_C_RESET\n");
  259. switch (dum->vdev[rhport].speed) {
  260. case USB_SPEED_HIGH:
  261. dum->port_status[rhport] |=
  262. USB_PORT_STAT_HIGH_SPEED;
  263. break;
  264. case USB_SPEED_LOW:
  265. dum->port_status[rhport] |=
  266. USB_PORT_STAT_LOW_SPEED;
  267. break;
  268. default:
  269. break;
  270. }
  271. default:
  272. usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
  273. wValue);
  274. dum->port_status[rhport] &= ~(1 << wValue);
  275. break;
  276. }
  277. break;
  278. case GetHubDescriptor:
  279. usbip_dbg_vhci_rh(" GetHubDescriptor\n");
  280. hub_descriptor((struct usb_hub_descriptor *) buf);
  281. break;
  282. case GetHubStatus:
  283. usbip_dbg_vhci_rh(" GetHubStatus\n");
  284. *(__le32 *) buf = cpu_to_le32(0);
  285. break;
  286. case GetPortStatus:
  287. usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
  288. if (wIndex > VHCI_HC_PORTS || wIndex < 1) {
  289. pr_err("invalid port number %d\n", wIndex);
  290. retval = -EPIPE;
  291. }
  292. /* we do not care about resume. */
  293. /* whoever resets or resumes must GetPortStatus to
  294. * complete it!!
  295. */
  296. if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
  297. dum->port_status[rhport] |=
  298. (1 << USB_PORT_FEAT_C_SUSPEND);
  299. dum->port_status[rhport] &=
  300. ~(1 << USB_PORT_FEAT_SUSPEND);
  301. dum->resuming = 0;
  302. dum->re_timeout = 0;
  303. }
  304. if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
  305. 0 && time_after(jiffies, dum->re_timeout)) {
  306. dum->port_status[rhport] |=
  307. (1 << USB_PORT_FEAT_C_RESET);
  308. dum->port_status[rhport] &=
  309. ~(1 << USB_PORT_FEAT_RESET);
  310. dum->re_timeout = 0;
  311. if (dum->vdev[rhport].ud.status ==
  312. VDEV_ST_NOTASSIGNED) {
  313. usbip_dbg_vhci_rh(
  314. " enable rhport %d (status %u)\n",
  315. rhport,
  316. dum->vdev[rhport].ud.status);
  317. dum->port_status[rhport] |=
  318. USB_PORT_STAT_ENABLE;
  319. }
  320. }
  321. ((__le16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
  322. ((__le16 *) buf)[1] =
  323. cpu_to_le16(dum->port_status[rhport] >> 16);
  324. usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
  325. ((u16 *)buf)[1]);
  326. break;
  327. case SetHubFeature:
  328. usbip_dbg_vhci_rh(" SetHubFeature\n");
  329. retval = -EPIPE;
  330. break;
  331. case SetPortFeature:
  332. switch (wValue) {
  333. case USB_PORT_FEAT_SUSPEND:
  334. usbip_dbg_vhci_rh(
  335. " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
  336. break;
  337. case USB_PORT_FEAT_RESET:
  338. usbip_dbg_vhci_rh(
  339. " SetPortFeature: USB_PORT_FEAT_RESET\n");
  340. /* if it's already running, disconnect first */
  341. if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
  342. dum->port_status[rhport] &=
  343. ~(USB_PORT_STAT_ENABLE |
  344. USB_PORT_STAT_LOW_SPEED |
  345. USB_PORT_STAT_HIGH_SPEED);
  346. /* FIXME test that code path! */
  347. }
  348. /* 50msec reset signaling */
  349. dum->re_timeout = jiffies + msecs_to_jiffies(50);
  350. /* FALLTHROUGH */
  351. default:
  352. usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
  353. wValue);
  354. dum->port_status[rhport] |= (1 << wValue);
  355. break;
  356. }
  357. break;
  358. default:
  359. pr_err("default: no such request\n");
  360. /* "protocol stall" on error */
  361. retval = -EPIPE;
  362. }
  363. if (usbip_dbg_flag_vhci_rh) {
  364. pr_debug("port %d\n", rhport);
  365. /* Only dump valid port status */
  366. if (rhport >= 0) {
  367. dump_port_status_diff(prev_port_status[rhport],
  368. dum->port_status[rhport]);
  369. }
  370. }
  371. usbip_dbg_vhci_rh(" bye\n");
  372. spin_unlock_irqrestore(&dum->lock, flags);
  373. return retval;
  374. }
  375. static struct vhci_device *get_vdev(struct usb_device *udev)
  376. {
  377. struct platform_device *pdev;
  378. struct usb_hcd *hcd;
  379. struct vhci_hcd *vhci;
  380. int pdev_nr, rhport;
  381. if (!udev)
  382. return NULL;
  383. for (pdev_nr = 0; pdev_nr < vhci_num_controllers; pdev_nr++) {
  384. pdev = *(vhci_pdevs + pdev_nr);
  385. if (pdev == NULL)
  386. continue;
  387. hcd = platform_get_drvdata(pdev);
  388. if (hcd == NULL)
  389. continue;
  390. vhci = hcd_to_vhci(hcd);
  391. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  392. if (vhci->vdev[rhport].udev == udev)
  393. return &vhci->vdev[rhport];
  394. }
  395. }
  396. return NULL;
  397. }
  398. static void vhci_tx_urb(struct urb *urb)
  399. {
  400. struct vhci_device *vdev = get_vdev(urb->dev);
  401. struct vhci_priv *priv;
  402. struct vhci_hcd *vhci = vdev_to_vhci(vdev);
  403. unsigned long flags;
  404. if (!vdev) {
  405. pr_err("could not get virtual device");
  406. return;
  407. }
  408. priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
  409. if (!priv) {
  410. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  411. return;
  412. }
  413. spin_lock_irqsave(&vdev->priv_lock, flags);
  414. priv->seqnum = atomic_inc_return(&vhci->seqnum);
  415. if (priv->seqnum == 0xffff)
  416. dev_info(&urb->dev->dev, "seqnum max\n");
  417. priv->vdev = vdev;
  418. priv->urb = urb;
  419. urb->hcpriv = (void *) priv;
  420. list_add_tail(&priv->list, &vdev->priv_tx);
  421. wake_up(&vdev->waitq_tx);
  422. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  423. }
  424. static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  425. gfp_t mem_flags)
  426. {
  427. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  428. struct device *dev = &urb->dev->dev;
  429. u8 portnum = urb->dev->portnum;
  430. int ret = 0;
  431. struct vhci_device *vdev;
  432. unsigned long 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. spin_lock_irqsave(&vhci->lock, flags);
  575. priv = urb->hcpriv;
  576. if (!priv) {
  577. /* URB was never linked! or will be soon given back by
  578. * vhci_rx. */
  579. spin_unlock_irqrestore(&vhci->lock, flags);
  580. return -EIDRM;
  581. }
  582. {
  583. int ret = 0;
  584. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  585. if (ret) {
  586. spin_unlock_irqrestore(&vhci->lock, flags);
  587. return ret;
  588. }
  589. }
  590. /* send unlink request here? */
  591. vdev = priv->vdev;
  592. if (!vdev->ud.tcp_socket) {
  593. /* tcp connection is closed */
  594. spin_lock(&vdev->priv_lock);
  595. list_del(&priv->list);
  596. kfree(priv);
  597. urb->hcpriv = NULL;
  598. spin_unlock(&vdev->priv_lock);
  599. /*
  600. * If tcp connection is alive, we have sent CMD_UNLINK.
  601. * vhci_rx will receive RET_UNLINK and give back the URB.
  602. * Otherwise, we give back it here.
  603. */
  604. usb_hcd_unlink_urb_from_ep(hcd, urb);
  605. spin_unlock_irqrestore(&vhci->lock, flags);
  606. usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
  607. spin_lock_irqsave(&vhci->lock, flags);
  608. } else {
  609. /* tcp connection is alive */
  610. struct vhci_unlink *unlink;
  611. spin_lock(&vdev->priv_lock);
  612. /* setup CMD_UNLINK pdu */
  613. unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
  614. if (!unlink) {
  615. spin_unlock(&vdev->priv_lock);
  616. spin_unlock_irqrestore(&vhci->lock, flags);
  617. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  618. return -ENOMEM;
  619. }
  620. unlink->seqnum = atomic_inc_return(&vhci->seqnum);
  621. if (unlink->seqnum == 0xffff)
  622. pr_info("seqnum max\n");
  623. unlink->unlink_seqnum = priv->seqnum;
  624. /* send cmd_unlink and try to cancel the pending URB in the
  625. * peer */
  626. list_add_tail(&unlink->list, &vdev->unlink_tx);
  627. wake_up(&vdev->waitq_tx);
  628. spin_unlock(&vdev->priv_lock);
  629. }
  630. spin_unlock_irqrestore(&vhci->lock, flags);
  631. usbip_dbg_vhci_hc("leave\n");
  632. return 0;
  633. }
  634. static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
  635. {
  636. struct vhci_hcd *vhci = vdev_to_vhci(vdev);
  637. struct usb_hcd *hcd = vhci_to_hcd(vhci);
  638. struct vhci_unlink *unlink, *tmp;
  639. unsigned long flags;
  640. spin_lock_irqsave(&vhci->lock, flags);
  641. spin_lock(&vdev->priv_lock);
  642. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
  643. pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
  644. list_del(&unlink->list);
  645. kfree(unlink);
  646. }
  647. while (!list_empty(&vdev->unlink_rx)) {
  648. struct urb *urb;
  649. unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
  650. list);
  651. /* give back URB of unanswered unlink request */
  652. pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
  653. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  654. if (!urb) {
  655. pr_info("the urb (seqnum %lu) was already given back\n",
  656. unlink->unlink_seqnum);
  657. list_del(&unlink->list);
  658. kfree(unlink);
  659. continue;
  660. }
  661. urb->status = -ENODEV;
  662. usb_hcd_unlink_urb_from_ep(hcd, urb);
  663. list_del(&unlink->list);
  664. spin_unlock(&vdev->priv_lock);
  665. spin_unlock_irqrestore(&vhci->lock, flags);
  666. usb_hcd_giveback_urb(hcd, urb, urb->status);
  667. spin_lock_irqsave(&vhci->lock, flags);
  668. spin_lock(&vdev->priv_lock);
  669. kfree(unlink);
  670. }
  671. spin_unlock(&vdev->priv_lock);
  672. spin_unlock_irqrestore(&vhci->lock, flags);
  673. }
  674. /*
  675. * The important thing is that only one context begins cleanup.
  676. * This is why error handling and cleanup become simple.
  677. * We do not want to consider race condition as possible.
  678. */
  679. static void vhci_shutdown_connection(struct usbip_device *ud)
  680. {
  681. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  682. /* need this? see stub_dev.c */
  683. if (ud->tcp_socket) {
  684. pr_debug("shutdown tcp_socket\n");
  685. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  686. }
  687. /* kill threads related to this sdev */
  688. if (vdev->ud.tcp_rx) {
  689. kthread_stop_put(vdev->ud.tcp_rx);
  690. vdev->ud.tcp_rx = NULL;
  691. }
  692. if (vdev->ud.tcp_tx) {
  693. kthread_stop_put(vdev->ud.tcp_tx);
  694. vdev->ud.tcp_tx = NULL;
  695. }
  696. pr_info("stop threads\n");
  697. /* active connection is closed */
  698. if (vdev->ud.tcp_socket) {
  699. sockfd_put(vdev->ud.tcp_socket);
  700. vdev->ud.tcp_socket = NULL;
  701. vdev->ud.sockfd = -1;
  702. }
  703. pr_info("release socket\n");
  704. vhci_device_unlink_cleanup(vdev);
  705. /*
  706. * rh_port_disconnect() is a trigger of ...
  707. * usb_disable_device():
  708. * disable all the endpoints for a USB device.
  709. * usb_disable_endpoint():
  710. * disable endpoints. pending urbs are unlinked(dequeued).
  711. *
  712. * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
  713. * detached device should release used urbs in a cleanup function (i.e.
  714. * xxx_disconnect()). Therefore, vhci_hcd does not need to release
  715. * pushed urbs and their private data in this function.
  716. *
  717. * NOTE: vhci_dequeue() must be considered carefully. When shutting down
  718. * a connection, vhci_shutdown_connection() expects vhci_dequeue()
  719. * gives back pushed urbs and frees their private data by request of
  720. * the cleanup function of a USB driver. When unlinking a urb with an
  721. * active connection, vhci_dequeue() does not give back the urb which
  722. * is actually given back by vhci_rx after receiving its return pdu.
  723. *
  724. */
  725. rh_port_disconnect(vdev);
  726. pr_info("disconnect device\n");
  727. }
  728. static void vhci_device_reset(struct usbip_device *ud)
  729. {
  730. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  731. unsigned long flags;
  732. spin_lock_irqsave(&ud->lock, flags);
  733. vdev->speed = 0;
  734. vdev->devid = 0;
  735. usb_put_dev(vdev->udev);
  736. vdev->udev = NULL;
  737. if (ud->tcp_socket) {
  738. sockfd_put(ud->tcp_socket);
  739. ud->tcp_socket = NULL;
  740. ud->sockfd = -1;
  741. }
  742. ud->status = VDEV_ST_NULL;
  743. spin_unlock_irqrestore(&ud->lock, flags);
  744. }
  745. static void vhci_device_unusable(struct usbip_device *ud)
  746. {
  747. unsigned long flags;
  748. spin_lock_irqsave(&ud->lock, flags);
  749. ud->status = VDEV_ST_ERROR;
  750. spin_unlock_irqrestore(&ud->lock, flags);
  751. }
  752. static void vhci_device_init(struct vhci_device *vdev)
  753. {
  754. memset(vdev, 0, sizeof(struct vhci_device));
  755. vdev->ud.side = USBIP_VHCI;
  756. vdev->ud.status = VDEV_ST_NULL;
  757. spin_lock_init(&vdev->ud.lock);
  758. INIT_LIST_HEAD(&vdev->priv_rx);
  759. INIT_LIST_HEAD(&vdev->priv_tx);
  760. INIT_LIST_HEAD(&vdev->unlink_tx);
  761. INIT_LIST_HEAD(&vdev->unlink_rx);
  762. spin_lock_init(&vdev->priv_lock);
  763. init_waitqueue_head(&vdev->waitq_tx);
  764. vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
  765. vdev->ud.eh_ops.reset = vhci_device_reset;
  766. vdev->ud.eh_ops.unusable = vhci_device_unusable;
  767. usbip_start_eh(&vdev->ud);
  768. }
  769. static int hcd_name_to_id(const char *name)
  770. {
  771. char *c;
  772. long val;
  773. int ret;
  774. c = strchr(name, '.');
  775. if (c == NULL)
  776. return 0;
  777. ret = kstrtol(c+1, 10, &val);
  778. if (ret < 0)
  779. return ret;
  780. return val;
  781. }
  782. static int vhci_start(struct usb_hcd *hcd)
  783. {
  784. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  785. int id, rhport;
  786. int err = 0;
  787. usbip_dbg_vhci_hc("enter vhci_start\n");
  788. /* initialize private data of usb_hcd */
  789. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  790. struct vhci_device *vdev = &vhci->vdev[rhport];
  791. vhci_device_init(vdev);
  792. vdev->rhport = rhport;
  793. }
  794. atomic_set(&vhci->seqnum, 0);
  795. spin_lock_init(&vhci->lock);
  796. hcd->power_budget = 0; /* no limit */
  797. hcd->uses_new_polling = 1;
  798. id = hcd_name_to_id(hcd_name(hcd));
  799. if (id < 0) {
  800. pr_err("invalid vhci name %s\n", hcd_name(hcd));
  801. return -EINVAL;
  802. }
  803. /* vhci_hcd is now ready to be controlled through sysfs */
  804. if (id == 0) {
  805. err = vhci_init_attr_group();
  806. if (err) {
  807. pr_err("init attr group\n");
  808. return err;
  809. }
  810. err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  811. if (err) {
  812. pr_err("create sysfs files\n");
  813. vhci_finish_attr_group();
  814. return err;
  815. }
  816. pr_info("created sysfs %s\n", hcd_name(hcd));
  817. }
  818. return 0;
  819. }
  820. static void vhci_stop(struct usb_hcd *hcd)
  821. {
  822. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  823. int id, rhport;
  824. usbip_dbg_vhci_hc("stop VHCI controller\n");
  825. /* 1. remove the userland interface of vhci_hcd */
  826. id = hcd_name_to_id(hcd_name(hcd));
  827. if (id == 0) {
  828. sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  829. vhci_finish_attr_group();
  830. }
  831. /* 2. shutdown all the ports of vhci_hcd */
  832. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  833. struct vhci_device *vdev = &vhci->vdev[rhport];
  834. usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
  835. usbip_stop_eh(&vdev->ud);
  836. }
  837. }
  838. static int vhci_get_frame_number(struct usb_hcd *hcd)
  839. {
  840. dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
  841. return 0;
  842. }
  843. #ifdef CONFIG_PM
  844. /* FIXME: suspend/resume */
  845. static int vhci_bus_suspend(struct usb_hcd *hcd)
  846. {
  847. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  848. unsigned long flags;
  849. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  850. spin_lock_irqsave(&vhci->lock, flags);
  851. hcd->state = HC_STATE_SUSPENDED;
  852. spin_unlock_irqrestore(&vhci->lock, flags);
  853. return 0;
  854. }
  855. static int vhci_bus_resume(struct usb_hcd *hcd)
  856. {
  857. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  858. int rc = 0;
  859. unsigned long flags;
  860. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  861. spin_lock_irqsave(&vhci->lock, flags);
  862. if (!HCD_HW_ACCESSIBLE(hcd))
  863. rc = -ESHUTDOWN;
  864. else
  865. hcd->state = HC_STATE_RUNNING;
  866. spin_unlock_irqrestore(&vhci->lock, flags);
  867. return rc;
  868. }
  869. #else
  870. #define vhci_bus_suspend NULL
  871. #define vhci_bus_resume NULL
  872. #endif
  873. static struct hc_driver vhci_hc_driver = {
  874. .description = driver_name,
  875. .product_desc = driver_desc,
  876. .hcd_priv_size = sizeof(struct vhci_hcd),
  877. .flags = HCD_USB2,
  878. .start = vhci_start,
  879. .stop = vhci_stop,
  880. .urb_enqueue = vhci_urb_enqueue,
  881. .urb_dequeue = vhci_urb_dequeue,
  882. .get_frame_number = vhci_get_frame_number,
  883. .hub_status_data = vhci_hub_status,
  884. .hub_control = vhci_hub_control,
  885. .bus_suspend = vhci_bus_suspend,
  886. .bus_resume = vhci_bus_resume,
  887. };
  888. static int vhci_hcd_probe(struct platform_device *pdev)
  889. {
  890. struct usb_hcd *hcd;
  891. int ret;
  892. usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
  893. /*
  894. * Allocate and initialize hcd.
  895. * Our private data is also allocated automatically.
  896. */
  897. hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  898. if (!hcd) {
  899. pr_err("create hcd failed\n");
  900. return -ENOMEM;
  901. }
  902. hcd->has_tt = 1;
  903. /*
  904. * Finish generic HCD structure initialization and register.
  905. * Call the driver's reset() and start() routines.
  906. */
  907. ret = usb_add_hcd(hcd, 0, 0);
  908. if (ret != 0) {
  909. pr_err("usb_add_hcd failed %d\n", ret);
  910. usb_put_hcd(hcd);
  911. return ret;
  912. }
  913. usbip_dbg_vhci_hc("bye\n");
  914. return 0;
  915. }
  916. static int vhci_hcd_remove(struct platform_device *pdev)
  917. {
  918. struct usb_hcd *hcd;
  919. hcd = platform_get_drvdata(pdev);
  920. if (!hcd)
  921. return 0;
  922. /*
  923. * Disconnects the root hub,
  924. * then reverses the effects of usb_add_hcd(),
  925. * invoking the HCD's stop() methods.
  926. */
  927. usb_remove_hcd(hcd);
  928. usb_put_hcd(hcd);
  929. return 0;
  930. }
  931. #ifdef CONFIG_PM
  932. /* what should happen for USB/IP under suspend/resume? */
  933. static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  934. {
  935. struct usb_hcd *hcd;
  936. struct vhci_hcd *vhci;
  937. int rhport;
  938. int connected = 0;
  939. int ret = 0;
  940. unsigned long flags;
  941. hcd = platform_get_drvdata(pdev);
  942. if (!hcd)
  943. return 0;
  944. vhci = hcd_to_vhci(hcd);
  945. spin_lock_irqsave(&vhci->lock, flags);
  946. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++)
  947. if (vhci->port_status[rhport] & USB_PORT_STAT_CONNECTION)
  948. connected += 1;
  949. spin_unlock_irqrestore(&vhci->lock, flags);
  950. if (connected > 0) {
  951. dev_info(&pdev->dev,
  952. "We have %d active connection%s. Do not suspend.\n",
  953. connected, (connected == 1 ? "" : "s"));
  954. ret = -EBUSY;
  955. } else {
  956. dev_info(&pdev->dev, "suspend vhci_hcd");
  957. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  958. }
  959. return ret;
  960. }
  961. static int vhci_hcd_resume(struct platform_device *pdev)
  962. {
  963. struct usb_hcd *hcd;
  964. dev_dbg(&pdev->dev, "%s\n", __func__);
  965. hcd = platform_get_drvdata(pdev);
  966. if (!hcd)
  967. return 0;
  968. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  969. usb_hcd_poll_rh_status(hcd);
  970. return 0;
  971. }
  972. #else
  973. #define vhci_hcd_suspend NULL
  974. #define vhci_hcd_resume NULL
  975. #endif
  976. static struct platform_driver vhci_driver = {
  977. .probe = vhci_hcd_probe,
  978. .remove = vhci_hcd_remove,
  979. .suspend = vhci_hcd_suspend,
  980. .resume = vhci_hcd_resume,
  981. .driver = {
  982. .name = driver_name,
  983. },
  984. };
  985. static int add_platform_device(int id)
  986. {
  987. struct platform_device *pdev;
  988. int dev_nr;
  989. if (id == 0)
  990. dev_nr = -1;
  991. else
  992. dev_nr = id;
  993. pdev = platform_device_register_simple(driver_name, dev_nr, NULL, 0);
  994. if (IS_ERR(pdev))
  995. return PTR_ERR(pdev);
  996. *(vhci_pdevs + id) = pdev;
  997. return 0;
  998. }
  999. static void del_platform_devices(void)
  1000. {
  1001. struct platform_device *pdev;
  1002. int i;
  1003. for (i = 0; i < vhci_num_controllers; i++) {
  1004. pdev = *(vhci_pdevs + i);
  1005. if (pdev != NULL)
  1006. platform_device_unregister(pdev);
  1007. *(vhci_pdevs + i) = NULL;
  1008. }
  1009. sysfs_remove_link(&platform_bus.kobj, driver_name);
  1010. }
  1011. static int __init vhci_hcd_init(void)
  1012. {
  1013. int i, ret;
  1014. if (usb_disabled())
  1015. return -ENODEV;
  1016. if (vhci_num_controllers < 1)
  1017. vhci_num_controllers = 1;
  1018. vhci_pdevs = kcalloc(vhci_num_controllers, sizeof(void *), GFP_KERNEL);
  1019. if (vhci_pdevs == NULL)
  1020. return -ENOMEM;
  1021. ret = platform_driver_register(&vhci_driver);
  1022. if (ret)
  1023. goto err_driver_register;
  1024. for (i = 0; i < vhci_num_controllers; i++) {
  1025. ret = add_platform_device(i);
  1026. if (ret)
  1027. goto err_platform_device_register;
  1028. }
  1029. pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
  1030. return ret;
  1031. err_platform_device_register:
  1032. del_platform_devices();
  1033. platform_driver_unregister(&vhci_driver);
  1034. err_driver_register:
  1035. kfree(vhci_pdevs);
  1036. return ret;
  1037. }
  1038. static void __exit vhci_hcd_exit(void)
  1039. {
  1040. del_platform_devices();
  1041. platform_driver_unregister(&vhci_driver);
  1042. kfree(vhci_pdevs);
  1043. }
  1044. module_init(vhci_hcd_init);
  1045. module_exit(vhci_hcd_exit);
  1046. MODULE_AUTHOR(DRIVER_AUTHOR);
  1047. MODULE_DESCRIPTION(DRIVER_DESC);
  1048. MODULE_LICENSE("GPL");
  1049. MODULE_VERSION(USBIP_VERSION);