pxa27x_udc.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Handles the Intel 27x USB Device Controller (UDC)
  4. *
  5. * Inspired by original driver by Frank Becker, David Brownell, and others.
  6. * Copyright (C) 2008 Robert Jarzmik
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/delay.h>
  15. #include <linux/list.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/clk.h>
  19. #include <linux/irq.h>
  20. #include <linux/gpio.h>
  21. #include <linux/gpio/consumer.h>
  22. #include <linux/slab.h>
  23. #include <linux/prefetch.h>
  24. #include <linux/byteorder/generic.h>
  25. #include <linux/platform_data/pxa2xx_udc.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/ch9.h>
  30. #include <linux/usb/gadget.h>
  31. #include <linux/usb/phy.h>
  32. #include "pxa27x_udc.h"
  33. /*
  34. * This driver handles the USB Device Controller (UDC) in Intel's PXA 27x
  35. * series processors.
  36. *
  37. * Such controller drivers work with a gadget driver. The gadget driver
  38. * returns descriptors, implements configuration and data protocols used
  39. * by the host to interact with this device, and allocates endpoints to
  40. * the different protocol interfaces. The controller driver virtualizes
  41. * usb hardware so that the gadget drivers will be more portable.
  42. *
  43. * This UDC hardware wants to implement a bit too much USB protocol. The
  44. * biggest issues are: that the endpoints have to be set up before the
  45. * controller can be enabled (minor, and not uncommon); and each endpoint
  46. * can only have one configuration, interface and alternative interface
  47. * number (major, and very unusual). Once set up, these cannot be changed
  48. * without a controller reset.
  49. *
  50. * The workaround is to setup all combinations necessary for the gadgets which
  51. * will work with this driver. This is done in pxa_udc structure, statically.
  52. * See pxa_udc, udc_usb_ep versus pxa_ep, and matching function find_pxa_ep.
  53. * (You could modify this if needed. Some drivers have a "fifo_mode" module
  54. * parameter to facilitate such changes.)
  55. *
  56. * The combinations have been tested with these gadgets :
  57. * - zero gadget
  58. * - file storage gadget
  59. * - ether gadget
  60. *
  61. * The driver doesn't use DMA, only IO access and IRQ callbacks. No use is
  62. * made of UDC's double buffering either. USB "On-The-Go" is not implemented.
  63. *
  64. * All the requests are handled the same way :
  65. * - the drivers tries to handle the request directly to the IO
  66. * - if the IO fifo is not big enough, the remaining is send/received in
  67. * interrupt handling.
  68. */
  69. #define DRIVER_VERSION "2008-04-18"
  70. #define DRIVER_DESC "PXA 27x USB Device Controller driver"
  71. static const char driver_name[] = "pxa27x_udc";
  72. static struct pxa_udc *the_controller;
  73. static void handle_ep(struct pxa_ep *ep);
  74. /*
  75. * Debug filesystem
  76. */
  77. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  78. #include <linux/debugfs.h>
  79. #include <linux/uaccess.h>
  80. #include <linux/seq_file.h>
  81. static int state_dbg_show(struct seq_file *s, void *p)
  82. {
  83. struct pxa_udc *udc = s->private;
  84. u32 tmp;
  85. if (!udc->driver)
  86. return -ENODEV;
  87. /* basic device status */
  88. seq_printf(s, DRIVER_DESC "\n"
  89. "%s version: %s\n"
  90. "Gadget driver: %s\n",
  91. driver_name, DRIVER_VERSION,
  92. udc->driver ? udc->driver->driver.name : "(none)");
  93. tmp = udc_readl(udc, UDCCR);
  94. seq_printf(s,
  95. "udccr=0x%0x(%s%s%s%s%s%s%s%s%s%s), con=%d,inter=%d,altinter=%d\n",
  96. tmp,
  97. (tmp & UDCCR_OEN) ? " oen":"",
  98. (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
  99. (tmp & UDCCR_AHNP) ? " rem" : "",
  100. (tmp & UDCCR_BHNP) ? " rstir" : "",
  101. (tmp & UDCCR_DWRE) ? " dwre" : "",
  102. (tmp & UDCCR_SMAC) ? " smac" : "",
  103. (tmp & UDCCR_EMCE) ? " emce" : "",
  104. (tmp & UDCCR_UDR) ? " udr" : "",
  105. (tmp & UDCCR_UDA) ? " uda" : "",
  106. (tmp & UDCCR_UDE) ? " ude" : "",
  107. (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
  108. (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
  109. (tmp & UDCCR_AAISN) >> UDCCR_AAISN_S);
  110. /* registers for device and ep0 */
  111. seq_printf(s, "udcicr0=0x%08x udcicr1=0x%08x\n",
  112. udc_readl(udc, UDCICR0), udc_readl(udc, UDCICR1));
  113. seq_printf(s, "udcisr0=0x%08x udcisr1=0x%08x\n",
  114. udc_readl(udc, UDCISR0), udc_readl(udc, UDCISR1));
  115. seq_printf(s, "udcfnr=%d\n", udc_readl(udc, UDCFNR));
  116. seq_printf(s, "irqs: reset=%lu, suspend=%lu, resume=%lu, reconfig=%lu\n",
  117. udc->stats.irqs_reset, udc->stats.irqs_suspend,
  118. udc->stats.irqs_resume, udc->stats.irqs_reconfig);
  119. return 0;
  120. }
  121. DEFINE_SHOW_ATTRIBUTE(state_dbg);
  122. static int queues_dbg_show(struct seq_file *s, void *p)
  123. {
  124. struct pxa_udc *udc = s->private;
  125. struct pxa_ep *ep;
  126. struct pxa27x_request *req;
  127. int i, maxpkt;
  128. if (!udc->driver)
  129. return -ENODEV;
  130. /* dump endpoint queues */
  131. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  132. ep = &udc->pxa_ep[i];
  133. maxpkt = ep->fifo_size;
  134. seq_printf(s, "%-12s max_pkt=%d %s\n",
  135. EPNAME(ep), maxpkt, "pio");
  136. if (list_empty(&ep->queue)) {
  137. seq_puts(s, "\t(nothing queued)\n");
  138. continue;
  139. }
  140. list_for_each_entry(req, &ep->queue, queue) {
  141. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  142. &req->req, req->req.actual,
  143. req->req.length, req->req.buf);
  144. }
  145. }
  146. return 0;
  147. }
  148. DEFINE_SHOW_ATTRIBUTE(queues_dbg);
  149. static int eps_dbg_show(struct seq_file *s, void *p)
  150. {
  151. struct pxa_udc *udc = s->private;
  152. struct pxa_ep *ep;
  153. int i;
  154. u32 tmp;
  155. if (!udc->driver)
  156. return -ENODEV;
  157. ep = &udc->pxa_ep[0];
  158. tmp = udc_ep_readl(ep, UDCCSR);
  159. seq_printf(s, "udccsr0=0x%03x(%s%s%s%s%s%s%s)\n",
  160. tmp,
  161. (tmp & UDCCSR0_SA) ? " sa" : "",
  162. (tmp & UDCCSR0_RNE) ? " rne" : "",
  163. (tmp & UDCCSR0_FST) ? " fst" : "",
  164. (tmp & UDCCSR0_SST) ? " sst" : "",
  165. (tmp & UDCCSR0_DME) ? " dme" : "",
  166. (tmp & UDCCSR0_IPR) ? " ipr" : "",
  167. (tmp & UDCCSR0_OPC) ? " opc" : "");
  168. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  169. ep = &udc->pxa_ep[i];
  170. tmp = i? udc_ep_readl(ep, UDCCR) : udc_readl(udc, UDCCR);
  171. seq_printf(s, "%-12s: IN %lu(%lu reqs), OUT %lu(%lu reqs), irqs=%lu, udccr=0x%08x, udccsr=0x%03x, udcbcr=%d\n",
  172. EPNAME(ep),
  173. ep->stats.in_bytes, ep->stats.in_ops,
  174. ep->stats.out_bytes, ep->stats.out_ops,
  175. ep->stats.irqs,
  176. tmp, udc_ep_readl(ep, UDCCSR),
  177. udc_ep_readl(ep, UDCBCR));
  178. }
  179. return 0;
  180. }
  181. DEFINE_SHOW_ATTRIBUTE(eps_dbg);
  182. static void pxa_init_debugfs(struct pxa_udc *udc)
  183. {
  184. struct dentry *root;
  185. root = debugfs_create_dir(udc->gadget.name, NULL);
  186. udc->debugfs_root = root;
  187. debugfs_create_file("udcstate", 0400, root, udc, &state_dbg_fops);
  188. debugfs_create_file("queues", 0400, root, udc, &queues_dbg_fops);
  189. debugfs_create_file("epstate", 0400, root, udc, &eps_dbg_fops);
  190. }
  191. static void pxa_cleanup_debugfs(struct pxa_udc *udc)
  192. {
  193. debugfs_remove_recursive(udc->debugfs_root);
  194. }
  195. #else
  196. static inline void pxa_init_debugfs(struct pxa_udc *udc)
  197. {
  198. }
  199. static inline void pxa_cleanup_debugfs(struct pxa_udc *udc)
  200. {
  201. }
  202. #endif
  203. /**
  204. * is_match_usb_pxa - check if usb_ep and pxa_ep match
  205. * @udc_usb_ep: usb endpoint
  206. * @ep: pxa endpoint
  207. * @config: configuration required in pxa_ep
  208. * @interface: interface required in pxa_ep
  209. * @altsetting: altsetting required in pxa_ep
  210. *
  211. * Returns 1 if all criteria match between pxa and usb endpoint, 0 otherwise
  212. */
  213. static int is_match_usb_pxa(struct udc_usb_ep *udc_usb_ep, struct pxa_ep *ep,
  214. int config, int interface, int altsetting)
  215. {
  216. if (usb_endpoint_num(&udc_usb_ep->desc) != ep->addr)
  217. return 0;
  218. if (usb_endpoint_dir_in(&udc_usb_ep->desc) != ep->dir_in)
  219. return 0;
  220. if (usb_endpoint_type(&udc_usb_ep->desc) != ep->type)
  221. return 0;
  222. if ((ep->config != config) || (ep->interface != interface)
  223. || (ep->alternate != altsetting))
  224. return 0;
  225. return 1;
  226. }
  227. /**
  228. * find_pxa_ep - find pxa_ep structure matching udc_usb_ep
  229. * @udc: pxa udc
  230. * @udc_usb_ep: udc_usb_ep structure
  231. *
  232. * Match udc_usb_ep and all pxa_ep available, to see if one matches.
  233. * This is necessary because of the strong pxa hardware restriction requiring
  234. * that once pxa endpoints are initialized, their configuration is freezed, and
  235. * no change can be made to their address, direction, or in which configuration,
  236. * interface or altsetting they are active ... which differs from more usual
  237. * models which have endpoints be roughly just addressable fifos, and leave
  238. * configuration events up to gadget drivers (like all control messages).
  239. *
  240. * Note that there is still a blurred point here :
  241. * - we rely on UDCCR register "active interface" and "active altsetting".
  242. * This is a nonsense in regard of USB spec, where multiple interfaces are
  243. * active at the same time.
  244. * - if we knew for sure that the pxa can handle multiple interface at the
  245. * same time, assuming Intel's Developer Guide is wrong, this function
  246. * should be reviewed, and a cache of couples (iface, altsetting) should
  247. * be kept in the pxa_udc structure. In this case this function would match
  248. * against the cache of couples instead of the "last altsetting" set up.
  249. *
  250. * Returns the matched pxa_ep structure or NULL if none found
  251. */
  252. static struct pxa_ep *find_pxa_ep(struct pxa_udc *udc,
  253. struct udc_usb_ep *udc_usb_ep)
  254. {
  255. int i;
  256. struct pxa_ep *ep;
  257. int cfg = udc->config;
  258. int iface = udc->last_interface;
  259. int alt = udc->last_alternate;
  260. if (udc_usb_ep == &udc->udc_usb_ep[0])
  261. return &udc->pxa_ep[0];
  262. for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
  263. ep = &udc->pxa_ep[i];
  264. if (is_match_usb_pxa(udc_usb_ep, ep, cfg, iface, alt))
  265. return ep;
  266. }
  267. return NULL;
  268. }
  269. /**
  270. * update_pxa_ep_matches - update pxa_ep cached values in all udc_usb_ep
  271. * @udc: pxa udc
  272. *
  273. * Context: in_interrupt()
  274. *
  275. * Updates all pxa_ep fields in udc_usb_ep structures, if this field was
  276. * previously set up (and is not NULL). The update is necessary is a
  277. * configuration change or altsetting change was issued by the USB host.
  278. */
  279. static void update_pxa_ep_matches(struct pxa_udc *udc)
  280. {
  281. int i;
  282. struct udc_usb_ep *udc_usb_ep;
  283. for (i = 1; i < NR_USB_ENDPOINTS; i++) {
  284. udc_usb_ep = &udc->udc_usb_ep[i];
  285. if (udc_usb_ep->pxa_ep)
  286. udc_usb_ep->pxa_ep = find_pxa_ep(udc, udc_usb_ep);
  287. }
  288. }
  289. /**
  290. * pio_irq_enable - Enables irq generation for one endpoint
  291. * @ep: udc endpoint
  292. */
  293. static void pio_irq_enable(struct pxa_ep *ep)
  294. {
  295. struct pxa_udc *udc = ep->dev;
  296. int index = EPIDX(ep);
  297. u32 udcicr0 = udc_readl(udc, UDCICR0);
  298. u32 udcicr1 = udc_readl(udc, UDCICR1);
  299. if (index < 16)
  300. udc_writel(udc, UDCICR0, udcicr0 | (3 << (index * 2)));
  301. else
  302. udc_writel(udc, UDCICR1, udcicr1 | (3 << ((index - 16) * 2)));
  303. }
  304. /**
  305. * pio_irq_disable - Disables irq generation for one endpoint
  306. * @ep: udc endpoint
  307. */
  308. static void pio_irq_disable(struct pxa_ep *ep)
  309. {
  310. struct pxa_udc *udc = ep->dev;
  311. int index = EPIDX(ep);
  312. u32 udcicr0 = udc_readl(udc, UDCICR0);
  313. u32 udcicr1 = udc_readl(udc, UDCICR1);
  314. if (index < 16)
  315. udc_writel(udc, UDCICR0, udcicr0 & ~(3 << (index * 2)));
  316. else
  317. udc_writel(udc, UDCICR1, udcicr1 & ~(3 << ((index - 16) * 2)));
  318. }
  319. /**
  320. * udc_set_mask_UDCCR - set bits in UDCCR
  321. * @udc: udc device
  322. * @mask: bits to set in UDCCR
  323. *
  324. * Sets bits in UDCCR, leaving DME and FST bits as they were.
  325. */
  326. static inline void udc_set_mask_UDCCR(struct pxa_udc *udc, int mask)
  327. {
  328. u32 udccr = udc_readl(udc, UDCCR);
  329. udc_writel(udc, UDCCR,
  330. (udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS));
  331. }
  332. /**
  333. * udc_clear_mask_UDCCR - clears bits in UDCCR
  334. * @udc: udc device
  335. * @mask: bit to clear in UDCCR
  336. *
  337. * Clears bits in UDCCR, leaving DME and FST bits as they were.
  338. */
  339. static inline void udc_clear_mask_UDCCR(struct pxa_udc *udc, int mask)
  340. {
  341. u32 udccr = udc_readl(udc, UDCCR);
  342. udc_writel(udc, UDCCR,
  343. (udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS));
  344. }
  345. /**
  346. * ep_write_UDCCSR - set bits in UDCCSR
  347. * @udc: udc device
  348. * @mask: bits to set in UDCCR
  349. *
  350. * Sets bits in UDCCSR (UDCCSR0 and UDCCSR*).
  351. *
  352. * A specific case is applied to ep0 : the ACM bit is always set to 1, for
  353. * SET_INTERFACE and SET_CONFIGURATION.
  354. */
  355. static inline void ep_write_UDCCSR(struct pxa_ep *ep, int mask)
  356. {
  357. if (is_ep0(ep))
  358. mask |= UDCCSR0_ACM;
  359. udc_ep_writel(ep, UDCCSR, mask);
  360. }
  361. /**
  362. * ep_count_bytes_remain - get how many bytes in udc endpoint
  363. * @ep: udc endpoint
  364. *
  365. * Returns number of bytes in OUT fifos. Broken for IN fifos (-EOPNOTSUPP)
  366. */
  367. static int ep_count_bytes_remain(struct pxa_ep *ep)
  368. {
  369. if (ep->dir_in)
  370. return -EOPNOTSUPP;
  371. return udc_ep_readl(ep, UDCBCR) & 0x3ff;
  372. }
  373. /**
  374. * ep_is_empty - checks if ep has byte ready for reading
  375. * @ep: udc endpoint
  376. *
  377. * If endpoint is the control endpoint, checks if there are bytes in the
  378. * control endpoint fifo. If endpoint is a data endpoint, checks if bytes
  379. * are ready for reading on OUT endpoint.
  380. *
  381. * Returns 0 if ep not empty, 1 if ep empty, -EOPNOTSUPP if IN endpoint
  382. */
  383. static int ep_is_empty(struct pxa_ep *ep)
  384. {
  385. int ret;
  386. if (!is_ep0(ep) && ep->dir_in)
  387. return -EOPNOTSUPP;
  388. if (is_ep0(ep))
  389. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR0_RNE);
  390. else
  391. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNE);
  392. return ret;
  393. }
  394. /**
  395. * ep_is_full - checks if ep has place to write bytes
  396. * @ep: udc endpoint
  397. *
  398. * If endpoint is not the control endpoint and is an IN endpoint, checks if
  399. * there is place to write bytes into the endpoint.
  400. *
  401. * Returns 0 if ep not full, 1 if ep full, -EOPNOTSUPP if OUT endpoint
  402. */
  403. static int ep_is_full(struct pxa_ep *ep)
  404. {
  405. if (is_ep0(ep))
  406. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_IPR);
  407. if (!ep->dir_in)
  408. return -EOPNOTSUPP;
  409. return (!(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNF));
  410. }
  411. /**
  412. * epout_has_pkt - checks if OUT endpoint fifo has a packet available
  413. * @ep: pxa endpoint
  414. *
  415. * Returns 1 if a complete packet is available, 0 if not, -EOPNOTSUPP for IN ep.
  416. */
  417. static int epout_has_pkt(struct pxa_ep *ep)
  418. {
  419. if (!is_ep0(ep) && ep->dir_in)
  420. return -EOPNOTSUPP;
  421. if (is_ep0(ep))
  422. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_OPC);
  423. return (udc_ep_readl(ep, UDCCSR) & UDCCSR_PC);
  424. }
  425. /**
  426. * set_ep0state - Set ep0 automata state
  427. * @dev: udc device
  428. * @state: state
  429. */
  430. static void set_ep0state(struct pxa_udc *udc, int state)
  431. {
  432. struct pxa_ep *ep = &udc->pxa_ep[0];
  433. char *old_stname = EP0_STNAME(udc);
  434. udc->ep0state = state;
  435. ep_dbg(ep, "state=%s->%s, udccsr0=0x%03x, udcbcr=%d\n", old_stname,
  436. EP0_STNAME(udc), udc_ep_readl(ep, UDCCSR),
  437. udc_ep_readl(ep, UDCBCR));
  438. }
  439. /**
  440. * ep0_idle - Put control endpoint into idle state
  441. * @dev: udc device
  442. */
  443. static void ep0_idle(struct pxa_udc *dev)
  444. {
  445. set_ep0state(dev, WAIT_FOR_SETUP);
  446. }
  447. /**
  448. * inc_ep_stats_reqs - Update ep stats counts
  449. * @ep: physical endpoint
  450. * @req: usb request
  451. * @is_in: ep direction (USB_DIR_IN or 0)
  452. *
  453. */
  454. static void inc_ep_stats_reqs(struct pxa_ep *ep, int is_in)
  455. {
  456. if (is_in)
  457. ep->stats.in_ops++;
  458. else
  459. ep->stats.out_ops++;
  460. }
  461. /**
  462. * inc_ep_stats_bytes - Update ep stats counts
  463. * @ep: physical endpoint
  464. * @count: bytes transferred on endpoint
  465. * @is_in: ep direction (USB_DIR_IN or 0)
  466. */
  467. static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
  468. {
  469. if (is_in)
  470. ep->stats.in_bytes += count;
  471. else
  472. ep->stats.out_bytes += count;
  473. }
  474. /**
  475. * pxa_ep_setup - Sets up an usb physical endpoint
  476. * @ep: pxa27x physical endpoint
  477. *
  478. * Find the physical pxa27x ep, and setup its UDCCR
  479. */
  480. static void pxa_ep_setup(struct pxa_ep *ep)
  481. {
  482. u32 new_udccr;
  483. new_udccr = ((ep->config << UDCCONR_CN_S) & UDCCONR_CN)
  484. | ((ep->interface << UDCCONR_IN_S) & UDCCONR_IN)
  485. | ((ep->alternate << UDCCONR_AISN_S) & UDCCONR_AISN)
  486. | ((EPADDR(ep) << UDCCONR_EN_S) & UDCCONR_EN)
  487. | ((EPXFERTYPE(ep) << UDCCONR_ET_S) & UDCCONR_ET)
  488. | ((ep->dir_in) ? UDCCONR_ED : 0)
  489. | ((ep->fifo_size << UDCCONR_MPS_S) & UDCCONR_MPS)
  490. | UDCCONR_EE;
  491. udc_ep_writel(ep, UDCCR, new_udccr);
  492. }
  493. /**
  494. * pxa_eps_setup - Sets up all usb physical endpoints
  495. * @dev: udc device
  496. *
  497. * Setup all pxa physical endpoints, except ep0
  498. */
  499. static void pxa_eps_setup(struct pxa_udc *dev)
  500. {
  501. unsigned int i;
  502. dev_dbg(dev->dev, "%s: dev=%p\n", __func__, dev);
  503. for (i = 1; i < NR_PXA_ENDPOINTS; i++)
  504. pxa_ep_setup(&dev->pxa_ep[i]);
  505. }
  506. /**
  507. * pxa_ep_alloc_request - Allocate usb request
  508. * @_ep: usb endpoint
  509. * @gfp_flags:
  510. *
  511. * For the pxa27x, these can just wrap kmalloc/kfree. gadget drivers
  512. * must still pass correctly initialized endpoints, since other controller
  513. * drivers may care about how it's currently set up (dma issues etc).
  514. */
  515. static struct usb_request *
  516. pxa_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  517. {
  518. struct pxa27x_request *req;
  519. req = kzalloc(sizeof *req, gfp_flags);
  520. if (!req)
  521. return NULL;
  522. INIT_LIST_HEAD(&req->queue);
  523. req->in_use = 0;
  524. req->udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  525. return &req->req;
  526. }
  527. /**
  528. * pxa_ep_free_request - Free usb request
  529. * @_ep: usb endpoint
  530. * @_req: usb request
  531. *
  532. * Wrapper around kfree to free _req
  533. */
  534. static void pxa_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  535. {
  536. struct pxa27x_request *req;
  537. req = container_of(_req, struct pxa27x_request, req);
  538. WARN_ON(!list_empty(&req->queue));
  539. kfree(req);
  540. }
  541. /**
  542. * ep_add_request - add a request to the endpoint's queue
  543. * @ep: usb endpoint
  544. * @req: usb request
  545. *
  546. * Context: ep->lock held
  547. *
  548. * Queues the request in the endpoint's queue, and enables the interrupts
  549. * on the endpoint.
  550. */
  551. static void ep_add_request(struct pxa_ep *ep, struct pxa27x_request *req)
  552. {
  553. if (unlikely(!req))
  554. return;
  555. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  556. req->req.length, udc_ep_readl(ep, UDCCSR));
  557. req->in_use = 1;
  558. list_add_tail(&req->queue, &ep->queue);
  559. pio_irq_enable(ep);
  560. }
  561. /**
  562. * ep_del_request - removes a request from the endpoint's queue
  563. * @ep: usb endpoint
  564. * @req: usb request
  565. *
  566. * Context: ep->lock held
  567. *
  568. * Unqueue the request from the endpoint's queue. If there are no more requests
  569. * on the endpoint, and if it's not the control endpoint, interrupts are
  570. * disabled on the endpoint.
  571. */
  572. static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
  573. {
  574. if (unlikely(!req))
  575. return;
  576. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  577. req->req.length, udc_ep_readl(ep, UDCCSR));
  578. list_del_init(&req->queue);
  579. req->in_use = 0;
  580. if (!is_ep0(ep) && list_empty(&ep->queue))
  581. pio_irq_disable(ep);
  582. }
  583. /**
  584. * req_done - Complete an usb request
  585. * @ep: pxa physical endpoint
  586. * @req: pxa request
  587. * @status: usb request status sent to gadget API
  588. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  589. *
  590. * Context: ep->lock held if flags not NULL, else ep->lock released
  591. *
  592. * Retire a pxa27x usb request. Endpoint must be locked.
  593. */
  594. static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status,
  595. unsigned long *pflags)
  596. {
  597. unsigned long flags;
  598. ep_del_request(ep, req);
  599. if (likely(req->req.status == -EINPROGRESS))
  600. req->req.status = status;
  601. else
  602. status = req->req.status;
  603. if (status && status != -ESHUTDOWN)
  604. ep_dbg(ep, "complete req %p stat %d len %u/%u\n",
  605. &req->req, status,
  606. req->req.actual, req->req.length);
  607. if (pflags)
  608. spin_unlock_irqrestore(&ep->lock, *pflags);
  609. local_irq_save(flags);
  610. usb_gadget_giveback_request(&req->udc_usb_ep->usb_ep, &req->req);
  611. local_irq_restore(flags);
  612. if (pflags)
  613. spin_lock_irqsave(&ep->lock, *pflags);
  614. }
  615. /**
  616. * ep_end_out_req - Ends endpoint OUT request
  617. * @ep: physical endpoint
  618. * @req: pxa request
  619. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  620. *
  621. * Context: ep->lock held or released (see req_done())
  622. *
  623. * Ends endpoint OUT request (completes usb request).
  624. */
  625. static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  626. unsigned long *pflags)
  627. {
  628. inc_ep_stats_reqs(ep, !USB_DIR_IN);
  629. req_done(ep, req, 0, pflags);
  630. }
  631. /**
  632. * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
  633. * @ep: physical endpoint
  634. * @req: pxa request
  635. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  636. *
  637. * Context: ep->lock held or released (see req_done())
  638. *
  639. * Ends control endpoint OUT request (completes usb request), and puts
  640. * control endpoint into idle state
  641. */
  642. static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req,
  643. unsigned long *pflags)
  644. {
  645. set_ep0state(ep->dev, OUT_STATUS_STAGE);
  646. ep_end_out_req(ep, req, pflags);
  647. ep0_idle(ep->dev);
  648. }
  649. /**
  650. * ep_end_in_req - Ends endpoint IN request
  651. * @ep: physical endpoint
  652. * @req: pxa request
  653. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  654. *
  655. * Context: ep->lock held or released (see req_done())
  656. *
  657. * Ends endpoint IN request (completes usb request).
  658. */
  659. static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  660. unsigned long *pflags)
  661. {
  662. inc_ep_stats_reqs(ep, USB_DIR_IN);
  663. req_done(ep, req, 0, pflags);
  664. }
  665. /**
  666. * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
  667. * @ep: physical endpoint
  668. * @req: pxa request
  669. * @pflags: flags of previous spinlock_irq_save() or NULL if no lock held
  670. *
  671. * Context: ep->lock held or released (see req_done())
  672. *
  673. * Ends control endpoint IN request (completes usb request), and puts
  674. * control endpoint into status state
  675. */
  676. static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req,
  677. unsigned long *pflags)
  678. {
  679. set_ep0state(ep->dev, IN_STATUS_STAGE);
  680. ep_end_in_req(ep, req, pflags);
  681. }
  682. /**
  683. * nuke - Dequeue all requests
  684. * @ep: pxa endpoint
  685. * @status: usb request status
  686. *
  687. * Context: ep->lock released
  688. *
  689. * Dequeues all requests on an endpoint. As a side effect, interrupts will be
  690. * disabled on that endpoint (because no more requests).
  691. */
  692. static void nuke(struct pxa_ep *ep, int status)
  693. {
  694. struct pxa27x_request *req;
  695. unsigned long flags;
  696. spin_lock_irqsave(&ep->lock, flags);
  697. while (!list_empty(&ep->queue)) {
  698. req = list_entry(ep->queue.next, struct pxa27x_request, queue);
  699. req_done(ep, req, status, &flags);
  700. }
  701. spin_unlock_irqrestore(&ep->lock, flags);
  702. }
  703. /**
  704. * read_packet - transfer 1 packet from an OUT endpoint into request
  705. * @ep: pxa physical endpoint
  706. * @req: usb request
  707. *
  708. * Takes bytes from OUT endpoint and transfers them info the usb request.
  709. * If there is less space in request than bytes received in OUT endpoint,
  710. * bytes are left in the OUT endpoint.
  711. *
  712. * Returns how many bytes were actually transferred
  713. */
  714. static int read_packet(struct pxa_ep *ep, struct pxa27x_request *req)
  715. {
  716. u32 *buf;
  717. int bytes_ep, bufferspace, count, i;
  718. bytes_ep = ep_count_bytes_remain(ep);
  719. bufferspace = req->req.length - req->req.actual;
  720. buf = (u32 *)(req->req.buf + req->req.actual);
  721. prefetchw(buf);
  722. if (likely(!ep_is_empty(ep)))
  723. count = min(bytes_ep, bufferspace);
  724. else /* zlp */
  725. count = 0;
  726. for (i = count; i > 0; i -= 4)
  727. *buf++ = udc_ep_readl(ep, UDCDR);
  728. req->req.actual += count;
  729. ep_write_UDCCSR(ep, UDCCSR_PC);
  730. return count;
  731. }
  732. /**
  733. * write_packet - transfer 1 packet from request into an IN endpoint
  734. * @ep: pxa physical endpoint
  735. * @req: usb request
  736. * @max: max bytes that fit into endpoint
  737. *
  738. * Takes bytes from usb request, and transfers them into the physical
  739. * endpoint. If there are no bytes to transfer, doesn't write anything
  740. * to physical endpoint.
  741. *
  742. * Returns how many bytes were actually transferred.
  743. */
  744. static int write_packet(struct pxa_ep *ep, struct pxa27x_request *req,
  745. unsigned int max)
  746. {
  747. int length, count, remain, i;
  748. u32 *buf;
  749. u8 *buf_8;
  750. buf = (u32 *)(req->req.buf + req->req.actual);
  751. prefetch(buf);
  752. length = min(req->req.length - req->req.actual, max);
  753. req->req.actual += length;
  754. remain = length & 0x3;
  755. count = length & ~(0x3);
  756. for (i = count; i > 0 ; i -= 4)
  757. udc_ep_writel(ep, UDCDR, *buf++);
  758. buf_8 = (u8 *)buf;
  759. for (i = remain; i > 0; i--)
  760. udc_ep_writeb(ep, UDCDR, *buf_8++);
  761. ep_vdbg(ep, "length=%d+%d, udccsr=0x%03x\n", count, remain,
  762. udc_ep_readl(ep, UDCCSR));
  763. return length;
  764. }
  765. /**
  766. * read_fifo - Transfer packets from OUT endpoint into usb request
  767. * @ep: pxa physical endpoint
  768. * @req: usb request
  769. *
  770. * Context: callable when in_interrupt()
  771. *
  772. * Unload as many packets as possible from the fifo we use for usb OUT
  773. * transfers and put them into the request. Caller should have made sure
  774. * there's at least one packet ready.
  775. * Doesn't complete the request, that's the caller's job
  776. *
  777. * Returns 1 if the request completed, 0 otherwise
  778. */
  779. static int read_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  780. {
  781. int count, is_short, completed = 0;
  782. while (epout_has_pkt(ep)) {
  783. count = read_packet(ep, req);
  784. inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
  785. is_short = (count < ep->fifo_size);
  786. ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
  787. udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
  788. &req->req, req->req.actual, req->req.length);
  789. /* completion */
  790. if (is_short || req->req.actual == req->req.length) {
  791. completed = 1;
  792. break;
  793. }
  794. /* finished that packet. the next one may be waiting... */
  795. }
  796. return completed;
  797. }
  798. /**
  799. * write_fifo - transfer packets from usb request into an IN endpoint
  800. * @ep: pxa physical endpoint
  801. * @req: pxa usb request
  802. *
  803. * Write to an IN endpoint fifo, as many packets as possible.
  804. * irqs will use this to write the rest later.
  805. * caller guarantees at least one packet buffer is ready (or a zlp).
  806. * Doesn't complete the request, that's the caller's job
  807. *
  808. * Returns 1 if request fully transferred, 0 if partial transfer
  809. */
  810. static int write_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  811. {
  812. unsigned max;
  813. int count, is_short, is_last = 0, completed = 0, totcount = 0;
  814. u32 udccsr;
  815. max = ep->fifo_size;
  816. do {
  817. udccsr = udc_ep_readl(ep, UDCCSR);
  818. if (udccsr & UDCCSR_PC) {
  819. ep_vdbg(ep, "Clearing Transmit Complete, udccsr=%x\n",
  820. udccsr);
  821. ep_write_UDCCSR(ep, UDCCSR_PC);
  822. }
  823. if (udccsr & UDCCSR_TRN) {
  824. ep_vdbg(ep, "Clearing Underrun on, udccsr=%x\n",
  825. udccsr);
  826. ep_write_UDCCSR(ep, UDCCSR_TRN);
  827. }
  828. count = write_packet(ep, req, max);
  829. inc_ep_stats_bytes(ep, count, USB_DIR_IN);
  830. totcount += count;
  831. /* last packet is usually short (or a zlp) */
  832. if (unlikely(count < max)) {
  833. is_last = 1;
  834. is_short = 1;
  835. } else {
  836. if (likely(req->req.length > req->req.actual)
  837. || req->req.zero)
  838. is_last = 0;
  839. else
  840. is_last = 1;
  841. /* interrupt/iso maxpacket may not fill the fifo */
  842. is_short = unlikely(max < ep->fifo_size);
  843. }
  844. if (is_short)
  845. ep_write_UDCCSR(ep, UDCCSR_SP);
  846. /* requests complete when all IN data is in the FIFO */
  847. if (is_last) {
  848. completed = 1;
  849. break;
  850. }
  851. } while (!ep_is_full(ep));
  852. ep_dbg(ep, "wrote count:%d bytes%s%s, left:%d req=%p\n",
  853. totcount, is_last ? "/L" : "", is_short ? "/S" : "",
  854. req->req.length - req->req.actual, &req->req);
  855. return completed;
  856. }
  857. /**
  858. * read_ep0_fifo - Transfer packets from control endpoint into usb request
  859. * @ep: control endpoint
  860. * @req: pxa usb request
  861. *
  862. * Special ep0 version of the above read_fifo. Reads as many bytes from control
  863. * endpoint as can be read, and stores them into usb request (limited by request
  864. * maximum length).
  865. *
  866. * Returns 0 if usb request only partially filled, 1 if fully filled
  867. */
  868. static int read_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  869. {
  870. int count, is_short, completed = 0;
  871. while (epout_has_pkt(ep)) {
  872. count = read_packet(ep, req);
  873. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  874. inc_ep_stats_bytes(ep, count, !USB_DIR_IN);
  875. is_short = (count < ep->fifo_size);
  876. ep_dbg(ep, "read udccsr:%03x, count:%d bytes%s req %p %d/%d\n",
  877. udc_ep_readl(ep, UDCCSR), count, is_short ? "/S" : "",
  878. &req->req, req->req.actual, req->req.length);
  879. if (is_short || req->req.actual >= req->req.length) {
  880. completed = 1;
  881. break;
  882. }
  883. }
  884. return completed;
  885. }
  886. /**
  887. * write_ep0_fifo - Send a request to control endpoint (ep0 in)
  888. * @ep: control endpoint
  889. * @req: request
  890. *
  891. * Context: callable when in_interrupt()
  892. *
  893. * Sends a request (or a part of the request) to the control endpoint (ep0 in).
  894. * If the request doesn't fit, the remaining part will be sent from irq.
  895. * The request is considered fully written only if either :
  896. * - last write transferred all remaining bytes, but fifo was not fully filled
  897. * - last write was a 0 length write
  898. *
  899. * Returns 1 if request fully written, 0 if request only partially sent
  900. */
  901. static int write_ep0_fifo(struct pxa_ep *ep, struct pxa27x_request *req)
  902. {
  903. unsigned count;
  904. int is_last, is_short;
  905. count = write_packet(ep, req, EP0_FIFO_SIZE);
  906. inc_ep_stats_bytes(ep, count, USB_DIR_IN);
  907. is_short = (count < EP0_FIFO_SIZE);
  908. is_last = ((count == 0) || (count < EP0_FIFO_SIZE));
  909. /* Sends either a short packet or a 0 length packet */
  910. if (unlikely(is_short))
  911. ep_write_UDCCSR(ep, UDCCSR0_IPR);
  912. ep_dbg(ep, "in %d bytes%s%s, %d left, req=%p, udccsr0=0x%03x\n",
  913. count, is_short ? "/S" : "", is_last ? "/L" : "",
  914. req->req.length - req->req.actual,
  915. &req->req, udc_ep_readl(ep, UDCCSR));
  916. return is_last;
  917. }
  918. /**
  919. * pxa_ep_queue - Queue a request into an IN endpoint
  920. * @_ep: usb endpoint
  921. * @_req: usb request
  922. * @gfp_flags: flags
  923. *
  924. * Context: normally called when !in_interrupt, but callable when in_interrupt()
  925. * in the special case of ep0 setup :
  926. * (irq->handle_ep0_ctrl_req->gadget_setup->pxa_ep_queue)
  927. *
  928. * Returns 0 if succedeed, error otherwise
  929. */
  930. static int pxa_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
  931. gfp_t gfp_flags)
  932. {
  933. struct udc_usb_ep *udc_usb_ep;
  934. struct pxa_ep *ep;
  935. struct pxa27x_request *req;
  936. struct pxa_udc *dev;
  937. unsigned long flags;
  938. int rc = 0;
  939. int is_first_req;
  940. unsigned length;
  941. int recursion_detected;
  942. req = container_of(_req, struct pxa27x_request, req);
  943. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  944. if (unlikely(!_req || !_req->complete || !_req->buf))
  945. return -EINVAL;
  946. if (unlikely(!_ep))
  947. return -EINVAL;
  948. ep = udc_usb_ep->pxa_ep;
  949. if (unlikely(!ep))
  950. return -EINVAL;
  951. dev = ep->dev;
  952. if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  953. ep_dbg(ep, "bogus device state\n");
  954. return -ESHUTDOWN;
  955. }
  956. /* iso is always one packet per request, that's the only way
  957. * we can report per-packet status. that also helps with dma.
  958. */
  959. if (unlikely(EPXFERTYPE_is_ISO(ep)
  960. && req->req.length > ep->fifo_size))
  961. return -EMSGSIZE;
  962. spin_lock_irqsave(&ep->lock, flags);
  963. recursion_detected = ep->in_handle_ep;
  964. is_first_req = list_empty(&ep->queue);
  965. ep_dbg(ep, "queue req %p(first=%s), len %d buf %p\n",
  966. _req, is_first_req ? "yes" : "no",
  967. _req->length, _req->buf);
  968. if (!ep->enabled) {
  969. _req->status = -ESHUTDOWN;
  970. rc = -ESHUTDOWN;
  971. goto out_locked;
  972. }
  973. if (req->in_use) {
  974. ep_err(ep, "refusing to queue req %p (already queued)\n", req);
  975. goto out_locked;
  976. }
  977. length = _req->length;
  978. _req->status = -EINPROGRESS;
  979. _req->actual = 0;
  980. ep_add_request(ep, req);
  981. spin_unlock_irqrestore(&ep->lock, flags);
  982. if (is_ep0(ep)) {
  983. switch (dev->ep0state) {
  984. case WAIT_ACK_SET_CONF_INTERF:
  985. if (length == 0) {
  986. ep_end_in_req(ep, req, NULL);
  987. } else {
  988. ep_err(ep, "got a request of %d bytes while"
  989. "in state WAIT_ACK_SET_CONF_INTERF\n",
  990. length);
  991. ep_del_request(ep, req);
  992. rc = -EL2HLT;
  993. }
  994. ep0_idle(ep->dev);
  995. break;
  996. case IN_DATA_STAGE:
  997. if (!ep_is_full(ep))
  998. if (write_ep0_fifo(ep, req))
  999. ep0_end_in_req(ep, req, NULL);
  1000. break;
  1001. case OUT_DATA_STAGE:
  1002. if ((length == 0) || !epout_has_pkt(ep))
  1003. if (read_ep0_fifo(ep, req))
  1004. ep0_end_out_req(ep, req, NULL);
  1005. break;
  1006. default:
  1007. ep_err(ep, "odd state %s to send me a request\n",
  1008. EP0_STNAME(ep->dev));
  1009. ep_del_request(ep, req);
  1010. rc = -EL2HLT;
  1011. break;
  1012. }
  1013. } else {
  1014. if (!recursion_detected)
  1015. handle_ep(ep);
  1016. }
  1017. out:
  1018. return rc;
  1019. out_locked:
  1020. spin_unlock_irqrestore(&ep->lock, flags);
  1021. goto out;
  1022. }
  1023. /**
  1024. * pxa_ep_dequeue - Dequeue one request
  1025. * @_ep: usb endpoint
  1026. * @_req: usb request
  1027. *
  1028. * Return 0 if no error, -EINVAL or -ECONNRESET otherwise
  1029. */
  1030. static int pxa_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1031. {
  1032. struct pxa_ep *ep;
  1033. struct udc_usb_ep *udc_usb_ep;
  1034. struct pxa27x_request *req;
  1035. unsigned long flags;
  1036. int rc = -EINVAL;
  1037. if (!_ep)
  1038. return rc;
  1039. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1040. ep = udc_usb_ep->pxa_ep;
  1041. if (!ep || is_ep0(ep))
  1042. return rc;
  1043. spin_lock_irqsave(&ep->lock, flags);
  1044. /* make sure it's actually queued on this endpoint */
  1045. list_for_each_entry(req, &ep->queue, queue) {
  1046. if (&req->req == _req) {
  1047. rc = 0;
  1048. break;
  1049. }
  1050. }
  1051. spin_unlock_irqrestore(&ep->lock, flags);
  1052. if (!rc)
  1053. req_done(ep, req, -ECONNRESET, NULL);
  1054. return rc;
  1055. }
  1056. /**
  1057. * pxa_ep_set_halt - Halts operations on one endpoint
  1058. * @_ep: usb endpoint
  1059. * @value:
  1060. *
  1061. * Returns 0 if no error, -EINVAL, -EROFS, -EAGAIN otherwise
  1062. */
  1063. static int pxa_ep_set_halt(struct usb_ep *_ep, int value)
  1064. {
  1065. struct pxa_ep *ep;
  1066. struct udc_usb_ep *udc_usb_ep;
  1067. unsigned long flags;
  1068. int rc;
  1069. if (!_ep)
  1070. return -EINVAL;
  1071. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1072. ep = udc_usb_ep->pxa_ep;
  1073. if (!ep || is_ep0(ep))
  1074. return -EINVAL;
  1075. if (value == 0) {
  1076. /*
  1077. * This path (reset toggle+halt) is needed to implement
  1078. * SET_INTERFACE on normal hardware. but it can't be
  1079. * done from software on the PXA UDC, and the hardware
  1080. * forgets to do it as part of SET_INTERFACE automagic.
  1081. */
  1082. ep_dbg(ep, "only host can clear halt\n");
  1083. return -EROFS;
  1084. }
  1085. spin_lock_irqsave(&ep->lock, flags);
  1086. rc = -EAGAIN;
  1087. if (ep->dir_in && (ep_is_full(ep) || !list_empty(&ep->queue)))
  1088. goto out;
  1089. /* FST, FEF bits are the same for control and non control endpoints */
  1090. rc = 0;
  1091. ep_write_UDCCSR(ep, UDCCSR_FST | UDCCSR_FEF);
  1092. if (is_ep0(ep))
  1093. set_ep0state(ep->dev, STALL);
  1094. out:
  1095. spin_unlock_irqrestore(&ep->lock, flags);
  1096. return rc;
  1097. }
  1098. /**
  1099. * pxa_ep_fifo_status - Get how many bytes in physical endpoint
  1100. * @_ep: usb endpoint
  1101. *
  1102. * Returns number of bytes in OUT fifos. Broken for IN fifos.
  1103. */
  1104. static int pxa_ep_fifo_status(struct usb_ep *_ep)
  1105. {
  1106. struct pxa_ep *ep;
  1107. struct udc_usb_ep *udc_usb_ep;
  1108. if (!_ep)
  1109. return -ENODEV;
  1110. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1111. ep = udc_usb_ep->pxa_ep;
  1112. if (!ep || is_ep0(ep))
  1113. return -ENODEV;
  1114. if (ep->dir_in)
  1115. return -EOPNOTSUPP;
  1116. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN || ep_is_empty(ep))
  1117. return 0;
  1118. else
  1119. return ep_count_bytes_remain(ep) + 1;
  1120. }
  1121. /**
  1122. * pxa_ep_fifo_flush - Flushes one endpoint
  1123. * @_ep: usb endpoint
  1124. *
  1125. * Discards all data in one endpoint(IN or OUT), except control endpoint.
  1126. */
  1127. static void pxa_ep_fifo_flush(struct usb_ep *_ep)
  1128. {
  1129. struct pxa_ep *ep;
  1130. struct udc_usb_ep *udc_usb_ep;
  1131. unsigned long flags;
  1132. if (!_ep)
  1133. return;
  1134. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1135. ep = udc_usb_ep->pxa_ep;
  1136. if (!ep || is_ep0(ep))
  1137. return;
  1138. spin_lock_irqsave(&ep->lock, flags);
  1139. if (unlikely(!list_empty(&ep->queue)))
  1140. ep_dbg(ep, "called while queue list not empty\n");
  1141. ep_dbg(ep, "called\n");
  1142. /* for OUT, just read and discard the FIFO contents. */
  1143. if (!ep->dir_in) {
  1144. while (!ep_is_empty(ep))
  1145. udc_ep_readl(ep, UDCDR);
  1146. } else {
  1147. /* most IN status is the same, but ISO can't stall */
  1148. ep_write_UDCCSR(ep,
  1149. UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN
  1150. | (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST));
  1151. }
  1152. spin_unlock_irqrestore(&ep->lock, flags);
  1153. }
  1154. /**
  1155. * pxa_ep_enable - Enables usb endpoint
  1156. * @_ep: usb endpoint
  1157. * @desc: usb endpoint descriptor
  1158. *
  1159. * Nothing much to do here, as ep configuration is done once and for all
  1160. * before udc is enabled. After udc enable, no physical endpoint configuration
  1161. * can be changed.
  1162. * Function makes sanity checks and flushes the endpoint.
  1163. */
  1164. static int pxa_ep_enable(struct usb_ep *_ep,
  1165. const struct usb_endpoint_descriptor *desc)
  1166. {
  1167. struct pxa_ep *ep;
  1168. struct udc_usb_ep *udc_usb_ep;
  1169. struct pxa_udc *udc;
  1170. if (!_ep || !desc)
  1171. return -EINVAL;
  1172. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1173. if (udc_usb_ep->pxa_ep) {
  1174. ep = udc_usb_ep->pxa_ep;
  1175. ep_warn(ep, "usb_ep %s already enabled, doing nothing\n",
  1176. _ep->name);
  1177. } else {
  1178. ep = find_pxa_ep(udc_usb_ep->dev, udc_usb_ep);
  1179. }
  1180. if (!ep || is_ep0(ep)) {
  1181. dev_err(udc_usb_ep->dev->dev,
  1182. "unable to match pxa_ep for ep %s\n",
  1183. _ep->name);
  1184. return -EINVAL;
  1185. }
  1186. if ((desc->bDescriptorType != USB_DT_ENDPOINT)
  1187. || (ep->type != usb_endpoint_type(desc))) {
  1188. ep_err(ep, "type mismatch\n");
  1189. return -EINVAL;
  1190. }
  1191. if (ep->fifo_size < usb_endpoint_maxp(desc)) {
  1192. ep_err(ep, "bad maxpacket\n");
  1193. return -ERANGE;
  1194. }
  1195. udc_usb_ep->pxa_ep = ep;
  1196. udc = ep->dev;
  1197. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  1198. ep_err(ep, "bogus device state\n");
  1199. return -ESHUTDOWN;
  1200. }
  1201. ep->enabled = 1;
  1202. /* flush fifo (mostly for OUT buffers) */
  1203. pxa_ep_fifo_flush(_ep);
  1204. ep_dbg(ep, "enabled\n");
  1205. return 0;
  1206. }
  1207. /**
  1208. * pxa_ep_disable - Disable usb endpoint
  1209. * @_ep: usb endpoint
  1210. *
  1211. * Same as for pxa_ep_enable, no physical endpoint configuration can be
  1212. * changed.
  1213. * Function flushes the endpoint and related requests.
  1214. */
  1215. static int pxa_ep_disable(struct usb_ep *_ep)
  1216. {
  1217. struct pxa_ep *ep;
  1218. struct udc_usb_ep *udc_usb_ep;
  1219. if (!_ep)
  1220. return -EINVAL;
  1221. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1222. ep = udc_usb_ep->pxa_ep;
  1223. if (!ep || is_ep0(ep) || !list_empty(&ep->queue))
  1224. return -EINVAL;
  1225. ep->enabled = 0;
  1226. nuke(ep, -ESHUTDOWN);
  1227. pxa_ep_fifo_flush(_ep);
  1228. udc_usb_ep->pxa_ep = NULL;
  1229. ep_dbg(ep, "disabled\n");
  1230. return 0;
  1231. }
  1232. static const struct usb_ep_ops pxa_ep_ops = {
  1233. .enable = pxa_ep_enable,
  1234. .disable = pxa_ep_disable,
  1235. .alloc_request = pxa_ep_alloc_request,
  1236. .free_request = pxa_ep_free_request,
  1237. .queue = pxa_ep_queue,
  1238. .dequeue = pxa_ep_dequeue,
  1239. .set_halt = pxa_ep_set_halt,
  1240. .fifo_status = pxa_ep_fifo_status,
  1241. .fifo_flush = pxa_ep_fifo_flush,
  1242. };
  1243. /**
  1244. * dplus_pullup - Connect or disconnect pullup resistor to D+ pin
  1245. * @udc: udc device
  1246. * @on: 0 if disconnect pullup resistor, 1 otherwise
  1247. * Context: any
  1248. *
  1249. * Handle D+ pullup resistor, make the device visible to the usb bus, and
  1250. * declare it as a full speed usb device
  1251. */
  1252. static void dplus_pullup(struct pxa_udc *udc, int on)
  1253. {
  1254. if (udc->gpiod) {
  1255. gpiod_set_value(udc->gpiod, on);
  1256. } else if (udc->udc_command) {
  1257. if (on)
  1258. udc->udc_command(PXA2XX_UDC_CMD_CONNECT);
  1259. else
  1260. udc->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  1261. }
  1262. udc->pullup_on = on;
  1263. }
  1264. /**
  1265. * pxa_udc_get_frame - Returns usb frame number
  1266. * @_gadget: usb gadget
  1267. */
  1268. static int pxa_udc_get_frame(struct usb_gadget *_gadget)
  1269. {
  1270. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1271. return (udc_readl(udc, UDCFNR) & 0x7ff);
  1272. }
  1273. /**
  1274. * pxa_udc_wakeup - Force udc device out of suspend
  1275. * @_gadget: usb gadget
  1276. *
  1277. * Returns 0 if successful, error code otherwise
  1278. */
  1279. static int pxa_udc_wakeup(struct usb_gadget *_gadget)
  1280. {
  1281. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1282. /* host may not have enabled remote wakeup */
  1283. if ((udc_readl(udc, UDCCR) & UDCCR_DWRE) == 0)
  1284. return -EHOSTUNREACH;
  1285. udc_set_mask_UDCCR(udc, UDCCR_UDR);
  1286. return 0;
  1287. }
  1288. static void udc_enable(struct pxa_udc *udc);
  1289. static void udc_disable(struct pxa_udc *udc);
  1290. /**
  1291. * should_enable_udc - Tells if UDC should be enabled
  1292. * @udc: udc device
  1293. * Context: any
  1294. *
  1295. * The UDC should be enabled if :
  1296. * - the pullup resistor is connected
  1297. * - and a gadget driver is bound
  1298. * - and vbus is sensed (or no vbus sense is available)
  1299. *
  1300. * Returns 1 if UDC should be enabled, 0 otherwise
  1301. */
  1302. static int should_enable_udc(struct pxa_udc *udc)
  1303. {
  1304. int put_on;
  1305. put_on = ((udc->pullup_on) && (udc->driver));
  1306. put_on &= ((udc->vbus_sensed) || (IS_ERR_OR_NULL(udc->transceiver)));
  1307. return put_on;
  1308. }
  1309. /**
  1310. * should_disable_udc - Tells if UDC should be disabled
  1311. * @udc: udc device
  1312. * Context: any
  1313. *
  1314. * The UDC should be disabled if :
  1315. * - the pullup resistor is not connected
  1316. * - or no gadget driver is bound
  1317. * - or no vbus is sensed (when vbus sesing is available)
  1318. *
  1319. * Returns 1 if UDC should be disabled
  1320. */
  1321. static int should_disable_udc(struct pxa_udc *udc)
  1322. {
  1323. int put_off;
  1324. put_off = ((!udc->pullup_on) || (!udc->driver));
  1325. put_off |= ((!udc->vbus_sensed) && (!IS_ERR_OR_NULL(udc->transceiver)));
  1326. return put_off;
  1327. }
  1328. /**
  1329. * pxa_udc_pullup - Offer manual D+ pullup control
  1330. * @_gadget: usb gadget using the control
  1331. * @is_active: 0 if disconnect, else connect D+ pullup resistor
  1332. * Context: !in_interrupt()
  1333. *
  1334. * Returns 0 if OK, -EOPNOTSUPP if udc driver doesn't handle D+ pullup
  1335. */
  1336. static int pxa_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1337. {
  1338. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1339. if (!udc->gpiod && !udc->udc_command)
  1340. return -EOPNOTSUPP;
  1341. dplus_pullup(udc, is_active);
  1342. if (should_enable_udc(udc))
  1343. udc_enable(udc);
  1344. if (should_disable_udc(udc))
  1345. udc_disable(udc);
  1346. return 0;
  1347. }
  1348. /**
  1349. * pxa_udc_vbus_session - Called by external transceiver to enable/disable udc
  1350. * @_gadget: usb gadget
  1351. * @is_active: 0 if should disable the udc, 1 if should enable
  1352. *
  1353. * Enables the udc, and optionnaly activates D+ pullup resistor. Or disables the
  1354. * udc, and deactivates D+ pullup resistor.
  1355. *
  1356. * Returns 0
  1357. */
  1358. static int pxa_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  1359. {
  1360. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1361. udc->vbus_sensed = is_active;
  1362. if (should_enable_udc(udc))
  1363. udc_enable(udc);
  1364. if (should_disable_udc(udc))
  1365. udc_disable(udc);
  1366. return 0;
  1367. }
  1368. /**
  1369. * pxa_udc_vbus_draw - Called by gadget driver after SET_CONFIGURATION completed
  1370. * @_gadget: usb gadget
  1371. * @mA: current drawn
  1372. *
  1373. * Context: !in_interrupt()
  1374. *
  1375. * Called after a configuration was chosen by a USB host, to inform how much
  1376. * current can be drawn by the device from VBus line.
  1377. *
  1378. * Returns 0 or -EOPNOTSUPP if no transceiver is handling the udc
  1379. */
  1380. static int pxa_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1381. {
  1382. struct pxa_udc *udc;
  1383. udc = to_gadget_udc(_gadget);
  1384. if (!IS_ERR_OR_NULL(udc->transceiver))
  1385. return usb_phy_set_power(udc->transceiver, mA);
  1386. return -EOPNOTSUPP;
  1387. }
  1388. /**
  1389. * pxa_udc_phy_event - Called by phy upon VBus event
  1390. * @nb: notifier block
  1391. * @action: phy action, is vbus connect or disconnect
  1392. * @data: the usb_gadget structure in pxa_udc
  1393. *
  1394. * Called by the USB Phy when a cable connect or disconnect is sensed.
  1395. *
  1396. * Returns 0
  1397. */
  1398. static int pxa_udc_phy_event(struct notifier_block *nb, unsigned long action,
  1399. void *data)
  1400. {
  1401. struct usb_gadget *gadget = data;
  1402. switch (action) {
  1403. case USB_EVENT_VBUS:
  1404. usb_gadget_vbus_connect(gadget);
  1405. return NOTIFY_OK;
  1406. case USB_EVENT_NONE:
  1407. usb_gadget_vbus_disconnect(gadget);
  1408. return NOTIFY_OK;
  1409. default:
  1410. return NOTIFY_DONE;
  1411. }
  1412. }
  1413. static struct notifier_block pxa27x_udc_phy = {
  1414. .notifier_call = pxa_udc_phy_event,
  1415. };
  1416. static int pxa27x_udc_start(struct usb_gadget *g,
  1417. struct usb_gadget_driver *driver);
  1418. static int pxa27x_udc_stop(struct usb_gadget *g);
  1419. static const struct usb_gadget_ops pxa_udc_ops = {
  1420. .get_frame = pxa_udc_get_frame,
  1421. .wakeup = pxa_udc_wakeup,
  1422. .pullup = pxa_udc_pullup,
  1423. .vbus_session = pxa_udc_vbus_session,
  1424. .vbus_draw = pxa_udc_vbus_draw,
  1425. .udc_start = pxa27x_udc_start,
  1426. .udc_stop = pxa27x_udc_stop,
  1427. };
  1428. /**
  1429. * udc_disable - disable udc device controller
  1430. * @udc: udc device
  1431. * Context: any
  1432. *
  1433. * Disables the udc device : disables clocks, udc interrupts, control endpoint
  1434. * interrupts.
  1435. */
  1436. static void udc_disable(struct pxa_udc *udc)
  1437. {
  1438. if (!udc->enabled)
  1439. return;
  1440. udc_writel(udc, UDCICR0, 0);
  1441. udc_writel(udc, UDCICR1, 0);
  1442. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1443. ep0_idle(udc);
  1444. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1445. clk_disable(udc->clk);
  1446. udc->enabled = 0;
  1447. }
  1448. /**
  1449. * udc_init_data - Initialize udc device data structures
  1450. * @dev: udc device
  1451. *
  1452. * Initializes gadget endpoint list, endpoints locks. No action is taken
  1453. * on the hardware.
  1454. */
  1455. static void udc_init_data(struct pxa_udc *dev)
  1456. {
  1457. int i;
  1458. struct pxa_ep *ep;
  1459. /* device/ep0 records init */
  1460. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1461. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1462. dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0];
  1463. dev->gadget.quirk_altset_not_supp = 1;
  1464. ep0_idle(dev);
  1465. /* PXA endpoints init */
  1466. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  1467. ep = &dev->pxa_ep[i];
  1468. ep->enabled = is_ep0(ep);
  1469. INIT_LIST_HEAD(&ep->queue);
  1470. spin_lock_init(&ep->lock);
  1471. }
  1472. /* USB endpoints init */
  1473. for (i = 1; i < NR_USB_ENDPOINTS; i++) {
  1474. list_add_tail(&dev->udc_usb_ep[i].usb_ep.ep_list,
  1475. &dev->gadget.ep_list);
  1476. usb_ep_set_maxpacket_limit(&dev->udc_usb_ep[i].usb_ep,
  1477. dev->udc_usb_ep[i].usb_ep.maxpacket);
  1478. }
  1479. }
  1480. /**
  1481. * udc_enable - Enables the udc device
  1482. * @dev: udc device
  1483. *
  1484. * Enables the udc device : enables clocks, udc interrupts, control endpoint
  1485. * interrupts, sets usb as UDC client and setups endpoints.
  1486. */
  1487. static void udc_enable(struct pxa_udc *udc)
  1488. {
  1489. if (udc->enabled)
  1490. return;
  1491. clk_enable(udc->clk);
  1492. udc_writel(udc, UDCICR0, 0);
  1493. udc_writel(udc, UDCICR1, 0);
  1494. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1495. ep0_idle(udc);
  1496. udc->gadget.speed = USB_SPEED_FULL;
  1497. memset(&udc->stats, 0, sizeof(udc->stats));
  1498. pxa_eps_setup(udc);
  1499. udc_set_mask_UDCCR(udc, UDCCR_UDE);
  1500. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_ACM);
  1501. udelay(2);
  1502. if (udc_readl(udc, UDCCR) & UDCCR_EMCE)
  1503. dev_err(udc->dev, "Configuration errors, udc disabled\n");
  1504. /*
  1505. * Caller must be able to sleep in order to cope with startup transients
  1506. */
  1507. msleep(100);
  1508. /* enable suspend/resume and reset irqs */
  1509. udc_writel(udc, UDCICR1,
  1510. UDCICR1_IECC | UDCICR1_IERU
  1511. | UDCICR1_IESU | UDCICR1_IERS);
  1512. /* enable ep0 irqs */
  1513. pio_irq_enable(&udc->pxa_ep[0]);
  1514. udc->enabled = 1;
  1515. }
  1516. /**
  1517. * pxa27x_start - Register gadget driver
  1518. * @driver: gadget driver
  1519. * @bind: bind function
  1520. *
  1521. * When a driver is successfully registered, it will receive control requests
  1522. * including set_configuration(), which enables non-control requests. Then
  1523. * usb traffic follows until a disconnect is reported. Then a host may connect
  1524. * again, or the driver might get unbound.
  1525. *
  1526. * Note that the udc is not automatically enabled. Check function
  1527. * should_enable_udc().
  1528. *
  1529. * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise
  1530. */
  1531. static int pxa27x_udc_start(struct usb_gadget *g,
  1532. struct usb_gadget_driver *driver)
  1533. {
  1534. struct pxa_udc *udc = to_pxa(g);
  1535. int retval;
  1536. /* first hook up the driver ... */
  1537. udc->driver = driver;
  1538. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  1539. retval = otg_set_peripheral(udc->transceiver->otg,
  1540. &udc->gadget);
  1541. if (retval) {
  1542. dev_err(udc->dev, "can't bind to transceiver\n");
  1543. goto fail;
  1544. }
  1545. }
  1546. if (should_enable_udc(udc))
  1547. udc_enable(udc);
  1548. return 0;
  1549. fail:
  1550. udc->driver = NULL;
  1551. return retval;
  1552. }
  1553. /**
  1554. * stop_activity - Stops udc endpoints
  1555. * @udc: udc device
  1556. * @driver: gadget driver
  1557. *
  1558. * Disables all udc endpoints (even control endpoint), report disconnect to
  1559. * the gadget user.
  1560. */
  1561. static void stop_activity(struct pxa_udc *udc)
  1562. {
  1563. int i;
  1564. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1565. for (i = 0; i < NR_USB_ENDPOINTS; i++)
  1566. pxa_ep_disable(&udc->udc_usb_ep[i].usb_ep);
  1567. }
  1568. /**
  1569. * pxa27x_udc_stop - Unregister the gadget driver
  1570. * @driver: gadget driver
  1571. *
  1572. * Returns 0 if no error, -ENODEV, -EINVAL otherwise
  1573. */
  1574. static int pxa27x_udc_stop(struct usb_gadget *g)
  1575. {
  1576. struct pxa_udc *udc = to_pxa(g);
  1577. stop_activity(udc);
  1578. udc_disable(udc);
  1579. udc->driver = NULL;
  1580. if (!IS_ERR_OR_NULL(udc->transceiver))
  1581. return otg_set_peripheral(udc->transceiver->otg, NULL);
  1582. return 0;
  1583. }
  1584. /**
  1585. * handle_ep0_ctrl_req - handle control endpoint control request
  1586. * @udc: udc device
  1587. * @req: control request
  1588. */
  1589. static void handle_ep0_ctrl_req(struct pxa_udc *udc,
  1590. struct pxa27x_request *req)
  1591. {
  1592. struct pxa_ep *ep = &udc->pxa_ep[0];
  1593. union {
  1594. struct usb_ctrlrequest r;
  1595. u32 word[2];
  1596. } u;
  1597. int i;
  1598. int have_extrabytes = 0;
  1599. unsigned long flags;
  1600. nuke(ep, -EPROTO);
  1601. spin_lock_irqsave(&ep->lock, flags);
  1602. /*
  1603. * In the PXA320 manual, in the section about Back-to-Back setup
  1604. * packets, it describes this situation. The solution is to set OPC to
  1605. * get rid of the status packet, and then continue with the setup
  1606. * packet. Generalize to pxa27x CPUs.
  1607. */
  1608. if (epout_has_pkt(ep) && (ep_count_bytes_remain(ep) == 0))
  1609. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  1610. /* read SETUP packet */
  1611. for (i = 0; i < 2; i++) {
  1612. if (unlikely(ep_is_empty(ep)))
  1613. goto stall;
  1614. u.word[i] = udc_ep_readl(ep, UDCDR);
  1615. }
  1616. have_extrabytes = !ep_is_empty(ep);
  1617. while (!ep_is_empty(ep)) {
  1618. i = udc_ep_readl(ep, UDCDR);
  1619. ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i);
  1620. }
  1621. ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1622. u.r.bRequestType, u.r.bRequest,
  1623. le16_to_cpu(u.r.wValue), le16_to_cpu(u.r.wIndex),
  1624. le16_to_cpu(u.r.wLength));
  1625. if (unlikely(have_extrabytes))
  1626. goto stall;
  1627. if (u.r.bRequestType & USB_DIR_IN)
  1628. set_ep0state(udc, IN_DATA_STAGE);
  1629. else
  1630. set_ep0state(udc, OUT_DATA_STAGE);
  1631. /* Tell UDC to enter Data Stage */
  1632. ep_write_UDCCSR(ep, UDCCSR0_SA | UDCCSR0_OPC);
  1633. spin_unlock_irqrestore(&ep->lock, flags);
  1634. i = udc->driver->setup(&udc->gadget, &u.r);
  1635. spin_lock_irqsave(&ep->lock, flags);
  1636. if (i < 0)
  1637. goto stall;
  1638. out:
  1639. spin_unlock_irqrestore(&ep->lock, flags);
  1640. return;
  1641. stall:
  1642. ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
  1643. udc_ep_readl(ep, UDCCSR), i);
  1644. ep_write_UDCCSR(ep, UDCCSR0_FST | UDCCSR0_FTF);
  1645. set_ep0state(udc, STALL);
  1646. goto out;
  1647. }
  1648. /**
  1649. * handle_ep0 - Handle control endpoint data transfers
  1650. * @udc: udc device
  1651. * @fifo_irq: 1 if triggered by fifo service type irq
  1652. * @opc_irq: 1 if triggered by output packet complete type irq
  1653. *
  1654. * Context : when in_interrupt() or with ep->lock held
  1655. *
  1656. * Tries to transfer all pending request data into the endpoint and/or
  1657. * transfer all pending data in the endpoint into usb requests.
  1658. * Handles states of ep0 automata.
  1659. *
  1660. * PXA27x hardware handles several standard usb control requests without
  1661. * driver notification. The requests fully handled by hardware are :
  1662. * SET_ADDRESS, SET_FEATURE, CLEAR_FEATURE, GET_CONFIGURATION, GET_INTERFACE,
  1663. * GET_STATUS
  1664. * The requests handled by hardware, but with irq notification are :
  1665. * SYNCH_FRAME, SET_CONFIGURATION, SET_INTERFACE
  1666. * The remaining standard requests really handled by handle_ep0 are :
  1667. * GET_DESCRIPTOR, SET_DESCRIPTOR, specific requests.
  1668. * Requests standardized outside of USB 2.0 chapter 9 are handled more
  1669. * uniformly, by gadget drivers.
  1670. *
  1671. * The control endpoint state machine is _not_ USB spec compliant, it's even
  1672. * hardly compliant with Intel PXA270 developers guide.
  1673. * The key points which inferred this state machine are :
  1674. * - on every setup token, bit UDCCSR0_SA is raised and held until cleared by
  1675. * software.
  1676. * - on every OUT packet received, UDCCSR0_OPC is raised and held until
  1677. * cleared by software.
  1678. * - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it
  1679. * before reading ep0.
  1680. * This is true only for PXA27x. This is not true anymore for PXA3xx family
  1681. * (check Back-to-Back setup packet in developers guide).
  1682. * - irq can be called on a "packet complete" event (opc_irq=1), while
  1683. * UDCCSR0_OPC is not yet raised (delta can be as big as 100ms
  1684. * from experimentation).
  1685. * - as UDCCSR0_SA can be activated while in irq handling, and clearing
  1686. * UDCCSR0_OPC would flush the setup data, we almost never clear UDCCSR0_OPC
  1687. * => we never actually read the "status stage" packet of an IN data stage
  1688. * => this is not documented in Intel documentation
  1689. * - hardware as no idea of STATUS STAGE, it only handle SETUP STAGE and DATA
  1690. * STAGE. The driver add STATUS STAGE to send last zero length packet in
  1691. * OUT_STATUS_STAGE.
  1692. * - special attention was needed for IN_STATUS_STAGE. If a packet complete
  1693. * event is detected, we terminate the status stage without ackowledging the
  1694. * packet (not to risk to loose a potential SETUP packet)
  1695. */
  1696. static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
  1697. {
  1698. u32 udccsr0;
  1699. struct pxa_ep *ep = &udc->pxa_ep[0];
  1700. struct pxa27x_request *req = NULL;
  1701. int completed = 0;
  1702. if (!list_empty(&ep->queue))
  1703. req = list_entry(ep->queue.next, struct pxa27x_request, queue);
  1704. udccsr0 = udc_ep_readl(ep, UDCCSR);
  1705. ep_dbg(ep, "state=%s, req=%p, udccsr0=0x%03x, udcbcr=%d, irq_msk=%x\n",
  1706. EP0_STNAME(udc), req, udccsr0, udc_ep_readl(ep, UDCBCR),
  1707. (fifo_irq << 1 | opc_irq));
  1708. if (udccsr0 & UDCCSR0_SST) {
  1709. ep_dbg(ep, "clearing stall status\n");
  1710. nuke(ep, -EPIPE);
  1711. ep_write_UDCCSR(ep, UDCCSR0_SST);
  1712. ep0_idle(udc);
  1713. }
  1714. if (udccsr0 & UDCCSR0_SA) {
  1715. nuke(ep, 0);
  1716. set_ep0state(udc, SETUP_STAGE);
  1717. }
  1718. switch (udc->ep0state) {
  1719. case WAIT_FOR_SETUP:
  1720. /*
  1721. * Hardware bug : beware, we cannot clear OPC, since we would
  1722. * miss a potential OPC irq for a setup packet.
  1723. * So, we only do ... nothing, and hope for a next irq with
  1724. * UDCCSR0_SA set.
  1725. */
  1726. break;
  1727. case SETUP_STAGE:
  1728. udccsr0 &= UDCCSR0_CTRL_REQ_MASK;
  1729. if (likely(udccsr0 == UDCCSR0_CTRL_REQ_MASK))
  1730. handle_ep0_ctrl_req(udc, req);
  1731. break;
  1732. case IN_DATA_STAGE: /* GET_DESCRIPTOR */
  1733. if (epout_has_pkt(ep))
  1734. ep_write_UDCCSR(ep, UDCCSR0_OPC);
  1735. if (req && !ep_is_full(ep))
  1736. completed = write_ep0_fifo(ep, req);
  1737. if (completed)
  1738. ep0_end_in_req(ep, req, NULL);
  1739. break;
  1740. case OUT_DATA_STAGE: /* SET_DESCRIPTOR */
  1741. if (epout_has_pkt(ep) && req)
  1742. completed = read_ep0_fifo(ep, req);
  1743. if (completed)
  1744. ep0_end_out_req(ep, req, NULL);
  1745. break;
  1746. case STALL:
  1747. ep_write_UDCCSR(ep, UDCCSR0_FST);
  1748. break;
  1749. case IN_STATUS_STAGE:
  1750. /*
  1751. * Hardware bug : beware, we cannot clear OPC, since we would
  1752. * miss a potential PC irq for a setup packet.
  1753. * So, we only put the ep0 into WAIT_FOR_SETUP state.
  1754. */
  1755. if (opc_irq)
  1756. ep0_idle(udc);
  1757. break;
  1758. case OUT_STATUS_STAGE:
  1759. case WAIT_ACK_SET_CONF_INTERF:
  1760. ep_warn(ep, "should never get in %s state here!!!\n",
  1761. EP0_STNAME(ep->dev));
  1762. ep0_idle(udc);
  1763. break;
  1764. }
  1765. }
  1766. /**
  1767. * handle_ep - Handle endpoint data tranfers
  1768. * @ep: pxa physical endpoint
  1769. *
  1770. * Tries to transfer all pending request data into the endpoint and/or
  1771. * transfer all pending data in the endpoint into usb requests.
  1772. *
  1773. * Is always called when in_interrupt() and with ep->lock released.
  1774. */
  1775. static void handle_ep(struct pxa_ep *ep)
  1776. {
  1777. struct pxa27x_request *req;
  1778. int completed;
  1779. u32 udccsr;
  1780. int is_in = ep->dir_in;
  1781. int loop = 0;
  1782. unsigned long flags;
  1783. spin_lock_irqsave(&ep->lock, flags);
  1784. if (ep->in_handle_ep)
  1785. goto recursion_detected;
  1786. ep->in_handle_ep = 1;
  1787. do {
  1788. completed = 0;
  1789. udccsr = udc_ep_readl(ep, UDCCSR);
  1790. if (likely(!list_empty(&ep->queue)))
  1791. req = list_entry(ep->queue.next,
  1792. struct pxa27x_request, queue);
  1793. else
  1794. req = NULL;
  1795. ep_dbg(ep, "req:%p, udccsr 0x%03x loop=%d\n",
  1796. req, udccsr, loop++);
  1797. if (unlikely(udccsr & (UDCCSR_SST | UDCCSR_TRN)))
  1798. udc_ep_writel(ep, UDCCSR,
  1799. udccsr & (UDCCSR_SST | UDCCSR_TRN));
  1800. if (!req)
  1801. break;
  1802. if (unlikely(is_in)) {
  1803. if (likely(!ep_is_full(ep)))
  1804. completed = write_fifo(ep, req);
  1805. } else {
  1806. if (likely(epout_has_pkt(ep)))
  1807. completed = read_fifo(ep, req);
  1808. }
  1809. if (completed) {
  1810. if (is_in)
  1811. ep_end_in_req(ep, req, &flags);
  1812. else
  1813. ep_end_out_req(ep, req, &flags);
  1814. }
  1815. } while (completed);
  1816. ep->in_handle_ep = 0;
  1817. recursion_detected:
  1818. spin_unlock_irqrestore(&ep->lock, flags);
  1819. }
  1820. /**
  1821. * pxa27x_change_configuration - Handle SET_CONF usb request notification
  1822. * @udc: udc device
  1823. * @config: usb configuration
  1824. *
  1825. * Post the request to upper level.
  1826. * Don't use any pxa specific harware configuration capabilities
  1827. */
  1828. static void pxa27x_change_configuration(struct pxa_udc *udc, int config)
  1829. {
  1830. struct usb_ctrlrequest req ;
  1831. dev_dbg(udc->dev, "config=%d\n", config);
  1832. udc->config = config;
  1833. udc->last_interface = 0;
  1834. udc->last_alternate = 0;
  1835. req.bRequestType = 0;
  1836. req.bRequest = USB_REQ_SET_CONFIGURATION;
  1837. req.wValue = config;
  1838. req.wIndex = 0;
  1839. req.wLength = 0;
  1840. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1841. udc->driver->setup(&udc->gadget, &req);
  1842. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
  1843. }
  1844. /**
  1845. * pxa27x_change_interface - Handle SET_INTERF usb request notification
  1846. * @udc: udc device
  1847. * @iface: interface number
  1848. * @alt: alternate setting number
  1849. *
  1850. * Post the request to upper level.
  1851. * Don't use any pxa specific harware configuration capabilities
  1852. */
  1853. static void pxa27x_change_interface(struct pxa_udc *udc, int iface, int alt)
  1854. {
  1855. struct usb_ctrlrequest req;
  1856. dev_dbg(udc->dev, "interface=%d, alternate setting=%d\n", iface, alt);
  1857. udc->last_interface = iface;
  1858. udc->last_alternate = alt;
  1859. req.bRequestType = USB_RECIP_INTERFACE;
  1860. req.bRequest = USB_REQ_SET_INTERFACE;
  1861. req.wValue = alt;
  1862. req.wIndex = iface;
  1863. req.wLength = 0;
  1864. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1865. udc->driver->setup(&udc->gadget, &req);
  1866. ep_write_UDCCSR(&udc->pxa_ep[0], UDCCSR0_AREN);
  1867. }
  1868. /*
  1869. * irq_handle_data - Handle data transfer
  1870. * @irq: irq IRQ number
  1871. * @udc: dev pxa_udc device structure
  1872. *
  1873. * Called from irq handler, transferts data to or from endpoint to queue
  1874. */
  1875. static void irq_handle_data(int irq, struct pxa_udc *udc)
  1876. {
  1877. int i;
  1878. struct pxa_ep *ep;
  1879. u32 udcisr0 = udc_readl(udc, UDCISR0) & UDCCISR0_EP_MASK;
  1880. u32 udcisr1 = udc_readl(udc, UDCISR1) & UDCCISR1_EP_MASK;
  1881. if (udcisr0 & UDCISR_INT_MASK) {
  1882. udc->pxa_ep[0].stats.irqs++;
  1883. udc_writel(udc, UDCISR0, UDCISR_INT(0, UDCISR_INT_MASK));
  1884. handle_ep0(udc, !!(udcisr0 & UDCICR_FIFOERR),
  1885. !!(udcisr0 & UDCICR_PKTCOMPL));
  1886. }
  1887. udcisr0 >>= 2;
  1888. for (i = 1; udcisr0 != 0 && i < 16; udcisr0 >>= 2, i++) {
  1889. if (!(udcisr0 & UDCISR_INT_MASK))
  1890. continue;
  1891. udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK));
  1892. WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
  1893. if (i < ARRAY_SIZE(udc->pxa_ep)) {
  1894. ep = &udc->pxa_ep[i];
  1895. ep->stats.irqs++;
  1896. handle_ep(ep);
  1897. }
  1898. }
  1899. for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
  1900. udc_writel(udc, UDCISR1, UDCISR_INT(i - 16, UDCISR_INT_MASK));
  1901. if (!(udcisr1 & UDCISR_INT_MASK))
  1902. continue;
  1903. WARN_ON(i >= ARRAY_SIZE(udc->pxa_ep));
  1904. if (i < ARRAY_SIZE(udc->pxa_ep)) {
  1905. ep = &udc->pxa_ep[i];
  1906. ep->stats.irqs++;
  1907. handle_ep(ep);
  1908. }
  1909. }
  1910. }
  1911. /**
  1912. * irq_udc_suspend - Handle IRQ "UDC Suspend"
  1913. * @udc: udc device
  1914. */
  1915. static void irq_udc_suspend(struct pxa_udc *udc)
  1916. {
  1917. udc_writel(udc, UDCISR1, UDCISR1_IRSU);
  1918. udc->stats.irqs_suspend++;
  1919. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1920. && udc->driver && udc->driver->suspend)
  1921. udc->driver->suspend(&udc->gadget);
  1922. ep0_idle(udc);
  1923. }
  1924. /**
  1925. * irq_udc_resume - Handle IRQ "UDC Resume"
  1926. * @udc: udc device
  1927. */
  1928. static void irq_udc_resume(struct pxa_udc *udc)
  1929. {
  1930. udc_writel(udc, UDCISR1, UDCISR1_IRRU);
  1931. udc->stats.irqs_resume++;
  1932. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1933. && udc->driver && udc->driver->resume)
  1934. udc->driver->resume(&udc->gadget);
  1935. }
  1936. /**
  1937. * irq_udc_reconfig - Handle IRQ "UDC Change Configuration"
  1938. * @udc: udc device
  1939. */
  1940. static void irq_udc_reconfig(struct pxa_udc *udc)
  1941. {
  1942. unsigned config, interface, alternate, config_change;
  1943. u32 udccr = udc_readl(udc, UDCCR);
  1944. udc_writel(udc, UDCISR1, UDCISR1_IRCC);
  1945. udc->stats.irqs_reconfig++;
  1946. config = (udccr & UDCCR_ACN) >> UDCCR_ACN_S;
  1947. config_change = (config != udc->config);
  1948. pxa27x_change_configuration(udc, config);
  1949. interface = (udccr & UDCCR_AIN) >> UDCCR_AIN_S;
  1950. alternate = (udccr & UDCCR_AAISN) >> UDCCR_AAISN_S;
  1951. pxa27x_change_interface(udc, interface, alternate);
  1952. if (config_change)
  1953. update_pxa_ep_matches(udc);
  1954. udc_set_mask_UDCCR(udc, UDCCR_SMAC);
  1955. }
  1956. /**
  1957. * irq_udc_reset - Handle IRQ "UDC Reset"
  1958. * @udc: udc device
  1959. */
  1960. static void irq_udc_reset(struct pxa_udc *udc)
  1961. {
  1962. u32 udccr = udc_readl(udc, UDCCR);
  1963. struct pxa_ep *ep = &udc->pxa_ep[0];
  1964. dev_info(udc->dev, "USB reset\n");
  1965. udc_writel(udc, UDCISR1, UDCISR1_IRRS);
  1966. udc->stats.irqs_reset++;
  1967. if ((udccr & UDCCR_UDA) == 0) {
  1968. dev_dbg(udc->dev, "USB reset start\n");
  1969. stop_activity(udc);
  1970. }
  1971. udc->gadget.speed = USB_SPEED_FULL;
  1972. memset(&udc->stats, 0, sizeof udc->stats);
  1973. nuke(ep, -EPROTO);
  1974. ep_write_UDCCSR(ep, UDCCSR0_FTF | UDCCSR0_OPC);
  1975. ep0_idle(udc);
  1976. }
  1977. /**
  1978. * pxa_udc_irq - Main irq handler
  1979. * @irq: irq number
  1980. * @_dev: udc device
  1981. *
  1982. * Handles all udc interrupts
  1983. */
  1984. static irqreturn_t pxa_udc_irq(int irq, void *_dev)
  1985. {
  1986. struct pxa_udc *udc = _dev;
  1987. u32 udcisr0 = udc_readl(udc, UDCISR0);
  1988. u32 udcisr1 = udc_readl(udc, UDCISR1);
  1989. u32 udccr = udc_readl(udc, UDCCR);
  1990. u32 udcisr1_spec;
  1991. dev_vdbg(udc->dev, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
  1992. "UDCCR:0x%08x\n", udcisr0, udcisr1, udccr);
  1993. udcisr1_spec = udcisr1 & 0xf8000000;
  1994. if (unlikely(udcisr1_spec & UDCISR1_IRSU))
  1995. irq_udc_suspend(udc);
  1996. if (unlikely(udcisr1_spec & UDCISR1_IRRU))
  1997. irq_udc_resume(udc);
  1998. if (unlikely(udcisr1_spec & UDCISR1_IRCC))
  1999. irq_udc_reconfig(udc);
  2000. if (unlikely(udcisr1_spec & UDCISR1_IRRS))
  2001. irq_udc_reset(udc);
  2002. if ((udcisr0 & UDCCISR0_EP_MASK) | (udcisr1 & UDCCISR1_EP_MASK))
  2003. irq_handle_data(irq, udc);
  2004. return IRQ_HANDLED;
  2005. }
  2006. static struct pxa_udc memory = {
  2007. .gadget = {
  2008. .ops = &pxa_udc_ops,
  2009. .ep0 = &memory.udc_usb_ep[0].usb_ep,
  2010. .name = driver_name,
  2011. .dev = {
  2012. .init_name = "gadget",
  2013. },
  2014. },
  2015. .udc_usb_ep = {
  2016. USB_EP_CTRL,
  2017. USB_EP_OUT_BULK(1),
  2018. USB_EP_IN_BULK(2),
  2019. USB_EP_IN_ISO(3),
  2020. USB_EP_OUT_ISO(4),
  2021. USB_EP_IN_INT(5),
  2022. },
  2023. .pxa_ep = {
  2024. PXA_EP_CTRL,
  2025. /* Endpoints for gadget zero */
  2026. PXA_EP_OUT_BULK(1, 1, 3, 0, 0),
  2027. PXA_EP_IN_BULK(2, 2, 3, 0, 0),
  2028. /* Endpoints for ether gadget, file storage gadget */
  2029. PXA_EP_OUT_BULK(3, 1, 1, 0, 0),
  2030. PXA_EP_IN_BULK(4, 2, 1, 0, 0),
  2031. PXA_EP_IN_ISO(5, 3, 1, 0, 0),
  2032. PXA_EP_OUT_ISO(6, 4, 1, 0, 0),
  2033. PXA_EP_IN_INT(7, 5, 1, 0, 0),
  2034. /* Endpoints for RNDIS, serial */
  2035. PXA_EP_OUT_BULK(8, 1, 2, 0, 0),
  2036. PXA_EP_IN_BULK(9, 2, 2, 0, 0),
  2037. PXA_EP_IN_INT(10, 5, 2, 0, 0),
  2038. /*
  2039. * All the following endpoints are only for completion. They
  2040. * won't never work, as multiple interfaces are really broken on
  2041. * the pxa.
  2042. */
  2043. PXA_EP_OUT_BULK(11, 1, 2, 1, 0),
  2044. PXA_EP_IN_BULK(12, 2, 2, 1, 0),
  2045. /* Endpoint for CDC Ether */
  2046. PXA_EP_OUT_BULK(13, 1, 1, 1, 1),
  2047. PXA_EP_IN_BULK(14, 2, 1, 1, 1),
  2048. }
  2049. };
  2050. #if defined(CONFIG_OF)
  2051. static const struct of_device_id udc_pxa_dt_ids[] = {
  2052. { .compatible = "marvell,pxa270-udc" },
  2053. {}
  2054. };
  2055. MODULE_DEVICE_TABLE(of, udc_pxa_dt_ids);
  2056. #endif
  2057. /**
  2058. * pxa_udc_probe - probes the udc device
  2059. * @_dev: platform device
  2060. *
  2061. * Perform basic init : allocates udc clock, creates sysfs files, requests
  2062. * irq.
  2063. */
  2064. static int pxa_udc_probe(struct platform_device *pdev)
  2065. {
  2066. struct resource *regs;
  2067. struct pxa_udc *udc = &memory;
  2068. int retval = 0, gpio;
  2069. struct pxa2xx_udc_mach_info *mach = dev_get_platdata(&pdev->dev);
  2070. unsigned long gpio_flags;
  2071. if (mach) {
  2072. gpio_flags = mach->gpio_pullup_inverted ? GPIOF_ACTIVE_LOW : 0;
  2073. gpio = mach->gpio_pullup;
  2074. if (gpio_is_valid(gpio)) {
  2075. retval = devm_gpio_request_one(&pdev->dev, gpio,
  2076. gpio_flags,
  2077. "USB D+ pullup");
  2078. if (retval)
  2079. return retval;
  2080. udc->gpiod = gpio_to_desc(mach->gpio_pullup);
  2081. }
  2082. udc->udc_command = mach->udc_command;
  2083. } else {
  2084. udc->gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_ASIS);
  2085. }
  2086. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2087. udc->regs = devm_ioremap_resource(&pdev->dev, regs);
  2088. if (IS_ERR(udc->regs))
  2089. return PTR_ERR(udc->regs);
  2090. udc->irq = platform_get_irq(pdev, 0);
  2091. if (udc->irq < 0)
  2092. return udc->irq;
  2093. udc->dev = &pdev->dev;
  2094. if (of_have_populated_dt()) {
  2095. udc->transceiver =
  2096. devm_usb_get_phy_by_phandle(udc->dev, "phys", 0);
  2097. if (IS_ERR(udc->transceiver))
  2098. return PTR_ERR(udc->transceiver);
  2099. } else {
  2100. udc->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  2101. }
  2102. if (IS_ERR(udc->gpiod)) {
  2103. dev_err(&pdev->dev, "Couldn't find or request D+ gpio : %ld\n",
  2104. PTR_ERR(udc->gpiod));
  2105. return PTR_ERR(udc->gpiod);
  2106. }
  2107. if (udc->gpiod)
  2108. gpiod_direction_output(udc->gpiod, 0);
  2109. udc->clk = devm_clk_get(&pdev->dev, NULL);
  2110. if (IS_ERR(udc->clk))
  2111. return PTR_ERR(udc->clk);
  2112. retval = clk_prepare(udc->clk);
  2113. if (retval)
  2114. return retval;
  2115. udc->vbus_sensed = 0;
  2116. the_controller = udc;
  2117. platform_set_drvdata(pdev, udc);
  2118. udc_init_data(udc);
  2119. /* irq setup after old hardware state is cleaned up */
  2120. retval = devm_request_irq(&pdev->dev, udc->irq, pxa_udc_irq,
  2121. IRQF_SHARED, driver_name, udc);
  2122. if (retval != 0) {
  2123. dev_err(udc->dev, "%s: can't get irq %i, err %d\n",
  2124. driver_name, udc->irq, retval);
  2125. goto err;
  2126. }
  2127. if (!IS_ERR_OR_NULL(udc->transceiver))
  2128. usb_register_notifier(udc->transceiver, &pxa27x_udc_phy);
  2129. retval = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
  2130. if (retval)
  2131. goto err_add_gadget;
  2132. pxa_init_debugfs(udc);
  2133. if (should_enable_udc(udc))
  2134. udc_enable(udc);
  2135. return 0;
  2136. err_add_gadget:
  2137. if (!IS_ERR_OR_NULL(udc->transceiver))
  2138. usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
  2139. err:
  2140. clk_unprepare(udc->clk);
  2141. return retval;
  2142. }
  2143. /**
  2144. * pxa_udc_remove - removes the udc device driver
  2145. * @_dev: platform device
  2146. */
  2147. static int pxa_udc_remove(struct platform_device *_dev)
  2148. {
  2149. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2150. usb_del_gadget_udc(&udc->gadget);
  2151. pxa_cleanup_debugfs(udc);
  2152. if (!IS_ERR_OR_NULL(udc->transceiver)) {
  2153. usb_unregister_notifier(udc->transceiver, &pxa27x_udc_phy);
  2154. usb_put_phy(udc->transceiver);
  2155. }
  2156. udc->transceiver = NULL;
  2157. the_controller = NULL;
  2158. clk_unprepare(udc->clk);
  2159. return 0;
  2160. }
  2161. static void pxa_udc_shutdown(struct platform_device *_dev)
  2162. {
  2163. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2164. if (udc_readl(udc, UDCCR) & UDCCR_UDE)
  2165. udc_disable(udc);
  2166. }
  2167. #ifdef CONFIG_PXA27x
  2168. extern void pxa27x_clear_otgph(void);
  2169. #else
  2170. #define pxa27x_clear_otgph() do {} while (0)
  2171. #endif
  2172. #ifdef CONFIG_PM
  2173. /**
  2174. * pxa_udc_suspend - Suspend udc device
  2175. * @_dev: platform device
  2176. * @state: suspend state
  2177. *
  2178. * Suspends udc : saves configuration registers (UDCCR*), then disables the udc
  2179. * device.
  2180. */
  2181. static int pxa_udc_suspend(struct platform_device *_dev, pm_message_t state)
  2182. {
  2183. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2184. struct pxa_ep *ep;
  2185. ep = &udc->pxa_ep[0];
  2186. udc->udccsr0 = udc_ep_readl(ep, UDCCSR);
  2187. udc_disable(udc);
  2188. udc->pullup_resume = udc->pullup_on;
  2189. dplus_pullup(udc, 0);
  2190. if (udc->driver)
  2191. udc->driver->disconnect(&udc->gadget);
  2192. return 0;
  2193. }
  2194. /**
  2195. * pxa_udc_resume - Resume udc device
  2196. * @_dev: platform device
  2197. *
  2198. * Resumes udc : restores configuration registers (UDCCR*), then enables the udc
  2199. * device.
  2200. */
  2201. static int pxa_udc_resume(struct platform_device *_dev)
  2202. {
  2203. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2204. struct pxa_ep *ep;
  2205. ep = &udc->pxa_ep[0];
  2206. udc_ep_writel(ep, UDCCSR, udc->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME));
  2207. dplus_pullup(udc, udc->pullup_resume);
  2208. if (should_enable_udc(udc))
  2209. udc_enable(udc);
  2210. /*
  2211. * We do not handle OTG yet.
  2212. *
  2213. * OTGPH bit is set when sleep mode is entered.
  2214. * it indicates that OTG pad is retaining its state.
  2215. * Upon exit from sleep mode and before clearing OTGPH,
  2216. * Software must configure the USB OTG pad, UDC, and UHC
  2217. * to the state they were in before entering sleep mode.
  2218. */
  2219. pxa27x_clear_otgph();
  2220. return 0;
  2221. }
  2222. #endif
  2223. /* work with hotplug and coldplug */
  2224. MODULE_ALIAS("platform:pxa27x-udc");
  2225. static struct platform_driver udc_driver = {
  2226. .driver = {
  2227. .name = "pxa27x-udc",
  2228. .of_match_table = of_match_ptr(udc_pxa_dt_ids),
  2229. },
  2230. .probe = pxa_udc_probe,
  2231. .remove = pxa_udc_remove,
  2232. .shutdown = pxa_udc_shutdown,
  2233. #ifdef CONFIG_PM
  2234. .suspend = pxa_udc_suspend,
  2235. .resume = pxa_udc_resume
  2236. #endif
  2237. };
  2238. module_platform_driver(udc_driver);
  2239. MODULE_DESCRIPTION(DRIVER_DESC);
  2240. MODULE_AUTHOR("Robert Jarzmik");
  2241. MODULE_LICENSE("GPL");