pxa27x_udc.c 68 KB

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