dummy_hcd.c 62 KB

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