pxa27x_udc.c 61 KB

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