dummy_hcd.c 73 KB

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