vhci_hcd.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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 = vdev_to_vhci(vdev);
  400. unsigned long flags;
  401. if (!vdev) {
  402. pr_err("could not get virtual device");
  403. return;
  404. }
  405. priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
  406. if (!priv) {
  407. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  408. return;
  409. }
  410. spin_lock_irqsave(&vdev->priv_lock, flags);
  411. priv->seqnum = atomic_inc_return(&vhci->seqnum);
  412. if (priv->seqnum == 0xffff)
  413. dev_info(&urb->dev->dev, "seqnum max\n");
  414. priv->vdev = vdev;
  415. priv->urb = urb;
  416. urb->hcpriv = (void *) priv;
  417. list_add_tail(&priv->list, &vdev->priv_tx);
  418. wake_up(&vdev->waitq_tx);
  419. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  420. }
  421. static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  422. gfp_t mem_flags)
  423. {
  424. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  425. struct device *dev = &urb->dev->dev;
  426. u8 portnum = urb->dev->portnum;
  427. int ret = 0;
  428. struct vhci_device *vdev;
  429. unsigned long flags;
  430. usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
  431. hcd, urb, mem_flags);
  432. if (portnum > VHCI_HC_PORTS) {
  433. pr_err("invalid port number %d\n", portnum);
  434. return -ENODEV;
  435. }
  436. vdev = &vhci->vdev[portnum-1];
  437. /* patch to usb_sg_init() is in 2.5.60 */
  438. BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
  439. spin_lock_irqsave(&vhci->lock, flags);
  440. if (urb->status != -EINPROGRESS) {
  441. dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
  442. spin_unlock_irqrestore(&vhci->lock, flags);
  443. return urb->status;
  444. }
  445. /* refuse enqueue for dead connection */
  446. spin_lock(&vdev->ud.lock);
  447. if (vdev->ud.status == VDEV_ST_NULL ||
  448. vdev->ud.status == VDEV_ST_ERROR) {
  449. dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
  450. spin_unlock(&vdev->ud.lock);
  451. spin_unlock_irqrestore(&vhci->lock, flags);
  452. return -ENODEV;
  453. }
  454. spin_unlock(&vdev->ud.lock);
  455. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  456. if (ret)
  457. goto no_need_unlink;
  458. /*
  459. * The enumeration process is as follows;
  460. *
  461. * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
  462. * to get max packet length of default pipe
  463. *
  464. * 2. Set_Address request to DevAddr(0) EndPoint(0)
  465. *
  466. */
  467. if (usb_pipedevice(urb->pipe) == 0) {
  468. __u8 type = usb_pipetype(urb->pipe);
  469. struct usb_ctrlrequest *ctrlreq =
  470. (struct usb_ctrlrequest *) urb->setup_packet;
  471. if (type != PIPE_CONTROL || !ctrlreq) {
  472. dev_err(dev, "invalid request to devnum 0\n");
  473. ret = -EINVAL;
  474. goto no_need_xmit;
  475. }
  476. switch (ctrlreq->bRequest) {
  477. case USB_REQ_SET_ADDRESS:
  478. /* set_address may come when a device is reset */
  479. dev_info(dev, "SetAddress Request (%d) to port %d\n",
  480. ctrlreq->wValue, vdev->rhport);
  481. usb_put_dev(vdev->udev);
  482. vdev->udev = usb_get_dev(urb->dev);
  483. spin_lock(&vdev->ud.lock);
  484. vdev->ud.status = VDEV_ST_USED;
  485. spin_unlock(&vdev->ud.lock);
  486. if (urb->status == -EINPROGRESS) {
  487. /* This request is successfully completed. */
  488. /* If not -EINPROGRESS, possibly unlinked. */
  489. urb->status = 0;
  490. }
  491. goto no_need_xmit;
  492. case USB_REQ_GET_DESCRIPTOR:
  493. if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
  494. usbip_dbg_vhci_hc(
  495. "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
  496. usb_put_dev(vdev->udev);
  497. vdev->udev = usb_get_dev(urb->dev);
  498. goto out;
  499. default:
  500. /* NOT REACHED */
  501. dev_err(dev,
  502. "invalid request to devnum 0 bRequest %u, wValue %u\n",
  503. ctrlreq->bRequest,
  504. ctrlreq->wValue);
  505. ret = -EINVAL;
  506. goto no_need_xmit;
  507. }
  508. }
  509. out:
  510. vhci_tx_urb(urb);
  511. spin_unlock_irqrestore(&vhci->lock, flags);
  512. return 0;
  513. no_need_xmit:
  514. usb_hcd_unlink_urb_from_ep(hcd, urb);
  515. no_need_unlink:
  516. spin_unlock_irqrestore(&vhci->lock, flags);
  517. if (!ret)
  518. usb_hcd_giveback_urb(hcd, urb, urb->status);
  519. return ret;
  520. }
  521. /*
  522. * vhci_rx gives back the urb after receiving the reply of the urb. If an
  523. * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
  524. * back its urb. For the driver unlinking the urb, the content of the urb is
  525. * not important, but the calling to its completion handler is important; the
  526. * completion of unlinking is notified by the completion handler.
  527. *
  528. *
  529. * CLIENT SIDE
  530. *
  531. * - When vhci_hcd receives RET_SUBMIT,
  532. *
  533. * - case 1a). the urb of the pdu is not unlinking.
  534. * - normal case
  535. * => just give back the urb
  536. *
  537. * - case 1b). the urb of the pdu is unlinking.
  538. * - usbip.ko will return a reply of the unlinking request.
  539. * => give back the urb now and go to case 2b).
  540. *
  541. * - When vhci_hcd receives RET_UNLINK,
  542. *
  543. * - case 2a). a submit request is still pending in vhci_hcd.
  544. * - urb was really pending in usbip.ko and urb_unlink_urb() was
  545. * completed there.
  546. * => free a pending submit request
  547. * => notify unlink completeness by giving back the urb
  548. *
  549. * - case 2b). a submit request is *not* pending in vhci_hcd.
  550. * - urb was already given back to the core driver.
  551. * => do not give back the urb
  552. *
  553. *
  554. * SERVER SIDE
  555. *
  556. * - When usbip receives CMD_UNLINK,
  557. *
  558. * - case 3a). the urb of the unlink request is now in submission.
  559. * => do usb_unlink_urb().
  560. * => after the unlink is completed, send RET_UNLINK.
  561. *
  562. * - case 3b). the urb of the unlink request is not in submission.
  563. * - may be already completed or never be received
  564. * => send RET_UNLINK
  565. *
  566. */
  567. static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  568. {
  569. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  570. struct vhci_priv *priv;
  571. struct vhci_device *vdev;
  572. unsigned long flags;
  573. pr_info("dequeue a urb %p\n", urb);
  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. pr_info("device %p seems to be disconnected\n", vdev);
  596. list_del(&priv->list);
  597. kfree(priv);
  598. urb->hcpriv = NULL;
  599. spin_unlock(&vdev->priv_lock);
  600. /*
  601. * If tcp connection is alive, we have sent CMD_UNLINK.
  602. * vhci_rx will receive RET_UNLINK and give back the URB.
  603. * Otherwise, we give back it here.
  604. */
  605. pr_info("gives back urb %p\n", urb);
  606. usb_hcd_unlink_urb_from_ep(hcd, urb);
  607. spin_unlock_irqrestore(&vhci->lock, flags);
  608. usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
  609. spin_lock_irqsave(&vhci->lock, flags);
  610. } else {
  611. /* tcp connection is alive */
  612. struct vhci_unlink *unlink;
  613. spin_lock(&vdev->priv_lock);
  614. /* setup CMD_UNLINK pdu */
  615. unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
  616. if (!unlink) {
  617. spin_unlock(&vdev->priv_lock);
  618. spin_unlock_irqrestore(&vhci->lock, flags);
  619. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
  620. return -ENOMEM;
  621. }
  622. unlink->seqnum = atomic_inc_return(&vhci->seqnum);
  623. if (unlink->seqnum == 0xffff)
  624. pr_info("seqnum max\n");
  625. unlink->unlink_seqnum = priv->seqnum;
  626. pr_info("device %p seems to be still connected\n", vdev);
  627. /* send cmd_unlink and try to cancel the pending URB in the
  628. * peer */
  629. list_add_tail(&unlink->list, &vdev->unlink_tx);
  630. wake_up(&vdev->waitq_tx);
  631. spin_unlock(&vdev->priv_lock);
  632. }
  633. spin_unlock_irqrestore(&vhci->lock, flags);
  634. usbip_dbg_vhci_hc("leave\n");
  635. return 0;
  636. }
  637. static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
  638. {
  639. struct vhci_hcd *vhci = vdev_to_vhci(vdev);
  640. struct usb_hcd *hcd = vhci_to_hcd(vhci);
  641. struct vhci_unlink *unlink, *tmp;
  642. unsigned long flags;
  643. spin_lock_irqsave(&vhci->lock, flags);
  644. spin_lock(&vdev->priv_lock);
  645. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
  646. pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
  647. list_del(&unlink->list);
  648. kfree(unlink);
  649. }
  650. while (!list_empty(&vdev->unlink_rx)) {
  651. struct urb *urb;
  652. unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
  653. list);
  654. /* give back URB of unanswered unlink request */
  655. pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
  656. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  657. if (!urb) {
  658. pr_info("the urb (seqnum %lu) was already given back\n",
  659. unlink->unlink_seqnum);
  660. list_del(&unlink->list);
  661. kfree(unlink);
  662. continue;
  663. }
  664. urb->status = -ENODEV;
  665. usb_hcd_unlink_urb_from_ep(hcd, urb);
  666. list_del(&unlink->list);
  667. spin_unlock(&vdev->priv_lock);
  668. spin_unlock_irqrestore(&vhci->lock, flags);
  669. usb_hcd_giveback_urb(hcd, urb, urb->status);
  670. spin_lock_irqsave(&vhci->lock, flags);
  671. spin_lock(&vdev->priv_lock);
  672. kfree(unlink);
  673. }
  674. spin_unlock(&vdev->priv_lock);
  675. spin_unlock_irqrestore(&vhci->lock, flags);
  676. }
  677. /*
  678. * The important thing is that only one context begins cleanup.
  679. * This is why error handling and cleanup become simple.
  680. * We do not want to consider race condition as possible.
  681. */
  682. static void vhci_shutdown_connection(struct usbip_device *ud)
  683. {
  684. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  685. /* need this? see stub_dev.c */
  686. if (ud->tcp_socket) {
  687. pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
  688. kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
  689. }
  690. /* kill threads related to this sdev */
  691. if (vdev->ud.tcp_rx) {
  692. kthread_stop_put(vdev->ud.tcp_rx);
  693. vdev->ud.tcp_rx = NULL;
  694. }
  695. if (vdev->ud.tcp_tx) {
  696. kthread_stop_put(vdev->ud.tcp_tx);
  697. vdev->ud.tcp_tx = NULL;
  698. }
  699. pr_info("stop threads\n");
  700. /* active connection is closed */
  701. if (vdev->ud.tcp_socket) {
  702. sockfd_put(vdev->ud.tcp_socket);
  703. vdev->ud.tcp_socket = NULL;
  704. }
  705. pr_info("release socket\n");
  706. vhci_device_unlink_cleanup(vdev);
  707. /*
  708. * rh_port_disconnect() is a trigger of ...
  709. * usb_disable_device():
  710. * disable all the endpoints for a USB device.
  711. * usb_disable_endpoint():
  712. * disable endpoints. pending urbs are unlinked(dequeued).
  713. *
  714. * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
  715. * detached device should release used urbs in a cleanup function (i.e.
  716. * xxx_disconnect()). Therefore, vhci_hcd does not need to release
  717. * pushed urbs and their private data in this function.
  718. *
  719. * NOTE: vhci_dequeue() must be considered carefully. When shutting down
  720. * a connection, vhci_shutdown_connection() expects vhci_dequeue()
  721. * gives back pushed urbs and frees their private data by request of
  722. * the cleanup function of a USB driver. When unlinking a urb with an
  723. * active connection, vhci_dequeue() does not give back the urb which
  724. * is actually given back by vhci_rx after receiving its return pdu.
  725. *
  726. */
  727. rh_port_disconnect(vdev);
  728. pr_info("disconnect device\n");
  729. }
  730. static void vhci_device_reset(struct usbip_device *ud)
  731. {
  732. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  733. unsigned long flags;
  734. spin_lock_irqsave(&ud->lock, flags);
  735. vdev->speed = 0;
  736. vdev->devid = 0;
  737. usb_put_dev(vdev->udev);
  738. vdev->udev = NULL;
  739. if (ud->tcp_socket) {
  740. sockfd_put(ud->tcp_socket);
  741. ud->tcp_socket = NULL;
  742. }
  743. ud->status = VDEV_ST_NULL;
  744. spin_unlock_irqrestore(&ud->lock, flags);
  745. }
  746. static void vhci_device_unusable(struct usbip_device *ud)
  747. {
  748. unsigned long flags;
  749. spin_lock_irqsave(&ud->lock, flags);
  750. ud->status = VDEV_ST_ERROR;
  751. spin_unlock_irqrestore(&ud->lock, flags);
  752. }
  753. static void vhci_device_init(struct vhci_device *vdev)
  754. {
  755. memset(vdev, 0, sizeof(struct vhci_device));
  756. vdev->ud.side = USBIP_VHCI;
  757. vdev->ud.status = VDEV_ST_NULL;
  758. spin_lock_init(&vdev->ud.lock);
  759. INIT_LIST_HEAD(&vdev->priv_rx);
  760. INIT_LIST_HEAD(&vdev->priv_tx);
  761. INIT_LIST_HEAD(&vdev->unlink_tx);
  762. INIT_LIST_HEAD(&vdev->unlink_rx);
  763. spin_lock_init(&vdev->priv_lock);
  764. init_waitqueue_head(&vdev->waitq_tx);
  765. vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
  766. vdev->ud.eh_ops.reset = vhci_device_reset;
  767. vdev->ud.eh_ops.unusable = vhci_device_unusable;
  768. usbip_start_eh(&vdev->ud);
  769. }
  770. static int hcd_name_to_id(const char *name)
  771. {
  772. char *c;
  773. long val;
  774. int ret;
  775. c = strchr(name, '.');
  776. if (c == NULL)
  777. return 0;
  778. ret = kstrtol(c+1, 10, &val);
  779. if (ret < 0)
  780. return ret;
  781. return val;
  782. }
  783. static int vhci_start(struct usb_hcd *hcd)
  784. {
  785. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  786. int id, rhport;
  787. int err = 0;
  788. usbip_dbg_vhci_hc("enter vhci_start\n");
  789. /* initialize private data of usb_hcd */
  790. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  791. struct vhci_device *vdev = &vhci->vdev[rhport];
  792. vhci_device_init(vdev);
  793. vdev->rhport = rhport;
  794. }
  795. atomic_set(&vhci->seqnum, 0);
  796. spin_lock_init(&vhci->lock);
  797. hcd->power_budget = 0; /* no limit */
  798. hcd->uses_new_polling = 1;
  799. id = hcd_name_to_id(hcd_name(hcd));
  800. if (id < 0) {
  801. pr_err("invalid vhci name %s\n", hcd_name(hcd));
  802. return -EINVAL;
  803. }
  804. /* vhci_hcd is now ready to be controlled through sysfs */
  805. if (id == 0) {
  806. err = vhci_init_attr_group();
  807. if (err) {
  808. pr_err("init attr group\n");
  809. return err;
  810. }
  811. err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  812. if (err) {
  813. pr_err("create sysfs files\n");
  814. vhci_finish_attr_group();
  815. return err;
  816. }
  817. pr_info("created sysfs %s\n", hcd_name(hcd));
  818. }
  819. return 0;
  820. }
  821. static void vhci_stop(struct usb_hcd *hcd)
  822. {
  823. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  824. int id, rhport;
  825. usbip_dbg_vhci_hc("stop VHCI controller\n");
  826. /* 1. remove the userland interface of vhci_hcd */
  827. id = hcd_name_to_id(hcd_name(hcd));
  828. if (id == 0) {
  829. sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
  830. vhci_finish_attr_group();
  831. }
  832. /* 2. shutdown all the ports of vhci_hcd */
  833. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
  834. struct vhci_device *vdev = &vhci->vdev[rhport];
  835. usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
  836. usbip_stop_eh(&vdev->ud);
  837. }
  838. }
  839. static int vhci_get_frame_number(struct usb_hcd *hcd)
  840. {
  841. dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
  842. return 0;
  843. }
  844. #ifdef CONFIG_PM
  845. /* FIXME: suspend/resume */
  846. static int vhci_bus_suspend(struct usb_hcd *hcd)
  847. {
  848. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  849. unsigned long flags;
  850. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  851. spin_lock_irqsave(&vhci->lock, flags);
  852. hcd->state = HC_STATE_SUSPENDED;
  853. spin_unlock_irqrestore(&vhci->lock, flags);
  854. return 0;
  855. }
  856. static int vhci_bus_resume(struct usb_hcd *hcd)
  857. {
  858. struct vhci_hcd *vhci = hcd_to_vhci(hcd);
  859. int rc = 0;
  860. unsigned long flags;
  861. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  862. spin_lock_irqsave(&vhci->lock, flags);
  863. if (!HCD_HW_ACCESSIBLE(hcd))
  864. rc = -ESHUTDOWN;
  865. else
  866. hcd->state = HC_STATE_RUNNING;
  867. spin_unlock_irqrestore(&vhci->lock, flags);
  868. return rc;
  869. }
  870. #else
  871. #define vhci_bus_suspend NULL
  872. #define vhci_bus_resume NULL
  873. #endif
  874. static struct hc_driver vhci_hc_driver = {
  875. .description = driver_name,
  876. .product_desc = driver_desc,
  877. .hcd_priv_size = sizeof(struct vhci_hcd),
  878. .flags = HCD_USB2,
  879. .start = vhci_start,
  880. .stop = vhci_stop,
  881. .urb_enqueue = vhci_urb_enqueue,
  882. .urb_dequeue = vhci_urb_dequeue,
  883. .get_frame_number = vhci_get_frame_number,
  884. .hub_status_data = vhci_hub_status,
  885. .hub_control = vhci_hub_control,
  886. .bus_suspend = vhci_bus_suspend,
  887. .bus_resume = vhci_bus_resume,
  888. };
  889. static int vhci_hcd_probe(struct platform_device *pdev)
  890. {
  891. struct usb_hcd *hcd;
  892. int ret;
  893. usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
  894. /*
  895. * Allocate and initialize hcd.
  896. * Our private data is also allocated automatically.
  897. */
  898. hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  899. if (!hcd) {
  900. pr_err("create hcd failed\n");
  901. return -ENOMEM;
  902. }
  903. hcd->has_tt = 1;
  904. /*
  905. * Finish generic HCD structure initialization and register.
  906. * Call the driver's reset() and start() routines.
  907. */
  908. ret = usb_add_hcd(hcd, 0, 0);
  909. if (ret != 0) {
  910. pr_err("usb_add_hcd failed %d\n", ret);
  911. usb_put_hcd(hcd);
  912. return ret;
  913. }
  914. usbip_dbg_vhci_hc("bye\n");
  915. return 0;
  916. }
  917. static int vhci_hcd_remove(struct platform_device *pdev)
  918. {
  919. struct usb_hcd *hcd;
  920. hcd = platform_get_drvdata(pdev);
  921. if (!hcd)
  922. return 0;
  923. /*
  924. * Disconnects the root hub,
  925. * then reverses the effects of usb_add_hcd(),
  926. * invoking the HCD's stop() methods.
  927. */
  928. usb_remove_hcd(hcd);
  929. usb_put_hcd(hcd);
  930. return 0;
  931. }
  932. #ifdef CONFIG_PM
  933. /* what should happen for USB/IP under suspend/resume? */
  934. static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  935. {
  936. struct usb_hcd *hcd;
  937. struct vhci_hcd *vhci;
  938. int rhport;
  939. int connected = 0;
  940. int ret = 0;
  941. unsigned long flags;
  942. hcd = platform_get_drvdata(pdev);
  943. if (!hcd)
  944. return 0;
  945. vhci = hcd_to_vhci(hcd);
  946. spin_lock_irqsave(&vhci->lock, flags);
  947. for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++)
  948. if (vhci->port_status[rhport] & USB_PORT_STAT_CONNECTION)
  949. connected += 1;
  950. spin_unlock_irqrestore(&vhci->lock, flags);
  951. if (connected > 0) {
  952. dev_info(&pdev->dev,
  953. "We have %d active connection%s. Do not suspend.\n",
  954. connected, (connected == 1 ? "" : "s"));
  955. ret = -EBUSY;
  956. } else {
  957. dev_info(&pdev->dev, "suspend vhci_hcd");
  958. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  959. }
  960. return ret;
  961. }
  962. static int vhci_hcd_resume(struct platform_device *pdev)
  963. {
  964. struct usb_hcd *hcd;
  965. dev_dbg(&pdev->dev, "%s\n", __func__);
  966. hcd = platform_get_drvdata(pdev);
  967. if (!hcd)
  968. return 0;
  969. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  970. usb_hcd_poll_rh_status(hcd);
  971. return 0;
  972. }
  973. #else
  974. #define vhci_hcd_suspend NULL
  975. #define vhci_hcd_resume NULL
  976. #endif
  977. static struct platform_driver vhci_driver = {
  978. .probe = vhci_hcd_probe,
  979. .remove = vhci_hcd_remove,
  980. .suspend = vhci_hcd_suspend,
  981. .resume = vhci_hcd_resume,
  982. .driver = {
  983. .name = driver_name,
  984. },
  985. };
  986. static int add_platform_device(int id)
  987. {
  988. struct platform_device *pdev;
  989. int dev_nr;
  990. if (id == 0)
  991. dev_nr = -1;
  992. else
  993. dev_nr = id;
  994. pdev = platform_device_register_simple(driver_name, dev_nr, NULL, 0);
  995. if (IS_ERR(pdev))
  996. return PTR_ERR(pdev);
  997. *(vhci_pdevs + id) = pdev;
  998. return 0;
  999. }
  1000. static void del_platform_devices(void)
  1001. {
  1002. struct platform_device *pdev;
  1003. int i;
  1004. for (i = 0; i < vhci_num_controllers; i++) {
  1005. pdev = *(vhci_pdevs + i);
  1006. if (pdev != NULL)
  1007. platform_device_unregister(pdev);
  1008. *(vhci_pdevs + i) = NULL;
  1009. }
  1010. sysfs_remove_link(&platform_bus.kobj, driver_name);
  1011. }
  1012. static int __init vhci_hcd_init(void)
  1013. {
  1014. int i, ret;
  1015. if (usb_disabled())
  1016. return -ENODEV;
  1017. if (vhci_num_controllers < 1)
  1018. vhci_num_controllers = 1;
  1019. vhci_pdevs = kcalloc(vhci_num_controllers, sizeof(void *), GFP_KERNEL);
  1020. if (vhci_pdevs == NULL)
  1021. return -ENOMEM;
  1022. ret = platform_driver_register(&vhci_driver);
  1023. if (ret)
  1024. goto err_driver_register;
  1025. for (i = 0; i < vhci_num_controllers; i++) {
  1026. ret = add_platform_device(i);
  1027. if (ret)
  1028. goto err_platform_device_register;
  1029. }
  1030. pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
  1031. return ret;
  1032. err_platform_device_register:
  1033. del_platform_devices();
  1034. platform_driver_unregister(&vhci_driver);
  1035. err_driver_register:
  1036. kfree(vhci_pdevs);
  1037. return ret;
  1038. }
  1039. static void __exit vhci_hcd_exit(void)
  1040. {
  1041. del_platform_devices();
  1042. platform_driver_unregister(&vhci_driver);
  1043. kfree(vhci_pdevs);
  1044. }
  1045. module_init(vhci_hcd_init);
  1046. module_exit(vhci_hcd_exit);
  1047. MODULE_AUTHOR(DRIVER_AUTHOR);
  1048. MODULE_DESCRIPTION(DRIVER_DESC);
  1049. MODULE_LICENSE("GPL");
  1050. MODULE_VERSION(USBIP_VERSION);