vhci_hcd.c 30 KB

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