dummy_hcd.c 71 KB

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