dummy_hcd.c 64 KB

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