pxa27x_udc.c 68 KB

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