pxa27x_udc.c 61 KB

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