dummy_hcd.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /*
  2. * dummy_hcd.c -- Dummy/Loopback USB host and device emulator driver.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * Copyright (C) 2003 David Brownell
  7. * Copyright (C) 2003-2005 Alan Stern
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. /*
  15. * This exposes a device side "USB gadget" API, driven by requests to a
  16. * Linux-USB host controller driver. USB traffic is simulated; there's
  17. * no need for USB hardware. Use this with two other drivers:
  18. *
  19. * - Gadget driver, responding to requests (slave);
  20. * - Host-side device driver, as already familiar in Linux.
  21. *
  22. * Having this all in one kernel can help some stages of development,
  23. * bypassing some hardware (and driver) issues. UML could help too.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/delay.h>
  28. #include <linux/ioport.h>
  29. #include <linux/slab.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/timer.h>
  33. #include <linux/list.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/gadget.h>
  38. #include <linux/usb/hcd.h>
  39. #include <linux/scatterlist.h>
  40. #include <asm/byteorder.h>
  41. #include <linux/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/unaligned.h>
  44. #define DRIVER_DESC "USB Host+Gadget Emulator"
  45. #define DRIVER_VERSION "02 May 2005"
  46. #define POWER_BUDGET 500 /* in mA; use 8 for low-power port testing */
  47. static const char driver_name[] = "dummy_hcd";
  48. static const char driver_desc[] = "USB Host+Gadget Emulator";
  49. static const char gadget_name[] = "dummy_udc";
  50. MODULE_DESCRIPTION(DRIVER_DESC);
  51. MODULE_AUTHOR("David Brownell");
  52. MODULE_LICENSE("GPL");
  53. struct dummy_hcd_module_parameters {
  54. bool is_super_speed;
  55. bool is_high_speed;
  56. };
  57. static struct dummy_hcd_module_parameters mod_data = {
  58. .is_super_speed = false,
  59. .is_high_speed = true,
  60. };
  61. module_param_named(is_super_speed, mod_data.is_super_speed, bool, S_IRUGO);
  62. MODULE_PARM_DESC(is_super_speed, "true to simulate SuperSpeed connection");
  63. module_param_named(is_high_speed, mod_data.is_high_speed, bool, S_IRUGO);
  64. MODULE_PARM_DESC(is_high_speed, "true to simulate HighSpeed connection");
  65. /*-------------------------------------------------------------------------*/
  66. /* gadget side driver data structres */
  67. struct dummy_ep {
  68. struct list_head queue;
  69. unsigned long last_io; /* jiffies timestamp */
  70. struct usb_gadget *gadget;
  71. const struct usb_endpoint_descriptor *desc;
  72. struct usb_ep ep;
  73. unsigned halted:1;
  74. unsigned wedged:1;
  75. unsigned already_seen:1;
  76. unsigned setup_stage:1;
  77. unsigned stream_en:1;
  78. };
  79. struct dummy_request {
  80. struct list_head queue; /* ep's requests */
  81. struct usb_request req;
  82. };
  83. static inline struct dummy_ep *usb_ep_to_dummy_ep(struct usb_ep *_ep)
  84. {
  85. return container_of(_ep, struct dummy_ep, ep);
  86. }
  87. static inline struct dummy_request *usb_request_to_dummy_request
  88. (struct usb_request *_req)
  89. {
  90. return container_of(_req, struct dummy_request, req);
  91. }
  92. /*-------------------------------------------------------------------------*/
  93. /*
  94. * Every device has ep0 for control requests, plus up to 30 more endpoints,
  95. * in one of two types:
  96. *
  97. * - Configurable: direction (in/out), type (bulk, iso, etc), and endpoint
  98. * number can be changed. Names like "ep-a" are used for this type.
  99. *
  100. * - Fixed Function: in other cases. some characteristics may be mutable;
  101. * that'd be hardware-specific. Names like "ep12out-bulk" are used.
  102. *
  103. * Gadget drivers are responsible for not setting up conflicting endpoint
  104. * configurations, illegal or unsupported packet lengths, and so on.
  105. */
  106. static const char ep0name[] = "ep0";
  107. static const char *const ep_name[] = {
  108. ep0name, /* everyone has ep0 */
  109. /* act like a pxa250: fifteen fixed function endpoints */
  110. "ep1in-bulk", "ep2out-bulk", "ep3in-iso", "ep4out-iso", "ep5in-int",
  111. "ep6in-bulk", "ep7out-bulk", "ep8in-iso", "ep9out-iso", "ep10in-int",
  112. "ep11in-bulk", "ep12out-bulk", "ep13in-iso", "ep14out-iso",
  113. "ep15in-int",
  114. /* or like sa1100: two fixed function endpoints */
  115. "ep1out-bulk", "ep2in-bulk",
  116. /* and now some generic EPs so we have enough in multi config */
  117. "ep3out", "ep4in", "ep5out", "ep6out", "ep7in", "ep8out", "ep9in",
  118. "ep10out", "ep11out", "ep12in", "ep13out", "ep14in", "ep15out",
  119. };
  120. #define DUMMY_ENDPOINTS ARRAY_SIZE(ep_name)
  121. /*-------------------------------------------------------------------------*/
  122. #define FIFO_SIZE 64
  123. struct urbp {
  124. struct urb *urb;
  125. struct list_head urbp_list;
  126. struct sg_mapping_iter miter;
  127. u32 miter_started;
  128. };
  129. enum dummy_rh_state {
  130. DUMMY_RH_RESET,
  131. DUMMY_RH_SUSPENDED,
  132. DUMMY_RH_RUNNING
  133. };
  134. struct dummy_hcd {
  135. struct dummy *dum;
  136. enum dummy_rh_state rh_state;
  137. struct timer_list timer;
  138. u32 port_status;
  139. u32 old_status;
  140. unsigned long re_timeout;
  141. struct usb_device *udev;
  142. struct list_head urbp_list;
  143. u32 stream_en_ep;
  144. u8 num_stream[30 / 2];
  145. unsigned active:1;
  146. unsigned old_active:1;
  147. unsigned resuming:1;
  148. };
  149. struct dummy {
  150. spinlock_t lock;
  151. /*
  152. * SLAVE/GADGET side support
  153. */
  154. struct dummy_ep ep[DUMMY_ENDPOINTS];
  155. int address;
  156. struct usb_gadget gadget;
  157. struct usb_gadget_driver *driver;
  158. struct dummy_request fifo_req;
  159. u8 fifo_buf[FIFO_SIZE];
  160. u16 devstatus;
  161. unsigned udc_suspended:1;
  162. unsigned pullup:1;
  163. /*
  164. * MASTER/HOST side support
  165. */
  166. struct dummy_hcd *hs_hcd;
  167. struct dummy_hcd *ss_hcd;
  168. };
  169. static inline struct dummy_hcd *hcd_to_dummy_hcd(struct usb_hcd *hcd)
  170. {
  171. return (struct dummy_hcd *) (hcd->hcd_priv);
  172. }
  173. static inline struct usb_hcd *dummy_hcd_to_hcd(struct dummy_hcd *dum)
  174. {
  175. return container_of((void *) dum, struct usb_hcd, hcd_priv);
  176. }
  177. static inline struct device *dummy_dev(struct dummy_hcd *dum)
  178. {
  179. return dummy_hcd_to_hcd(dum)->self.controller;
  180. }
  181. static inline struct device *udc_dev(struct dummy *dum)
  182. {
  183. return dum->gadget.dev.parent;
  184. }
  185. static inline struct dummy *ep_to_dummy(struct dummy_ep *ep)
  186. {
  187. return container_of(ep->gadget, struct dummy, gadget);
  188. }
  189. static inline struct dummy_hcd *gadget_to_dummy_hcd(struct usb_gadget *gadget)
  190. {
  191. struct dummy *dum = container_of(gadget, struct dummy, gadget);
  192. if (dum->gadget.speed == USB_SPEED_SUPER)
  193. return dum->ss_hcd;
  194. else
  195. return dum->hs_hcd;
  196. }
  197. static inline struct dummy *gadget_dev_to_dummy(struct device *dev)
  198. {
  199. return container_of(dev, struct dummy, gadget.dev);
  200. }
  201. static struct dummy the_controller;
  202. /*-------------------------------------------------------------------------*/
  203. /* SLAVE/GADGET SIDE UTILITY ROUTINES */
  204. /* called with spinlock held */
  205. static void nuke(struct dummy *dum, struct dummy_ep *ep)
  206. {
  207. while (!list_empty(&ep->queue)) {
  208. struct dummy_request *req;
  209. req = list_entry(ep->queue.next, struct dummy_request, queue);
  210. list_del_init(&req->queue);
  211. req->req.status = -ESHUTDOWN;
  212. spin_unlock(&dum->lock);
  213. req->req.complete(&ep->ep, &req->req);
  214. spin_lock(&dum->lock);
  215. }
  216. }
  217. /* caller must hold lock */
  218. static void stop_activity(struct dummy *dum)
  219. {
  220. struct dummy_ep *ep;
  221. /* prevent any more requests */
  222. dum->address = 0;
  223. /* The timer is left running so that outstanding URBs can fail */
  224. /* nuke any pending requests first, so driver i/o is quiesced */
  225. list_for_each_entry(ep, &dum->gadget.ep_list, ep.ep_list)
  226. nuke(dum, ep);
  227. /* driver now does any non-usb quiescing necessary */
  228. }
  229. /**
  230. * set_link_state_by_speed() - Sets the current state of the link according to
  231. * the hcd speed
  232. * @dum_hcd: pointer to the dummy_hcd structure to update the link state for
  233. *
  234. * This function updates the port_status according to the link state and the
  235. * speed of the hcd.
  236. */
  237. static void set_link_state_by_speed(struct dummy_hcd *dum_hcd)
  238. {
  239. struct dummy *dum = dum_hcd->dum;
  240. if (dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3) {
  241. if ((dum_hcd->port_status & USB_SS_PORT_STAT_POWER) == 0) {
  242. dum_hcd->port_status = 0;
  243. } else if (!dum->pullup || dum->udc_suspended) {
  244. /* UDC suspend must cause a disconnect */
  245. dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
  246. USB_PORT_STAT_ENABLE);
  247. if ((dum_hcd->old_status &
  248. USB_PORT_STAT_CONNECTION) != 0)
  249. dum_hcd->port_status |=
  250. (USB_PORT_STAT_C_CONNECTION << 16);
  251. } else {
  252. /* device is connected and not suspended */
  253. dum_hcd->port_status |= (USB_PORT_STAT_CONNECTION |
  254. USB_PORT_STAT_SPEED_5GBPS) ;
  255. if ((dum_hcd->old_status &
  256. USB_PORT_STAT_CONNECTION) == 0)
  257. dum_hcd->port_status |=
  258. (USB_PORT_STAT_C_CONNECTION << 16);
  259. if ((dum_hcd->port_status &
  260. USB_PORT_STAT_ENABLE) == 1 &&
  261. (dum_hcd->port_status &
  262. USB_SS_PORT_LS_U0) == 1 &&
  263. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  264. dum_hcd->active = 1;
  265. }
  266. } else {
  267. if ((dum_hcd->port_status & USB_PORT_STAT_POWER) == 0) {
  268. dum_hcd->port_status = 0;
  269. } else if (!dum->pullup || dum->udc_suspended) {
  270. /* UDC suspend must cause a disconnect */
  271. dum_hcd->port_status &= ~(USB_PORT_STAT_CONNECTION |
  272. USB_PORT_STAT_ENABLE |
  273. USB_PORT_STAT_LOW_SPEED |
  274. USB_PORT_STAT_HIGH_SPEED |
  275. USB_PORT_STAT_SUSPEND);
  276. if ((dum_hcd->old_status &
  277. USB_PORT_STAT_CONNECTION) != 0)
  278. dum_hcd->port_status |=
  279. (USB_PORT_STAT_C_CONNECTION << 16);
  280. } else {
  281. dum_hcd->port_status |= USB_PORT_STAT_CONNECTION;
  282. if ((dum_hcd->old_status &
  283. USB_PORT_STAT_CONNECTION) == 0)
  284. dum_hcd->port_status |=
  285. (USB_PORT_STAT_C_CONNECTION << 16);
  286. if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0)
  287. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  288. else if ((dum_hcd->port_status &
  289. USB_PORT_STAT_SUSPEND) == 0 &&
  290. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  291. dum_hcd->active = 1;
  292. }
  293. }
  294. }
  295. /* caller must hold lock */
  296. static void set_link_state(struct dummy_hcd *dum_hcd)
  297. {
  298. struct dummy *dum = dum_hcd->dum;
  299. dum_hcd->active = 0;
  300. if (dum->pullup)
  301. if ((dummy_hcd_to_hcd(dum_hcd)->speed == HCD_USB3 &&
  302. dum->gadget.speed != USB_SPEED_SUPER) ||
  303. (dummy_hcd_to_hcd(dum_hcd)->speed != HCD_USB3 &&
  304. dum->gadget.speed == USB_SPEED_SUPER))
  305. return;
  306. set_link_state_by_speed(dum_hcd);
  307. if ((dum_hcd->port_status & USB_PORT_STAT_ENABLE) == 0 ||
  308. dum_hcd->active)
  309. dum_hcd->resuming = 0;
  310. /* if !connected or reset */
  311. if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
  312. (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
  313. /*
  314. * We're connected and not reset (reset occurred now),
  315. * and driver attached - disconnect!
  316. */
  317. if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
  318. (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
  319. dum->driver) {
  320. stop_activity(dum);
  321. spin_unlock(&dum->lock);
  322. dum->driver->disconnect(&dum->gadget);
  323. spin_lock(&dum->lock);
  324. }
  325. } else if (dum_hcd->active != dum_hcd->old_active) {
  326. if (dum_hcd->old_active && dum->driver->suspend) {
  327. spin_unlock(&dum->lock);
  328. dum->driver->suspend(&dum->gadget);
  329. spin_lock(&dum->lock);
  330. } else if (!dum_hcd->old_active && dum->driver->resume) {
  331. spin_unlock(&dum->lock);
  332. dum->driver->resume(&dum->gadget);
  333. spin_lock(&dum->lock);
  334. }
  335. }
  336. dum_hcd->old_status = dum_hcd->port_status;
  337. dum_hcd->old_active = dum_hcd->active;
  338. }
  339. /*-------------------------------------------------------------------------*/
  340. /* SLAVE/GADGET SIDE DRIVER
  341. *
  342. * This only tracks gadget state. All the work is done when the host
  343. * side tries some (emulated) i/o operation. Real device controller
  344. * drivers would do real i/o using dma, fifos, irqs, timers, etc.
  345. */
  346. #define is_enabled(dum) \
  347. (dum->port_status & USB_PORT_STAT_ENABLE)
  348. static int dummy_enable(struct usb_ep *_ep,
  349. const struct usb_endpoint_descriptor *desc)
  350. {
  351. struct dummy *dum;
  352. struct dummy_hcd *dum_hcd;
  353. struct dummy_ep *ep;
  354. unsigned max;
  355. int retval;
  356. ep = usb_ep_to_dummy_ep(_ep);
  357. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  358. || desc->bDescriptorType != USB_DT_ENDPOINT)
  359. return -EINVAL;
  360. dum = ep_to_dummy(ep);
  361. if (!dum->driver)
  362. return -ESHUTDOWN;
  363. dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  364. if (!is_enabled(dum_hcd))
  365. return -ESHUTDOWN;
  366. /*
  367. * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
  368. * maximum packet size.
  369. * For SS devices the wMaxPacketSize is limited by 1024.
  370. */
  371. max = usb_endpoint_maxp(desc) & 0x7ff;
  372. /* drivers must not request bad settings, since lower levels
  373. * (hardware or its drivers) may not check. some endpoints
  374. * can't do iso, many have maxpacket limitations, etc.
  375. *
  376. * since this "hardware" driver is here to help debugging, we
  377. * have some extra sanity checks. (there could be more though,
  378. * especially for "ep9out" style fixed function ones.)
  379. */
  380. retval = -EINVAL;
  381. switch (usb_endpoint_type(desc)) {
  382. case USB_ENDPOINT_XFER_BULK:
  383. if (strstr(ep->ep.name, "-iso")
  384. || strstr(ep->ep.name, "-int")) {
  385. goto done;
  386. }
  387. switch (dum->gadget.speed) {
  388. case USB_SPEED_SUPER:
  389. if (max == 1024)
  390. break;
  391. goto done;
  392. case USB_SPEED_HIGH:
  393. if (max == 512)
  394. break;
  395. goto done;
  396. case USB_SPEED_FULL:
  397. if (max == 8 || max == 16 || max == 32 || max == 64)
  398. /* we'll fake any legal size */
  399. break;
  400. /* save a return statement */
  401. default:
  402. goto done;
  403. }
  404. break;
  405. case USB_ENDPOINT_XFER_INT:
  406. if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
  407. goto done;
  408. /* real hardware might not handle all packet sizes */
  409. switch (dum->gadget.speed) {
  410. case USB_SPEED_SUPER:
  411. case USB_SPEED_HIGH:
  412. if (max <= 1024)
  413. break;
  414. /* save a return statement */
  415. case USB_SPEED_FULL:
  416. if (max <= 64)
  417. break;
  418. /* save a return statement */
  419. default:
  420. if (max <= 8)
  421. break;
  422. goto done;
  423. }
  424. break;
  425. case USB_ENDPOINT_XFER_ISOC:
  426. if (strstr(ep->ep.name, "-bulk")
  427. || strstr(ep->ep.name, "-int"))
  428. goto done;
  429. /* real hardware might not handle all packet sizes */
  430. switch (dum->gadget.speed) {
  431. case USB_SPEED_SUPER:
  432. case USB_SPEED_HIGH:
  433. if (max <= 1024)
  434. break;
  435. /* save a return statement */
  436. case USB_SPEED_FULL:
  437. if (max <= 1023)
  438. break;
  439. /* save a return statement */
  440. default:
  441. goto done;
  442. }
  443. break;
  444. default:
  445. /* few chips support control except on ep0 */
  446. goto done;
  447. }
  448. _ep->maxpacket = max;
  449. if (usb_ss_max_streams(_ep->comp_desc)) {
  450. if (!usb_endpoint_xfer_bulk(desc)) {
  451. dev_err(udc_dev(dum), "Can't enable stream support on "
  452. "non-bulk ep %s\n", _ep->name);
  453. return -EINVAL;
  454. }
  455. ep->stream_en = 1;
  456. }
  457. ep->desc = desc;
  458. dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
  459. _ep->name,
  460. desc->bEndpointAddress & 0x0f,
  461. (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  462. ({ char *val;
  463. switch (usb_endpoint_type(desc)) {
  464. case USB_ENDPOINT_XFER_BULK:
  465. val = "bulk";
  466. break;
  467. case USB_ENDPOINT_XFER_ISOC:
  468. val = "iso";
  469. break;
  470. case USB_ENDPOINT_XFER_INT:
  471. val = "intr";
  472. break;
  473. default:
  474. val = "ctrl";
  475. break;
  476. }; val; }),
  477. max, ep->stream_en ? "enabled" : "disabled");
  478. /* at this point real hardware should be NAKing transfers
  479. * to that endpoint, until a buffer is queued to it.
  480. */
  481. ep->halted = ep->wedged = 0;
  482. retval = 0;
  483. done:
  484. return retval;
  485. }
  486. static int dummy_disable(struct usb_ep *_ep)
  487. {
  488. struct dummy_ep *ep;
  489. struct dummy *dum;
  490. unsigned long flags;
  491. int retval;
  492. ep = usb_ep_to_dummy_ep(_ep);
  493. if (!_ep || !ep->desc || _ep->name == ep0name)
  494. return -EINVAL;
  495. dum = ep_to_dummy(ep);
  496. spin_lock_irqsave(&dum->lock, flags);
  497. ep->desc = NULL;
  498. ep->stream_en = 0;
  499. retval = 0;
  500. nuke(dum, ep);
  501. spin_unlock_irqrestore(&dum->lock, flags);
  502. dev_dbg(udc_dev(dum), "disabled %s\n", _ep->name);
  503. return retval;
  504. }
  505. static struct usb_request *dummy_alloc_request(struct usb_ep *_ep,
  506. gfp_t mem_flags)
  507. {
  508. struct dummy_ep *ep;
  509. struct dummy_request *req;
  510. if (!_ep)
  511. return NULL;
  512. ep = usb_ep_to_dummy_ep(_ep);
  513. req = kzalloc(sizeof(*req), mem_flags);
  514. if (!req)
  515. return NULL;
  516. INIT_LIST_HEAD(&req->queue);
  517. return &req->req;
  518. }
  519. static void dummy_free_request(struct usb_ep *_ep, struct usb_request *_req)
  520. {
  521. struct dummy_ep *ep;
  522. struct dummy_request *req;
  523. if (!_ep || !_req)
  524. return;
  525. ep = usb_ep_to_dummy_ep(_ep);
  526. if (!ep->desc && _ep->name != ep0name)
  527. return;
  528. req = usb_request_to_dummy_request(_req);
  529. WARN_ON(!list_empty(&req->queue));
  530. kfree(req);
  531. }
  532. static void fifo_complete(struct usb_ep *ep, struct usb_request *req)
  533. {
  534. }
  535. static int dummy_queue(struct usb_ep *_ep, struct usb_request *_req,
  536. gfp_t mem_flags)
  537. {
  538. struct dummy_ep *ep;
  539. struct dummy_request *req;
  540. struct dummy *dum;
  541. struct dummy_hcd *dum_hcd;
  542. unsigned long flags;
  543. req = usb_request_to_dummy_request(_req);
  544. if (!_req || !list_empty(&req->queue) || !_req->complete)
  545. return -EINVAL;
  546. ep = usb_ep_to_dummy_ep(_ep);
  547. if (!_ep || (!ep->desc && _ep->name != ep0name))
  548. return -EINVAL;
  549. dum = ep_to_dummy(ep);
  550. dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  551. if (!dum->driver || !is_enabled(dum_hcd))
  552. return -ESHUTDOWN;
  553. #if 0
  554. dev_dbg(udc_dev(dum), "ep %pK queue req %pK to %s, len %d buf %pK\n",
  555. ep, _req, _ep->name, _req->length, _req->buf);
  556. #endif
  557. _req->status = -EINPROGRESS;
  558. _req->actual = 0;
  559. spin_lock_irqsave(&dum->lock, flags);
  560. /* implement an emulated single-request FIFO */
  561. if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  562. list_empty(&dum->fifo_req.queue) &&
  563. list_empty(&ep->queue) &&
  564. _req->length <= FIFO_SIZE) {
  565. req = &dum->fifo_req;
  566. req->req = *_req;
  567. req->req.buf = dum->fifo_buf;
  568. memcpy(dum->fifo_buf, _req->buf, _req->length);
  569. req->req.context = dum;
  570. req->req.complete = fifo_complete;
  571. list_add_tail(&req->queue, &ep->queue);
  572. spin_unlock(&dum->lock);
  573. _req->actual = _req->length;
  574. _req->status = 0;
  575. _req->complete(_ep, _req);
  576. spin_lock(&dum->lock);
  577. } else
  578. list_add_tail(&req->queue, &ep->queue);
  579. spin_unlock_irqrestore(&dum->lock, flags);
  580. /* real hardware would likely enable transfers here, in case
  581. * it'd been left NAKing.
  582. */
  583. return 0;
  584. }
  585. static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  586. {
  587. struct dummy_ep *ep;
  588. struct dummy *dum;
  589. int retval = -EINVAL;
  590. unsigned long flags;
  591. struct dummy_request *req = NULL;
  592. if (!_ep || !_req)
  593. return retval;
  594. ep = usb_ep_to_dummy_ep(_ep);
  595. dum = ep_to_dummy(ep);
  596. if (!dum->driver)
  597. return -ESHUTDOWN;
  598. local_irq_save(flags);
  599. spin_lock(&dum->lock);
  600. list_for_each_entry(req, &ep->queue, queue) {
  601. if (&req->req == _req) {
  602. list_del_init(&req->queue);
  603. _req->status = -ECONNRESET;
  604. retval = 0;
  605. break;
  606. }
  607. }
  608. spin_unlock(&dum->lock);
  609. if (retval == 0) {
  610. dev_dbg(udc_dev(dum),
  611. "dequeued req %pK from %s, len %d buf %pK\n",
  612. req, _ep->name, _req->length, _req->buf);
  613. _req->complete(_ep, _req);
  614. }
  615. local_irq_restore(flags);
  616. return retval;
  617. }
  618. static int
  619. dummy_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
  620. {
  621. struct dummy_ep *ep;
  622. struct dummy *dum;
  623. if (!_ep)
  624. return -EINVAL;
  625. ep = usb_ep_to_dummy_ep(_ep);
  626. dum = ep_to_dummy(ep);
  627. if (!dum->driver)
  628. return -ESHUTDOWN;
  629. if (!value)
  630. ep->halted = ep->wedged = 0;
  631. else if (ep->desc && (ep->desc->bEndpointAddress & USB_DIR_IN) &&
  632. !list_empty(&ep->queue))
  633. return -EAGAIN;
  634. else {
  635. ep->halted = 1;
  636. if (wedged)
  637. ep->wedged = 1;
  638. }
  639. /* FIXME clear emulated data toggle too */
  640. return 0;
  641. }
  642. static int
  643. dummy_set_halt(struct usb_ep *_ep, int value)
  644. {
  645. return dummy_set_halt_and_wedge(_ep, value, 0);
  646. }
  647. static int dummy_set_wedge(struct usb_ep *_ep)
  648. {
  649. if (!_ep || _ep->name == ep0name)
  650. return -EINVAL;
  651. return dummy_set_halt_and_wedge(_ep, 1, 1);
  652. }
  653. static const struct usb_ep_ops dummy_ep_ops = {
  654. .enable = dummy_enable,
  655. .disable = dummy_disable,
  656. .alloc_request = dummy_alloc_request,
  657. .free_request = dummy_free_request,
  658. .queue = dummy_queue,
  659. .dequeue = dummy_dequeue,
  660. .set_halt = dummy_set_halt,
  661. .set_wedge = dummy_set_wedge,
  662. };
  663. /*-------------------------------------------------------------------------*/
  664. /* there are both host and device side versions of this call ... */
  665. static int dummy_g_get_frame(struct usb_gadget *_gadget)
  666. {
  667. struct timeval tv;
  668. do_gettimeofday(&tv);
  669. return tv.tv_usec / 1000;
  670. }
  671. static int dummy_wakeup(struct usb_gadget *_gadget)
  672. {
  673. struct dummy_hcd *dum_hcd;
  674. dum_hcd = gadget_to_dummy_hcd(_gadget);
  675. if (!(dum_hcd->dum->devstatus & ((1 << USB_DEVICE_B_HNP_ENABLE)
  676. | (1 << USB_DEVICE_REMOTE_WAKEUP))))
  677. return -EINVAL;
  678. if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0)
  679. return -ENOLINK;
  680. if ((dum_hcd->port_status & USB_PORT_STAT_SUSPEND) == 0 &&
  681. dum_hcd->rh_state != DUMMY_RH_SUSPENDED)
  682. return -EIO;
  683. /* FIXME: What if the root hub is suspended but the port isn't? */
  684. /* hub notices our request, issues downstream resume, etc */
  685. dum_hcd->resuming = 1;
  686. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
  687. mod_timer(&dummy_hcd_to_hcd(dum_hcd)->rh_timer, dum_hcd->re_timeout);
  688. return 0;
  689. }
  690. static int dummy_set_selfpowered(struct usb_gadget *_gadget, int value)
  691. {
  692. struct dummy *dum;
  693. dum = gadget_to_dummy_hcd(_gadget)->dum;
  694. if (value)
  695. dum->devstatus |= (1 << USB_DEVICE_SELF_POWERED);
  696. else
  697. dum->devstatus &= ~(1 << USB_DEVICE_SELF_POWERED);
  698. return 0;
  699. }
  700. static void dummy_udc_update_ep0(struct dummy *dum)
  701. {
  702. if (dum->gadget.speed == USB_SPEED_SUPER)
  703. dum->ep[0].ep.maxpacket = 9;
  704. else
  705. dum->ep[0].ep.maxpacket = 64;
  706. }
  707. static int dummy_pullup(struct usb_gadget *_gadget, int value)
  708. {
  709. struct dummy_hcd *dum_hcd;
  710. struct dummy *dum;
  711. unsigned long flags;
  712. dum = gadget_dev_to_dummy(&_gadget->dev);
  713. if (value && dum->driver) {
  714. if (mod_data.is_super_speed)
  715. dum->gadget.speed = dum->driver->max_speed;
  716. else if (mod_data.is_high_speed)
  717. dum->gadget.speed = min_t(u8, USB_SPEED_HIGH,
  718. dum->driver->max_speed);
  719. else
  720. dum->gadget.speed = USB_SPEED_FULL;
  721. dummy_udc_update_ep0(dum);
  722. if (dum->gadget.speed < dum->driver->max_speed)
  723. dev_dbg(udc_dev(dum), "This device can perform faster"
  724. " if you connect it to a %s port...\n",
  725. usb_speed_string(dum->driver->max_speed));
  726. }
  727. dum_hcd = gadget_to_dummy_hcd(_gadget);
  728. spin_lock_irqsave(&dum->lock, flags);
  729. dum->pullup = (value != 0);
  730. set_link_state(dum_hcd);
  731. spin_unlock_irqrestore(&dum->lock, flags);
  732. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  733. return 0;
  734. }
  735. static int dummy_udc_start(struct usb_gadget *g,
  736. struct usb_gadget_driver *driver);
  737. static int dummy_udc_stop(struct usb_gadget *g,
  738. struct usb_gadget_driver *driver);
  739. static const struct usb_gadget_ops dummy_ops = {
  740. .get_frame = dummy_g_get_frame,
  741. .wakeup = dummy_wakeup,
  742. .set_selfpowered = dummy_set_selfpowered,
  743. .pullup = dummy_pullup,
  744. .udc_start = dummy_udc_start,
  745. .udc_stop = dummy_udc_stop,
  746. };
  747. /*-------------------------------------------------------------------------*/
  748. /* "function" sysfs attribute */
  749. static ssize_t show_function(struct device *dev, struct device_attribute *attr,
  750. char *buf)
  751. {
  752. struct dummy *dum = gadget_dev_to_dummy(dev);
  753. if (!dum->driver || !dum->driver->function)
  754. return 0;
  755. return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
  756. }
  757. static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
  758. /*-------------------------------------------------------------------------*/
  759. /*
  760. * Driver registration/unregistration.
  761. *
  762. * This is basically hardware-specific; there's usually only one real USB
  763. * device (not host) controller since that's how USB devices are intended
  764. * to work. So most implementations of these api calls will rely on the
  765. * fact that only one driver will ever bind to the hardware. But curious
  766. * hardware can be built with discrete components, so the gadget API doesn't
  767. * require that assumption.
  768. *
  769. * For this emulator, it might be convenient to create a usb slave device
  770. * for each driver that registers: just add to a big root hub.
  771. */
  772. static int dummy_udc_start(struct usb_gadget *g,
  773. struct usb_gadget_driver *driver)
  774. {
  775. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  776. struct dummy *dum = dum_hcd->dum;
  777. if (driver->max_speed == USB_SPEED_UNKNOWN)
  778. return -EINVAL;
  779. /*
  780. * SLAVE side init ... the layer above hardware, which
  781. * can't enumerate without help from the driver we're binding.
  782. */
  783. dum->devstatus = 0;
  784. dum->driver = driver;
  785. dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
  786. driver->driver.name);
  787. return 0;
  788. }
  789. static int dummy_udc_stop(struct usb_gadget *g,
  790. struct usb_gadget_driver *driver)
  791. {
  792. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  793. struct dummy *dum = dum_hcd->dum;
  794. dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
  795. driver->driver.name);
  796. dum->driver = NULL;
  797. return 0;
  798. }
  799. #undef is_enabled
  800. /* The gadget structure is stored inside the hcd structure and will be
  801. * released along with it. */
  802. static void dummy_gadget_release(struct device *dev)
  803. {
  804. return;
  805. }
  806. static void init_dummy_udc_hw(struct dummy *dum)
  807. {
  808. int i;
  809. INIT_LIST_HEAD(&dum->gadget.ep_list);
  810. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  811. struct dummy_ep *ep = &dum->ep[i];
  812. if (!ep_name[i])
  813. break;
  814. ep->ep.name = ep_name[i];
  815. ep->ep.ops = &dummy_ep_ops;
  816. list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
  817. ep->halted = ep->wedged = ep->already_seen =
  818. ep->setup_stage = 0;
  819. ep->ep.maxpacket = ~0;
  820. ep->ep.max_streams = 16;
  821. ep->last_io = jiffies;
  822. ep->gadget = &dum->gadget;
  823. ep->desc = NULL;
  824. INIT_LIST_HEAD(&ep->queue);
  825. }
  826. dum->gadget.ep0 = &dum->ep[0].ep;
  827. list_del_init(&dum->ep[0].ep.ep_list);
  828. INIT_LIST_HEAD(&dum->fifo_req.queue);
  829. #ifdef CONFIG_USB_OTG
  830. dum->gadget.is_otg = 1;
  831. #endif
  832. }
  833. static int dummy_udc_probe(struct platform_device *pdev)
  834. {
  835. struct dummy *dum = &the_controller;
  836. int rc;
  837. dum->gadget.name = gadget_name;
  838. dum->gadget.ops = &dummy_ops;
  839. dum->gadget.max_speed = USB_SPEED_SUPER;
  840. dev_set_name(&dum->gadget.dev, "gadget");
  841. dum->gadget.dev.parent = &pdev->dev;
  842. dum->gadget.dev.release = dummy_gadget_release;
  843. rc = device_register(&dum->gadget.dev);
  844. if (rc < 0) {
  845. put_device(&dum->gadget.dev);
  846. return rc;
  847. }
  848. init_dummy_udc_hw(dum);
  849. rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
  850. if (rc < 0)
  851. goto err_udc;
  852. rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
  853. if (rc < 0)
  854. goto err_dev;
  855. platform_set_drvdata(pdev, dum);
  856. return rc;
  857. err_dev:
  858. usb_del_gadget_udc(&dum->gadget);
  859. err_udc:
  860. device_unregister(&dum->gadget.dev);
  861. return rc;
  862. }
  863. static int dummy_udc_remove(struct platform_device *pdev)
  864. {
  865. struct dummy *dum = platform_get_drvdata(pdev);
  866. usb_del_gadget_udc(&dum->gadget);
  867. platform_set_drvdata(pdev, NULL);
  868. device_remove_file(&dum->gadget.dev, &dev_attr_function);
  869. device_unregister(&dum->gadget.dev);
  870. return 0;
  871. }
  872. static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
  873. int suspend)
  874. {
  875. spin_lock_irq(&dum->lock);
  876. dum->udc_suspended = suspend;
  877. set_link_state(dum_hcd);
  878. spin_unlock_irq(&dum->lock);
  879. }
  880. static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
  881. {
  882. struct dummy *dum = platform_get_drvdata(pdev);
  883. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  884. dev_dbg(&pdev->dev, "%s\n", __func__);
  885. dummy_udc_pm(dum, dum_hcd, 1);
  886. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  887. return 0;
  888. }
  889. static int dummy_udc_resume(struct platform_device *pdev)
  890. {
  891. struct dummy *dum = platform_get_drvdata(pdev);
  892. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  893. dev_dbg(&pdev->dev, "%s\n", __func__);
  894. dummy_udc_pm(dum, dum_hcd, 0);
  895. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  896. return 0;
  897. }
  898. static struct platform_driver dummy_udc_driver = {
  899. .probe = dummy_udc_probe,
  900. .remove = dummy_udc_remove,
  901. .suspend = dummy_udc_suspend,
  902. .resume = dummy_udc_resume,
  903. .driver = {
  904. .name = (char *) gadget_name,
  905. .owner = THIS_MODULE,
  906. },
  907. };
  908. /*-------------------------------------------------------------------------*/
  909. static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
  910. {
  911. unsigned int index;
  912. index = usb_endpoint_num(desc) << 1;
  913. if (usb_endpoint_dir_in(desc))
  914. index |= 1;
  915. return index;
  916. }
  917. /* MASTER/HOST SIDE DRIVER
  918. *
  919. * this uses the hcd framework to hook up to host side drivers.
  920. * its root hub will only have one device, otherwise it acts like
  921. * a normal host controller.
  922. *
  923. * when urbs are queued, they're just stuck on a list that we
  924. * scan in a timer callback. that callback connects writes from
  925. * the host with reads from the device, and so on, based on the
  926. * usb 2.0 rules.
  927. */
  928. static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
  929. {
  930. const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
  931. u32 index;
  932. if (!usb_endpoint_xfer_bulk(desc))
  933. return 0;
  934. index = dummy_get_ep_idx(desc);
  935. return (1 << index) & dum_hcd->stream_en_ep;
  936. }
  937. /*
  938. * The max stream number is saved as a nibble so for the 30 possible endpoints
  939. * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
  940. * means we use only 1 stream). The maximum according to the spec is 16bit so
  941. * if the 16 stream limit is about to go, the array size should be incremented
  942. * to 30 elements of type u16.
  943. */
  944. static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
  945. unsigned int pipe)
  946. {
  947. int max_streams;
  948. max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
  949. if (usb_pipeout(pipe))
  950. max_streams >>= 4;
  951. else
  952. max_streams &= 0xf;
  953. max_streams++;
  954. return max_streams;
  955. }
  956. static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
  957. unsigned int pipe, unsigned int streams)
  958. {
  959. int max_streams;
  960. streams--;
  961. max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
  962. if (usb_pipeout(pipe)) {
  963. streams <<= 4;
  964. max_streams &= 0xf;
  965. } else {
  966. max_streams &= 0xf0;
  967. }
  968. max_streams |= streams;
  969. dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
  970. }
  971. static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
  972. {
  973. unsigned int max_streams;
  974. int enabled;
  975. enabled = dummy_ep_stream_en(dum_hcd, urb);
  976. if (!urb->stream_id) {
  977. if (enabled)
  978. return -EINVAL;
  979. return 0;
  980. }
  981. if (!enabled)
  982. return -EINVAL;
  983. max_streams = get_max_streams_for_pipe(dum_hcd,
  984. usb_pipeendpoint(urb->pipe));
  985. if (urb->stream_id > max_streams) {
  986. dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
  987. urb->stream_id);
  988. BUG();
  989. return -EINVAL;
  990. }
  991. return 0;
  992. }
  993. static int dummy_urb_enqueue(
  994. struct usb_hcd *hcd,
  995. struct urb *urb,
  996. gfp_t mem_flags
  997. ) {
  998. struct dummy_hcd *dum_hcd;
  999. struct urbp *urbp;
  1000. unsigned long flags;
  1001. int rc;
  1002. urbp = kmalloc(sizeof *urbp, mem_flags);
  1003. if (!urbp)
  1004. return -ENOMEM;
  1005. urbp->urb = urb;
  1006. urbp->miter_started = 0;
  1007. dum_hcd = hcd_to_dummy_hcd(hcd);
  1008. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1009. rc = dummy_validate_stream(dum_hcd, urb);
  1010. if (rc) {
  1011. kfree(urbp);
  1012. goto done;
  1013. }
  1014. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  1015. if (rc) {
  1016. kfree(urbp);
  1017. goto done;
  1018. }
  1019. if (!dum_hcd->udev) {
  1020. dum_hcd->udev = urb->dev;
  1021. usb_get_dev(dum_hcd->udev);
  1022. } else if (unlikely(dum_hcd->udev != urb->dev))
  1023. dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
  1024. list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
  1025. urb->hcpriv = urbp;
  1026. if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
  1027. urb->error_count = 1; /* mark as a new urb */
  1028. /* kick the scheduler, it'll do the rest */
  1029. if (!timer_pending(&dum_hcd->timer))
  1030. mod_timer(&dum_hcd->timer, jiffies + 1);
  1031. done:
  1032. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1033. return rc;
  1034. }
  1035. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1036. {
  1037. struct dummy_hcd *dum_hcd;
  1038. unsigned long flags;
  1039. int rc;
  1040. /* giveback happens automatically in timer callback,
  1041. * so make sure the callback happens */
  1042. dum_hcd = hcd_to_dummy_hcd(hcd);
  1043. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1044. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1045. if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
  1046. !list_empty(&dum_hcd->urbp_list))
  1047. mod_timer(&dum_hcd->timer, jiffies);
  1048. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1049. return rc;
  1050. }
  1051. static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
  1052. u32 len)
  1053. {
  1054. void *ubuf, *rbuf;
  1055. struct urbp *urbp = urb->hcpriv;
  1056. int to_host;
  1057. struct sg_mapping_iter *miter = &urbp->miter;
  1058. u32 trans = 0;
  1059. u32 this_sg;
  1060. bool next_sg;
  1061. to_host = usb_pipein(urb->pipe);
  1062. rbuf = req->req.buf + req->req.actual;
  1063. if (!urb->num_sgs) {
  1064. ubuf = urb->transfer_buffer + urb->actual_length;
  1065. if (to_host)
  1066. memcpy(ubuf, rbuf, len);
  1067. else
  1068. memcpy(rbuf, ubuf, len);
  1069. return len;
  1070. }
  1071. if (!urbp->miter_started) {
  1072. u32 flags = SG_MITER_ATOMIC;
  1073. if (to_host)
  1074. flags |= SG_MITER_TO_SG;
  1075. else
  1076. flags |= SG_MITER_FROM_SG;
  1077. sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
  1078. urbp->miter_started = 1;
  1079. }
  1080. next_sg = sg_miter_next(miter);
  1081. if (next_sg == false) {
  1082. WARN_ON_ONCE(1);
  1083. return -EINVAL;
  1084. }
  1085. do {
  1086. ubuf = miter->addr;
  1087. this_sg = min_t(u32, len, miter->length);
  1088. miter->consumed = this_sg;
  1089. trans += this_sg;
  1090. if (to_host)
  1091. memcpy(ubuf, rbuf, this_sg);
  1092. else
  1093. memcpy(rbuf, ubuf, this_sg);
  1094. len -= this_sg;
  1095. if (!len)
  1096. break;
  1097. next_sg = sg_miter_next(miter);
  1098. if (next_sg == false) {
  1099. WARN_ON_ONCE(1);
  1100. return -EINVAL;
  1101. }
  1102. rbuf += this_sg;
  1103. } while (1);
  1104. sg_miter_stop(miter);
  1105. return trans;
  1106. }
  1107. /* transfer up to a frame's worth; caller must own lock */
  1108. static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
  1109. struct dummy_ep *ep, int limit, int *status)
  1110. {
  1111. struct dummy *dum = dum_hcd->dum;
  1112. struct dummy_request *req;
  1113. top:
  1114. /* if there's no request queued, the device is NAKing; return */
  1115. list_for_each_entry(req, &ep->queue, queue) {
  1116. unsigned host_len, dev_len, len;
  1117. int is_short, to_host;
  1118. int rescan = 0;
  1119. if (dummy_ep_stream_en(dum_hcd, urb)) {
  1120. if ((urb->stream_id != req->req.stream_id))
  1121. continue;
  1122. }
  1123. /* 1..N packets of ep->ep.maxpacket each ... the last one
  1124. * may be short (including zero length).
  1125. *
  1126. * writer can send a zlp explicitly (length 0) or implicitly
  1127. * (length mod maxpacket zero, and 'zero' flag); they always
  1128. * terminate reads.
  1129. */
  1130. host_len = urb->transfer_buffer_length - urb->actual_length;
  1131. dev_len = req->req.length - req->req.actual;
  1132. len = min(host_len, dev_len);
  1133. /* FIXME update emulated data toggle too */
  1134. to_host = usb_pipein(urb->pipe);
  1135. if (unlikely(len == 0))
  1136. is_short = 1;
  1137. else {
  1138. /* not enough bandwidth left? */
  1139. if (limit < ep->ep.maxpacket && limit < len)
  1140. break;
  1141. len = min_t(unsigned, len, limit);
  1142. if (len == 0)
  1143. break;
  1144. /* use an extra pass for the final short packet */
  1145. if (len > ep->ep.maxpacket) {
  1146. rescan = 1;
  1147. len -= (len % ep->ep.maxpacket);
  1148. }
  1149. is_short = (len % ep->ep.maxpacket) != 0;
  1150. len = dummy_perform_transfer(urb, req, len);
  1151. ep->last_io = jiffies;
  1152. if ((int)len < 0) {
  1153. req->req.status = len;
  1154. } else {
  1155. limit -= len;
  1156. urb->actual_length += len;
  1157. req->req.actual += len;
  1158. }
  1159. }
  1160. /* short packets terminate, maybe with overflow/underflow.
  1161. * it's only really an error to write too much.
  1162. *
  1163. * partially filling a buffer optionally blocks queue advances
  1164. * (so completion handlers can clean up the queue) but we don't
  1165. * need to emulate such data-in-flight.
  1166. */
  1167. if (is_short) {
  1168. if (host_len == dev_len) {
  1169. req->req.status = 0;
  1170. *status = 0;
  1171. } else if (to_host) {
  1172. req->req.status = 0;
  1173. if (dev_len > host_len)
  1174. *status = -EOVERFLOW;
  1175. else
  1176. *status = 0;
  1177. } else if (!to_host) {
  1178. *status = 0;
  1179. if (host_len > dev_len)
  1180. req->req.status = -EOVERFLOW;
  1181. else
  1182. req->req.status = 0;
  1183. }
  1184. /* many requests terminate without a short packet */
  1185. } else {
  1186. if (req->req.length == req->req.actual
  1187. && !req->req.zero)
  1188. req->req.status = 0;
  1189. if (urb->transfer_buffer_length == urb->actual_length
  1190. && !(urb->transfer_flags
  1191. & URB_ZERO_PACKET))
  1192. *status = 0;
  1193. }
  1194. /* device side completion --> continuable */
  1195. if (req->req.status != -EINPROGRESS) {
  1196. list_del_init(&req->queue);
  1197. spin_unlock(&dum->lock);
  1198. req->req.complete(&ep->ep, &req->req);
  1199. spin_lock(&dum->lock);
  1200. /* requests might have been unlinked... */
  1201. rescan = 1;
  1202. }
  1203. /* host side completion --> terminate */
  1204. if (*status != -EINPROGRESS)
  1205. break;
  1206. /* rescan to continue with any other queued i/o */
  1207. if (rescan)
  1208. goto top;
  1209. }
  1210. return limit;
  1211. }
  1212. static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
  1213. {
  1214. int limit = ep->ep.maxpacket;
  1215. if (dum->gadget.speed == USB_SPEED_HIGH) {
  1216. int tmp;
  1217. /* high bandwidth mode */
  1218. tmp = usb_endpoint_maxp(ep->desc);
  1219. tmp = (tmp >> 11) & 0x03;
  1220. tmp *= 8 /* applies to entire frame */;
  1221. limit += limit * tmp;
  1222. }
  1223. if (dum->gadget.speed == USB_SPEED_SUPER) {
  1224. switch (usb_endpoint_type(ep->desc)) {
  1225. case USB_ENDPOINT_XFER_ISOC:
  1226. /* Sec. 4.4.8.2 USB3.0 Spec */
  1227. limit = 3 * 16 * 1024 * 8;
  1228. break;
  1229. case USB_ENDPOINT_XFER_INT:
  1230. /* Sec. 4.4.7.2 USB3.0 Spec */
  1231. limit = 3 * 1024 * 8;
  1232. break;
  1233. case USB_ENDPOINT_XFER_BULK:
  1234. default:
  1235. break;
  1236. }
  1237. }
  1238. return limit;
  1239. }
  1240. #define is_active(dum_hcd) ((dum_hcd->port_status & \
  1241. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  1242. USB_PORT_STAT_SUSPEND)) \
  1243. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  1244. static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
  1245. {
  1246. int i;
  1247. if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
  1248. dum->ss_hcd : dum->hs_hcd)))
  1249. return NULL;
  1250. if ((address & ~USB_DIR_IN) == 0)
  1251. return &dum->ep[0];
  1252. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  1253. struct dummy_ep *ep = &dum->ep[i];
  1254. if (!ep->desc)
  1255. continue;
  1256. if (ep->desc->bEndpointAddress == address)
  1257. return ep;
  1258. }
  1259. return NULL;
  1260. }
  1261. #undef is_active
  1262. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1263. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  1264. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  1265. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  1266. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  1267. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  1268. /**
  1269. * handle_control_request() - handles all control transfers
  1270. * @dum: pointer to dummy (the_controller)
  1271. * @urb: the urb request to handle
  1272. * @setup: pointer to the setup data for a USB device control
  1273. * request
  1274. * @status: pointer to request handling status
  1275. *
  1276. * Return 0 - if the request was handled
  1277. * 1 - if the request wasn't handles
  1278. * error code on error
  1279. */
  1280. static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
  1281. struct usb_ctrlrequest *setup,
  1282. int *status)
  1283. {
  1284. struct dummy_ep *ep2;
  1285. struct dummy *dum = dum_hcd->dum;
  1286. int ret_val = 1;
  1287. unsigned w_index;
  1288. unsigned w_value;
  1289. w_index = le16_to_cpu(setup->wIndex);
  1290. w_value = le16_to_cpu(setup->wValue);
  1291. switch (setup->bRequest) {
  1292. case USB_REQ_SET_ADDRESS:
  1293. if (setup->bRequestType != Dev_Request)
  1294. break;
  1295. dum->address = w_value;
  1296. *status = 0;
  1297. dev_dbg(udc_dev(dum), "set_address = %d\n",
  1298. w_value);
  1299. ret_val = 0;
  1300. break;
  1301. case USB_REQ_SET_FEATURE:
  1302. if (setup->bRequestType == Dev_Request) {
  1303. ret_val = 0;
  1304. switch (w_value) {
  1305. case USB_DEVICE_REMOTE_WAKEUP:
  1306. break;
  1307. case USB_DEVICE_B_HNP_ENABLE:
  1308. dum->gadget.b_hnp_enable = 1;
  1309. break;
  1310. case USB_DEVICE_A_HNP_SUPPORT:
  1311. dum->gadget.a_hnp_support = 1;
  1312. break;
  1313. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1314. dum->gadget.a_alt_hnp_support = 1;
  1315. break;
  1316. case USB_DEVICE_U1_ENABLE:
  1317. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1318. HCD_USB3)
  1319. w_value = USB_DEV_STAT_U1_ENABLED;
  1320. else
  1321. ret_val = -EOPNOTSUPP;
  1322. break;
  1323. case USB_DEVICE_U2_ENABLE:
  1324. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1325. HCD_USB3)
  1326. w_value = USB_DEV_STAT_U2_ENABLED;
  1327. else
  1328. ret_val = -EOPNOTSUPP;
  1329. break;
  1330. case USB_DEVICE_LTM_ENABLE:
  1331. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1332. HCD_USB3)
  1333. w_value = USB_DEV_STAT_LTM_ENABLED;
  1334. else
  1335. ret_val = -EOPNOTSUPP;
  1336. break;
  1337. default:
  1338. ret_val = -EOPNOTSUPP;
  1339. }
  1340. if (ret_val == 0) {
  1341. dum->devstatus |= (1 << w_value);
  1342. *status = 0;
  1343. }
  1344. } else if (setup->bRequestType == Ep_Request) {
  1345. /* endpoint halt */
  1346. ep2 = find_endpoint(dum, w_index);
  1347. if (!ep2 || ep2->ep.name == ep0name) {
  1348. ret_val = -EOPNOTSUPP;
  1349. break;
  1350. }
  1351. ep2->halted = 1;
  1352. ret_val = 0;
  1353. *status = 0;
  1354. }
  1355. break;
  1356. case USB_REQ_CLEAR_FEATURE:
  1357. if (setup->bRequestType == Dev_Request) {
  1358. ret_val = 0;
  1359. switch (w_value) {
  1360. case USB_DEVICE_REMOTE_WAKEUP:
  1361. w_value = USB_DEVICE_REMOTE_WAKEUP;
  1362. break;
  1363. case USB_DEVICE_U1_ENABLE:
  1364. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1365. HCD_USB3)
  1366. w_value = USB_DEV_STAT_U1_ENABLED;
  1367. else
  1368. ret_val = -EOPNOTSUPP;
  1369. break;
  1370. case USB_DEVICE_U2_ENABLE:
  1371. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1372. HCD_USB3)
  1373. w_value = USB_DEV_STAT_U2_ENABLED;
  1374. else
  1375. ret_val = -EOPNOTSUPP;
  1376. break;
  1377. case USB_DEVICE_LTM_ENABLE:
  1378. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1379. HCD_USB3)
  1380. w_value = USB_DEV_STAT_LTM_ENABLED;
  1381. else
  1382. ret_val = -EOPNOTSUPP;
  1383. break;
  1384. default:
  1385. ret_val = -EOPNOTSUPP;
  1386. break;
  1387. }
  1388. if (ret_val == 0) {
  1389. dum->devstatus &= ~(1 << w_value);
  1390. *status = 0;
  1391. }
  1392. } else if (setup->bRequestType == Ep_Request) {
  1393. /* endpoint halt */
  1394. ep2 = find_endpoint(dum, w_index);
  1395. if (!ep2) {
  1396. ret_val = -EOPNOTSUPP;
  1397. break;
  1398. }
  1399. if (!ep2->wedged)
  1400. ep2->halted = 0;
  1401. ret_val = 0;
  1402. *status = 0;
  1403. }
  1404. break;
  1405. case USB_REQ_GET_STATUS:
  1406. if (setup->bRequestType == Dev_InRequest
  1407. || setup->bRequestType == Intf_InRequest
  1408. || setup->bRequestType == Ep_InRequest) {
  1409. char *buf;
  1410. /*
  1411. * device: remote wakeup, selfpowered
  1412. * interface: nothing
  1413. * endpoint: halt
  1414. */
  1415. buf = (char *)urb->transfer_buffer;
  1416. if (urb->transfer_buffer_length > 0) {
  1417. if (setup->bRequestType == Ep_InRequest) {
  1418. ep2 = find_endpoint(dum, w_index);
  1419. if (!ep2) {
  1420. ret_val = -EOPNOTSUPP;
  1421. break;
  1422. }
  1423. buf[0] = ep2->halted;
  1424. } else if (setup->bRequestType ==
  1425. Dev_InRequest) {
  1426. buf[0] = (u8)dum->devstatus;
  1427. } else
  1428. buf[0] = 0;
  1429. }
  1430. if (urb->transfer_buffer_length > 1)
  1431. buf[1] = 0;
  1432. urb->actual_length = min_t(u32, 2,
  1433. urb->transfer_buffer_length);
  1434. ret_val = 0;
  1435. *status = 0;
  1436. }
  1437. break;
  1438. }
  1439. return ret_val;
  1440. }
  1441. /* drive both sides of the transfers; looks like irq handlers to
  1442. * both drivers except the callbacks aren't in_irq().
  1443. */
  1444. static void dummy_timer(unsigned long _dum_hcd)
  1445. {
  1446. struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
  1447. struct dummy *dum = dum_hcd->dum;
  1448. struct urbp *urbp, *tmp;
  1449. unsigned long flags;
  1450. int limit, total;
  1451. int i;
  1452. /* simplistic model for one frame's bandwidth */
  1453. switch (dum->gadget.speed) {
  1454. case USB_SPEED_LOW:
  1455. total = 8/*bytes*/ * 12/*packets*/;
  1456. break;
  1457. case USB_SPEED_FULL:
  1458. total = 64/*bytes*/ * 19/*packets*/;
  1459. break;
  1460. case USB_SPEED_HIGH:
  1461. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1462. break;
  1463. case USB_SPEED_SUPER:
  1464. /* Bus speed is 500000 bytes/ms, so use a little less */
  1465. total = 490000;
  1466. break;
  1467. default:
  1468. dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
  1469. return;
  1470. }
  1471. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1472. /* look at each urb queued by the host side driver */
  1473. spin_lock_irqsave(&dum->lock, flags);
  1474. if (!dum_hcd->udev) {
  1475. dev_err(dummy_dev(dum_hcd),
  1476. "timer fired with no URBs pending?\n");
  1477. spin_unlock_irqrestore(&dum->lock, flags);
  1478. return;
  1479. }
  1480. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1481. if (!ep_name[i])
  1482. break;
  1483. dum->ep[i].already_seen = 0;
  1484. }
  1485. restart:
  1486. list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
  1487. struct urb *urb;
  1488. struct dummy_request *req;
  1489. u8 address;
  1490. struct dummy_ep *ep = NULL;
  1491. int type;
  1492. int status = -EINPROGRESS;
  1493. urb = urbp->urb;
  1494. if (urb->unlinked)
  1495. goto return_urb;
  1496. else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
  1497. continue;
  1498. type = usb_pipetype(urb->pipe);
  1499. /* used up this frame's non-periodic bandwidth?
  1500. * FIXME there's infinite bandwidth for control and
  1501. * periodic transfers ... unrealistic.
  1502. */
  1503. if (total <= 0 && type == PIPE_BULK)
  1504. continue;
  1505. /* find the gadget's ep for this request (if configured) */
  1506. address = usb_pipeendpoint (urb->pipe);
  1507. if (usb_pipein(urb->pipe))
  1508. address |= USB_DIR_IN;
  1509. ep = find_endpoint(dum, address);
  1510. if (!ep) {
  1511. /* set_configuration() disagreement */
  1512. dev_dbg(dummy_dev(dum_hcd),
  1513. "no ep configured for urb %pK\n",
  1514. urb);
  1515. status = -EPROTO;
  1516. goto return_urb;
  1517. }
  1518. if (ep->already_seen)
  1519. continue;
  1520. ep->already_seen = 1;
  1521. if (ep == &dum->ep[0] && urb->error_count) {
  1522. ep->setup_stage = 1; /* a new urb */
  1523. urb->error_count = 0;
  1524. }
  1525. if (ep->halted && !ep->setup_stage) {
  1526. /* NOTE: must not be iso! */
  1527. dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %pK\n",
  1528. ep->ep.name, urb);
  1529. status = -EPIPE;
  1530. goto return_urb;
  1531. }
  1532. /* FIXME make sure both ends agree on maxpacket */
  1533. /* handle control requests */
  1534. if (ep == &dum->ep[0] && ep->setup_stage) {
  1535. struct usb_ctrlrequest setup;
  1536. int value = 1;
  1537. setup = *(struct usb_ctrlrequest *) urb->setup_packet;
  1538. /* paranoia, in case of stale queued data */
  1539. list_for_each_entry(req, &ep->queue, queue) {
  1540. list_del_init(&req->queue);
  1541. req->req.status = -EOVERFLOW;
  1542. dev_dbg(udc_dev(dum), "stale req = %pK\n",
  1543. req);
  1544. spin_unlock(&dum->lock);
  1545. req->req.complete(&ep->ep, &req->req);
  1546. spin_lock(&dum->lock);
  1547. ep->already_seen = 0;
  1548. goto restart;
  1549. }
  1550. /* gadget driver never sees set_address or operations
  1551. * on standard feature flags. some hardware doesn't
  1552. * even expose them.
  1553. */
  1554. ep->last_io = jiffies;
  1555. ep->setup_stage = 0;
  1556. ep->halted = 0;
  1557. value = handle_control_request(dum_hcd, urb, &setup,
  1558. &status);
  1559. /* gadget driver handles all other requests. block
  1560. * until setup() returns; no reentrancy issues etc.
  1561. */
  1562. if (value > 0) {
  1563. spin_unlock(&dum->lock);
  1564. value = dum->driver->setup(&dum->gadget,
  1565. &setup);
  1566. spin_lock(&dum->lock);
  1567. if (value >= 0) {
  1568. /* no delays (max 64KB data stage) */
  1569. limit = 64*1024;
  1570. goto treat_control_like_bulk;
  1571. }
  1572. /* error, see below */
  1573. }
  1574. if (value < 0) {
  1575. if (value != -EOPNOTSUPP)
  1576. dev_dbg(udc_dev(dum),
  1577. "setup --> %d\n",
  1578. value);
  1579. status = -EPIPE;
  1580. urb->actual_length = 0;
  1581. }
  1582. goto return_urb;
  1583. }
  1584. /* non-control requests */
  1585. limit = total;
  1586. switch (usb_pipetype(urb->pipe)) {
  1587. case PIPE_ISOCHRONOUS:
  1588. /* FIXME is it urb->interval since the last xfer?
  1589. * use urb->iso_frame_desc[i].
  1590. * complete whether or not ep has requests queued.
  1591. * report random errors, to debug drivers.
  1592. */
  1593. limit = max(limit, periodic_bytes(dum, ep));
  1594. status = -ENOSYS;
  1595. break;
  1596. case PIPE_INTERRUPT:
  1597. /* FIXME is it urb->interval since the last xfer?
  1598. * this almost certainly polls too fast.
  1599. */
  1600. limit = max(limit, periodic_bytes(dum, ep));
  1601. /* FALLTHROUGH */
  1602. default:
  1603. treat_control_like_bulk:
  1604. ep->last_io = jiffies;
  1605. total = transfer(dum_hcd, urb, ep, limit, &status);
  1606. break;
  1607. }
  1608. /* incomplete transfer? */
  1609. if (status == -EINPROGRESS)
  1610. continue;
  1611. return_urb:
  1612. list_del(&urbp->urbp_list);
  1613. kfree(urbp);
  1614. if (ep)
  1615. ep->already_seen = ep->setup_stage = 0;
  1616. usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
  1617. spin_unlock(&dum->lock);
  1618. usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
  1619. spin_lock(&dum->lock);
  1620. goto restart;
  1621. }
  1622. if (list_empty(&dum_hcd->urbp_list)) {
  1623. usb_put_dev(dum_hcd->udev);
  1624. dum_hcd->udev = NULL;
  1625. } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  1626. /* want a 1 msec delay here */
  1627. mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
  1628. }
  1629. spin_unlock_irqrestore(&dum->lock, flags);
  1630. }
  1631. /*-------------------------------------------------------------------------*/
  1632. #define PORT_C_MASK \
  1633. ((USB_PORT_STAT_C_CONNECTION \
  1634. | USB_PORT_STAT_C_ENABLE \
  1635. | USB_PORT_STAT_C_SUSPEND \
  1636. | USB_PORT_STAT_C_OVERCURRENT \
  1637. | USB_PORT_STAT_C_RESET) << 16)
  1638. static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
  1639. {
  1640. struct dummy_hcd *dum_hcd;
  1641. unsigned long flags;
  1642. int retval = 0;
  1643. dum_hcd = hcd_to_dummy_hcd(hcd);
  1644. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1645. if (!HCD_HW_ACCESSIBLE(hcd))
  1646. goto done;
  1647. if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1648. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1649. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1650. set_link_state(dum_hcd);
  1651. }
  1652. if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
  1653. *buf = (1 << 1);
  1654. dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
  1655. dum_hcd->port_status);
  1656. retval = 1;
  1657. if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
  1658. usb_hcd_resume_root_hub(hcd);
  1659. }
  1660. done:
  1661. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1662. return retval;
  1663. }
  1664. static inline void
  1665. ss_hub_descriptor(struct usb_hub_descriptor *desc)
  1666. {
  1667. memset(desc, 0, sizeof *desc);
  1668. desc->bDescriptorType = 0x2a;
  1669. desc->bDescLength = 12;
  1670. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1671. desc->bNbrPorts = 1;
  1672. desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
  1673. desc->u.ss.DeviceRemovable = 0xffff;
  1674. }
  1675. static inline void hub_descriptor(struct usb_hub_descriptor *desc)
  1676. {
  1677. memset(desc, 0, sizeof *desc);
  1678. desc->bDescriptorType = 0x29;
  1679. desc->bDescLength = 9;
  1680. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1681. desc->bNbrPorts = 1;
  1682. desc->u.hs.DeviceRemovable[0] = 0xff;
  1683. desc->u.hs.DeviceRemovable[1] = 0xff;
  1684. }
  1685. static int dummy_hub_control(
  1686. struct usb_hcd *hcd,
  1687. u16 typeReq,
  1688. u16 wValue,
  1689. u16 wIndex,
  1690. char *buf,
  1691. u16 wLength
  1692. ) {
  1693. struct dummy_hcd *dum_hcd;
  1694. int retval = 0;
  1695. unsigned long flags;
  1696. if (!HCD_HW_ACCESSIBLE(hcd))
  1697. return -ETIMEDOUT;
  1698. dum_hcd = hcd_to_dummy_hcd(hcd);
  1699. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1700. switch (typeReq) {
  1701. case ClearHubFeature:
  1702. break;
  1703. case ClearPortFeature:
  1704. switch (wValue) {
  1705. case USB_PORT_FEAT_SUSPEND:
  1706. if (hcd->speed == HCD_USB3) {
  1707. dev_dbg(dummy_dev(dum_hcd),
  1708. "USB_PORT_FEAT_SUSPEND req not "
  1709. "supported for USB 3.0 roothub\n");
  1710. goto error;
  1711. }
  1712. if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
  1713. /* 20msec resume signaling */
  1714. dum_hcd->resuming = 1;
  1715. dum_hcd->re_timeout = jiffies +
  1716. msecs_to_jiffies(20);
  1717. }
  1718. break;
  1719. case USB_PORT_FEAT_POWER:
  1720. if (hcd->speed == HCD_USB3) {
  1721. if (dum_hcd->port_status & USB_PORT_STAT_POWER)
  1722. dev_dbg(dummy_dev(dum_hcd),
  1723. "power-off\n");
  1724. } else
  1725. if (dum_hcd->port_status &
  1726. USB_SS_PORT_STAT_POWER)
  1727. dev_dbg(dummy_dev(dum_hcd),
  1728. "power-off\n");
  1729. /* FALLS THROUGH */
  1730. default:
  1731. dum_hcd->port_status &= ~(1 << wValue);
  1732. set_link_state(dum_hcd);
  1733. }
  1734. break;
  1735. case GetHubDescriptor:
  1736. if (hcd->speed == HCD_USB3 &&
  1737. (wLength < USB_DT_SS_HUB_SIZE ||
  1738. wValue != (USB_DT_SS_HUB << 8))) {
  1739. dev_dbg(dummy_dev(dum_hcd),
  1740. "Wrong hub descriptor type for "
  1741. "USB 3.0 roothub.\n");
  1742. goto error;
  1743. }
  1744. if (hcd->speed == HCD_USB3)
  1745. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  1746. else
  1747. hub_descriptor((struct usb_hub_descriptor *) buf);
  1748. break;
  1749. case GetHubStatus:
  1750. *(__le32 *) buf = cpu_to_le32(0);
  1751. break;
  1752. case GetPortStatus:
  1753. if (wIndex != 1)
  1754. retval = -EPIPE;
  1755. /* whoever resets or resumes must GetPortStatus to
  1756. * complete it!!
  1757. */
  1758. if (dum_hcd->resuming &&
  1759. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1760. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1761. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1762. }
  1763. if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
  1764. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1765. dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1766. dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
  1767. if (dum_hcd->dum->pullup) {
  1768. dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
  1769. if (hcd->speed < HCD_USB3) {
  1770. switch (dum_hcd->dum->gadget.speed) {
  1771. case USB_SPEED_HIGH:
  1772. dum_hcd->port_status |=
  1773. USB_PORT_STAT_HIGH_SPEED;
  1774. break;
  1775. case USB_SPEED_LOW:
  1776. dum_hcd->dum->gadget.ep0->
  1777. maxpacket = 8;
  1778. dum_hcd->port_status |=
  1779. USB_PORT_STAT_LOW_SPEED;
  1780. break;
  1781. default:
  1782. dum_hcd->dum->gadget.speed =
  1783. USB_SPEED_FULL;
  1784. break;
  1785. }
  1786. }
  1787. }
  1788. }
  1789. set_link_state(dum_hcd);
  1790. ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
  1791. ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
  1792. break;
  1793. case SetHubFeature:
  1794. retval = -EPIPE;
  1795. break;
  1796. case SetPortFeature:
  1797. switch (wValue) {
  1798. case USB_PORT_FEAT_LINK_STATE:
  1799. if (hcd->speed != HCD_USB3) {
  1800. dev_dbg(dummy_dev(dum_hcd),
  1801. "USB_PORT_FEAT_LINK_STATE req not "
  1802. "supported for USB 2.0 roothub\n");
  1803. goto error;
  1804. }
  1805. /*
  1806. * Since this is dummy we don't have an actual link so
  1807. * there is nothing to do for the SET_LINK_STATE cmd
  1808. */
  1809. break;
  1810. case USB_PORT_FEAT_U1_TIMEOUT:
  1811. case USB_PORT_FEAT_U2_TIMEOUT:
  1812. /* TODO: add suspend/resume support! */
  1813. if (hcd->speed != HCD_USB3) {
  1814. dev_dbg(dummy_dev(dum_hcd),
  1815. "USB_PORT_FEAT_U1/2_TIMEOUT req not "
  1816. "supported for USB 2.0 roothub\n");
  1817. goto error;
  1818. }
  1819. break;
  1820. case USB_PORT_FEAT_SUSPEND:
  1821. /* Applicable only for USB2.0 hub */
  1822. if (hcd->speed == HCD_USB3) {
  1823. dev_dbg(dummy_dev(dum_hcd),
  1824. "USB_PORT_FEAT_SUSPEND req not "
  1825. "supported for USB 3.0 roothub\n");
  1826. goto error;
  1827. }
  1828. if (dum_hcd->active) {
  1829. dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
  1830. /* HNP would happen here; for now we
  1831. * assume b_bus_req is always true.
  1832. */
  1833. set_link_state(dum_hcd);
  1834. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1835. & dum_hcd->dum->devstatus) != 0)
  1836. dev_dbg(dummy_dev(dum_hcd),
  1837. "no HNP yet!\n");
  1838. }
  1839. break;
  1840. case USB_PORT_FEAT_POWER:
  1841. if (hcd->speed == HCD_USB3)
  1842. dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
  1843. else
  1844. dum_hcd->port_status |= USB_PORT_STAT_POWER;
  1845. set_link_state(dum_hcd);
  1846. break;
  1847. case USB_PORT_FEAT_BH_PORT_RESET:
  1848. /* Applicable only for USB3.0 hub */
  1849. if (hcd->speed != HCD_USB3) {
  1850. dev_dbg(dummy_dev(dum_hcd),
  1851. "USB_PORT_FEAT_BH_PORT_RESET req not "
  1852. "supported for USB 2.0 roothub\n");
  1853. goto error;
  1854. }
  1855. /* FALLS THROUGH */
  1856. case USB_PORT_FEAT_RESET:
  1857. /* if it's already enabled, disable */
  1858. if (hcd->speed == HCD_USB3) {
  1859. dum_hcd->port_status = 0;
  1860. dum_hcd->port_status =
  1861. (USB_SS_PORT_STAT_POWER |
  1862. USB_PORT_STAT_CONNECTION |
  1863. USB_PORT_STAT_RESET);
  1864. } else
  1865. dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
  1866. | USB_PORT_STAT_LOW_SPEED
  1867. | USB_PORT_STAT_HIGH_SPEED);
  1868. /*
  1869. * We want to reset device status. All but the
  1870. * Self powered feature
  1871. */
  1872. dum_hcd->dum->devstatus &=
  1873. (1 << USB_DEVICE_SELF_POWERED);
  1874. /*
  1875. * FIXME USB3.0: what is the correct reset signaling
  1876. * interval? Is it still 50msec as for HS?
  1877. */
  1878. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  1879. /* FALLS THROUGH */
  1880. default:
  1881. if (hcd->speed == HCD_USB3) {
  1882. if ((dum_hcd->port_status &
  1883. USB_SS_PORT_STAT_POWER) != 0) {
  1884. dum_hcd->port_status |= (1 << wValue);
  1885. set_link_state(dum_hcd);
  1886. }
  1887. } else
  1888. if ((dum_hcd->port_status &
  1889. USB_PORT_STAT_POWER) != 0) {
  1890. dum_hcd->port_status |= (1 << wValue);
  1891. set_link_state(dum_hcd);
  1892. }
  1893. }
  1894. break;
  1895. case GetPortErrorCount:
  1896. if (hcd->speed != HCD_USB3) {
  1897. dev_dbg(dummy_dev(dum_hcd),
  1898. "GetPortErrorCount req not "
  1899. "supported for USB 2.0 roothub\n");
  1900. goto error;
  1901. }
  1902. /* We'll always return 0 since this is a dummy hub */
  1903. *(__le32 *) buf = cpu_to_le32(0);
  1904. break;
  1905. case SetHubDepth:
  1906. if (hcd->speed != HCD_USB3) {
  1907. dev_dbg(dummy_dev(dum_hcd),
  1908. "SetHubDepth req not supported for "
  1909. "USB 2.0 roothub\n");
  1910. goto error;
  1911. }
  1912. break;
  1913. default:
  1914. dev_dbg(dummy_dev(dum_hcd),
  1915. "hub control req%04x v%04x i%04x l%d\n",
  1916. typeReq, wValue, wIndex, wLength);
  1917. error:
  1918. /* "protocol stall" on error */
  1919. retval = -EPIPE;
  1920. }
  1921. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1922. if ((dum_hcd->port_status & PORT_C_MASK) != 0)
  1923. usb_hcd_poll_rh_status(hcd);
  1924. return retval;
  1925. }
  1926. static int dummy_bus_suspend(struct usb_hcd *hcd)
  1927. {
  1928. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1929. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1930. spin_lock_irq(&dum_hcd->dum->lock);
  1931. dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
  1932. set_link_state(dum_hcd);
  1933. hcd->state = HC_STATE_SUSPENDED;
  1934. spin_unlock_irq(&dum_hcd->dum->lock);
  1935. return 0;
  1936. }
  1937. static int dummy_bus_resume(struct usb_hcd *hcd)
  1938. {
  1939. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1940. int rc = 0;
  1941. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1942. spin_lock_irq(&dum_hcd->dum->lock);
  1943. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1944. rc = -ESHUTDOWN;
  1945. } else {
  1946. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1947. set_link_state(dum_hcd);
  1948. if (!list_empty(&dum_hcd->urbp_list))
  1949. mod_timer(&dum_hcd->timer, jiffies);
  1950. hcd->state = HC_STATE_RUNNING;
  1951. }
  1952. spin_unlock_irq(&dum_hcd->dum->lock);
  1953. return rc;
  1954. }
  1955. /*-------------------------------------------------------------------------*/
  1956. static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
  1957. {
  1958. int ep = usb_pipeendpoint(urb->pipe);
  1959. return snprintf(buf, size,
  1960. "urb/%pK %s ep%d%s%s len %d/%d\n",
  1961. urb,
  1962. ({ char *s;
  1963. switch (urb->dev->speed) {
  1964. case USB_SPEED_LOW:
  1965. s = "ls";
  1966. break;
  1967. case USB_SPEED_FULL:
  1968. s = "fs";
  1969. break;
  1970. case USB_SPEED_HIGH:
  1971. s = "hs";
  1972. break;
  1973. case USB_SPEED_SUPER:
  1974. s = "ss";
  1975. break;
  1976. default:
  1977. s = "?";
  1978. break;
  1979. }; s; }),
  1980. ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
  1981. ({ char *s; \
  1982. switch (usb_pipetype(urb->pipe)) { \
  1983. case PIPE_CONTROL: \
  1984. s = ""; \
  1985. break; \
  1986. case PIPE_BULK: \
  1987. s = "-bulk"; \
  1988. break; \
  1989. case PIPE_INTERRUPT: \
  1990. s = "-int"; \
  1991. break; \
  1992. default: \
  1993. s = "-iso"; \
  1994. break; \
  1995. }; s; }),
  1996. urb->actual_length, urb->transfer_buffer_length);
  1997. }
  1998. static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
  1999. char *buf)
  2000. {
  2001. struct usb_hcd *hcd = dev_get_drvdata(dev);
  2002. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2003. struct urbp *urbp;
  2004. size_t size = 0;
  2005. unsigned long flags;
  2006. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2007. list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
  2008. size_t temp;
  2009. temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
  2010. buf += temp;
  2011. size += temp;
  2012. }
  2013. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2014. return size;
  2015. }
  2016. static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
  2017. static int dummy_start_ss(struct dummy_hcd *dum_hcd)
  2018. {
  2019. init_timer(&dum_hcd->timer);
  2020. dum_hcd->timer.function = dummy_timer;
  2021. dum_hcd->timer.data = (unsigned long)dum_hcd;
  2022. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  2023. dum_hcd->stream_en_ep = 0;
  2024. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  2025. dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
  2026. dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
  2027. dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
  2028. #ifdef CONFIG_USB_OTG
  2029. dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
  2030. #endif
  2031. return 0;
  2032. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  2033. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  2034. }
  2035. static int dummy_start(struct usb_hcd *hcd)
  2036. {
  2037. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2038. /*
  2039. * MASTER side init ... we emulate a root hub that'll only ever
  2040. * talk to one device (the slave side). Also appears in sysfs,
  2041. * just like more familiar pci-based HCDs.
  2042. */
  2043. if (!usb_hcd_is_primary_hcd(hcd))
  2044. return dummy_start_ss(dum_hcd);
  2045. spin_lock_init(&dum_hcd->dum->lock);
  2046. init_timer(&dum_hcd->timer);
  2047. dum_hcd->timer.function = dummy_timer;
  2048. dum_hcd->timer.data = (unsigned long)dum_hcd;
  2049. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  2050. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  2051. hcd->power_budget = POWER_BUDGET;
  2052. hcd->state = HC_STATE_RUNNING;
  2053. hcd->uses_new_polling = 1;
  2054. #ifdef CONFIG_USB_OTG
  2055. hcd->self.otg_port = 1;
  2056. #endif
  2057. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  2058. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  2059. }
  2060. static void dummy_stop(struct usb_hcd *hcd)
  2061. {
  2062. struct dummy *dum;
  2063. dum = hcd_to_dummy_hcd(hcd)->dum;
  2064. device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
  2065. usb_gadget_unregister_driver(dum->driver);
  2066. dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
  2067. }
  2068. /*-------------------------------------------------------------------------*/
  2069. static int dummy_h_get_frame(struct usb_hcd *hcd)
  2070. {
  2071. return dummy_g_get_frame(NULL);
  2072. }
  2073. static int dummy_setup(struct usb_hcd *hcd)
  2074. {
  2075. hcd->self.sg_tablesize = ~0;
  2076. if (usb_hcd_is_primary_hcd(hcd)) {
  2077. the_controller.hs_hcd = hcd_to_dummy_hcd(hcd);
  2078. the_controller.hs_hcd->dum = &the_controller;
  2079. /*
  2080. * Mark the first roothub as being USB 2.0.
  2081. * The USB 3.0 roothub will be registered later by
  2082. * dummy_hcd_probe()
  2083. */
  2084. hcd->speed = HCD_USB2;
  2085. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  2086. } else {
  2087. the_controller.ss_hcd = hcd_to_dummy_hcd(hcd);
  2088. the_controller.ss_hcd->dum = &the_controller;
  2089. hcd->speed = HCD_USB3;
  2090. hcd->self.root_hub->speed = USB_SPEED_SUPER;
  2091. }
  2092. return 0;
  2093. }
  2094. /* Change a group of bulk endpoints to support multiple stream IDs */
  2095. static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2096. struct usb_host_endpoint **eps, unsigned int num_eps,
  2097. unsigned int num_streams, gfp_t mem_flags)
  2098. {
  2099. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2100. unsigned long flags;
  2101. int max_stream;
  2102. int ret_streams = num_streams;
  2103. unsigned int index;
  2104. unsigned int i;
  2105. if (!num_eps)
  2106. return -EINVAL;
  2107. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2108. for (i = 0; i < num_eps; i++) {
  2109. index = dummy_get_ep_idx(&eps[i]->desc);
  2110. if ((1 << index) & dum_hcd->stream_en_ep) {
  2111. ret_streams = -EINVAL;
  2112. goto out;
  2113. }
  2114. max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  2115. if (!max_stream) {
  2116. ret_streams = -EINVAL;
  2117. goto out;
  2118. }
  2119. if (max_stream < ret_streams) {
  2120. dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
  2121. "stream IDs.\n",
  2122. eps[i]->desc.bEndpointAddress,
  2123. max_stream);
  2124. ret_streams = max_stream;
  2125. }
  2126. }
  2127. for (i = 0; i < num_eps; i++) {
  2128. index = dummy_get_ep_idx(&eps[i]->desc);
  2129. dum_hcd->stream_en_ep |= 1 << index;
  2130. set_max_streams_for_pipe(dum_hcd,
  2131. usb_endpoint_num(&eps[i]->desc), ret_streams);
  2132. }
  2133. out:
  2134. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2135. return ret_streams;
  2136. }
  2137. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  2138. static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2139. struct usb_host_endpoint **eps, unsigned int num_eps,
  2140. gfp_t mem_flags)
  2141. {
  2142. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2143. unsigned long flags;
  2144. int ret;
  2145. unsigned int index;
  2146. unsigned int i;
  2147. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2148. for (i = 0; i < num_eps; i++) {
  2149. index = dummy_get_ep_idx(&eps[i]->desc);
  2150. if (!((1 << index) & dum_hcd->stream_en_ep)) {
  2151. ret = -EINVAL;
  2152. goto out;
  2153. }
  2154. }
  2155. for (i = 0; i < num_eps; i++) {
  2156. index = dummy_get_ep_idx(&eps[i]->desc);
  2157. dum_hcd->stream_en_ep &= ~(1 << index);
  2158. set_max_streams_for_pipe(dum_hcd,
  2159. usb_endpoint_num(&eps[i]->desc), 0);
  2160. }
  2161. ret = 0;
  2162. out:
  2163. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2164. return ret;
  2165. }
  2166. static struct hc_driver dummy_hcd = {
  2167. .description = (char *) driver_name,
  2168. .product_desc = "Dummy host controller",
  2169. .hcd_priv_size = sizeof(struct dummy_hcd),
  2170. .flags = HCD_USB3 | HCD_SHARED,
  2171. .reset = dummy_setup,
  2172. .start = dummy_start,
  2173. .stop = dummy_stop,
  2174. .urb_enqueue = dummy_urb_enqueue,
  2175. .urb_dequeue = dummy_urb_dequeue,
  2176. .get_frame_number = dummy_h_get_frame,
  2177. .hub_status_data = dummy_hub_status,
  2178. .hub_control = dummy_hub_control,
  2179. .bus_suspend = dummy_bus_suspend,
  2180. .bus_resume = dummy_bus_resume,
  2181. .alloc_streams = dummy_alloc_streams,
  2182. .free_streams = dummy_free_streams,
  2183. };
  2184. static int dummy_hcd_probe(struct platform_device *pdev)
  2185. {
  2186. struct usb_hcd *hs_hcd;
  2187. struct usb_hcd *ss_hcd;
  2188. int retval;
  2189. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  2190. if (!mod_data.is_super_speed)
  2191. dummy_hcd.flags = HCD_USB2;
  2192. hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  2193. if (!hs_hcd)
  2194. return -ENOMEM;
  2195. hs_hcd->has_tt = 1;
  2196. retval = usb_add_hcd(hs_hcd, 0, 0);
  2197. if (retval)
  2198. goto put_usb2_hcd;
  2199. if (mod_data.is_super_speed) {
  2200. ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
  2201. dev_name(&pdev->dev), hs_hcd);
  2202. if (!ss_hcd) {
  2203. retval = -ENOMEM;
  2204. goto dealloc_usb2_hcd;
  2205. }
  2206. retval = usb_add_hcd(ss_hcd, 0, 0);
  2207. if (retval)
  2208. goto put_usb3_hcd;
  2209. }
  2210. return 0;
  2211. put_usb3_hcd:
  2212. usb_put_hcd(ss_hcd);
  2213. dealloc_usb2_hcd:
  2214. usb_remove_hcd(hs_hcd);
  2215. put_usb2_hcd:
  2216. usb_put_hcd(hs_hcd);
  2217. the_controller.hs_hcd = the_controller.ss_hcd = NULL;
  2218. return retval;
  2219. }
  2220. static int dummy_hcd_remove(struct platform_device *pdev)
  2221. {
  2222. struct dummy *dum;
  2223. dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
  2224. if (dum->ss_hcd) {
  2225. usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2226. usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2227. }
  2228. usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2229. usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2230. the_controller.hs_hcd = NULL;
  2231. the_controller.ss_hcd = NULL;
  2232. return 0;
  2233. }
  2234. static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  2235. {
  2236. struct usb_hcd *hcd;
  2237. struct dummy_hcd *dum_hcd;
  2238. int rc = 0;
  2239. dev_dbg(&pdev->dev, "%s\n", __func__);
  2240. hcd = platform_get_drvdata(pdev);
  2241. dum_hcd = hcd_to_dummy_hcd(hcd);
  2242. if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  2243. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  2244. rc = -EBUSY;
  2245. } else
  2246. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2247. return rc;
  2248. }
  2249. static int dummy_hcd_resume(struct platform_device *pdev)
  2250. {
  2251. struct usb_hcd *hcd;
  2252. dev_dbg(&pdev->dev, "%s\n", __func__);
  2253. hcd = platform_get_drvdata(pdev);
  2254. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2255. usb_hcd_poll_rh_status(hcd);
  2256. return 0;
  2257. }
  2258. static struct platform_driver dummy_hcd_driver = {
  2259. .probe = dummy_hcd_probe,
  2260. .remove = dummy_hcd_remove,
  2261. .suspend = dummy_hcd_suspend,
  2262. .resume = dummy_hcd_resume,
  2263. .driver = {
  2264. .name = (char *) driver_name,
  2265. .owner = THIS_MODULE,
  2266. },
  2267. };
  2268. /*-------------------------------------------------------------------------*/
  2269. static struct platform_device *the_udc_pdev;
  2270. static struct platform_device *the_hcd_pdev;
  2271. static int __init init(void)
  2272. {
  2273. int retval = -ENOMEM;
  2274. if (usb_disabled())
  2275. return -ENODEV;
  2276. if (!mod_data.is_high_speed && mod_data.is_super_speed)
  2277. return -EINVAL;
  2278. the_hcd_pdev = platform_device_alloc(driver_name, -1);
  2279. if (!the_hcd_pdev)
  2280. return retval;
  2281. the_udc_pdev = platform_device_alloc(gadget_name, -1);
  2282. if (!the_udc_pdev)
  2283. goto err_alloc_udc;
  2284. retval = platform_driver_register(&dummy_hcd_driver);
  2285. if (retval < 0)
  2286. goto err_register_hcd_driver;
  2287. retval = platform_driver_register(&dummy_udc_driver);
  2288. if (retval < 0)
  2289. goto err_register_udc_driver;
  2290. retval = platform_device_add(the_hcd_pdev);
  2291. if (retval < 0)
  2292. goto err_add_hcd;
  2293. if (!the_controller.hs_hcd ||
  2294. (!the_controller.ss_hcd && mod_data.is_super_speed)) {
  2295. /*
  2296. * The hcd was added successfully but its probe function failed
  2297. * for some reason.
  2298. */
  2299. retval = -EINVAL;
  2300. goto err_add_udc;
  2301. }
  2302. retval = platform_device_add(the_udc_pdev);
  2303. if (retval < 0)
  2304. goto err_add_udc;
  2305. if (!platform_get_drvdata(the_udc_pdev)) {
  2306. /*
  2307. * The udc was added successfully but its probe function failed
  2308. * for some reason.
  2309. */
  2310. retval = -EINVAL;
  2311. goto err_probe_udc;
  2312. }
  2313. return retval;
  2314. err_probe_udc:
  2315. platform_device_del(the_udc_pdev);
  2316. err_add_udc:
  2317. platform_device_del(the_hcd_pdev);
  2318. err_add_hcd:
  2319. platform_driver_unregister(&dummy_udc_driver);
  2320. err_register_udc_driver:
  2321. platform_driver_unregister(&dummy_hcd_driver);
  2322. err_register_hcd_driver:
  2323. platform_device_put(the_udc_pdev);
  2324. err_alloc_udc:
  2325. platform_device_put(the_hcd_pdev);
  2326. return retval;
  2327. }
  2328. module_init(init);
  2329. static void __exit cleanup(void)
  2330. {
  2331. platform_device_unregister(the_udc_pdev);
  2332. platform_device_unregister(the_hcd_pdev);
  2333. platform_driver_unregister(&dummy_udc_driver);
  2334. platform_driver_unregister(&dummy_hcd_driver);
  2335. }
  2336. module_exit(cleanup);