dummy_hcd.c 63 KB

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