xen-hcd.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * xen-hcd.c
  4. *
  5. * Xen USB Virtual Host Controller driver
  6. *
  7. * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
  8. * Author: Noboru Iwamatsu <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/usb.h>
  12. #include <linux/list.h>
  13. #include <linux/usb/hcd.h>
  14. #include <linux/io.h>
  15. #include <xen/xen.h>
  16. #include <xen/xenbus.h>
  17. #include <xen/grant_table.h>
  18. #include <xen/events.h>
  19. #include <xen/page.h>
  20. #include <xen/interface/io/usbif.h>
  21. /* Private per-URB data */
  22. struct urb_priv {
  23. struct list_head list;
  24. struct urb *urb;
  25. int req_id; /* RING_REQUEST id for submitting */
  26. int unlink_req_id; /* RING_REQUEST id for unlinking */
  27. int status;
  28. bool unlinked; /* dequeued marker */
  29. };
  30. /* virtual roothub port status */
  31. struct rhport_status {
  32. __u32 status;
  33. bool resuming; /* in resuming */
  34. bool c_connection; /* connection changed */
  35. unsigned long timeout;
  36. };
  37. /* status of attached device */
  38. struct vdevice_status {
  39. int devnum;
  40. enum usb_device_state status;
  41. enum usb_device_speed speed;
  42. };
  43. /* RING request shadow */
  44. struct usb_shadow {
  45. struct xenusb_urb_request req;
  46. struct urb *urb;
  47. bool in_flight;
  48. };
  49. struct xenhcd_info {
  50. /* Virtual Host Controller has 4 urb queues */
  51. struct list_head pending_submit_list;
  52. struct list_head pending_unlink_list;
  53. struct list_head in_progress_list;
  54. struct list_head giveback_waiting_list;
  55. spinlock_t lock;
  56. /* timer that kick pending and giveback waiting urbs */
  57. struct timer_list watchdog;
  58. unsigned long actions;
  59. /* virtual root hub */
  60. int rh_numports;
  61. struct rhport_status ports[XENUSB_MAX_PORTNR];
  62. struct vdevice_status devices[XENUSB_MAX_PORTNR];
  63. /* Xen related staff */
  64. struct xenbus_device *xbdev;
  65. int urb_ring_ref;
  66. int conn_ring_ref;
  67. struct xenusb_urb_front_ring urb_ring;
  68. struct xenusb_conn_front_ring conn_ring;
  69. unsigned int evtchn;
  70. unsigned int irq;
  71. struct usb_shadow shadow[XENUSB_URB_RING_SIZE];
  72. unsigned int shadow_free;
  73. bool error;
  74. };
  75. #define XENHCD_RING_JIFFIES (HZ/200)
  76. #define XENHCD_SCAN_JIFFIES 1
  77. enum xenhcd_timer_action {
  78. TIMER_RING_WATCHDOG,
  79. TIMER_SCAN_PENDING_URBS,
  80. };
  81. static struct kmem_cache *xenhcd_urbp_cachep;
  82. static inline struct xenhcd_info *xenhcd_hcd_to_info(struct usb_hcd *hcd)
  83. {
  84. return (struct xenhcd_info *)hcd->hcd_priv;
  85. }
  86. static inline struct usb_hcd *xenhcd_info_to_hcd(struct xenhcd_info *info)
  87. {
  88. return container_of((void *)info, struct usb_hcd, hcd_priv);
  89. }
  90. static void xenhcd_set_error(struct xenhcd_info *info, const char *msg)
  91. {
  92. info->error = true;
  93. pr_alert("xen-hcd: protocol error: %s!\n", msg);
  94. }
  95. static inline void xenhcd_timer_action_done(struct xenhcd_info *info,
  96. enum xenhcd_timer_action action)
  97. {
  98. clear_bit(action, &info->actions);
  99. }
  100. static void xenhcd_timer_action(struct xenhcd_info *info,
  101. enum xenhcd_timer_action action)
  102. {
  103. if (timer_pending(&info->watchdog) &&
  104. test_bit(TIMER_SCAN_PENDING_URBS, &info->actions))
  105. return;
  106. if (!test_and_set_bit(action, &info->actions)) {
  107. unsigned long t;
  108. switch (action) {
  109. case TIMER_RING_WATCHDOG:
  110. t = XENHCD_RING_JIFFIES;
  111. break;
  112. default:
  113. t = XENHCD_SCAN_JIFFIES;
  114. break;
  115. }
  116. mod_timer(&info->watchdog, t + jiffies);
  117. }
  118. }
  119. /*
  120. * set virtual port connection status
  121. */
  122. static void xenhcd_set_connect_state(struct xenhcd_info *info, int portnum)
  123. {
  124. int port;
  125. port = portnum - 1;
  126. if (info->ports[port].status & USB_PORT_STAT_POWER) {
  127. switch (info->devices[port].speed) {
  128. case XENUSB_SPEED_NONE:
  129. info->ports[port].status &=
  130. ~(USB_PORT_STAT_CONNECTION |
  131. USB_PORT_STAT_ENABLE |
  132. USB_PORT_STAT_LOW_SPEED |
  133. USB_PORT_STAT_HIGH_SPEED |
  134. USB_PORT_STAT_SUSPEND);
  135. break;
  136. case XENUSB_SPEED_LOW:
  137. info->ports[port].status |= USB_PORT_STAT_CONNECTION;
  138. info->ports[port].status |= USB_PORT_STAT_LOW_SPEED;
  139. break;
  140. case XENUSB_SPEED_FULL:
  141. info->ports[port].status |= USB_PORT_STAT_CONNECTION;
  142. break;
  143. case XENUSB_SPEED_HIGH:
  144. info->ports[port].status |= USB_PORT_STAT_CONNECTION;
  145. info->ports[port].status |= USB_PORT_STAT_HIGH_SPEED;
  146. break;
  147. default: /* error */
  148. return;
  149. }
  150. info->ports[port].status |= (USB_PORT_STAT_C_CONNECTION << 16);
  151. }
  152. }
  153. /*
  154. * set virtual device connection status
  155. */
  156. static int xenhcd_rhport_connect(struct xenhcd_info *info, __u8 portnum,
  157. __u8 speed)
  158. {
  159. int port;
  160. if (portnum < 1 || portnum > info->rh_numports)
  161. return -EINVAL; /* invalid port number */
  162. port = portnum - 1;
  163. if (info->devices[port].speed != speed) {
  164. switch (speed) {
  165. case XENUSB_SPEED_NONE: /* disconnect */
  166. info->devices[port].status = USB_STATE_NOTATTACHED;
  167. break;
  168. case XENUSB_SPEED_LOW:
  169. case XENUSB_SPEED_FULL:
  170. case XENUSB_SPEED_HIGH:
  171. info->devices[port].status = USB_STATE_ATTACHED;
  172. break;
  173. default: /* error */
  174. return -EINVAL;
  175. }
  176. info->devices[port].speed = speed;
  177. info->ports[port].c_connection = true;
  178. xenhcd_set_connect_state(info, portnum);
  179. }
  180. return 0;
  181. }
  182. /*
  183. * SetPortFeature(PORT_SUSPENDED)
  184. */
  185. static void xenhcd_rhport_suspend(struct xenhcd_info *info, int portnum)
  186. {
  187. int port;
  188. port = portnum - 1;
  189. info->ports[port].status |= USB_PORT_STAT_SUSPEND;
  190. info->devices[port].status = USB_STATE_SUSPENDED;
  191. }
  192. /*
  193. * ClearPortFeature(PORT_SUSPENDED)
  194. */
  195. static void xenhcd_rhport_resume(struct xenhcd_info *info, int portnum)
  196. {
  197. int port;
  198. port = portnum - 1;
  199. if (info->ports[port].status & USB_PORT_STAT_SUSPEND) {
  200. info->ports[port].resuming = true;
  201. info->ports[port].timeout = jiffies + msecs_to_jiffies(20);
  202. }
  203. }
  204. /*
  205. * SetPortFeature(PORT_POWER)
  206. */
  207. static void xenhcd_rhport_power_on(struct xenhcd_info *info, int portnum)
  208. {
  209. int port;
  210. port = portnum - 1;
  211. if ((info->ports[port].status & USB_PORT_STAT_POWER) == 0) {
  212. info->ports[port].status |= USB_PORT_STAT_POWER;
  213. if (info->devices[port].status != USB_STATE_NOTATTACHED)
  214. info->devices[port].status = USB_STATE_POWERED;
  215. if (info->ports[port].c_connection)
  216. xenhcd_set_connect_state(info, portnum);
  217. }
  218. }
  219. /*
  220. * ClearPortFeature(PORT_POWER)
  221. * SetConfiguration(non-zero)
  222. * Power_Source_Off
  223. * Over-current
  224. */
  225. static void xenhcd_rhport_power_off(struct xenhcd_info *info, int portnum)
  226. {
  227. int port;
  228. port = portnum - 1;
  229. if (info->ports[port].status & USB_PORT_STAT_POWER) {
  230. info->ports[port].status = 0;
  231. if (info->devices[port].status != USB_STATE_NOTATTACHED)
  232. info->devices[port].status = USB_STATE_ATTACHED;
  233. }
  234. }
  235. /*
  236. * ClearPortFeature(PORT_ENABLE)
  237. */
  238. static void xenhcd_rhport_disable(struct xenhcd_info *info, int portnum)
  239. {
  240. int port;
  241. port = portnum - 1;
  242. info->ports[port].status &= ~USB_PORT_STAT_ENABLE;
  243. info->ports[port].status &= ~USB_PORT_STAT_SUSPEND;
  244. info->ports[port].resuming = false;
  245. if (info->devices[port].status != USB_STATE_NOTATTACHED)
  246. info->devices[port].status = USB_STATE_POWERED;
  247. }
  248. /*
  249. * SetPortFeature(PORT_RESET)
  250. */
  251. static void xenhcd_rhport_reset(struct xenhcd_info *info, int portnum)
  252. {
  253. int port;
  254. port = portnum - 1;
  255. info->ports[port].status &= ~(USB_PORT_STAT_ENABLE |
  256. USB_PORT_STAT_LOW_SPEED |
  257. USB_PORT_STAT_HIGH_SPEED);
  258. info->ports[port].status |= USB_PORT_STAT_RESET;
  259. if (info->devices[port].status != USB_STATE_NOTATTACHED)
  260. info->devices[port].status = USB_STATE_ATTACHED;
  261. /* 10msec reset signaling */
  262. info->ports[port].timeout = jiffies + msecs_to_jiffies(10);
  263. }
  264. #ifdef CONFIG_PM
  265. static int xenhcd_bus_suspend(struct usb_hcd *hcd)
  266. {
  267. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  268. int ret = 0;
  269. int i, ports;
  270. ports = info->rh_numports;
  271. spin_lock_irq(&info->lock);
  272. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  273. ret = -ESHUTDOWN;
  274. } else {
  275. /* suspend any active ports*/
  276. for (i = 1; i <= ports; i++)
  277. xenhcd_rhport_suspend(info, i);
  278. }
  279. spin_unlock_irq(&info->lock);
  280. del_timer_sync(&info->watchdog);
  281. return ret;
  282. }
  283. static int xenhcd_bus_resume(struct usb_hcd *hcd)
  284. {
  285. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  286. int ret = 0;
  287. int i, ports;
  288. ports = info->rh_numports;
  289. spin_lock_irq(&info->lock);
  290. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  291. ret = -ESHUTDOWN;
  292. } else {
  293. /* resume any suspended ports*/
  294. for (i = 1; i <= ports; i++)
  295. xenhcd_rhport_resume(info, i);
  296. }
  297. spin_unlock_irq(&info->lock);
  298. return ret;
  299. }
  300. #endif
  301. static void xenhcd_hub_descriptor(struct xenhcd_info *info,
  302. struct usb_hub_descriptor *desc)
  303. {
  304. __u16 temp;
  305. int ports = info->rh_numports;
  306. desc->bDescriptorType = 0x29;
  307. desc->bPwrOn2PwrGood = 10; /* EHCI says 20ms max */
  308. desc->bHubContrCurrent = 0;
  309. desc->bNbrPorts = ports;
  310. /* size of DeviceRemovable and PortPwrCtrlMask fields */
  311. temp = 1 + (ports / 8);
  312. desc->bDescLength = 7 + 2 * temp;
  313. /* bitmaps for DeviceRemovable and PortPwrCtrlMask */
  314. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  315. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  316. /* per-port over current reporting and no power switching */
  317. temp = 0x000a;
  318. desc->wHubCharacteristics = cpu_to_le16(temp);
  319. }
  320. /* port status change mask for hub_status_data */
  321. #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | \
  322. USB_PORT_STAT_C_ENABLE | \
  323. USB_PORT_STAT_C_SUSPEND | \
  324. USB_PORT_STAT_C_OVERCURRENT | \
  325. USB_PORT_STAT_C_RESET) << 16)
  326. /*
  327. * See USB 2.0 Spec, 11.12.4 Hub and Port Status Change Bitmap.
  328. * If port status changed, writes the bitmap to buf and return
  329. * that length(number of bytes).
  330. * If Nothing changed, return 0.
  331. */
  332. static int xenhcd_hub_status_data(struct usb_hcd *hcd, char *buf)
  333. {
  334. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  335. int ports;
  336. int i;
  337. unsigned long flags;
  338. int ret;
  339. int changed = 0;
  340. /* initialize the status to no-changes */
  341. ports = info->rh_numports;
  342. ret = 1 + (ports / 8);
  343. memset(buf, 0, ret);
  344. spin_lock_irqsave(&info->lock, flags);
  345. for (i = 0; i < ports; i++) {
  346. /* check status for each port */
  347. if (info->ports[i].status & PORT_C_MASK) {
  348. buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
  349. changed = 1;
  350. }
  351. }
  352. if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
  353. usb_hcd_resume_root_hub(hcd);
  354. spin_unlock_irqrestore(&info->lock, flags);
  355. return changed ? ret : 0;
  356. }
  357. static int xenhcd_hub_control(struct usb_hcd *hcd, __u16 typeReq, __u16 wValue,
  358. __u16 wIndex, char *buf, __u16 wLength)
  359. {
  360. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  361. int ports = info->rh_numports;
  362. unsigned long flags;
  363. int ret = 0;
  364. int i;
  365. int changed = 0;
  366. spin_lock_irqsave(&info->lock, flags);
  367. switch (typeReq) {
  368. case ClearHubFeature:
  369. /* ignore this request */
  370. break;
  371. case ClearPortFeature:
  372. if (!wIndex || wIndex > ports)
  373. goto error;
  374. switch (wValue) {
  375. case USB_PORT_FEAT_SUSPEND:
  376. xenhcd_rhport_resume(info, wIndex);
  377. break;
  378. case USB_PORT_FEAT_POWER:
  379. xenhcd_rhport_power_off(info, wIndex);
  380. break;
  381. case USB_PORT_FEAT_ENABLE:
  382. xenhcd_rhport_disable(info, wIndex);
  383. break;
  384. case USB_PORT_FEAT_C_CONNECTION:
  385. info->ports[wIndex - 1].c_connection = false;
  386. fallthrough;
  387. default:
  388. info->ports[wIndex - 1].status &= ~(1 << wValue);
  389. break;
  390. }
  391. break;
  392. case GetHubDescriptor:
  393. xenhcd_hub_descriptor(info, (struct usb_hub_descriptor *)buf);
  394. break;
  395. case GetHubStatus:
  396. /* always local power supply good and no over-current exists. */
  397. *(__le32 *)buf = cpu_to_le32(0);
  398. break;
  399. case GetPortStatus:
  400. if (!wIndex || wIndex > ports)
  401. goto error;
  402. wIndex--;
  403. /* resume completion */
  404. if (info->ports[wIndex].resuming &&
  405. time_after_eq(jiffies, info->ports[wIndex].timeout)) {
  406. info->ports[wIndex].status |=
  407. USB_PORT_STAT_C_SUSPEND << 16;
  408. info->ports[wIndex].status &= ~USB_PORT_STAT_SUSPEND;
  409. }
  410. /* reset completion */
  411. if ((info->ports[wIndex].status & USB_PORT_STAT_RESET) != 0 &&
  412. time_after_eq(jiffies, info->ports[wIndex].timeout)) {
  413. info->ports[wIndex].status |=
  414. USB_PORT_STAT_C_RESET << 16;
  415. info->ports[wIndex].status &= ~USB_PORT_STAT_RESET;
  416. if (info->devices[wIndex].status !=
  417. USB_STATE_NOTATTACHED) {
  418. info->ports[wIndex].status |=
  419. USB_PORT_STAT_ENABLE;
  420. info->devices[wIndex].status =
  421. USB_STATE_DEFAULT;
  422. }
  423. switch (info->devices[wIndex].speed) {
  424. case XENUSB_SPEED_LOW:
  425. info->ports[wIndex].status |=
  426. USB_PORT_STAT_LOW_SPEED;
  427. break;
  428. case XENUSB_SPEED_HIGH:
  429. info->ports[wIndex].status |=
  430. USB_PORT_STAT_HIGH_SPEED;
  431. break;
  432. default:
  433. break;
  434. }
  435. }
  436. *(__le32 *)buf = cpu_to_le32(info->ports[wIndex].status);
  437. break;
  438. case SetPortFeature:
  439. if (!wIndex || wIndex > ports)
  440. goto error;
  441. switch (wValue) {
  442. case USB_PORT_FEAT_POWER:
  443. xenhcd_rhport_power_on(info, wIndex);
  444. break;
  445. case USB_PORT_FEAT_RESET:
  446. xenhcd_rhport_reset(info, wIndex);
  447. break;
  448. case USB_PORT_FEAT_SUSPEND:
  449. xenhcd_rhport_suspend(info, wIndex);
  450. break;
  451. default:
  452. if (info->ports[wIndex-1].status & USB_PORT_STAT_POWER)
  453. info->ports[wIndex-1].status |= (1 << wValue);
  454. }
  455. break;
  456. case SetHubFeature:
  457. /* not supported */
  458. default:
  459. error:
  460. ret = -EPIPE;
  461. }
  462. spin_unlock_irqrestore(&info->lock, flags);
  463. /* check status for each port */
  464. for (i = 0; i < ports; i++) {
  465. if (info->ports[i].status & PORT_C_MASK)
  466. changed = 1;
  467. }
  468. if (changed)
  469. usb_hcd_poll_rh_status(hcd);
  470. return ret;
  471. }
  472. static void xenhcd_free_urb_priv(struct urb_priv *urbp)
  473. {
  474. urbp->urb->hcpriv = NULL;
  475. kmem_cache_free(xenhcd_urbp_cachep, urbp);
  476. }
  477. static inline unsigned int xenhcd_get_id_from_freelist(struct xenhcd_info *info)
  478. {
  479. unsigned int free;
  480. free = info->shadow_free;
  481. info->shadow_free = info->shadow[free].req.id;
  482. info->shadow[free].req.id = 0x0fff; /* debug */
  483. return free;
  484. }
  485. static inline void xenhcd_add_id_to_freelist(struct xenhcd_info *info,
  486. unsigned int id)
  487. {
  488. info->shadow[id].req.id = info->shadow_free;
  489. info->shadow[id].urb = NULL;
  490. info->shadow_free = id;
  491. }
  492. static inline int xenhcd_count_pages(void *addr, int length)
  493. {
  494. unsigned long vaddr = (unsigned long)addr;
  495. return PFN_UP(vaddr + length) - PFN_DOWN(vaddr);
  496. }
  497. static void xenhcd_gnttab_map(struct xenhcd_info *info, void *addr, int length,
  498. grant_ref_t *gref_head,
  499. struct xenusb_request_segment *seg,
  500. int nr_pages, int flags)
  501. {
  502. grant_ref_t ref;
  503. unsigned int offset;
  504. unsigned int len = length;
  505. unsigned int bytes;
  506. int i;
  507. for (i = 0; i < nr_pages; i++) {
  508. offset = offset_in_page(addr);
  509. bytes = PAGE_SIZE - offset;
  510. if (bytes > len)
  511. bytes = len;
  512. ref = gnttab_claim_grant_reference(gref_head);
  513. gnttab_grant_foreign_access_ref(ref, info->xbdev->otherend_id,
  514. virt_to_gfn(addr), flags);
  515. seg[i].gref = ref;
  516. seg[i].offset = (__u16)offset;
  517. seg[i].length = (__u16)bytes;
  518. addr += bytes;
  519. len -= bytes;
  520. }
  521. }
  522. static __u32 xenhcd_pipe_urb_to_xenusb(__u32 urb_pipe, __u8 port)
  523. {
  524. static __u32 pipe;
  525. pipe = usb_pipedevice(urb_pipe) << XENUSB_PIPE_DEV_SHIFT;
  526. pipe |= usb_pipeendpoint(urb_pipe) << XENUSB_PIPE_EP_SHIFT;
  527. if (usb_pipein(urb_pipe))
  528. pipe |= XENUSB_PIPE_DIR;
  529. switch (usb_pipetype(urb_pipe)) {
  530. case PIPE_ISOCHRONOUS:
  531. pipe |= XENUSB_PIPE_TYPE_ISOC << XENUSB_PIPE_TYPE_SHIFT;
  532. break;
  533. case PIPE_INTERRUPT:
  534. pipe |= XENUSB_PIPE_TYPE_INT << XENUSB_PIPE_TYPE_SHIFT;
  535. break;
  536. case PIPE_CONTROL:
  537. pipe |= XENUSB_PIPE_TYPE_CTRL << XENUSB_PIPE_TYPE_SHIFT;
  538. break;
  539. case PIPE_BULK:
  540. pipe |= XENUSB_PIPE_TYPE_BULK << XENUSB_PIPE_TYPE_SHIFT;
  541. break;
  542. }
  543. pipe = xenusb_setportnum_pipe(pipe, port);
  544. return pipe;
  545. }
  546. static int xenhcd_map_urb_for_request(struct xenhcd_info *info, struct urb *urb,
  547. struct xenusb_urb_request *req)
  548. {
  549. grant_ref_t gref_head;
  550. int nr_buff_pages = 0;
  551. int nr_isodesc_pages = 0;
  552. int nr_grants = 0;
  553. if (urb->transfer_buffer_length) {
  554. nr_buff_pages = xenhcd_count_pages(urb->transfer_buffer,
  555. urb->transfer_buffer_length);
  556. if (usb_pipeisoc(urb->pipe))
  557. nr_isodesc_pages = xenhcd_count_pages(
  558. &urb->iso_frame_desc[0],
  559. sizeof(struct usb_iso_packet_descriptor) *
  560. urb->number_of_packets);
  561. nr_grants = nr_buff_pages + nr_isodesc_pages;
  562. if (nr_grants > XENUSB_MAX_SEGMENTS_PER_REQUEST) {
  563. pr_err("xenhcd: error: %d grants\n", nr_grants);
  564. return -E2BIG;
  565. }
  566. if (gnttab_alloc_grant_references(nr_grants, &gref_head)) {
  567. pr_err("xenhcd: gnttab_alloc_grant_references() error\n");
  568. return -ENOMEM;
  569. }
  570. xenhcd_gnttab_map(info, urb->transfer_buffer,
  571. urb->transfer_buffer_length, &gref_head,
  572. &req->seg[0], nr_buff_pages,
  573. usb_pipein(urb->pipe) ? 0 : GTF_readonly);
  574. }
  575. req->pipe = xenhcd_pipe_urb_to_xenusb(urb->pipe, urb->dev->portnum);
  576. req->transfer_flags = 0;
  577. if (urb->transfer_flags & URB_SHORT_NOT_OK)
  578. req->transfer_flags |= XENUSB_SHORT_NOT_OK;
  579. req->buffer_length = urb->transfer_buffer_length;
  580. req->nr_buffer_segs = nr_buff_pages;
  581. switch (usb_pipetype(urb->pipe)) {
  582. case PIPE_ISOCHRONOUS:
  583. req->u.isoc.interval = urb->interval;
  584. req->u.isoc.start_frame = urb->start_frame;
  585. req->u.isoc.number_of_packets = urb->number_of_packets;
  586. req->u.isoc.nr_frame_desc_segs = nr_isodesc_pages;
  587. xenhcd_gnttab_map(info, &urb->iso_frame_desc[0],
  588. sizeof(struct usb_iso_packet_descriptor) *
  589. urb->number_of_packets,
  590. &gref_head, &req->seg[nr_buff_pages],
  591. nr_isodesc_pages, 0);
  592. break;
  593. case PIPE_INTERRUPT:
  594. req->u.intr.interval = urb->interval;
  595. break;
  596. case PIPE_CONTROL:
  597. if (urb->setup_packet)
  598. memcpy(req->u.ctrl, urb->setup_packet, 8);
  599. break;
  600. case PIPE_BULK:
  601. break;
  602. default:
  603. break;
  604. }
  605. if (nr_grants)
  606. gnttab_free_grant_references(gref_head);
  607. return 0;
  608. }
  609. static void xenhcd_gnttab_done(struct xenhcd_info *info, unsigned int id)
  610. {
  611. struct usb_shadow *shadow = info->shadow + id;
  612. int nr_segs = 0;
  613. int i;
  614. if (!shadow->in_flight) {
  615. xenhcd_set_error(info, "Illegal request id");
  616. return;
  617. }
  618. shadow->in_flight = false;
  619. nr_segs = shadow->req.nr_buffer_segs;
  620. if (xenusb_pipeisoc(shadow->req.pipe))
  621. nr_segs += shadow->req.u.isoc.nr_frame_desc_segs;
  622. for (i = 0; i < nr_segs; i++) {
  623. if (!gnttab_try_end_foreign_access(shadow->req.seg[i].gref))
  624. xenhcd_set_error(info, "backend didn't release grant");
  625. }
  626. shadow->req.nr_buffer_segs = 0;
  627. shadow->req.u.isoc.nr_frame_desc_segs = 0;
  628. }
  629. static int xenhcd_translate_status(int status)
  630. {
  631. switch (status) {
  632. case XENUSB_STATUS_OK:
  633. return 0;
  634. case XENUSB_STATUS_NODEV:
  635. return -ENODEV;
  636. case XENUSB_STATUS_INVAL:
  637. return -EINVAL;
  638. case XENUSB_STATUS_STALL:
  639. return -EPIPE;
  640. case XENUSB_STATUS_IOERROR:
  641. return -EPROTO;
  642. case XENUSB_STATUS_BABBLE:
  643. return -EOVERFLOW;
  644. default:
  645. return -ESHUTDOWN;
  646. }
  647. }
  648. static void xenhcd_giveback_urb(struct xenhcd_info *info, struct urb *urb,
  649. int status)
  650. {
  651. struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
  652. int priv_status = urbp->status;
  653. list_del_init(&urbp->list);
  654. xenhcd_free_urb_priv(urbp);
  655. if (urb->status == -EINPROGRESS)
  656. urb->status = xenhcd_translate_status(status);
  657. spin_unlock(&info->lock);
  658. usb_hcd_giveback_urb(xenhcd_info_to_hcd(info), urb,
  659. priv_status <= 0 ? priv_status : urb->status);
  660. spin_lock(&info->lock);
  661. }
  662. static int xenhcd_do_request(struct xenhcd_info *info, struct urb_priv *urbp)
  663. {
  664. struct xenusb_urb_request *req;
  665. struct urb *urb = urbp->urb;
  666. unsigned int id;
  667. int notify;
  668. int ret;
  669. id = xenhcd_get_id_from_freelist(info);
  670. req = &info->shadow[id].req;
  671. req->id = id;
  672. if (unlikely(urbp->unlinked)) {
  673. req->u.unlink.unlink_id = urbp->req_id;
  674. req->pipe = xenusb_setunlink_pipe(xenhcd_pipe_urb_to_xenusb(
  675. urb->pipe, urb->dev->portnum));
  676. urbp->unlink_req_id = id;
  677. } else {
  678. ret = xenhcd_map_urb_for_request(info, urb, req);
  679. if (ret) {
  680. xenhcd_add_id_to_freelist(info, id);
  681. return ret;
  682. }
  683. urbp->req_id = id;
  684. }
  685. req = RING_GET_REQUEST(&info->urb_ring, info->urb_ring.req_prod_pvt);
  686. *req = info->shadow[id].req;
  687. info->urb_ring.req_prod_pvt++;
  688. info->shadow[id].urb = urb;
  689. info->shadow[id].in_flight = true;
  690. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->urb_ring, notify);
  691. if (notify)
  692. notify_remote_via_irq(info->irq);
  693. return 0;
  694. }
  695. static void xenhcd_kick_pending_urbs(struct xenhcd_info *info)
  696. {
  697. struct urb_priv *urbp;
  698. while (!list_empty(&info->pending_submit_list)) {
  699. if (RING_FULL(&info->urb_ring)) {
  700. xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
  701. return;
  702. }
  703. urbp = list_entry(info->pending_submit_list.next,
  704. struct urb_priv, list);
  705. if (!xenhcd_do_request(info, urbp))
  706. list_move_tail(&urbp->list, &info->in_progress_list);
  707. else
  708. xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
  709. }
  710. xenhcd_timer_action_done(info, TIMER_SCAN_PENDING_URBS);
  711. }
  712. /*
  713. * caller must lock info->lock
  714. */
  715. static void xenhcd_cancel_all_enqueued_urbs(struct xenhcd_info *info)
  716. {
  717. struct urb_priv *urbp, *tmp;
  718. int req_id;
  719. list_for_each_entry_safe(urbp, tmp, &info->in_progress_list, list) {
  720. req_id = urbp->req_id;
  721. if (!urbp->unlinked) {
  722. xenhcd_gnttab_done(info, req_id);
  723. if (info->error)
  724. return;
  725. if (urbp->urb->status == -EINPROGRESS)
  726. /* not dequeued */
  727. xenhcd_giveback_urb(info, urbp->urb,
  728. -ESHUTDOWN);
  729. else /* dequeued */
  730. xenhcd_giveback_urb(info, urbp->urb,
  731. urbp->urb->status);
  732. }
  733. info->shadow[req_id].urb = NULL;
  734. }
  735. list_for_each_entry_safe(urbp, tmp, &info->pending_submit_list, list)
  736. xenhcd_giveback_urb(info, urbp->urb, -ESHUTDOWN);
  737. }
  738. /*
  739. * caller must lock info->lock
  740. */
  741. static void xenhcd_giveback_unlinked_urbs(struct xenhcd_info *info)
  742. {
  743. struct urb_priv *urbp, *tmp;
  744. list_for_each_entry_safe(urbp, tmp, &info->giveback_waiting_list, list)
  745. xenhcd_giveback_urb(info, urbp->urb, urbp->urb->status);
  746. }
  747. static int xenhcd_submit_urb(struct xenhcd_info *info, struct urb_priv *urbp)
  748. {
  749. int ret;
  750. if (RING_FULL(&info->urb_ring)) {
  751. list_add_tail(&urbp->list, &info->pending_submit_list);
  752. xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
  753. return 0;
  754. }
  755. if (!list_empty(&info->pending_submit_list)) {
  756. list_add_tail(&urbp->list, &info->pending_submit_list);
  757. xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
  758. return 0;
  759. }
  760. ret = xenhcd_do_request(info, urbp);
  761. if (ret == 0)
  762. list_add_tail(&urbp->list, &info->in_progress_list);
  763. return ret;
  764. }
  765. static int xenhcd_unlink_urb(struct xenhcd_info *info, struct urb_priv *urbp)
  766. {
  767. int ret;
  768. /* already unlinked? */
  769. if (urbp->unlinked)
  770. return -EBUSY;
  771. urbp->unlinked = true;
  772. /* the urb is still in pending_submit queue */
  773. if (urbp->req_id == ~0) {
  774. list_move_tail(&urbp->list, &info->giveback_waiting_list);
  775. xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
  776. return 0;
  777. }
  778. /* send unlink request to backend */
  779. if (RING_FULL(&info->urb_ring)) {
  780. list_move_tail(&urbp->list, &info->pending_unlink_list);
  781. xenhcd_timer_action(info, TIMER_RING_WATCHDOG);
  782. return 0;
  783. }
  784. if (!list_empty(&info->pending_unlink_list)) {
  785. list_move_tail(&urbp->list, &info->pending_unlink_list);
  786. xenhcd_timer_action(info, TIMER_SCAN_PENDING_URBS);
  787. return 0;
  788. }
  789. ret = xenhcd_do_request(info, urbp);
  790. if (ret == 0)
  791. list_move_tail(&urbp->list, &info->in_progress_list);
  792. return ret;
  793. }
  794. static void xenhcd_res_to_urb(struct xenhcd_info *info,
  795. struct xenusb_urb_response *res, struct urb *urb)
  796. {
  797. if (unlikely(!urb))
  798. return;
  799. if (res->actual_length > urb->transfer_buffer_length)
  800. urb->actual_length = urb->transfer_buffer_length;
  801. else if (res->actual_length < 0)
  802. urb->actual_length = 0;
  803. else
  804. urb->actual_length = res->actual_length;
  805. urb->error_count = res->error_count;
  806. urb->start_frame = res->start_frame;
  807. xenhcd_giveback_urb(info, urb, res->status);
  808. }
  809. static int xenhcd_urb_request_done(struct xenhcd_info *info,
  810. unsigned int *eoiflag)
  811. {
  812. struct xenusb_urb_response res;
  813. RING_IDX i, rp;
  814. __u16 id;
  815. int more_to_do = 0;
  816. unsigned long flags;
  817. spin_lock_irqsave(&info->lock, flags);
  818. rp = info->urb_ring.sring->rsp_prod;
  819. if (RING_RESPONSE_PROD_OVERFLOW(&info->urb_ring, rp)) {
  820. xenhcd_set_error(info, "Illegal index on urb-ring");
  821. goto err;
  822. }
  823. rmb(); /* ensure we see queued responses up to "rp" */
  824. for (i = info->urb_ring.rsp_cons; i != rp; i++) {
  825. RING_COPY_RESPONSE(&info->urb_ring, i, &res);
  826. id = res.id;
  827. if (id >= XENUSB_URB_RING_SIZE) {
  828. xenhcd_set_error(info, "Illegal data on urb-ring");
  829. goto err;
  830. }
  831. if (likely(xenusb_pipesubmit(info->shadow[id].req.pipe))) {
  832. xenhcd_gnttab_done(info, id);
  833. if (info->error)
  834. goto err;
  835. xenhcd_res_to_urb(info, &res, info->shadow[id].urb);
  836. }
  837. xenhcd_add_id_to_freelist(info, id);
  838. *eoiflag = 0;
  839. }
  840. info->urb_ring.rsp_cons = i;
  841. if (i != info->urb_ring.req_prod_pvt)
  842. RING_FINAL_CHECK_FOR_RESPONSES(&info->urb_ring, more_to_do);
  843. else
  844. info->urb_ring.sring->rsp_event = i + 1;
  845. spin_unlock_irqrestore(&info->lock, flags);
  846. return more_to_do;
  847. err:
  848. spin_unlock_irqrestore(&info->lock, flags);
  849. return 0;
  850. }
  851. static int xenhcd_conn_notify(struct xenhcd_info *info, unsigned int *eoiflag)
  852. {
  853. struct xenusb_conn_response res;
  854. struct xenusb_conn_request *req;
  855. RING_IDX rc, rp;
  856. __u16 id;
  857. __u8 portnum, speed;
  858. int more_to_do = 0;
  859. int notify;
  860. int port_changed = 0;
  861. unsigned long flags;
  862. spin_lock_irqsave(&info->lock, flags);
  863. rc = info->conn_ring.rsp_cons;
  864. rp = info->conn_ring.sring->rsp_prod;
  865. if (RING_RESPONSE_PROD_OVERFLOW(&info->conn_ring, rp)) {
  866. xenhcd_set_error(info, "Illegal index on conn-ring");
  867. spin_unlock_irqrestore(&info->lock, flags);
  868. return 0;
  869. }
  870. rmb(); /* ensure we see queued responses up to "rp" */
  871. while (rc != rp) {
  872. RING_COPY_RESPONSE(&info->conn_ring, rc, &res);
  873. id = res.id;
  874. portnum = res.portnum;
  875. speed = res.speed;
  876. info->conn_ring.rsp_cons = ++rc;
  877. if (xenhcd_rhport_connect(info, portnum, speed)) {
  878. xenhcd_set_error(info, "Illegal data on conn-ring");
  879. spin_unlock_irqrestore(&info->lock, flags);
  880. return 0;
  881. }
  882. if (info->ports[portnum - 1].c_connection)
  883. port_changed = 1;
  884. barrier();
  885. req = RING_GET_REQUEST(&info->conn_ring,
  886. info->conn_ring.req_prod_pvt);
  887. req->id = id;
  888. info->conn_ring.req_prod_pvt++;
  889. *eoiflag = 0;
  890. }
  891. if (rc != info->conn_ring.req_prod_pvt)
  892. RING_FINAL_CHECK_FOR_RESPONSES(&info->conn_ring, more_to_do);
  893. else
  894. info->conn_ring.sring->rsp_event = rc + 1;
  895. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
  896. if (notify)
  897. notify_remote_via_irq(info->irq);
  898. spin_unlock_irqrestore(&info->lock, flags);
  899. if (port_changed)
  900. usb_hcd_poll_rh_status(xenhcd_info_to_hcd(info));
  901. return more_to_do;
  902. }
  903. static irqreturn_t xenhcd_int(int irq, void *dev_id)
  904. {
  905. struct xenhcd_info *info = (struct xenhcd_info *)dev_id;
  906. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  907. if (unlikely(info->error)) {
  908. xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
  909. return IRQ_HANDLED;
  910. }
  911. while (xenhcd_urb_request_done(info, &eoiflag) |
  912. xenhcd_conn_notify(info, &eoiflag))
  913. /* Yield point for this unbounded loop. */
  914. cond_resched();
  915. xen_irq_lateeoi(irq, eoiflag);
  916. return IRQ_HANDLED;
  917. }
  918. static void xenhcd_destroy_rings(struct xenhcd_info *info)
  919. {
  920. if (info->irq)
  921. unbind_from_irqhandler(info->irq, info);
  922. info->irq = 0;
  923. xenbus_teardown_ring((void **)&info->urb_ring.sring, 1,
  924. &info->urb_ring_ref);
  925. xenbus_teardown_ring((void **)&info->conn_ring.sring, 1,
  926. &info->conn_ring_ref);
  927. }
  928. static int xenhcd_setup_rings(struct xenbus_device *dev,
  929. struct xenhcd_info *info)
  930. {
  931. struct xenusb_urb_sring *urb_sring;
  932. struct xenusb_conn_sring *conn_sring;
  933. int err;
  934. info->conn_ring_ref = INVALID_GRANT_REF;
  935. err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH,
  936. (void **)&urb_sring, 1, &info->urb_ring_ref);
  937. if (err) {
  938. xenbus_dev_fatal(dev, err, "allocating urb ring");
  939. return err;
  940. }
  941. XEN_FRONT_RING_INIT(&info->urb_ring, urb_sring, PAGE_SIZE);
  942. err = xenbus_setup_ring(dev, GFP_NOIO | __GFP_HIGH,
  943. (void **)&conn_sring, 1, &info->conn_ring_ref);
  944. if (err) {
  945. xenbus_dev_fatal(dev, err, "allocating conn ring");
  946. goto fail;
  947. }
  948. XEN_FRONT_RING_INIT(&info->conn_ring, conn_sring, PAGE_SIZE);
  949. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  950. if (err) {
  951. xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
  952. goto fail;
  953. }
  954. err = bind_evtchn_to_irq_lateeoi(info->evtchn);
  955. if (err <= 0) {
  956. xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq_lateeoi");
  957. goto fail;
  958. }
  959. info->irq = err;
  960. err = request_threaded_irq(info->irq, NULL, xenhcd_int,
  961. IRQF_ONESHOT, "xenhcd", info);
  962. if (err) {
  963. xenbus_dev_fatal(dev, err, "request_threaded_irq");
  964. goto free_irq;
  965. }
  966. return 0;
  967. free_irq:
  968. unbind_from_irqhandler(info->irq, info);
  969. fail:
  970. xenhcd_destroy_rings(info);
  971. return err;
  972. }
  973. static int xenhcd_talk_to_backend(struct xenbus_device *dev,
  974. struct xenhcd_info *info)
  975. {
  976. const char *message;
  977. struct xenbus_transaction xbt;
  978. int err;
  979. err = xenhcd_setup_rings(dev, info);
  980. if (err)
  981. return err;
  982. again:
  983. err = xenbus_transaction_start(&xbt);
  984. if (err) {
  985. xenbus_dev_fatal(dev, err, "starting transaction");
  986. goto destroy_ring;
  987. }
  988. err = xenbus_printf(xbt, dev->nodename, "urb-ring-ref", "%u",
  989. info->urb_ring_ref);
  990. if (err) {
  991. message = "writing urb-ring-ref";
  992. goto abort_transaction;
  993. }
  994. err = xenbus_printf(xbt, dev->nodename, "conn-ring-ref", "%u",
  995. info->conn_ring_ref);
  996. if (err) {
  997. message = "writing conn-ring-ref";
  998. goto abort_transaction;
  999. }
  1000. err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  1001. info->evtchn);
  1002. if (err) {
  1003. message = "writing event-channel";
  1004. goto abort_transaction;
  1005. }
  1006. err = xenbus_transaction_end(xbt, 0);
  1007. if (err) {
  1008. if (err == -EAGAIN)
  1009. goto again;
  1010. xenbus_dev_fatal(dev, err, "completing transaction");
  1011. goto destroy_ring;
  1012. }
  1013. return 0;
  1014. abort_transaction:
  1015. xenbus_transaction_end(xbt, 1);
  1016. xenbus_dev_fatal(dev, err, "%s", message);
  1017. destroy_ring:
  1018. xenhcd_destroy_rings(info);
  1019. return err;
  1020. }
  1021. static int xenhcd_connect(struct xenbus_device *dev)
  1022. {
  1023. struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
  1024. struct xenusb_conn_request *req;
  1025. int idx, err;
  1026. int notify;
  1027. char name[TASK_COMM_LEN];
  1028. struct usb_hcd *hcd;
  1029. hcd = xenhcd_info_to_hcd(info);
  1030. snprintf(name, TASK_COMM_LEN, "xenhcd.%d", hcd->self.busnum);
  1031. err = xenhcd_talk_to_backend(dev, info);
  1032. if (err)
  1033. return err;
  1034. /* prepare ring for hotplug notification */
  1035. for (idx = 0; idx < XENUSB_CONN_RING_SIZE; idx++) {
  1036. req = RING_GET_REQUEST(&info->conn_ring, idx);
  1037. req->id = idx;
  1038. }
  1039. info->conn_ring.req_prod_pvt = idx;
  1040. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->conn_ring, notify);
  1041. if (notify)
  1042. notify_remote_via_irq(info->irq);
  1043. return 0;
  1044. }
  1045. static void xenhcd_disconnect(struct xenbus_device *dev)
  1046. {
  1047. struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
  1048. struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
  1049. usb_remove_hcd(hcd);
  1050. xenbus_frontend_closed(dev);
  1051. }
  1052. static void xenhcd_watchdog(struct timer_list *timer)
  1053. {
  1054. struct xenhcd_info *info = from_timer(info, timer, watchdog);
  1055. unsigned long flags;
  1056. spin_lock_irqsave(&info->lock, flags);
  1057. if (likely(HC_IS_RUNNING(xenhcd_info_to_hcd(info)->state))) {
  1058. xenhcd_timer_action_done(info, TIMER_RING_WATCHDOG);
  1059. xenhcd_giveback_unlinked_urbs(info);
  1060. xenhcd_kick_pending_urbs(info);
  1061. }
  1062. spin_unlock_irqrestore(&info->lock, flags);
  1063. }
  1064. /*
  1065. * one-time HC init
  1066. */
  1067. static int xenhcd_setup(struct usb_hcd *hcd)
  1068. {
  1069. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  1070. spin_lock_init(&info->lock);
  1071. INIT_LIST_HEAD(&info->pending_submit_list);
  1072. INIT_LIST_HEAD(&info->pending_unlink_list);
  1073. INIT_LIST_HEAD(&info->in_progress_list);
  1074. INIT_LIST_HEAD(&info->giveback_waiting_list);
  1075. timer_setup(&info->watchdog, xenhcd_watchdog, 0);
  1076. hcd->has_tt = (hcd->driver->flags & HCD_MASK) != HCD_USB11;
  1077. return 0;
  1078. }
  1079. /*
  1080. * start HC running
  1081. */
  1082. static int xenhcd_run(struct usb_hcd *hcd)
  1083. {
  1084. hcd->uses_new_polling = 1;
  1085. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  1086. hcd->state = HC_STATE_RUNNING;
  1087. return 0;
  1088. }
  1089. /*
  1090. * stop running HC
  1091. */
  1092. static void xenhcd_stop(struct usb_hcd *hcd)
  1093. {
  1094. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  1095. del_timer_sync(&info->watchdog);
  1096. spin_lock_irq(&info->lock);
  1097. /* cancel all urbs */
  1098. hcd->state = HC_STATE_HALT;
  1099. xenhcd_cancel_all_enqueued_urbs(info);
  1100. xenhcd_giveback_unlinked_urbs(info);
  1101. spin_unlock_irq(&info->lock);
  1102. }
  1103. /*
  1104. * called as .urb_enqueue()
  1105. * non-error returns are promise to giveback the urb later
  1106. */
  1107. static int xenhcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  1108. gfp_t mem_flags)
  1109. {
  1110. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  1111. struct urb_priv *urbp;
  1112. unsigned long flags;
  1113. int ret;
  1114. if (unlikely(info->error))
  1115. return -ESHUTDOWN;
  1116. urbp = kmem_cache_zalloc(xenhcd_urbp_cachep, mem_flags);
  1117. if (!urbp)
  1118. return -ENOMEM;
  1119. spin_lock_irqsave(&info->lock, flags);
  1120. urbp->urb = urb;
  1121. urb->hcpriv = urbp;
  1122. urbp->req_id = ~0;
  1123. urbp->unlink_req_id = ~0;
  1124. INIT_LIST_HEAD(&urbp->list);
  1125. urbp->status = 1;
  1126. urb->unlinked = false;
  1127. ret = xenhcd_submit_urb(info, urbp);
  1128. if (ret)
  1129. xenhcd_free_urb_priv(urbp);
  1130. spin_unlock_irqrestore(&info->lock, flags);
  1131. return ret;
  1132. }
  1133. /*
  1134. * called as .urb_dequeue()
  1135. */
  1136. static int xenhcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1137. {
  1138. struct xenhcd_info *info = xenhcd_hcd_to_info(hcd);
  1139. struct urb_priv *urbp;
  1140. unsigned long flags;
  1141. int ret = 0;
  1142. spin_lock_irqsave(&info->lock, flags);
  1143. urbp = urb->hcpriv;
  1144. if (urbp) {
  1145. urbp->status = status;
  1146. ret = xenhcd_unlink_urb(info, urbp);
  1147. }
  1148. spin_unlock_irqrestore(&info->lock, flags);
  1149. return ret;
  1150. }
  1151. /*
  1152. * called from usb_get_current_frame_number(),
  1153. * but, almost all drivers not use such function.
  1154. */
  1155. static int xenhcd_get_frame(struct usb_hcd *hcd)
  1156. {
  1157. /* it means error, but probably no problem :-) */
  1158. return 0;
  1159. }
  1160. static struct hc_driver xenhcd_usb20_hc_driver = {
  1161. .description = "xen-hcd",
  1162. .product_desc = "Xen USB2.0 Virtual Host Controller",
  1163. .hcd_priv_size = sizeof(struct xenhcd_info),
  1164. .flags = HCD_USB2,
  1165. /* basic HC lifecycle operations */
  1166. .reset = xenhcd_setup,
  1167. .start = xenhcd_run,
  1168. .stop = xenhcd_stop,
  1169. /* managing urb I/O */
  1170. .urb_enqueue = xenhcd_urb_enqueue,
  1171. .urb_dequeue = xenhcd_urb_dequeue,
  1172. .get_frame_number = xenhcd_get_frame,
  1173. /* root hub operations */
  1174. .hub_status_data = xenhcd_hub_status_data,
  1175. .hub_control = xenhcd_hub_control,
  1176. #ifdef CONFIG_PM
  1177. .bus_suspend = xenhcd_bus_suspend,
  1178. .bus_resume = xenhcd_bus_resume,
  1179. #endif
  1180. };
  1181. static struct hc_driver xenhcd_usb11_hc_driver = {
  1182. .description = "xen-hcd",
  1183. .product_desc = "Xen USB1.1 Virtual Host Controller",
  1184. .hcd_priv_size = sizeof(struct xenhcd_info),
  1185. .flags = HCD_USB11,
  1186. /* basic HC lifecycle operations */
  1187. .reset = xenhcd_setup,
  1188. .start = xenhcd_run,
  1189. .stop = xenhcd_stop,
  1190. /* managing urb I/O */
  1191. .urb_enqueue = xenhcd_urb_enqueue,
  1192. .urb_dequeue = xenhcd_urb_dequeue,
  1193. .get_frame_number = xenhcd_get_frame,
  1194. /* root hub operations */
  1195. .hub_status_data = xenhcd_hub_status_data,
  1196. .hub_control = xenhcd_hub_control,
  1197. #ifdef CONFIG_PM
  1198. .bus_suspend = xenhcd_bus_suspend,
  1199. .bus_resume = xenhcd_bus_resume,
  1200. #endif
  1201. };
  1202. static struct usb_hcd *xenhcd_create_hcd(struct xenbus_device *dev)
  1203. {
  1204. int i;
  1205. int err = 0;
  1206. int num_ports;
  1207. int usb_ver;
  1208. struct usb_hcd *hcd = NULL;
  1209. struct xenhcd_info *info;
  1210. err = xenbus_scanf(XBT_NIL, dev->otherend, "num-ports", "%d",
  1211. &num_ports);
  1212. if (err != 1) {
  1213. xenbus_dev_fatal(dev, err, "reading num-ports");
  1214. return ERR_PTR(-EINVAL);
  1215. }
  1216. if (num_ports < 1 || num_ports > XENUSB_MAX_PORTNR) {
  1217. xenbus_dev_fatal(dev, err, "invalid num-ports");
  1218. return ERR_PTR(-EINVAL);
  1219. }
  1220. err = xenbus_scanf(XBT_NIL, dev->otherend, "usb-ver", "%d", &usb_ver);
  1221. if (err != 1) {
  1222. xenbus_dev_fatal(dev, err, "reading usb-ver");
  1223. return ERR_PTR(-EINVAL);
  1224. }
  1225. switch (usb_ver) {
  1226. case XENUSB_VER_USB11:
  1227. hcd = usb_create_hcd(&xenhcd_usb11_hc_driver, &dev->dev,
  1228. dev_name(&dev->dev));
  1229. break;
  1230. case XENUSB_VER_USB20:
  1231. hcd = usb_create_hcd(&xenhcd_usb20_hc_driver, &dev->dev,
  1232. dev_name(&dev->dev));
  1233. break;
  1234. default:
  1235. xenbus_dev_fatal(dev, err, "invalid usb-ver");
  1236. return ERR_PTR(-EINVAL);
  1237. }
  1238. if (!hcd) {
  1239. xenbus_dev_fatal(dev, err,
  1240. "fail to allocate USB host controller");
  1241. return ERR_PTR(-ENOMEM);
  1242. }
  1243. info = xenhcd_hcd_to_info(hcd);
  1244. info->xbdev = dev;
  1245. info->rh_numports = num_ports;
  1246. for (i = 0; i < XENUSB_URB_RING_SIZE; i++) {
  1247. info->shadow[i].req.id = i + 1;
  1248. info->shadow[i].urb = NULL;
  1249. info->shadow[i].in_flight = false;
  1250. }
  1251. info->shadow[XENUSB_URB_RING_SIZE - 1].req.id = 0x0fff;
  1252. return hcd;
  1253. }
  1254. static void xenhcd_backend_changed(struct xenbus_device *dev,
  1255. enum xenbus_state backend_state)
  1256. {
  1257. switch (backend_state) {
  1258. case XenbusStateInitialising:
  1259. case XenbusStateReconfiguring:
  1260. case XenbusStateReconfigured:
  1261. case XenbusStateUnknown:
  1262. break;
  1263. case XenbusStateInitWait:
  1264. case XenbusStateInitialised:
  1265. case XenbusStateConnected:
  1266. if (dev->state != XenbusStateInitialising)
  1267. break;
  1268. if (!xenhcd_connect(dev))
  1269. xenbus_switch_state(dev, XenbusStateConnected);
  1270. break;
  1271. case XenbusStateClosed:
  1272. if (dev->state == XenbusStateClosed)
  1273. break;
  1274. fallthrough; /* Missed the backend's Closing state. */
  1275. case XenbusStateClosing:
  1276. xenhcd_disconnect(dev);
  1277. break;
  1278. default:
  1279. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  1280. backend_state);
  1281. break;
  1282. }
  1283. }
  1284. static int xenhcd_remove(struct xenbus_device *dev)
  1285. {
  1286. struct xenhcd_info *info = dev_get_drvdata(&dev->dev);
  1287. struct usb_hcd *hcd = xenhcd_info_to_hcd(info);
  1288. xenhcd_destroy_rings(info);
  1289. usb_put_hcd(hcd);
  1290. return 0;
  1291. }
  1292. static int xenhcd_probe(struct xenbus_device *dev,
  1293. const struct xenbus_device_id *id)
  1294. {
  1295. int err;
  1296. struct usb_hcd *hcd;
  1297. struct xenhcd_info *info;
  1298. if (usb_disabled())
  1299. return -ENODEV;
  1300. hcd = xenhcd_create_hcd(dev);
  1301. if (IS_ERR(hcd)) {
  1302. err = PTR_ERR(hcd);
  1303. xenbus_dev_fatal(dev, err,
  1304. "fail to create usb host controller");
  1305. return err;
  1306. }
  1307. info = xenhcd_hcd_to_info(hcd);
  1308. dev_set_drvdata(&dev->dev, info);
  1309. err = usb_add_hcd(hcd, 0, 0);
  1310. if (err) {
  1311. xenbus_dev_fatal(dev, err, "fail to add USB host controller");
  1312. usb_put_hcd(hcd);
  1313. dev_set_drvdata(&dev->dev, NULL);
  1314. }
  1315. return err;
  1316. }
  1317. static const struct xenbus_device_id xenhcd_ids[] = {
  1318. { "vusb" },
  1319. { "" },
  1320. };
  1321. static struct xenbus_driver xenhcd_driver = {
  1322. .ids = xenhcd_ids,
  1323. .probe = xenhcd_probe,
  1324. .otherend_changed = xenhcd_backend_changed,
  1325. .remove = xenhcd_remove,
  1326. };
  1327. static int __init xenhcd_init(void)
  1328. {
  1329. if (!xen_domain())
  1330. return -ENODEV;
  1331. xenhcd_urbp_cachep = kmem_cache_create("xenhcd_urb_priv",
  1332. sizeof(struct urb_priv), 0, 0, NULL);
  1333. if (!xenhcd_urbp_cachep) {
  1334. pr_err("xenhcd failed to create kmem cache\n");
  1335. return -ENOMEM;
  1336. }
  1337. return xenbus_register_frontend(&xenhcd_driver);
  1338. }
  1339. module_init(xenhcd_init);
  1340. static void __exit xenhcd_exit(void)
  1341. {
  1342. kmem_cache_destroy(xenhcd_urbp_cachep);
  1343. xenbus_unregister_driver(&xenhcd_driver);
  1344. }
  1345. module_exit(xenhcd_exit);
  1346. MODULE_ALIAS("xen:vusb");
  1347. MODULE_AUTHOR("Juergen Gross <[email protected]>");
  1348. MODULE_DESCRIPTION("Xen USB Virtual Host Controller driver (xen-hcd)");
  1349. MODULE_LICENSE("Dual BSD/GPL");