dummy_hcd.c 71 KB

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