pxa27x_udc.c 61 KB

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