dummy_hcd.c 66 KB

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