dummy_hcd.c 66 KB

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