dummy_hcd.c 67 KB

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