dummy_hcd.c 69 KB

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