pxa27x_udc.c 67 KB

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