dummy_hcd.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  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. req->req.complete(&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. /* if !connected or reset */
  314. if ((dum_hcd->port_status & USB_PORT_STAT_CONNECTION) == 0 ||
  315. (dum_hcd->port_status & USB_PORT_STAT_RESET) != 0) {
  316. /*
  317. * We're connected and not reset (reset occurred now),
  318. * and driver attached - disconnect!
  319. */
  320. if ((dum_hcd->old_status & USB_PORT_STAT_CONNECTION) != 0 &&
  321. (dum_hcd->old_status & USB_PORT_STAT_RESET) == 0 &&
  322. dum->driver) {
  323. stop_activity(dum);
  324. spin_unlock(&dum->lock);
  325. dum->driver->disconnect(&dum->gadget);
  326. spin_lock(&dum->lock);
  327. }
  328. } else if (dum_hcd->active != dum_hcd->old_active) {
  329. if (dum_hcd->old_active && dum->driver->suspend) {
  330. spin_unlock(&dum->lock);
  331. dum->driver->suspend(&dum->gadget);
  332. spin_lock(&dum->lock);
  333. } else if (!dum_hcd->old_active && dum->driver->resume) {
  334. spin_unlock(&dum->lock);
  335. dum->driver->resume(&dum->gadget);
  336. spin_lock(&dum->lock);
  337. }
  338. }
  339. dum_hcd->old_status = dum_hcd->port_status;
  340. dum_hcd->old_active = dum_hcd->active;
  341. }
  342. /*-------------------------------------------------------------------------*/
  343. /* SLAVE/GADGET SIDE DRIVER
  344. *
  345. * This only tracks gadget state. All the work is done when the host
  346. * side tries some (emulated) i/o operation. Real device controller
  347. * drivers would do real i/o using dma, fifos, irqs, timers, etc.
  348. */
  349. #define is_enabled(dum) \
  350. (dum->port_status & USB_PORT_STAT_ENABLE)
  351. static int dummy_enable(struct usb_ep *_ep,
  352. const struct usb_endpoint_descriptor *desc)
  353. {
  354. struct dummy *dum;
  355. struct dummy_hcd *dum_hcd;
  356. struct dummy_ep *ep;
  357. unsigned max;
  358. int retval;
  359. ep = usb_ep_to_dummy_ep(_ep);
  360. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  361. || desc->bDescriptorType != USB_DT_ENDPOINT)
  362. return -EINVAL;
  363. dum = ep_to_dummy(ep);
  364. if (!dum->driver)
  365. return -ESHUTDOWN;
  366. dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  367. if (!is_enabled(dum_hcd))
  368. return -ESHUTDOWN;
  369. /*
  370. * For HS/FS devices only bits 0..10 of the wMaxPacketSize represent the
  371. * maximum packet size.
  372. * For SS devices the wMaxPacketSize is limited by 1024.
  373. */
  374. max = usb_endpoint_maxp(desc) & 0x7ff;
  375. /* drivers must not request bad settings, since lower levels
  376. * (hardware or its drivers) may not check. some endpoints
  377. * can't do iso, many have maxpacket limitations, etc.
  378. *
  379. * since this "hardware" driver is here to help debugging, we
  380. * have some extra sanity checks. (there could be more though,
  381. * especially for "ep9out" style fixed function ones.)
  382. */
  383. retval = -EINVAL;
  384. switch (usb_endpoint_type(desc)) {
  385. case USB_ENDPOINT_XFER_BULK:
  386. if (strstr(ep->ep.name, "-iso")
  387. || strstr(ep->ep.name, "-int")) {
  388. goto done;
  389. }
  390. switch (dum->gadget.speed) {
  391. case USB_SPEED_SUPER:
  392. if (max == 1024)
  393. break;
  394. goto done;
  395. case USB_SPEED_HIGH:
  396. if (max == 512)
  397. break;
  398. goto done;
  399. case USB_SPEED_FULL:
  400. if (max == 8 || max == 16 || max == 32 || max == 64)
  401. /* we'll fake any legal size */
  402. break;
  403. /* save a return statement */
  404. default:
  405. goto done;
  406. }
  407. break;
  408. case USB_ENDPOINT_XFER_INT:
  409. if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
  410. goto done;
  411. /* real hardware might not handle all packet sizes */
  412. switch (dum->gadget.speed) {
  413. case USB_SPEED_SUPER:
  414. case USB_SPEED_HIGH:
  415. if (max <= 1024)
  416. break;
  417. /* save a return statement */
  418. case USB_SPEED_FULL:
  419. if (max <= 64)
  420. break;
  421. /* save a return statement */
  422. default:
  423. if (max <= 8)
  424. break;
  425. goto done;
  426. }
  427. break;
  428. case USB_ENDPOINT_XFER_ISOC:
  429. if (strstr(ep->ep.name, "-bulk")
  430. || strstr(ep->ep.name, "-int"))
  431. goto done;
  432. /* real hardware might not handle all packet sizes */
  433. switch (dum->gadget.speed) {
  434. case USB_SPEED_SUPER:
  435. case USB_SPEED_HIGH:
  436. if (max <= 1024)
  437. break;
  438. /* save a return statement */
  439. case USB_SPEED_FULL:
  440. if (max <= 1023)
  441. break;
  442. /* save a return statement */
  443. default:
  444. goto done;
  445. }
  446. break;
  447. default:
  448. /* few chips support control except on ep0 */
  449. goto done;
  450. }
  451. _ep->maxpacket = max;
  452. if (usb_ss_max_streams(_ep->comp_desc)) {
  453. if (!usb_endpoint_xfer_bulk(desc)) {
  454. dev_err(udc_dev(dum), "Can't enable stream support on "
  455. "non-bulk ep %s\n", _ep->name);
  456. return -EINVAL;
  457. }
  458. ep->stream_en = 1;
  459. }
  460. ep->desc = desc;
  461. dev_dbg(udc_dev(dum), "enabled %s (ep%d%s-%s) maxpacket %d stream %s\n",
  462. _ep->name,
  463. desc->bEndpointAddress & 0x0f,
  464. (desc->bEndpointAddress & USB_DIR_IN) ? "in" : "out",
  465. ({ char *val;
  466. switch (usb_endpoint_type(desc)) {
  467. case USB_ENDPOINT_XFER_BULK:
  468. val = "bulk";
  469. break;
  470. case USB_ENDPOINT_XFER_ISOC:
  471. val = "iso";
  472. break;
  473. case USB_ENDPOINT_XFER_INT:
  474. val = "intr";
  475. break;
  476. default:
  477. val = "ctrl";
  478. break;
  479. }; val; }),
  480. max, ep->stream_en ? "enabled" : "disabled");
  481. /* at this point real hardware should be NAKing transfers
  482. * to that endpoint, until a buffer is queued to it.
  483. */
  484. ep->halted = ep->wedged = 0;
  485. retval = 0;
  486. done:
  487. return retval;
  488. }
  489. static int dummy_disable(struct usb_ep *_ep)
  490. {
  491. struct dummy_ep *ep;
  492. struct dummy *dum;
  493. unsigned long flags;
  494. int retval;
  495. ep = usb_ep_to_dummy_ep(_ep);
  496. if (!_ep || !ep->desc || _ep->name == ep0name)
  497. return -EINVAL;
  498. dum = ep_to_dummy(ep);
  499. spin_lock_irqsave(&dum->lock, flags);
  500. ep->desc = NULL;
  501. ep->stream_en = 0;
  502. retval = 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 retval;
  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. _req->complete(_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. _req->complete(_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. struct usb_gadget_driver *driver);
  740. static const struct usb_gadget_ops dummy_ops = {
  741. .get_frame = dummy_g_get_frame,
  742. .wakeup = dummy_wakeup,
  743. .set_selfpowered = dummy_set_selfpowered,
  744. .pullup = dummy_pullup,
  745. .udc_start = dummy_udc_start,
  746. .udc_stop = dummy_udc_stop,
  747. };
  748. /*-------------------------------------------------------------------------*/
  749. /* "function" sysfs attribute */
  750. static ssize_t show_function(struct device *dev, struct device_attribute *attr,
  751. char *buf)
  752. {
  753. struct dummy *dum = gadget_dev_to_dummy(dev);
  754. if (!dum->driver || !dum->driver->function)
  755. return 0;
  756. return scnprintf(buf, PAGE_SIZE, "%s\n", dum->driver->function);
  757. }
  758. static DEVICE_ATTR(function, S_IRUGO, show_function, NULL);
  759. /*-------------------------------------------------------------------------*/
  760. /*
  761. * Driver registration/unregistration.
  762. *
  763. * This is basically hardware-specific; there's usually only one real USB
  764. * device (not host) controller since that's how USB devices are intended
  765. * to work. So most implementations of these api calls will rely on the
  766. * fact that only one driver will ever bind to the hardware. But curious
  767. * hardware can be built with discrete components, so the gadget API doesn't
  768. * require that assumption.
  769. *
  770. * For this emulator, it might be convenient to create a usb slave device
  771. * for each driver that registers: just add to a big root hub.
  772. */
  773. static int dummy_udc_start(struct usb_gadget *g,
  774. struct usb_gadget_driver *driver)
  775. {
  776. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  777. struct dummy *dum = dum_hcd->dum;
  778. if (driver->max_speed == USB_SPEED_UNKNOWN)
  779. return -EINVAL;
  780. /*
  781. * SLAVE side init ... the layer above hardware, which
  782. * can't enumerate without help from the driver we're binding.
  783. */
  784. dum->devstatus = 0;
  785. dum->driver = driver;
  786. dev_dbg(udc_dev(dum), "binding gadget driver '%s'\n",
  787. driver->driver.name);
  788. return 0;
  789. }
  790. static int dummy_udc_stop(struct usb_gadget *g,
  791. struct usb_gadget_driver *driver)
  792. {
  793. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(g);
  794. struct dummy *dum = dum_hcd->dum;
  795. dev_dbg(udc_dev(dum), "unregister gadget driver '%s'\n",
  796. driver->driver.name);
  797. dum->driver = NULL;
  798. return 0;
  799. }
  800. #undef is_enabled
  801. /* The gadget structure is stored inside the hcd structure and will be
  802. * released along with it. */
  803. static void init_dummy_udc_hw(struct dummy *dum)
  804. {
  805. int i;
  806. INIT_LIST_HEAD(&dum->gadget.ep_list);
  807. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  808. struct dummy_ep *ep = &dum->ep[i];
  809. if (!ep_name[i])
  810. break;
  811. ep->ep.name = ep_name[i];
  812. ep->ep.ops = &dummy_ep_ops;
  813. list_add_tail(&ep->ep.ep_list, &dum->gadget.ep_list);
  814. ep->halted = ep->wedged = ep->already_seen =
  815. ep->setup_stage = 0;
  816. ep->ep.maxpacket = ~0;
  817. ep->ep.max_streams = 16;
  818. ep->last_io = jiffies;
  819. ep->gadget = &dum->gadget;
  820. ep->desc = NULL;
  821. INIT_LIST_HEAD(&ep->queue);
  822. }
  823. dum->gadget.ep0 = &dum->ep[0].ep;
  824. list_del_init(&dum->ep[0].ep.ep_list);
  825. INIT_LIST_HEAD(&dum->fifo_req.queue);
  826. #ifdef CONFIG_USB_OTG
  827. dum->gadget.is_otg = 1;
  828. #endif
  829. }
  830. static int dummy_udc_probe(struct platform_device *pdev)
  831. {
  832. struct dummy *dum;
  833. int rc;
  834. dum = *((void **)dev_get_platdata(&pdev->dev));
  835. dum->gadget.name = gadget_name;
  836. dum->gadget.ops = &dummy_ops;
  837. dum->gadget.max_speed = USB_SPEED_SUPER;
  838. dum->gadget.dev.parent = &pdev->dev;
  839. init_dummy_udc_hw(dum);
  840. rc = usb_add_gadget_udc(&pdev->dev, &dum->gadget);
  841. if (rc < 0)
  842. goto err_udc;
  843. rc = device_create_file(&dum->gadget.dev, &dev_attr_function);
  844. if (rc < 0)
  845. goto err_dev;
  846. platform_set_drvdata(pdev, dum);
  847. return rc;
  848. err_dev:
  849. usb_del_gadget_udc(&dum->gadget);
  850. err_udc:
  851. return rc;
  852. }
  853. static int dummy_udc_remove(struct platform_device *pdev)
  854. {
  855. struct dummy *dum = platform_get_drvdata(pdev);
  856. usb_del_gadget_udc(&dum->gadget);
  857. device_remove_file(&dum->gadget.dev, &dev_attr_function);
  858. return 0;
  859. }
  860. static void dummy_udc_pm(struct dummy *dum, struct dummy_hcd *dum_hcd,
  861. int suspend)
  862. {
  863. spin_lock_irq(&dum->lock);
  864. dum->udc_suspended = suspend;
  865. set_link_state(dum_hcd);
  866. spin_unlock_irq(&dum->lock);
  867. }
  868. static int dummy_udc_suspend(struct platform_device *pdev, pm_message_t state)
  869. {
  870. struct dummy *dum = platform_get_drvdata(pdev);
  871. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  872. dev_dbg(&pdev->dev, "%s\n", __func__);
  873. dummy_udc_pm(dum, dum_hcd, 1);
  874. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  875. return 0;
  876. }
  877. static int dummy_udc_resume(struct platform_device *pdev)
  878. {
  879. struct dummy *dum = platform_get_drvdata(pdev);
  880. struct dummy_hcd *dum_hcd = gadget_to_dummy_hcd(&dum->gadget);
  881. dev_dbg(&pdev->dev, "%s\n", __func__);
  882. dummy_udc_pm(dum, dum_hcd, 0);
  883. usb_hcd_poll_rh_status(dummy_hcd_to_hcd(dum_hcd));
  884. return 0;
  885. }
  886. static struct platform_driver dummy_udc_driver = {
  887. .probe = dummy_udc_probe,
  888. .remove = dummy_udc_remove,
  889. .suspend = dummy_udc_suspend,
  890. .resume = dummy_udc_resume,
  891. .driver = {
  892. .name = (char *) gadget_name,
  893. .owner = THIS_MODULE,
  894. },
  895. };
  896. /*-------------------------------------------------------------------------*/
  897. static unsigned int dummy_get_ep_idx(const struct usb_endpoint_descriptor *desc)
  898. {
  899. unsigned int index;
  900. index = usb_endpoint_num(desc) << 1;
  901. if (usb_endpoint_dir_in(desc))
  902. index |= 1;
  903. return index;
  904. }
  905. /* MASTER/HOST SIDE DRIVER
  906. *
  907. * this uses the hcd framework to hook up to host side drivers.
  908. * its root hub will only have one device, otherwise it acts like
  909. * a normal host controller.
  910. *
  911. * when urbs are queued, they're just stuck on a list that we
  912. * scan in a timer callback. that callback connects writes from
  913. * the host with reads from the device, and so on, based on the
  914. * usb 2.0 rules.
  915. */
  916. static int dummy_ep_stream_en(struct dummy_hcd *dum_hcd, struct urb *urb)
  917. {
  918. const struct usb_endpoint_descriptor *desc = &urb->ep->desc;
  919. u32 index;
  920. if (!usb_endpoint_xfer_bulk(desc))
  921. return 0;
  922. index = dummy_get_ep_idx(desc);
  923. return (1 << index) & dum_hcd->stream_en_ep;
  924. }
  925. /*
  926. * The max stream number is saved as a nibble so for the 30 possible endpoints
  927. * we only 15 bytes of memory. Therefore we are limited to max 16 streams (0
  928. * means we use only 1 stream). The maximum according to the spec is 16bit so
  929. * if the 16 stream limit is about to go, the array size should be incremented
  930. * to 30 elements of type u16.
  931. */
  932. static int get_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
  933. unsigned int pipe)
  934. {
  935. int max_streams;
  936. max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
  937. if (usb_pipeout(pipe))
  938. max_streams >>= 4;
  939. else
  940. max_streams &= 0xf;
  941. max_streams++;
  942. return max_streams;
  943. }
  944. static void set_max_streams_for_pipe(struct dummy_hcd *dum_hcd,
  945. unsigned int pipe, unsigned int streams)
  946. {
  947. int max_streams;
  948. streams--;
  949. max_streams = dum_hcd->num_stream[usb_pipeendpoint(pipe)];
  950. if (usb_pipeout(pipe)) {
  951. streams <<= 4;
  952. max_streams &= 0xf;
  953. } else {
  954. max_streams &= 0xf0;
  955. }
  956. max_streams |= streams;
  957. dum_hcd->num_stream[usb_pipeendpoint(pipe)] = max_streams;
  958. }
  959. static int dummy_validate_stream(struct dummy_hcd *dum_hcd, struct urb *urb)
  960. {
  961. unsigned int max_streams;
  962. int enabled;
  963. enabled = dummy_ep_stream_en(dum_hcd, urb);
  964. if (!urb->stream_id) {
  965. if (enabled)
  966. return -EINVAL;
  967. return 0;
  968. }
  969. if (!enabled)
  970. return -EINVAL;
  971. max_streams = get_max_streams_for_pipe(dum_hcd,
  972. usb_pipeendpoint(urb->pipe));
  973. if (urb->stream_id > max_streams) {
  974. dev_err(dummy_dev(dum_hcd), "Stream id %d is out of range.\n",
  975. urb->stream_id);
  976. BUG();
  977. return -EINVAL;
  978. }
  979. return 0;
  980. }
  981. static int dummy_urb_enqueue(
  982. struct usb_hcd *hcd,
  983. struct urb *urb,
  984. gfp_t mem_flags
  985. ) {
  986. struct dummy_hcd *dum_hcd;
  987. struct urbp *urbp;
  988. unsigned long flags;
  989. int rc;
  990. urbp = kmalloc(sizeof *urbp, mem_flags);
  991. if (!urbp)
  992. return -ENOMEM;
  993. urbp->urb = urb;
  994. urbp->miter_started = 0;
  995. dum_hcd = hcd_to_dummy_hcd(hcd);
  996. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  997. rc = dummy_validate_stream(dum_hcd, urb);
  998. if (rc) {
  999. kfree(urbp);
  1000. goto done;
  1001. }
  1002. rc = usb_hcd_link_urb_to_ep(hcd, urb);
  1003. if (rc) {
  1004. kfree(urbp);
  1005. goto done;
  1006. }
  1007. if (!dum_hcd->udev) {
  1008. dum_hcd->udev = urb->dev;
  1009. usb_get_dev(dum_hcd->udev);
  1010. } else if (unlikely(dum_hcd->udev != urb->dev))
  1011. dev_err(dummy_dev(dum_hcd), "usb_device address has changed!\n");
  1012. list_add_tail(&urbp->urbp_list, &dum_hcd->urbp_list);
  1013. urb->hcpriv = urbp;
  1014. if (usb_pipetype(urb->pipe) == PIPE_CONTROL)
  1015. urb->error_count = 1; /* mark as a new urb */
  1016. /* kick the scheduler, it'll do the rest */
  1017. if (!timer_pending(&dum_hcd->timer))
  1018. mod_timer(&dum_hcd->timer, jiffies + 1);
  1019. done:
  1020. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1021. return rc;
  1022. }
  1023. static int dummy_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  1024. {
  1025. struct dummy_hcd *dum_hcd;
  1026. unsigned long flags;
  1027. int rc;
  1028. /* giveback happens automatically in timer callback,
  1029. * so make sure the callback happens */
  1030. dum_hcd = hcd_to_dummy_hcd(hcd);
  1031. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1032. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1033. if (!rc && dum_hcd->rh_state != DUMMY_RH_RUNNING &&
  1034. !list_empty(&dum_hcd->urbp_list))
  1035. mod_timer(&dum_hcd->timer, jiffies);
  1036. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1037. return rc;
  1038. }
  1039. static int dummy_perform_transfer(struct urb *urb, struct dummy_request *req,
  1040. u32 len)
  1041. {
  1042. void *ubuf, *rbuf;
  1043. struct urbp *urbp = urb->hcpriv;
  1044. int to_host;
  1045. struct sg_mapping_iter *miter = &urbp->miter;
  1046. u32 trans = 0;
  1047. u32 this_sg;
  1048. bool next_sg;
  1049. to_host = usb_pipein(urb->pipe);
  1050. rbuf = req->req.buf + req->req.actual;
  1051. if (!urb->num_sgs) {
  1052. ubuf = urb->transfer_buffer + urb->actual_length;
  1053. if (to_host)
  1054. memcpy(ubuf, rbuf, len);
  1055. else
  1056. memcpy(rbuf, ubuf, len);
  1057. return len;
  1058. }
  1059. if (!urbp->miter_started) {
  1060. u32 flags = SG_MITER_ATOMIC;
  1061. if (to_host)
  1062. flags |= SG_MITER_TO_SG;
  1063. else
  1064. flags |= SG_MITER_FROM_SG;
  1065. sg_miter_start(miter, urb->sg, urb->num_sgs, flags);
  1066. urbp->miter_started = 1;
  1067. }
  1068. next_sg = sg_miter_next(miter);
  1069. if (next_sg == false) {
  1070. WARN_ON_ONCE(1);
  1071. return -EINVAL;
  1072. }
  1073. do {
  1074. ubuf = miter->addr;
  1075. this_sg = min_t(u32, len, miter->length);
  1076. miter->consumed = this_sg;
  1077. trans += this_sg;
  1078. if (to_host)
  1079. memcpy(ubuf, rbuf, this_sg);
  1080. else
  1081. memcpy(rbuf, ubuf, this_sg);
  1082. len -= this_sg;
  1083. if (!len)
  1084. break;
  1085. next_sg = sg_miter_next(miter);
  1086. if (next_sg == false) {
  1087. WARN_ON_ONCE(1);
  1088. return -EINVAL;
  1089. }
  1090. rbuf += this_sg;
  1091. } while (1);
  1092. sg_miter_stop(miter);
  1093. return trans;
  1094. }
  1095. /* transfer up to a frame's worth; caller must own lock */
  1096. static int transfer(struct dummy_hcd *dum_hcd, struct urb *urb,
  1097. struct dummy_ep *ep, int limit, int *status)
  1098. {
  1099. struct dummy *dum = dum_hcd->dum;
  1100. struct dummy_request *req;
  1101. top:
  1102. /* if there's no request queued, the device is NAKing; return */
  1103. list_for_each_entry(req, &ep->queue, queue) {
  1104. unsigned host_len, dev_len, len;
  1105. int is_short, to_host;
  1106. int rescan = 0;
  1107. if (dummy_ep_stream_en(dum_hcd, urb)) {
  1108. if ((urb->stream_id != req->req.stream_id))
  1109. continue;
  1110. }
  1111. /* 1..N packets of ep->ep.maxpacket each ... the last one
  1112. * may be short (including zero length).
  1113. *
  1114. * writer can send a zlp explicitly (length 0) or implicitly
  1115. * (length mod maxpacket zero, and 'zero' flag); they always
  1116. * terminate reads.
  1117. */
  1118. host_len = urb->transfer_buffer_length - urb->actual_length;
  1119. dev_len = req->req.length - req->req.actual;
  1120. len = min(host_len, dev_len);
  1121. /* FIXME update emulated data toggle too */
  1122. to_host = usb_pipein(urb->pipe);
  1123. if (unlikely(len == 0))
  1124. is_short = 1;
  1125. else {
  1126. /* not enough bandwidth left? */
  1127. if (limit < ep->ep.maxpacket && limit < len)
  1128. break;
  1129. len = min_t(unsigned, len, limit);
  1130. if (len == 0)
  1131. break;
  1132. /* use an extra pass for the final short packet */
  1133. if (len > ep->ep.maxpacket) {
  1134. rescan = 1;
  1135. len -= (len % ep->ep.maxpacket);
  1136. }
  1137. is_short = (len % ep->ep.maxpacket) != 0;
  1138. len = dummy_perform_transfer(urb, req, len);
  1139. ep->last_io = jiffies;
  1140. if ((int)len < 0) {
  1141. req->req.status = len;
  1142. } else {
  1143. limit -= len;
  1144. urb->actual_length += len;
  1145. req->req.actual += len;
  1146. }
  1147. }
  1148. /* short packets terminate, maybe with overflow/underflow.
  1149. * it's only really an error to write too much.
  1150. *
  1151. * partially filling a buffer optionally blocks queue advances
  1152. * (so completion handlers can clean up the queue) but we don't
  1153. * need to emulate such data-in-flight.
  1154. */
  1155. if (is_short) {
  1156. if (host_len == dev_len) {
  1157. req->req.status = 0;
  1158. *status = 0;
  1159. } else if (to_host) {
  1160. req->req.status = 0;
  1161. if (dev_len > host_len)
  1162. *status = -EOVERFLOW;
  1163. else
  1164. *status = 0;
  1165. } else if (!to_host) {
  1166. *status = 0;
  1167. if (host_len > dev_len)
  1168. req->req.status = -EOVERFLOW;
  1169. else
  1170. req->req.status = 0;
  1171. }
  1172. /* many requests terminate without a short packet */
  1173. } else {
  1174. if (req->req.length == req->req.actual
  1175. && !req->req.zero)
  1176. req->req.status = 0;
  1177. if (urb->transfer_buffer_length == urb->actual_length
  1178. && !(urb->transfer_flags
  1179. & URB_ZERO_PACKET))
  1180. *status = 0;
  1181. }
  1182. /* device side completion --> continuable */
  1183. if (req->req.status != -EINPROGRESS) {
  1184. list_del_init(&req->queue);
  1185. spin_unlock(&dum->lock);
  1186. req->req.complete(&ep->ep, &req->req);
  1187. spin_lock(&dum->lock);
  1188. /* requests might have been unlinked... */
  1189. rescan = 1;
  1190. }
  1191. /* host side completion --> terminate */
  1192. if (*status != -EINPROGRESS)
  1193. break;
  1194. /* rescan to continue with any other queued i/o */
  1195. if (rescan)
  1196. goto top;
  1197. }
  1198. return limit;
  1199. }
  1200. static int periodic_bytes(struct dummy *dum, struct dummy_ep *ep)
  1201. {
  1202. int limit = ep->ep.maxpacket;
  1203. if (dum->gadget.speed == USB_SPEED_HIGH) {
  1204. int tmp;
  1205. /* high bandwidth mode */
  1206. tmp = usb_endpoint_maxp(ep->desc);
  1207. tmp = (tmp >> 11) & 0x03;
  1208. tmp *= 8 /* applies to entire frame */;
  1209. limit += limit * tmp;
  1210. }
  1211. if (dum->gadget.speed == USB_SPEED_SUPER) {
  1212. switch (usb_endpoint_type(ep->desc)) {
  1213. case USB_ENDPOINT_XFER_ISOC:
  1214. /* Sec. 4.4.8.2 USB3.0 Spec */
  1215. limit = 3 * 16 * 1024 * 8;
  1216. break;
  1217. case USB_ENDPOINT_XFER_INT:
  1218. /* Sec. 4.4.7.2 USB3.0 Spec */
  1219. limit = 3 * 1024 * 8;
  1220. break;
  1221. case USB_ENDPOINT_XFER_BULK:
  1222. default:
  1223. break;
  1224. }
  1225. }
  1226. return limit;
  1227. }
  1228. #define is_active(dum_hcd) ((dum_hcd->port_status & \
  1229. (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE | \
  1230. USB_PORT_STAT_SUSPEND)) \
  1231. == (USB_PORT_STAT_CONNECTION | USB_PORT_STAT_ENABLE))
  1232. static struct dummy_ep *find_endpoint(struct dummy *dum, u8 address)
  1233. {
  1234. int i;
  1235. if (!is_active((dum->gadget.speed == USB_SPEED_SUPER ?
  1236. dum->ss_hcd : dum->hs_hcd)))
  1237. return NULL;
  1238. if ((address & ~USB_DIR_IN) == 0)
  1239. return &dum->ep[0];
  1240. for (i = 1; i < DUMMY_ENDPOINTS; i++) {
  1241. struct dummy_ep *ep = &dum->ep[i];
  1242. if (!ep->desc)
  1243. continue;
  1244. if (ep->desc->bEndpointAddress == address)
  1245. return ep;
  1246. }
  1247. return NULL;
  1248. }
  1249. #undef is_active
  1250. #define Dev_Request (USB_TYPE_STANDARD | USB_RECIP_DEVICE)
  1251. #define Dev_InRequest (Dev_Request | USB_DIR_IN)
  1252. #define Intf_Request (USB_TYPE_STANDARD | USB_RECIP_INTERFACE)
  1253. #define Intf_InRequest (Intf_Request | USB_DIR_IN)
  1254. #define Ep_Request (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT)
  1255. #define Ep_InRequest (Ep_Request | USB_DIR_IN)
  1256. /**
  1257. * handle_control_request() - handles all control transfers
  1258. * @dum: pointer to dummy (the_controller)
  1259. * @urb: the urb request to handle
  1260. * @setup: pointer to the setup data for a USB device control
  1261. * request
  1262. * @status: pointer to request handling status
  1263. *
  1264. * Return 0 - if the request was handled
  1265. * 1 - if the request wasn't handles
  1266. * error code on error
  1267. */
  1268. static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb,
  1269. struct usb_ctrlrequest *setup,
  1270. int *status)
  1271. {
  1272. struct dummy_ep *ep2;
  1273. struct dummy *dum = dum_hcd->dum;
  1274. int ret_val = 1;
  1275. unsigned w_index;
  1276. unsigned w_value;
  1277. w_index = le16_to_cpu(setup->wIndex);
  1278. w_value = le16_to_cpu(setup->wValue);
  1279. switch (setup->bRequest) {
  1280. case USB_REQ_SET_ADDRESS:
  1281. if (setup->bRequestType != Dev_Request)
  1282. break;
  1283. dum->address = w_value;
  1284. *status = 0;
  1285. dev_dbg(udc_dev(dum), "set_address = %d\n",
  1286. w_value);
  1287. ret_val = 0;
  1288. break;
  1289. case USB_REQ_SET_FEATURE:
  1290. if (setup->bRequestType == Dev_Request) {
  1291. ret_val = 0;
  1292. switch (w_value) {
  1293. case USB_DEVICE_REMOTE_WAKEUP:
  1294. break;
  1295. case USB_DEVICE_B_HNP_ENABLE:
  1296. dum->gadget.b_hnp_enable = 1;
  1297. break;
  1298. case USB_DEVICE_A_HNP_SUPPORT:
  1299. dum->gadget.a_hnp_support = 1;
  1300. break;
  1301. case USB_DEVICE_A_ALT_HNP_SUPPORT:
  1302. dum->gadget.a_alt_hnp_support = 1;
  1303. break;
  1304. case USB_DEVICE_U1_ENABLE:
  1305. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1306. HCD_USB3)
  1307. w_value = USB_DEV_STAT_U1_ENABLED;
  1308. else
  1309. ret_val = -EOPNOTSUPP;
  1310. break;
  1311. case USB_DEVICE_U2_ENABLE:
  1312. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1313. HCD_USB3)
  1314. w_value = USB_DEV_STAT_U2_ENABLED;
  1315. else
  1316. ret_val = -EOPNOTSUPP;
  1317. break;
  1318. case USB_DEVICE_LTM_ENABLE:
  1319. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1320. HCD_USB3)
  1321. w_value = USB_DEV_STAT_LTM_ENABLED;
  1322. else
  1323. ret_val = -EOPNOTSUPP;
  1324. break;
  1325. default:
  1326. ret_val = -EOPNOTSUPP;
  1327. }
  1328. if (ret_val == 0) {
  1329. dum->devstatus |= (1 << w_value);
  1330. *status = 0;
  1331. }
  1332. } else if (setup->bRequestType == Ep_Request) {
  1333. /* endpoint halt */
  1334. ep2 = find_endpoint(dum, w_index);
  1335. if (!ep2 || ep2->ep.name == ep0name) {
  1336. ret_val = -EOPNOTSUPP;
  1337. break;
  1338. }
  1339. ep2->halted = 1;
  1340. ret_val = 0;
  1341. *status = 0;
  1342. }
  1343. break;
  1344. case USB_REQ_CLEAR_FEATURE:
  1345. if (setup->bRequestType == Dev_Request) {
  1346. ret_val = 0;
  1347. switch (w_value) {
  1348. case USB_DEVICE_REMOTE_WAKEUP:
  1349. w_value = USB_DEVICE_REMOTE_WAKEUP;
  1350. break;
  1351. case USB_DEVICE_U1_ENABLE:
  1352. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1353. HCD_USB3)
  1354. w_value = USB_DEV_STAT_U1_ENABLED;
  1355. else
  1356. ret_val = -EOPNOTSUPP;
  1357. break;
  1358. case USB_DEVICE_U2_ENABLE:
  1359. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1360. HCD_USB3)
  1361. w_value = USB_DEV_STAT_U2_ENABLED;
  1362. else
  1363. ret_val = -EOPNOTSUPP;
  1364. break;
  1365. case USB_DEVICE_LTM_ENABLE:
  1366. if (dummy_hcd_to_hcd(dum_hcd)->speed ==
  1367. HCD_USB3)
  1368. w_value = USB_DEV_STAT_LTM_ENABLED;
  1369. else
  1370. ret_val = -EOPNOTSUPP;
  1371. break;
  1372. default:
  1373. ret_val = -EOPNOTSUPP;
  1374. break;
  1375. }
  1376. if (ret_val == 0) {
  1377. dum->devstatus &= ~(1 << w_value);
  1378. *status = 0;
  1379. }
  1380. } else if (setup->bRequestType == Ep_Request) {
  1381. /* endpoint halt */
  1382. ep2 = find_endpoint(dum, w_index);
  1383. if (!ep2) {
  1384. ret_val = -EOPNOTSUPP;
  1385. break;
  1386. }
  1387. if (!ep2->wedged)
  1388. ep2->halted = 0;
  1389. ret_val = 0;
  1390. *status = 0;
  1391. }
  1392. break;
  1393. case USB_REQ_GET_STATUS:
  1394. if (setup->bRequestType == Dev_InRequest
  1395. || setup->bRequestType == Intf_InRequest
  1396. || setup->bRequestType == Ep_InRequest) {
  1397. char *buf;
  1398. /*
  1399. * device: remote wakeup, selfpowered
  1400. * interface: nothing
  1401. * endpoint: halt
  1402. */
  1403. buf = (char *)urb->transfer_buffer;
  1404. if (urb->transfer_buffer_length > 0) {
  1405. if (setup->bRequestType == Ep_InRequest) {
  1406. ep2 = find_endpoint(dum, w_index);
  1407. if (!ep2) {
  1408. ret_val = -EOPNOTSUPP;
  1409. break;
  1410. }
  1411. buf[0] = ep2->halted;
  1412. } else if (setup->bRequestType ==
  1413. Dev_InRequest) {
  1414. buf[0] = (u8)dum->devstatus;
  1415. } else
  1416. buf[0] = 0;
  1417. }
  1418. if (urb->transfer_buffer_length > 1)
  1419. buf[1] = 0;
  1420. urb->actual_length = min_t(u32, 2,
  1421. urb->transfer_buffer_length);
  1422. ret_val = 0;
  1423. *status = 0;
  1424. }
  1425. break;
  1426. }
  1427. return ret_val;
  1428. }
  1429. /* drive both sides of the transfers; looks like irq handlers to
  1430. * both drivers except the callbacks aren't in_irq().
  1431. */
  1432. static void dummy_timer(unsigned long _dum_hcd)
  1433. {
  1434. struct dummy_hcd *dum_hcd = (struct dummy_hcd *) _dum_hcd;
  1435. struct dummy *dum = dum_hcd->dum;
  1436. struct urbp *urbp, *tmp;
  1437. unsigned long flags;
  1438. int limit, total;
  1439. int i;
  1440. /* simplistic model for one frame's bandwidth */
  1441. switch (dum->gadget.speed) {
  1442. case USB_SPEED_LOW:
  1443. total = 8/*bytes*/ * 12/*packets*/;
  1444. break;
  1445. case USB_SPEED_FULL:
  1446. total = 64/*bytes*/ * 19/*packets*/;
  1447. break;
  1448. case USB_SPEED_HIGH:
  1449. total = 512/*bytes*/ * 13/*packets*/ * 8/*uframes*/;
  1450. break;
  1451. case USB_SPEED_SUPER:
  1452. /* Bus speed is 500000 bytes/ms, so use a little less */
  1453. total = 490000;
  1454. break;
  1455. default:
  1456. dev_err(dummy_dev(dum_hcd), "bogus device speed\n");
  1457. return;
  1458. }
  1459. /* FIXME if HZ != 1000 this will probably misbehave ... */
  1460. /* look at each urb queued by the host side driver */
  1461. spin_lock_irqsave(&dum->lock, flags);
  1462. if (!dum_hcd->udev) {
  1463. dev_err(dummy_dev(dum_hcd),
  1464. "timer fired with no URBs pending?\n");
  1465. spin_unlock_irqrestore(&dum->lock, flags);
  1466. return;
  1467. }
  1468. for (i = 0; i < DUMMY_ENDPOINTS; i++) {
  1469. if (!ep_name[i])
  1470. break;
  1471. dum->ep[i].already_seen = 0;
  1472. }
  1473. restart:
  1474. list_for_each_entry_safe(urbp, tmp, &dum_hcd->urbp_list, urbp_list) {
  1475. struct urb *urb;
  1476. struct dummy_request *req;
  1477. u8 address;
  1478. struct dummy_ep *ep = NULL;
  1479. int type;
  1480. int status = -EINPROGRESS;
  1481. urb = urbp->urb;
  1482. if (urb->unlinked)
  1483. goto return_urb;
  1484. else if (dum_hcd->rh_state != DUMMY_RH_RUNNING)
  1485. continue;
  1486. type = usb_pipetype(urb->pipe);
  1487. /* used up this frame's non-periodic bandwidth?
  1488. * FIXME there's infinite bandwidth for control and
  1489. * periodic transfers ... unrealistic.
  1490. */
  1491. if (total <= 0 && type == PIPE_BULK)
  1492. continue;
  1493. /* find the gadget's ep for this request (if configured) */
  1494. address = usb_pipeendpoint (urb->pipe);
  1495. if (usb_pipein(urb->pipe))
  1496. address |= USB_DIR_IN;
  1497. ep = find_endpoint(dum, address);
  1498. if (!ep) {
  1499. /* set_configuration() disagreement */
  1500. dev_dbg(dummy_dev(dum_hcd),
  1501. "no ep configured for urb %p\n",
  1502. urb);
  1503. status = -EPROTO;
  1504. goto return_urb;
  1505. }
  1506. if (ep->already_seen)
  1507. continue;
  1508. ep->already_seen = 1;
  1509. if (ep == &dum->ep[0] && urb->error_count) {
  1510. ep->setup_stage = 1; /* a new urb */
  1511. urb->error_count = 0;
  1512. }
  1513. if (ep->halted && !ep->setup_stage) {
  1514. /* NOTE: must not be iso! */
  1515. dev_dbg(dummy_dev(dum_hcd), "ep %s halted, urb %p\n",
  1516. ep->ep.name, urb);
  1517. status = -EPIPE;
  1518. goto return_urb;
  1519. }
  1520. /* FIXME make sure both ends agree on maxpacket */
  1521. /* handle control requests */
  1522. if (ep == &dum->ep[0] && ep->setup_stage) {
  1523. struct usb_ctrlrequest setup;
  1524. int value = 1;
  1525. setup = *(struct usb_ctrlrequest *) urb->setup_packet;
  1526. /* paranoia, in case of stale queued data */
  1527. list_for_each_entry(req, &ep->queue, queue) {
  1528. list_del_init(&req->queue);
  1529. req->req.status = -EOVERFLOW;
  1530. dev_dbg(udc_dev(dum), "stale req = %p\n",
  1531. req);
  1532. spin_unlock(&dum->lock);
  1533. req->req.complete(&ep->ep, &req->req);
  1534. spin_lock(&dum->lock);
  1535. ep->already_seen = 0;
  1536. goto restart;
  1537. }
  1538. /* gadget driver never sees set_address or operations
  1539. * on standard feature flags. some hardware doesn't
  1540. * even expose them.
  1541. */
  1542. ep->last_io = jiffies;
  1543. ep->setup_stage = 0;
  1544. ep->halted = 0;
  1545. value = handle_control_request(dum_hcd, urb, &setup,
  1546. &status);
  1547. /* gadget driver handles all other requests. block
  1548. * until setup() returns; no reentrancy issues etc.
  1549. */
  1550. if (value > 0) {
  1551. spin_unlock(&dum->lock);
  1552. value = dum->driver->setup(&dum->gadget,
  1553. &setup);
  1554. spin_lock(&dum->lock);
  1555. if (value >= 0) {
  1556. /* no delays (max 64KB data stage) */
  1557. limit = 64*1024;
  1558. goto treat_control_like_bulk;
  1559. }
  1560. /* error, see below */
  1561. }
  1562. if (value < 0) {
  1563. if (value != -EOPNOTSUPP)
  1564. dev_dbg(udc_dev(dum),
  1565. "setup --> %d\n",
  1566. value);
  1567. status = -EPIPE;
  1568. urb->actual_length = 0;
  1569. }
  1570. goto return_urb;
  1571. }
  1572. /* non-control requests */
  1573. limit = total;
  1574. switch (usb_pipetype(urb->pipe)) {
  1575. case PIPE_ISOCHRONOUS:
  1576. /* FIXME is it urb->interval since the last xfer?
  1577. * use urb->iso_frame_desc[i].
  1578. * complete whether or not ep has requests queued.
  1579. * report random errors, to debug drivers.
  1580. */
  1581. limit = max(limit, periodic_bytes(dum, ep));
  1582. status = -ENOSYS;
  1583. break;
  1584. case PIPE_INTERRUPT:
  1585. /* FIXME is it urb->interval since the last xfer?
  1586. * this almost certainly polls too fast.
  1587. */
  1588. limit = max(limit, periodic_bytes(dum, ep));
  1589. /* FALLTHROUGH */
  1590. default:
  1591. treat_control_like_bulk:
  1592. ep->last_io = jiffies;
  1593. total = transfer(dum_hcd, urb, ep, limit, &status);
  1594. break;
  1595. }
  1596. /* incomplete transfer? */
  1597. if (status == -EINPROGRESS)
  1598. continue;
  1599. return_urb:
  1600. list_del(&urbp->urbp_list);
  1601. kfree(urbp);
  1602. if (ep)
  1603. ep->already_seen = ep->setup_stage = 0;
  1604. usb_hcd_unlink_urb_from_ep(dummy_hcd_to_hcd(dum_hcd), urb);
  1605. spin_unlock(&dum->lock);
  1606. usb_hcd_giveback_urb(dummy_hcd_to_hcd(dum_hcd), urb, status);
  1607. spin_lock(&dum->lock);
  1608. goto restart;
  1609. }
  1610. if (list_empty(&dum_hcd->urbp_list)) {
  1611. usb_put_dev(dum_hcd->udev);
  1612. dum_hcd->udev = NULL;
  1613. } else if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  1614. /* want a 1 msec delay here */
  1615. mod_timer(&dum_hcd->timer, jiffies + msecs_to_jiffies(1));
  1616. }
  1617. spin_unlock_irqrestore(&dum->lock, flags);
  1618. }
  1619. /*-------------------------------------------------------------------------*/
  1620. #define PORT_C_MASK \
  1621. ((USB_PORT_STAT_C_CONNECTION \
  1622. | USB_PORT_STAT_C_ENABLE \
  1623. | USB_PORT_STAT_C_SUSPEND \
  1624. | USB_PORT_STAT_C_OVERCURRENT \
  1625. | USB_PORT_STAT_C_RESET) << 16)
  1626. static int dummy_hub_status(struct usb_hcd *hcd, char *buf)
  1627. {
  1628. struct dummy_hcd *dum_hcd;
  1629. unsigned long flags;
  1630. int retval = 0;
  1631. dum_hcd = hcd_to_dummy_hcd(hcd);
  1632. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1633. if (!HCD_HW_ACCESSIBLE(hcd))
  1634. goto done;
  1635. if (dum_hcd->resuming && time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1636. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1637. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1638. set_link_state(dum_hcd);
  1639. }
  1640. if ((dum_hcd->port_status & PORT_C_MASK) != 0) {
  1641. *buf = (1 << 1);
  1642. dev_dbg(dummy_dev(dum_hcd), "port status 0x%08x has changes\n",
  1643. dum_hcd->port_status);
  1644. retval = 1;
  1645. if (dum_hcd->rh_state == DUMMY_RH_SUSPENDED)
  1646. usb_hcd_resume_root_hub(hcd);
  1647. }
  1648. done:
  1649. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1650. return retval;
  1651. }
  1652. /* usb 3.0 root hub device descriptor */
  1653. static struct {
  1654. struct usb_bos_descriptor bos;
  1655. struct usb_ss_cap_descriptor ss_cap;
  1656. } __packed usb3_bos_desc = {
  1657. .bos = {
  1658. .bLength = USB_DT_BOS_SIZE,
  1659. .bDescriptorType = USB_DT_BOS,
  1660. .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
  1661. .bNumDeviceCaps = 1,
  1662. },
  1663. .ss_cap = {
  1664. .bLength = USB_DT_USB_SS_CAP_SIZE,
  1665. .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
  1666. .bDevCapabilityType = USB_SS_CAP_TYPE,
  1667. .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
  1668. .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
  1669. },
  1670. };
  1671. static inline void
  1672. ss_hub_descriptor(struct usb_hub_descriptor *desc)
  1673. {
  1674. memset(desc, 0, sizeof *desc);
  1675. desc->bDescriptorType = 0x2a;
  1676. desc->bDescLength = 12;
  1677. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1678. desc->bNbrPorts = 1;
  1679. desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
  1680. desc->u.ss.DeviceRemovable = 0xffff;
  1681. }
  1682. static inline void hub_descriptor(struct usb_hub_descriptor *desc)
  1683. {
  1684. memset(desc, 0, sizeof *desc);
  1685. desc->bDescriptorType = 0x29;
  1686. desc->bDescLength = 9;
  1687. desc->wHubCharacteristics = cpu_to_le16(0x0001);
  1688. desc->bNbrPorts = 1;
  1689. desc->u.hs.DeviceRemovable[0] = 0xff;
  1690. desc->u.hs.DeviceRemovable[1] = 0xff;
  1691. }
  1692. static int dummy_hub_control(
  1693. struct usb_hcd *hcd,
  1694. u16 typeReq,
  1695. u16 wValue,
  1696. u16 wIndex,
  1697. char *buf,
  1698. u16 wLength
  1699. ) {
  1700. struct dummy_hcd *dum_hcd;
  1701. int retval = 0;
  1702. unsigned long flags;
  1703. if (!HCD_HW_ACCESSIBLE(hcd))
  1704. return -ETIMEDOUT;
  1705. dum_hcd = hcd_to_dummy_hcd(hcd);
  1706. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  1707. switch (typeReq) {
  1708. case ClearHubFeature:
  1709. break;
  1710. case ClearPortFeature:
  1711. switch (wValue) {
  1712. case USB_PORT_FEAT_SUSPEND:
  1713. if (hcd->speed == HCD_USB3) {
  1714. dev_dbg(dummy_dev(dum_hcd),
  1715. "USB_PORT_FEAT_SUSPEND req not "
  1716. "supported for USB 3.0 roothub\n");
  1717. goto error;
  1718. }
  1719. if (dum_hcd->port_status & USB_PORT_STAT_SUSPEND) {
  1720. /* 20msec resume signaling */
  1721. dum_hcd->resuming = 1;
  1722. dum_hcd->re_timeout = jiffies +
  1723. msecs_to_jiffies(20);
  1724. }
  1725. break;
  1726. case USB_PORT_FEAT_POWER:
  1727. if (hcd->speed == HCD_USB3) {
  1728. if (dum_hcd->port_status & USB_PORT_STAT_POWER)
  1729. dev_dbg(dummy_dev(dum_hcd),
  1730. "power-off\n");
  1731. } else
  1732. if (dum_hcd->port_status &
  1733. USB_SS_PORT_STAT_POWER)
  1734. dev_dbg(dummy_dev(dum_hcd),
  1735. "power-off\n");
  1736. /* FALLS THROUGH */
  1737. default:
  1738. dum_hcd->port_status &= ~(1 << wValue);
  1739. set_link_state(dum_hcd);
  1740. }
  1741. break;
  1742. case GetHubDescriptor:
  1743. if (hcd->speed == HCD_USB3 &&
  1744. (wLength < USB_DT_SS_HUB_SIZE ||
  1745. wValue != (USB_DT_SS_HUB << 8))) {
  1746. dev_dbg(dummy_dev(dum_hcd),
  1747. "Wrong hub descriptor type for "
  1748. "USB 3.0 roothub.\n");
  1749. goto error;
  1750. }
  1751. if (hcd->speed == HCD_USB3)
  1752. ss_hub_descriptor((struct usb_hub_descriptor *) buf);
  1753. else
  1754. hub_descriptor((struct usb_hub_descriptor *) buf);
  1755. break;
  1756. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  1757. if (hcd->speed != HCD_USB3)
  1758. goto error;
  1759. if ((wValue >> 8) != USB_DT_BOS)
  1760. goto error;
  1761. memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
  1762. retval = sizeof(usb3_bos_desc);
  1763. break;
  1764. case GetHubStatus:
  1765. *(__le32 *) buf = cpu_to_le32(0);
  1766. break;
  1767. case GetPortStatus:
  1768. if (wIndex != 1)
  1769. retval = -EPIPE;
  1770. /* whoever resets or resumes must GetPortStatus to
  1771. * complete it!!
  1772. */
  1773. if (dum_hcd->resuming &&
  1774. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1775. dum_hcd->port_status |= (USB_PORT_STAT_C_SUSPEND << 16);
  1776. dum_hcd->port_status &= ~USB_PORT_STAT_SUSPEND;
  1777. }
  1778. if ((dum_hcd->port_status & USB_PORT_STAT_RESET) != 0 &&
  1779. time_after_eq(jiffies, dum_hcd->re_timeout)) {
  1780. dum_hcd->port_status |= (USB_PORT_STAT_C_RESET << 16);
  1781. dum_hcd->port_status &= ~USB_PORT_STAT_RESET;
  1782. if (dum_hcd->dum->pullup) {
  1783. dum_hcd->port_status |= USB_PORT_STAT_ENABLE;
  1784. if (hcd->speed < HCD_USB3) {
  1785. switch (dum_hcd->dum->gadget.speed) {
  1786. case USB_SPEED_HIGH:
  1787. dum_hcd->port_status |=
  1788. USB_PORT_STAT_HIGH_SPEED;
  1789. break;
  1790. case USB_SPEED_LOW:
  1791. dum_hcd->dum->gadget.ep0->
  1792. maxpacket = 8;
  1793. dum_hcd->port_status |=
  1794. USB_PORT_STAT_LOW_SPEED;
  1795. break;
  1796. default:
  1797. dum_hcd->dum->gadget.speed =
  1798. USB_SPEED_FULL;
  1799. break;
  1800. }
  1801. }
  1802. }
  1803. }
  1804. set_link_state(dum_hcd);
  1805. ((__le16 *) buf)[0] = cpu_to_le16(dum_hcd->port_status);
  1806. ((__le16 *) buf)[1] = cpu_to_le16(dum_hcd->port_status >> 16);
  1807. break;
  1808. case SetHubFeature:
  1809. retval = -EPIPE;
  1810. break;
  1811. case SetPortFeature:
  1812. switch (wValue) {
  1813. case USB_PORT_FEAT_LINK_STATE:
  1814. if (hcd->speed != HCD_USB3) {
  1815. dev_dbg(dummy_dev(dum_hcd),
  1816. "USB_PORT_FEAT_LINK_STATE req not "
  1817. "supported for USB 2.0 roothub\n");
  1818. goto error;
  1819. }
  1820. /*
  1821. * Since this is dummy we don't have an actual link so
  1822. * there is nothing to do for the SET_LINK_STATE cmd
  1823. */
  1824. break;
  1825. case USB_PORT_FEAT_U1_TIMEOUT:
  1826. case USB_PORT_FEAT_U2_TIMEOUT:
  1827. /* TODO: add suspend/resume support! */
  1828. if (hcd->speed != HCD_USB3) {
  1829. dev_dbg(dummy_dev(dum_hcd),
  1830. "USB_PORT_FEAT_U1/2_TIMEOUT req not "
  1831. "supported for USB 2.0 roothub\n");
  1832. goto error;
  1833. }
  1834. break;
  1835. case USB_PORT_FEAT_SUSPEND:
  1836. /* Applicable only for USB2.0 hub */
  1837. if (hcd->speed == HCD_USB3) {
  1838. dev_dbg(dummy_dev(dum_hcd),
  1839. "USB_PORT_FEAT_SUSPEND req not "
  1840. "supported for USB 3.0 roothub\n");
  1841. goto error;
  1842. }
  1843. if (dum_hcd->active) {
  1844. dum_hcd->port_status |= USB_PORT_STAT_SUSPEND;
  1845. /* HNP would happen here; for now we
  1846. * assume b_bus_req is always true.
  1847. */
  1848. set_link_state(dum_hcd);
  1849. if (((1 << USB_DEVICE_B_HNP_ENABLE)
  1850. & dum_hcd->dum->devstatus) != 0)
  1851. dev_dbg(dummy_dev(dum_hcd),
  1852. "no HNP yet!\n");
  1853. }
  1854. break;
  1855. case USB_PORT_FEAT_POWER:
  1856. if (hcd->speed == HCD_USB3)
  1857. dum_hcd->port_status |= USB_SS_PORT_STAT_POWER;
  1858. else
  1859. dum_hcd->port_status |= USB_PORT_STAT_POWER;
  1860. set_link_state(dum_hcd);
  1861. break;
  1862. case USB_PORT_FEAT_BH_PORT_RESET:
  1863. /* Applicable only for USB3.0 hub */
  1864. if (hcd->speed != HCD_USB3) {
  1865. dev_dbg(dummy_dev(dum_hcd),
  1866. "USB_PORT_FEAT_BH_PORT_RESET req not "
  1867. "supported for USB 2.0 roothub\n");
  1868. goto error;
  1869. }
  1870. /* FALLS THROUGH */
  1871. case USB_PORT_FEAT_RESET:
  1872. /* if it's already enabled, disable */
  1873. if (hcd->speed == HCD_USB3) {
  1874. dum_hcd->port_status = 0;
  1875. dum_hcd->port_status =
  1876. (USB_SS_PORT_STAT_POWER |
  1877. USB_PORT_STAT_CONNECTION |
  1878. USB_PORT_STAT_RESET);
  1879. } else
  1880. dum_hcd->port_status &= ~(USB_PORT_STAT_ENABLE
  1881. | USB_PORT_STAT_LOW_SPEED
  1882. | USB_PORT_STAT_HIGH_SPEED);
  1883. /*
  1884. * We want to reset device status. All but the
  1885. * Self powered feature
  1886. */
  1887. dum_hcd->dum->devstatus &=
  1888. (1 << USB_DEVICE_SELF_POWERED);
  1889. /*
  1890. * FIXME USB3.0: what is the correct reset signaling
  1891. * interval? Is it still 50msec as for HS?
  1892. */
  1893. dum_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
  1894. /* FALLS THROUGH */
  1895. default:
  1896. if (hcd->speed == HCD_USB3) {
  1897. if ((dum_hcd->port_status &
  1898. USB_SS_PORT_STAT_POWER) != 0) {
  1899. dum_hcd->port_status |= (1 << wValue);
  1900. set_link_state(dum_hcd);
  1901. }
  1902. } else
  1903. if ((dum_hcd->port_status &
  1904. USB_PORT_STAT_POWER) != 0) {
  1905. dum_hcd->port_status |= (1 << wValue);
  1906. set_link_state(dum_hcd);
  1907. }
  1908. }
  1909. break;
  1910. case GetPortErrorCount:
  1911. if (hcd->speed != HCD_USB3) {
  1912. dev_dbg(dummy_dev(dum_hcd),
  1913. "GetPortErrorCount req not "
  1914. "supported for USB 2.0 roothub\n");
  1915. goto error;
  1916. }
  1917. /* We'll always return 0 since this is a dummy hub */
  1918. *(__le32 *) buf = cpu_to_le32(0);
  1919. break;
  1920. case SetHubDepth:
  1921. if (hcd->speed != HCD_USB3) {
  1922. dev_dbg(dummy_dev(dum_hcd),
  1923. "SetHubDepth req not supported for "
  1924. "USB 2.0 roothub\n");
  1925. goto error;
  1926. }
  1927. break;
  1928. default:
  1929. dev_dbg(dummy_dev(dum_hcd),
  1930. "hub control req%04x v%04x i%04x l%d\n",
  1931. typeReq, wValue, wIndex, wLength);
  1932. error:
  1933. /* "protocol stall" on error */
  1934. retval = -EPIPE;
  1935. }
  1936. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  1937. if ((dum_hcd->port_status & PORT_C_MASK) != 0)
  1938. usb_hcd_poll_rh_status(hcd);
  1939. return retval;
  1940. }
  1941. static int dummy_bus_suspend(struct usb_hcd *hcd)
  1942. {
  1943. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1944. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1945. spin_lock_irq(&dum_hcd->dum->lock);
  1946. dum_hcd->rh_state = DUMMY_RH_SUSPENDED;
  1947. set_link_state(dum_hcd);
  1948. hcd->state = HC_STATE_SUSPENDED;
  1949. spin_unlock_irq(&dum_hcd->dum->lock);
  1950. return 0;
  1951. }
  1952. static int dummy_bus_resume(struct usb_hcd *hcd)
  1953. {
  1954. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  1955. int rc = 0;
  1956. dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
  1957. spin_lock_irq(&dum_hcd->dum->lock);
  1958. if (!HCD_HW_ACCESSIBLE(hcd)) {
  1959. rc = -ESHUTDOWN;
  1960. } else {
  1961. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  1962. set_link_state(dum_hcd);
  1963. if (!list_empty(&dum_hcd->urbp_list))
  1964. mod_timer(&dum_hcd->timer, jiffies);
  1965. hcd->state = HC_STATE_RUNNING;
  1966. }
  1967. spin_unlock_irq(&dum_hcd->dum->lock);
  1968. return rc;
  1969. }
  1970. /*-------------------------------------------------------------------------*/
  1971. static inline ssize_t show_urb(char *buf, size_t size, struct urb *urb)
  1972. {
  1973. int ep = usb_pipeendpoint(urb->pipe);
  1974. return snprintf(buf, size,
  1975. "urb/%p %s ep%d%s%s len %d/%d\n",
  1976. urb,
  1977. ({ char *s;
  1978. switch (urb->dev->speed) {
  1979. case USB_SPEED_LOW:
  1980. s = "ls";
  1981. break;
  1982. case USB_SPEED_FULL:
  1983. s = "fs";
  1984. break;
  1985. case USB_SPEED_HIGH:
  1986. s = "hs";
  1987. break;
  1988. case USB_SPEED_SUPER:
  1989. s = "ss";
  1990. break;
  1991. default:
  1992. s = "?";
  1993. break;
  1994. }; s; }),
  1995. ep, ep ? (usb_pipein(urb->pipe) ? "in" : "out") : "",
  1996. ({ char *s; \
  1997. switch (usb_pipetype(urb->pipe)) { \
  1998. case PIPE_CONTROL: \
  1999. s = ""; \
  2000. break; \
  2001. case PIPE_BULK: \
  2002. s = "-bulk"; \
  2003. break; \
  2004. case PIPE_INTERRUPT: \
  2005. s = "-int"; \
  2006. break; \
  2007. default: \
  2008. s = "-iso"; \
  2009. break; \
  2010. }; s; }),
  2011. urb->actual_length, urb->transfer_buffer_length);
  2012. }
  2013. static ssize_t show_urbs(struct device *dev, struct device_attribute *attr,
  2014. char *buf)
  2015. {
  2016. struct usb_hcd *hcd = dev_get_drvdata(dev);
  2017. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2018. struct urbp *urbp;
  2019. size_t size = 0;
  2020. unsigned long flags;
  2021. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2022. list_for_each_entry(urbp, &dum_hcd->urbp_list, urbp_list) {
  2023. size_t temp;
  2024. temp = show_urb(buf, PAGE_SIZE - size, urbp->urb);
  2025. buf += temp;
  2026. size += temp;
  2027. }
  2028. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2029. return size;
  2030. }
  2031. static DEVICE_ATTR(urbs, S_IRUGO, show_urbs, NULL);
  2032. static int dummy_start_ss(struct dummy_hcd *dum_hcd)
  2033. {
  2034. init_timer(&dum_hcd->timer);
  2035. dum_hcd->timer.function = dummy_timer;
  2036. dum_hcd->timer.data = (unsigned long)dum_hcd;
  2037. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  2038. dum_hcd->stream_en_ep = 0;
  2039. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  2040. dummy_hcd_to_hcd(dum_hcd)->power_budget = POWER_BUDGET;
  2041. dummy_hcd_to_hcd(dum_hcd)->state = HC_STATE_RUNNING;
  2042. dummy_hcd_to_hcd(dum_hcd)->uses_new_polling = 1;
  2043. #ifdef CONFIG_USB_OTG
  2044. dummy_hcd_to_hcd(dum_hcd)->self.otg_port = 1;
  2045. #endif
  2046. return 0;
  2047. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  2048. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  2049. }
  2050. static int dummy_start(struct usb_hcd *hcd)
  2051. {
  2052. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2053. /*
  2054. * MASTER side init ... we emulate a root hub that'll only ever
  2055. * talk to one device (the slave side). Also appears in sysfs,
  2056. * just like more familiar pci-based HCDs.
  2057. */
  2058. if (!usb_hcd_is_primary_hcd(hcd))
  2059. return dummy_start_ss(dum_hcd);
  2060. spin_lock_init(&dum_hcd->dum->lock);
  2061. init_timer(&dum_hcd->timer);
  2062. dum_hcd->timer.function = dummy_timer;
  2063. dum_hcd->timer.data = (unsigned long)dum_hcd;
  2064. dum_hcd->rh_state = DUMMY_RH_RUNNING;
  2065. INIT_LIST_HEAD(&dum_hcd->urbp_list);
  2066. hcd->power_budget = POWER_BUDGET;
  2067. hcd->state = HC_STATE_RUNNING;
  2068. hcd->uses_new_polling = 1;
  2069. #ifdef CONFIG_USB_OTG
  2070. hcd->self.otg_port = 1;
  2071. #endif
  2072. /* FIXME 'urbs' should be a per-device thing, maybe in usbcore */
  2073. return device_create_file(dummy_dev(dum_hcd), &dev_attr_urbs);
  2074. }
  2075. static void dummy_stop(struct usb_hcd *hcd)
  2076. {
  2077. struct dummy *dum;
  2078. dum = hcd_to_dummy_hcd(hcd)->dum;
  2079. device_remove_file(dummy_dev(hcd_to_dummy_hcd(hcd)), &dev_attr_urbs);
  2080. usb_gadget_unregister_driver(dum->driver);
  2081. dev_info(dummy_dev(hcd_to_dummy_hcd(hcd)), "stopped\n");
  2082. }
  2083. /*-------------------------------------------------------------------------*/
  2084. static int dummy_h_get_frame(struct usb_hcd *hcd)
  2085. {
  2086. return dummy_g_get_frame(NULL);
  2087. }
  2088. static int dummy_setup(struct usb_hcd *hcd)
  2089. {
  2090. struct dummy *dum;
  2091. dum = *((void **)dev_get_platdata(hcd->self.controller));
  2092. hcd->self.sg_tablesize = ~0;
  2093. if (usb_hcd_is_primary_hcd(hcd)) {
  2094. dum->hs_hcd = hcd_to_dummy_hcd(hcd);
  2095. dum->hs_hcd->dum = dum;
  2096. /*
  2097. * Mark the first roothub as being USB 2.0.
  2098. * The USB 3.0 roothub will be registered later by
  2099. * dummy_hcd_probe()
  2100. */
  2101. hcd->speed = HCD_USB2;
  2102. hcd->self.root_hub->speed = USB_SPEED_HIGH;
  2103. } else {
  2104. dum->ss_hcd = hcd_to_dummy_hcd(hcd);
  2105. dum->ss_hcd->dum = dum;
  2106. hcd->speed = HCD_USB3;
  2107. hcd->self.root_hub->speed = USB_SPEED_SUPER;
  2108. }
  2109. return 0;
  2110. }
  2111. /* Change a group of bulk endpoints to support multiple stream IDs */
  2112. static int dummy_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2113. struct usb_host_endpoint **eps, unsigned int num_eps,
  2114. unsigned int num_streams, gfp_t mem_flags)
  2115. {
  2116. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2117. unsigned long flags;
  2118. int max_stream;
  2119. int ret_streams = num_streams;
  2120. unsigned int index;
  2121. unsigned int i;
  2122. if (!num_eps)
  2123. return -EINVAL;
  2124. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2125. for (i = 0; i < num_eps; i++) {
  2126. index = dummy_get_ep_idx(&eps[i]->desc);
  2127. if ((1 << index) & dum_hcd->stream_en_ep) {
  2128. ret_streams = -EINVAL;
  2129. goto out;
  2130. }
  2131. max_stream = usb_ss_max_streams(&eps[i]->ss_ep_comp);
  2132. if (!max_stream) {
  2133. ret_streams = -EINVAL;
  2134. goto out;
  2135. }
  2136. if (max_stream < ret_streams) {
  2137. dev_dbg(dummy_dev(dum_hcd), "Ep 0x%x only supports %u "
  2138. "stream IDs.\n",
  2139. eps[i]->desc.bEndpointAddress,
  2140. max_stream);
  2141. ret_streams = max_stream;
  2142. }
  2143. }
  2144. for (i = 0; i < num_eps; i++) {
  2145. index = dummy_get_ep_idx(&eps[i]->desc);
  2146. dum_hcd->stream_en_ep |= 1 << index;
  2147. set_max_streams_for_pipe(dum_hcd,
  2148. usb_endpoint_num(&eps[i]->desc), ret_streams);
  2149. }
  2150. out:
  2151. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2152. return ret_streams;
  2153. }
  2154. /* Reverts a group of bulk endpoints back to not using stream IDs. */
  2155. static int dummy_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
  2156. struct usb_host_endpoint **eps, unsigned int num_eps,
  2157. gfp_t mem_flags)
  2158. {
  2159. struct dummy_hcd *dum_hcd = hcd_to_dummy_hcd(hcd);
  2160. unsigned long flags;
  2161. int ret;
  2162. unsigned int index;
  2163. unsigned int i;
  2164. spin_lock_irqsave(&dum_hcd->dum->lock, flags);
  2165. for (i = 0; i < num_eps; i++) {
  2166. index = dummy_get_ep_idx(&eps[i]->desc);
  2167. if (!((1 << index) & dum_hcd->stream_en_ep)) {
  2168. ret = -EINVAL;
  2169. goto out;
  2170. }
  2171. }
  2172. for (i = 0; i < num_eps; i++) {
  2173. index = dummy_get_ep_idx(&eps[i]->desc);
  2174. dum_hcd->stream_en_ep &= ~(1 << index);
  2175. set_max_streams_for_pipe(dum_hcd,
  2176. usb_endpoint_num(&eps[i]->desc), 0);
  2177. }
  2178. ret = 0;
  2179. out:
  2180. spin_unlock_irqrestore(&dum_hcd->dum->lock, flags);
  2181. return ret;
  2182. }
  2183. static struct hc_driver dummy_hcd = {
  2184. .description = (char *) driver_name,
  2185. .product_desc = "Dummy host controller",
  2186. .hcd_priv_size = sizeof(struct dummy_hcd),
  2187. .flags = HCD_USB3 | HCD_SHARED,
  2188. .reset = dummy_setup,
  2189. .start = dummy_start,
  2190. .stop = dummy_stop,
  2191. .urb_enqueue = dummy_urb_enqueue,
  2192. .urb_dequeue = dummy_urb_dequeue,
  2193. .get_frame_number = dummy_h_get_frame,
  2194. .hub_status_data = dummy_hub_status,
  2195. .hub_control = dummy_hub_control,
  2196. .bus_suspend = dummy_bus_suspend,
  2197. .bus_resume = dummy_bus_resume,
  2198. .alloc_streams = dummy_alloc_streams,
  2199. .free_streams = dummy_free_streams,
  2200. };
  2201. static int dummy_hcd_probe(struct platform_device *pdev)
  2202. {
  2203. struct dummy *dum;
  2204. struct usb_hcd *hs_hcd;
  2205. struct usb_hcd *ss_hcd;
  2206. int retval;
  2207. dev_info(&pdev->dev, "%s, driver " DRIVER_VERSION "\n", driver_desc);
  2208. dum = *((void **)dev_get_platdata(&pdev->dev));
  2209. if (!mod_data.is_super_speed)
  2210. dummy_hcd.flags = HCD_USB2;
  2211. hs_hcd = usb_create_hcd(&dummy_hcd, &pdev->dev, dev_name(&pdev->dev));
  2212. if (!hs_hcd)
  2213. return -ENOMEM;
  2214. hs_hcd->has_tt = 1;
  2215. retval = usb_add_hcd(hs_hcd, 0, 0);
  2216. if (retval)
  2217. goto put_usb2_hcd;
  2218. if (mod_data.is_super_speed) {
  2219. ss_hcd = usb_create_shared_hcd(&dummy_hcd, &pdev->dev,
  2220. dev_name(&pdev->dev), hs_hcd);
  2221. if (!ss_hcd) {
  2222. retval = -ENOMEM;
  2223. goto dealloc_usb2_hcd;
  2224. }
  2225. retval = usb_add_hcd(ss_hcd, 0, 0);
  2226. if (retval)
  2227. goto put_usb3_hcd;
  2228. }
  2229. return 0;
  2230. put_usb3_hcd:
  2231. usb_put_hcd(ss_hcd);
  2232. dealloc_usb2_hcd:
  2233. usb_remove_hcd(hs_hcd);
  2234. put_usb2_hcd:
  2235. usb_put_hcd(hs_hcd);
  2236. dum->hs_hcd = dum->ss_hcd = NULL;
  2237. return retval;
  2238. }
  2239. static int dummy_hcd_remove(struct platform_device *pdev)
  2240. {
  2241. struct dummy *dum;
  2242. dum = hcd_to_dummy_hcd(platform_get_drvdata(pdev))->dum;
  2243. if (dum->ss_hcd) {
  2244. usb_remove_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2245. usb_put_hcd(dummy_hcd_to_hcd(dum->ss_hcd));
  2246. }
  2247. usb_remove_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2248. usb_put_hcd(dummy_hcd_to_hcd(dum->hs_hcd));
  2249. dum->hs_hcd = NULL;
  2250. dum->ss_hcd = NULL;
  2251. return 0;
  2252. }
  2253. static int dummy_hcd_suspend(struct platform_device *pdev, pm_message_t state)
  2254. {
  2255. struct usb_hcd *hcd;
  2256. struct dummy_hcd *dum_hcd;
  2257. int rc = 0;
  2258. dev_dbg(&pdev->dev, "%s\n", __func__);
  2259. hcd = platform_get_drvdata(pdev);
  2260. dum_hcd = hcd_to_dummy_hcd(hcd);
  2261. if (dum_hcd->rh_state == DUMMY_RH_RUNNING) {
  2262. dev_warn(&pdev->dev, "Root hub isn't suspended!\n");
  2263. rc = -EBUSY;
  2264. } else
  2265. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2266. return rc;
  2267. }
  2268. static int dummy_hcd_resume(struct platform_device *pdev)
  2269. {
  2270. struct usb_hcd *hcd;
  2271. dev_dbg(&pdev->dev, "%s\n", __func__);
  2272. hcd = platform_get_drvdata(pdev);
  2273. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  2274. usb_hcd_poll_rh_status(hcd);
  2275. return 0;
  2276. }
  2277. static struct platform_driver dummy_hcd_driver = {
  2278. .probe = dummy_hcd_probe,
  2279. .remove = dummy_hcd_remove,
  2280. .suspend = dummy_hcd_suspend,
  2281. .resume = dummy_hcd_resume,
  2282. .driver = {
  2283. .name = (char *) driver_name,
  2284. .owner = THIS_MODULE,
  2285. },
  2286. };
  2287. /*-------------------------------------------------------------------------*/
  2288. #define MAX_NUM_UDC 2
  2289. static struct platform_device *the_udc_pdev[MAX_NUM_UDC];
  2290. static struct platform_device *the_hcd_pdev[MAX_NUM_UDC];
  2291. static int __init init(void)
  2292. {
  2293. int retval = -ENOMEM;
  2294. int i;
  2295. struct dummy *dum[MAX_NUM_UDC];
  2296. if (usb_disabled())
  2297. return -ENODEV;
  2298. if (!mod_data.is_high_speed && mod_data.is_super_speed)
  2299. return -EINVAL;
  2300. if (mod_data.num < 1 || mod_data.num > MAX_NUM_UDC) {
  2301. pr_err("Number of emulated UDC must be in range of 1…%d\n",
  2302. MAX_NUM_UDC);
  2303. return -EINVAL;
  2304. }
  2305. for (i = 0; i < mod_data.num; i++) {
  2306. the_hcd_pdev[i] = platform_device_alloc(driver_name, i);
  2307. if (!the_hcd_pdev[i]) {
  2308. i--;
  2309. while (i >= 0)
  2310. platform_device_put(the_hcd_pdev[i--]);
  2311. return retval;
  2312. }
  2313. }
  2314. for (i = 0; i < mod_data.num; i++) {
  2315. the_udc_pdev[i] = platform_device_alloc(gadget_name, i);
  2316. if (!the_udc_pdev[i]) {
  2317. i--;
  2318. while (i >= 0)
  2319. platform_device_put(the_udc_pdev[i--]);
  2320. goto err_alloc_udc;
  2321. }
  2322. }
  2323. for (i = 0; i < mod_data.num; i++) {
  2324. dum[i] = kzalloc(sizeof(struct dummy), GFP_KERNEL);
  2325. if (!dum[i]) {
  2326. retval = -ENOMEM;
  2327. goto err_add_pdata;
  2328. }
  2329. retval = platform_device_add_data(the_hcd_pdev[i], &dum[i],
  2330. sizeof(void *));
  2331. if (retval)
  2332. goto err_add_pdata;
  2333. retval = platform_device_add_data(the_udc_pdev[i], &dum[i],
  2334. sizeof(void *));
  2335. if (retval)
  2336. goto err_add_pdata;
  2337. }
  2338. retval = platform_driver_register(&dummy_hcd_driver);
  2339. if (retval < 0)
  2340. goto err_add_pdata;
  2341. retval = platform_driver_register(&dummy_udc_driver);
  2342. if (retval < 0)
  2343. goto err_register_udc_driver;
  2344. for (i = 0; i < mod_data.num; i++) {
  2345. retval = platform_device_add(the_hcd_pdev[i]);
  2346. if (retval < 0) {
  2347. i--;
  2348. while (i >= 0)
  2349. platform_device_del(the_hcd_pdev[i--]);
  2350. goto err_add_hcd;
  2351. }
  2352. }
  2353. for (i = 0; i < mod_data.num; i++) {
  2354. if (!dum[i]->hs_hcd ||
  2355. (!dum[i]->ss_hcd && mod_data.is_super_speed)) {
  2356. /*
  2357. * The hcd was added successfully but its probe
  2358. * function failed for some reason.
  2359. */
  2360. retval = -EINVAL;
  2361. goto err_add_udc;
  2362. }
  2363. }
  2364. for (i = 0; i < mod_data.num; i++) {
  2365. retval = platform_device_add(the_udc_pdev[i]);
  2366. if (retval < 0) {
  2367. i--;
  2368. while (i >= 0)
  2369. platform_device_del(the_udc_pdev[i]);
  2370. goto err_add_udc;
  2371. }
  2372. }
  2373. for (i = 0; i < mod_data.num; i++) {
  2374. if (!platform_get_drvdata(the_udc_pdev[i])) {
  2375. /*
  2376. * The udc was added successfully but its probe
  2377. * function failed for some reason.
  2378. */
  2379. retval = -EINVAL;
  2380. goto err_probe_udc;
  2381. }
  2382. }
  2383. return retval;
  2384. err_probe_udc:
  2385. for (i = 0; i < mod_data.num; i++)
  2386. platform_device_del(the_udc_pdev[i]);
  2387. err_add_udc:
  2388. for (i = 0; i < mod_data.num; i++)
  2389. platform_device_del(the_hcd_pdev[i]);
  2390. err_add_hcd:
  2391. platform_driver_unregister(&dummy_udc_driver);
  2392. err_register_udc_driver:
  2393. platform_driver_unregister(&dummy_hcd_driver);
  2394. err_add_pdata:
  2395. for (i = 0; i < mod_data.num; i++)
  2396. kfree(dum[i]);
  2397. for (i = 0; i < mod_data.num; i++)
  2398. platform_device_put(the_udc_pdev[i]);
  2399. err_alloc_udc:
  2400. for (i = 0; i < mod_data.num; i++)
  2401. platform_device_put(the_hcd_pdev[i]);
  2402. return retval;
  2403. }
  2404. module_init(init);
  2405. static void __exit cleanup(void)
  2406. {
  2407. int i;
  2408. for (i = 0; i < mod_data.num; i++) {
  2409. struct dummy *dum;
  2410. dum = *((void **)dev_get_platdata(&the_udc_pdev[i]->dev));
  2411. platform_device_unregister(the_udc_pdev[i]);
  2412. platform_device_unregister(the_hcd_pdev[i]);
  2413. kfree(dum);
  2414. }
  2415. platform_driver_unregister(&dummy_udc_driver);
  2416. platform_driver_unregister(&dummy_hcd_driver);
  2417. }
  2418. module_exit(cleanup);