pxa27x_udc.c 67 KB

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