dummy_hcd.c 72 KB

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