dummy_hcd.c 68 KB

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