pxa27x_udc.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. /*
  2. * Handles the Intel 27x USB Device Controller (UDC)
  3. *
  4. * Inspired by original driver by Frank Becker, David Brownell, and others.
  5. * Copyright (C) 2008 Robert Jarzmik
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/errno.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/delay.h>
  28. #include <linux/list.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/clk.h>
  32. #include <linux/irq.h>
  33. #include <linux/gpio.h>
  34. #include <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 (!eps)
  250. goto err_eps;
  251. udc->debugfs_root = root;
  252. udc->debugfs_state = state;
  253. udc->debugfs_queues = queues;
  254. udc->debugfs_eps = eps;
  255. return;
  256. err_eps:
  257. debugfs_remove(eps);
  258. err_queues:
  259. debugfs_remove(queues);
  260. err_state:
  261. debugfs_remove(root);
  262. err_root:
  263. dev_err(udc->dev, "debugfs is not available\n");
  264. }
  265. static void pxa_cleanup_debugfs(struct pxa_udc *udc)
  266. {
  267. debugfs_remove(udc->debugfs_eps);
  268. debugfs_remove(udc->debugfs_queues);
  269. debugfs_remove(udc->debugfs_state);
  270. debugfs_remove(udc->debugfs_root);
  271. udc->debugfs_eps = NULL;
  272. udc->debugfs_queues = NULL;
  273. udc->debugfs_state = NULL;
  274. udc->debugfs_root = NULL;
  275. }
  276. #else
  277. static inline void pxa_init_debugfs(struct pxa_udc *udc)
  278. {
  279. }
  280. static inline void pxa_cleanup_debugfs(struct pxa_udc *udc)
  281. {
  282. }
  283. #endif
  284. /**
  285. * is_match_usb_pxa - check if usb_ep and pxa_ep match
  286. * @udc_usb_ep: usb endpoint
  287. * @ep: pxa endpoint
  288. * @config: configuration required in pxa_ep
  289. * @interface: interface required in pxa_ep
  290. * @altsetting: altsetting required in pxa_ep
  291. *
  292. * Returns 1 if all criteria match between pxa and usb endpoint, 0 otherwise
  293. */
  294. static int is_match_usb_pxa(struct udc_usb_ep *udc_usb_ep, struct pxa_ep *ep,
  295. int config, int interface, int altsetting)
  296. {
  297. if (usb_endpoint_num(&udc_usb_ep->desc) != ep->addr)
  298. return 0;
  299. if (usb_endpoint_dir_in(&udc_usb_ep->desc) != ep->dir_in)
  300. return 0;
  301. if (usb_endpoint_type(&udc_usb_ep->desc) != ep->type)
  302. return 0;
  303. if ((ep->config != config) || (ep->interface != interface)
  304. || (ep->alternate != altsetting))
  305. return 0;
  306. return 1;
  307. }
  308. /**
  309. * find_pxa_ep - find pxa_ep structure matching udc_usb_ep
  310. * @udc: pxa udc
  311. * @udc_usb_ep: udc_usb_ep structure
  312. *
  313. * Match udc_usb_ep and all pxa_ep available, to see if one matches.
  314. * This is necessary because of the strong pxa hardware restriction requiring
  315. * that once pxa endpoints are initialized, their configuration is freezed, and
  316. * no change can be made to their address, direction, or in which configuration,
  317. * interface or altsetting they are active ... which differs from more usual
  318. * models which have endpoints be roughly just addressable fifos, and leave
  319. * configuration events up to gadget drivers (like all control messages).
  320. *
  321. * Note that there is still a blurred point here :
  322. * - we rely on UDCCR register "active interface" and "active altsetting".
  323. * This is a nonsense in regard of USB spec, where multiple interfaces are
  324. * active at the same time.
  325. * - if we knew for sure that the pxa can handle multiple interface at the
  326. * same time, assuming Intel's Developer Guide is wrong, this function
  327. * should be reviewed, and a cache of couples (iface, altsetting) should
  328. * be kept in the pxa_udc structure. In this case this function would match
  329. * against the cache of couples instead of the "last altsetting" set up.
  330. *
  331. * Returns the matched pxa_ep structure or NULL if none found
  332. */
  333. static struct pxa_ep *find_pxa_ep(struct pxa_udc *udc,
  334. struct udc_usb_ep *udc_usb_ep)
  335. {
  336. int i;
  337. struct pxa_ep *ep;
  338. int cfg = udc->config;
  339. int iface = udc->last_interface;
  340. int alt = udc->last_alternate;
  341. if (udc_usb_ep == &udc->udc_usb_ep[0])
  342. return &udc->pxa_ep[0];
  343. for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
  344. ep = &udc->pxa_ep[i];
  345. if (is_match_usb_pxa(udc_usb_ep, ep, cfg, iface, alt))
  346. return ep;
  347. }
  348. return NULL;
  349. }
  350. /**
  351. * update_pxa_ep_matches - update pxa_ep cached values in all udc_usb_ep
  352. * @udc: pxa udc
  353. *
  354. * Context: in_interrupt()
  355. *
  356. * Updates all pxa_ep fields in udc_usb_ep structures, if this field was
  357. * previously set up (and is not NULL). The update is necessary is a
  358. * configuration change or altsetting change was issued by the USB host.
  359. */
  360. static void update_pxa_ep_matches(struct pxa_udc *udc)
  361. {
  362. int i;
  363. struct udc_usb_ep *udc_usb_ep;
  364. for (i = 1; i < NR_USB_ENDPOINTS; i++) {
  365. udc_usb_ep = &udc->udc_usb_ep[i];
  366. if (udc_usb_ep->pxa_ep)
  367. udc_usb_ep->pxa_ep = find_pxa_ep(udc, udc_usb_ep);
  368. }
  369. }
  370. /**
  371. * pio_irq_enable - Enables irq generation for one endpoint
  372. * @ep: udc endpoint
  373. */
  374. static void pio_irq_enable(struct pxa_ep *ep)
  375. {
  376. struct pxa_udc *udc = ep->dev;
  377. int index = EPIDX(ep);
  378. u32 udcicr0 = udc_readl(udc, UDCICR0);
  379. u32 udcicr1 = udc_readl(udc, UDCICR1);
  380. if (index < 16)
  381. udc_writel(udc, UDCICR0, udcicr0 | (3 << (index * 2)));
  382. else
  383. udc_writel(udc, UDCICR1, udcicr1 | (3 << ((index - 16) * 2)));
  384. }
  385. /**
  386. * pio_irq_disable - Disables irq generation for one endpoint
  387. * @ep: udc endpoint
  388. */
  389. static void pio_irq_disable(struct pxa_ep *ep)
  390. {
  391. struct pxa_udc *udc = ep->dev;
  392. int index = EPIDX(ep);
  393. u32 udcicr0 = udc_readl(udc, UDCICR0);
  394. u32 udcicr1 = udc_readl(udc, UDCICR1);
  395. if (index < 16)
  396. udc_writel(udc, UDCICR0, udcicr0 & ~(3 << (index * 2)));
  397. else
  398. udc_writel(udc, UDCICR1, udcicr1 & ~(3 << ((index - 16) * 2)));
  399. }
  400. /**
  401. * udc_set_mask_UDCCR - set bits in UDCCR
  402. * @udc: udc device
  403. * @mask: bits to set in UDCCR
  404. *
  405. * Sets bits in UDCCR, leaving DME and FST bits as they were.
  406. */
  407. static inline void udc_set_mask_UDCCR(struct pxa_udc *udc, int mask)
  408. {
  409. u32 udccr = udc_readl(udc, UDCCR);
  410. udc_writel(udc, UDCCR,
  411. (udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS));
  412. }
  413. /**
  414. * udc_clear_mask_UDCCR - clears bits in UDCCR
  415. * @udc: udc device
  416. * @mask: bit to clear in UDCCR
  417. *
  418. * Clears bits in UDCCR, leaving DME and FST bits as they were.
  419. */
  420. static inline void udc_clear_mask_UDCCR(struct pxa_udc *udc, int mask)
  421. {
  422. u32 udccr = udc_readl(udc, UDCCR);
  423. udc_writel(udc, UDCCR,
  424. (udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS));
  425. }
  426. /**
  427. * ep_count_bytes_remain - get how many bytes in udc endpoint
  428. * @ep: udc endpoint
  429. *
  430. * Returns number of bytes in OUT fifos. Broken for IN fifos (-EOPNOTSUPP)
  431. */
  432. static int ep_count_bytes_remain(struct pxa_ep *ep)
  433. {
  434. if (ep->dir_in)
  435. return -EOPNOTSUPP;
  436. return udc_ep_readl(ep, UDCBCR) & 0x3ff;
  437. }
  438. /**
  439. * ep_is_empty - checks if ep has byte ready for reading
  440. * @ep: udc endpoint
  441. *
  442. * If endpoint is the control endpoint, checks if there are bytes in the
  443. * control endpoint fifo. If endpoint is a data endpoint, checks if bytes
  444. * are ready for reading on OUT endpoint.
  445. *
  446. * Returns 0 if ep not empty, 1 if ep empty, -EOPNOTSUPP if IN endpoint
  447. */
  448. static int ep_is_empty(struct pxa_ep *ep)
  449. {
  450. int ret;
  451. if (!is_ep0(ep) && ep->dir_in)
  452. return -EOPNOTSUPP;
  453. if (is_ep0(ep))
  454. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR0_RNE);
  455. else
  456. ret = !(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNE);
  457. return ret;
  458. }
  459. /**
  460. * ep_is_full - checks if ep has place to write bytes
  461. * @ep: udc endpoint
  462. *
  463. * If endpoint is not the control endpoint and is an IN endpoint, checks if
  464. * there is place to write bytes into the endpoint.
  465. *
  466. * Returns 0 if ep not full, 1 if ep full, -EOPNOTSUPP if OUT endpoint
  467. */
  468. static int ep_is_full(struct pxa_ep *ep)
  469. {
  470. if (is_ep0(ep))
  471. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_IPR);
  472. if (!ep->dir_in)
  473. return -EOPNOTSUPP;
  474. return (!(udc_ep_readl(ep, UDCCSR) & UDCCSR_BNF));
  475. }
  476. /**
  477. * epout_has_pkt - checks if OUT endpoint fifo has a packet available
  478. * @ep: pxa endpoint
  479. *
  480. * Returns 1 if a complete packet is available, 0 if not, -EOPNOTSUPP for IN ep.
  481. */
  482. static int epout_has_pkt(struct pxa_ep *ep)
  483. {
  484. if (!is_ep0(ep) && ep->dir_in)
  485. return -EOPNOTSUPP;
  486. if (is_ep0(ep))
  487. return (udc_ep_readl(ep, UDCCSR) & UDCCSR0_OPC);
  488. return (udc_ep_readl(ep, UDCCSR) & UDCCSR_PC);
  489. }
  490. /**
  491. * set_ep0state - Set ep0 automata state
  492. * @dev: udc device
  493. * @state: state
  494. */
  495. static void set_ep0state(struct pxa_udc *udc, int state)
  496. {
  497. struct pxa_ep *ep = &udc->pxa_ep[0];
  498. char *old_stname = EP0_STNAME(udc);
  499. udc->ep0state = state;
  500. ep_dbg(ep, "state=%s->%s, udccsr0=0x%03x, udcbcr=%d\n", old_stname,
  501. EP0_STNAME(udc), udc_ep_readl(ep, UDCCSR),
  502. udc_ep_readl(ep, UDCBCR));
  503. }
  504. /**
  505. * ep0_idle - Put control endpoint into idle state
  506. * @dev: udc device
  507. */
  508. static void ep0_idle(struct pxa_udc *dev)
  509. {
  510. set_ep0state(dev, WAIT_FOR_SETUP);
  511. }
  512. /**
  513. * inc_ep_stats_reqs - Update ep stats counts
  514. * @ep: physical endpoint
  515. * @req: usb request
  516. * @is_in: ep direction (USB_DIR_IN or 0)
  517. *
  518. */
  519. static void inc_ep_stats_reqs(struct pxa_ep *ep, int is_in)
  520. {
  521. if (is_in)
  522. ep->stats.in_ops++;
  523. else
  524. ep->stats.out_ops++;
  525. }
  526. /**
  527. * inc_ep_stats_bytes - Update ep stats counts
  528. * @ep: physical endpoint
  529. * @count: bytes transfered on endpoint
  530. * @is_in: ep direction (USB_DIR_IN or 0)
  531. */
  532. static void inc_ep_stats_bytes(struct pxa_ep *ep, int count, int is_in)
  533. {
  534. if (is_in)
  535. ep->stats.in_bytes += count;
  536. else
  537. ep->stats.out_bytes += count;
  538. }
  539. /**
  540. * pxa_ep_setup - Sets up an usb physical endpoint
  541. * @ep: pxa27x physical endpoint
  542. *
  543. * Find the physical pxa27x ep, and setup its UDCCR
  544. */
  545. static __init void pxa_ep_setup(struct pxa_ep *ep)
  546. {
  547. u32 new_udccr;
  548. new_udccr = ((ep->config << UDCCONR_CN_S) & UDCCONR_CN)
  549. | ((ep->interface << UDCCONR_IN_S) & UDCCONR_IN)
  550. | ((ep->alternate << UDCCONR_AISN_S) & UDCCONR_AISN)
  551. | ((EPADDR(ep) << UDCCONR_EN_S) & UDCCONR_EN)
  552. | ((EPXFERTYPE(ep) << UDCCONR_ET_S) & UDCCONR_ET)
  553. | ((ep->dir_in) ? UDCCONR_ED : 0)
  554. | ((ep->fifo_size << UDCCONR_MPS_S) & UDCCONR_MPS)
  555. | UDCCONR_EE;
  556. udc_ep_writel(ep, UDCCR, new_udccr);
  557. }
  558. /**
  559. * pxa_eps_setup - Sets up all usb physical endpoints
  560. * @dev: udc device
  561. *
  562. * Setup all pxa physical endpoints, except ep0
  563. */
  564. static __init void pxa_eps_setup(struct pxa_udc *dev)
  565. {
  566. unsigned int i;
  567. dev_dbg(dev->dev, "%s: dev=%p\n", __func__, dev);
  568. for (i = 1; i < NR_PXA_ENDPOINTS; i++)
  569. pxa_ep_setup(&dev->pxa_ep[i]);
  570. }
  571. /**
  572. * pxa_ep_alloc_request - Allocate usb request
  573. * @_ep: usb endpoint
  574. * @gfp_flags:
  575. *
  576. * For the pxa27x, these can just wrap kmalloc/kfree. gadget drivers
  577. * must still pass correctly initialized endpoints, since other controller
  578. * drivers may care about how it's currently set up (dma issues etc).
  579. */
  580. static struct usb_request *
  581. pxa_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  582. {
  583. struct pxa27x_request *req;
  584. req = kzalloc(sizeof *req, gfp_flags);
  585. if (!req)
  586. return NULL;
  587. INIT_LIST_HEAD(&req->queue);
  588. req->in_use = 0;
  589. req->udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  590. return &req->req;
  591. }
  592. /**
  593. * pxa_ep_free_request - Free usb request
  594. * @_ep: usb endpoint
  595. * @_req: usb request
  596. *
  597. * Wrapper around kfree to free _req
  598. */
  599. static void pxa_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
  600. {
  601. struct pxa27x_request *req;
  602. req = container_of(_req, struct pxa27x_request, req);
  603. WARN_ON(!list_empty(&req->queue));
  604. kfree(req);
  605. }
  606. /**
  607. * ep_add_request - add a request to the endpoint's queue
  608. * @ep: usb endpoint
  609. * @req: usb request
  610. *
  611. * Context: ep->lock held
  612. *
  613. * Queues the request in the endpoint's queue, and enables the interrupts
  614. * on the endpoint.
  615. */
  616. static void ep_add_request(struct pxa_ep *ep, struct pxa27x_request *req)
  617. {
  618. if (unlikely(!req))
  619. return;
  620. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  621. req->req.length, udc_ep_readl(ep, UDCCSR));
  622. req->in_use = 1;
  623. list_add_tail(&req->queue, &ep->queue);
  624. pio_irq_enable(ep);
  625. }
  626. /**
  627. * ep_del_request - removes a request from the endpoint's queue
  628. * @ep: usb endpoint
  629. * @req: usb request
  630. *
  631. * Context: ep->lock held
  632. *
  633. * Unqueue the request from the endpoint's queue. If there are no more requests
  634. * on the endpoint, and if it's not the control endpoint, interrupts are
  635. * disabled on the endpoint.
  636. */
  637. static void ep_del_request(struct pxa_ep *ep, struct pxa27x_request *req)
  638. {
  639. if (unlikely(!req))
  640. return;
  641. ep_vdbg(ep, "req:%p, lg=%d, udccsr=0x%03x\n", req,
  642. req->req.length, udc_ep_readl(ep, UDCCSR));
  643. list_del_init(&req->queue);
  644. req->in_use = 0;
  645. if (!is_ep0(ep) && list_empty(&ep->queue))
  646. pio_irq_disable(ep);
  647. }
  648. /**
  649. * req_done - Complete an usb request
  650. * @ep: pxa physical endpoint
  651. * @req: pxa request
  652. * @status: usb request status sent to gadget API
  653. *
  654. * Context: ep->lock held
  655. *
  656. * Retire a pxa27x usb request. Endpoint must be locked.
  657. */
  658. static void req_done(struct pxa_ep *ep, struct pxa27x_request *req, int status)
  659. {
  660. ep_del_request(ep, req);
  661. if (likely(req->req.status == -EINPROGRESS))
  662. req->req.status = status;
  663. else
  664. status = req->req.status;
  665. if (status && status != -ESHUTDOWN)
  666. ep_dbg(ep, "complete req %p stat %d len %u/%u\n",
  667. &req->req, status,
  668. req->req.actual, req->req.length);
  669. req->req.complete(&req->udc_usb_ep->usb_ep, &req->req);
  670. }
  671. /**
  672. * ep_end_out_req - Ends endpoint OUT request
  673. * @ep: physical endpoint
  674. * @req: pxa request
  675. *
  676. * Context: ep->lock held
  677. *
  678. * Ends endpoint OUT request (completes usb request).
  679. */
  680. static void ep_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req)
  681. {
  682. inc_ep_stats_reqs(ep, !USB_DIR_IN);
  683. req_done(ep, req, 0);
  684. }
  685. /**
  686. * ep0_end_out_req - Ends control endpoint OUT request (ends data stage)
  687. * @ep: physical endpoint
  688. * @req: pxa request
  689. *
  690. * Context: ep->lock held
  691. *
  692. * Ends control endpoint OUT request (completes usb request), and puts
  693. * control endpoint into idle state
  694. */
  695. static void ep0_end_out_req(struct pxa_ep *ep, struct pxa27x_request *req)
  696. {
  697. set_ep0state(ep->dev, OUT_STATUS_STAGE);
  698. ep_end_out_req(ep, req);
  699. ep0_idle(ep->dev);
  700. }
  701. /**
  702. * ep_end_in_req - Ends endpoint IN request
  703. * @ep: physical endpoint
  704. * @req: pxa request
  705. *
  706. * Context: ep->lock held
  707. *
  708. * Ends endpoint IN request (completes usb request).
  709. */
  710. static void ep_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req)
  711. {
  712. inc_ep_stats_reqs(ep, USB_DIR_IN);
  713. req_done(ep, req, 0);
  714. }
  715. /**
  716. * ep0_end_in_req - Ends control endpoint IN request (ends data stage)
  717. * @ep: physical endpoint
  718. * @req: pxa request
  719. *
  720. * Context: ep->lock held
  721. *
  722. * Ends control endpoint IN request (completes usb request), and puts
  723. * control endpoint into status state
  724. */
  725. static void ep0_end_in_req(struct pxa_ep *ep, struct pxa27x_request *req)
  726. {
  727. set_ep0state(ep->dev, 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 WAIT_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 = -EINVAL;
  1078. if (!_ep)
  1079. return rc;
  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 rc;
  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. req_done(ep, req, -ECONNRESET);
  1089. rc = 0;
  1090. break;
  1091. }
  1092. }
  1093. spin_unlock_irqrestore(&ep->lock, flags);
  1094. return rc;
  1095. }
  1096. /**
  1097. * pxa_ep_set_halt - Halts operations on one endpoint
  1098. * @_ep: usb endpoint
  1099. * @value:
  1100. *
  1101. * Returns 0 if no error, -EINVAL, -EROFS, -EAGAIN otherwise
  1102. */
  1103. static int pxa_ep_set_halt(struct usb_ep *_ep, int value)
  1104. {
  1105. struct pxa_ep *ep;
  1106. struct udc_usb_ep *udc_usb_ep;
  1107. unsigned long flags;
  1108. int rc;
  1109. if (!_ep)
  1110. return -EINVAL;
  1111. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1112. ep = udc_usb_ep->pxa_ep;
  1113. if (!ep || is_ep0(ep))
  1114. return -EINVAL;
  1115. if (value == 0) {
  1116. /*
  1117. * This path (reset toggle+halt) is needed to implement
  1118. * SET_INTERFACE on normal hardware. but it can't be
  1119. * done from software on the PXA UDC, and the hardware
  1120. * forgets to do it as part of SET_INTERFACE automagic.
  1121. */
  1122. ep_dbg(ep, "only host can clear halt\n");
  1123. return -EROFS;
  1124. }
  1125. spin_lock_irqsave(&ep->lock, flags);
  1126. rc = -EAGAIN;
  1127. if (ep->dir_in && (ep_is_full(ep) || !list_empty(&ep->queue)))
  1128. goto out;
  1129. /* FST, FEF bits are the same for control and non control endpoints */
  1130. rc = 0;
  1131. udc_ep_writel(ep, UDCCSR, UDCCSR_FST | UDCCSR_FEF);
  1132. if (is_ep0(ep))
  1133. set_ep0state(ep->dev, STALL);
  1134. out:
  1135. spin_unlock_irqrestore(&ep->lock, flags);
  1136. return rc;
  1137. }
  1138. /**
  1139. * pxa_ep_fifo_status - Get how many bytes in physical endpoint
  1140. * @_ep: usb endpoint
  1141. *
  1142. * Returns number of bytes in OUT fifos. Broken for IN fifos.
  1143. */
  1144. static int pxa_ep_fifo_status(struct usb_ep *_ep)
  1145. {
  1146. struct pxa_ep *ep;
  1147. struct udc_usb_ep *udc_usb_ep;
  1148. if (!_ep)
  1149. return -ENODEV;
  1150. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1151. ep = udc_usb_ep->pxa_ep;
  1152. if (!ep || is_ep0(ep))
  1153. return -ENODEV;
  1154. if (ep->dir_in)
  1155. return -EOPNOTSUPP;
  1156. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN || ep_is_empty(ep))
  1157. return 0;
  1158. else
  1159. return ep_count_bytes_remain(ep) + 1;
  1160. }
  1161. /**
  1162. * pxa_ep_fifo_flush - Flushes one endpoint
  1163. * @_ep: usb endpoint
  1164. *
  1165. * Discards all data in one endpoint(IN or OUT), except control endpoint.
  1166. */
  1167. static void pxa_ep_fifo_flush(struct usb_ep *_ep)
  1168. {
  1169. struct pxa_ep *ep;
  1170. struct udc_usb_ep *udc_usb_ep;
  1171. unsigned long flags;
  1172. if (!_ep)
  1173. return;
  1174. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1175. ep = udc_usb_ep->pxa_ep;
  1176. if (!ep || is_ep0(ep))
  1177. return;
  1178. spin_lock_irqsave(&ep->lock, flags);
  1179. if (unlikely(!list_empty(&ep->queue)))
  1180. ep_dbg(ep, "called while queue list not empty\n");
  1181. ep_dbg(ep, "called\n");
  1182. /* for OUT, just read and discard the FIFO contents. */
  1183. if (!ep->dir_in) {
  1184. while (!ep_is_empty(ep))
  1185. udc_ep_readl(ep, UDCDR);
  1186. } else {
  1187. /* most IN status is the same, but ISO can't stall */
  1188. udc_ep_writel(ep, UDCCSR,
  1189. UDCCSR_PC | UDCCSR_FEF | UDCCSR_TRN
  1190. | (EPXFERTYPE_is_ISO(ep) ? 0 : UDCCSR_SST));
  1191. }
  1192. spin_unlock_irqrestore(&ep->lock, flags);
  1193. return;
  1194. }
  1195. /**
  1196. * pxa_ep_enable - Enables usb endpoint
  1197. * @_ep: usb endpoint
  1198. * @desc: usb endpoint descriptor
  1199. *
  1200. * Nothing much to do here, as ep configuration is done once and for all
  1201. * before udc is enabled. After udc enable, no physical endpoint configuration
  1202. * can be changed.
  1203. * Function makes sanity checks and flushes the endpoint.
  1204. */
  1205. static int pxa_ep_enable(struct usb_ep *_ep,
  1206. const struct usb_endpoint_descriptor *desc)
  1207. {
  1208. struct pxa_ep *ep;
  1209. struct udc_usb_ep *udc_usb_ep;
  1210. struct pxa_udc *udc;
  1211. if (!_ep || !desc)
  1212. return -EINVAL;
  1213. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1214. if (udc_usb_ep->pxa_ep) {
  1215. ep = udc_usb_ep->pxa_ep;
  1216. ep_warn(ep, "usb_ep %s already enabled, doing nothing\n",
  1217. _ep->name);
  1218. } else {
  1219. ep = find_pxa_ep(udc_usb_ep->dev, udc_usb_ep);
  1220. }
  1221. if (!ep || is_ep0(ep)) {
  1222. dev_err(udc_usb_ep->dev->dev,
  1223. "unable to match pxa_ep for ep %s\n",
  1224. _ep->name);
  1225. return -EINVAL;
  1226. }
  1227. if ((desc->bDescriptorType != USB_DT_ENDPOINT)
  1228. || (ep->type != usb_endpoint_type(desc))) {
  1229. ep_err(ep, "type mismatch\n");
  1230. return -EINVAL;
  1231. }
  1232. if (ep->fifo_size < le16_to_cpu(desc->wMaxPacketSize)) {
  1233. ep_err(ep, "bad maxpacket\n");
  1234. return -ERANGE;
  1235. }
  1236. udc_usb_ep->pxa_ep = ep;
  1237. udc = ep->dev;
  1238. if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
  1239. ep_err(ep, "bogus device state\n");
  1240. return -ESHUTDOWN;
  1241. }
  1242. ep->enabled = 1;
  1243. /* flush fifo (mostly for OUT buffers) */
  1244. pxa_ep_fifo_flush(_ep);
  1245. ep_dbg(ep, "enabled\n");
  1246. return 0;
  1247. }
  1248. /**
  1249. * pxa_ep_disable - Disable usb endpoint
  1250. * @_ep: usb endpoint
  1251. *
  1252. * Same as for pxa_ep_enable, no physical endpoint configuration can be
  1253. * changed.
  1254. * Function flushes the endpoint and related requests.
  1255. */
  1256. static int pxa_ep_disable(struct usb_ep *_ep)
  1257. {
  1258. struct pxa_ep *ep;
  1259. struct udc_usb_ep *udc_usb_ep;
  1260. unsigned long flags;
  1261. if (!_ep)
  1262. return -EINVAL;
  1263. udc_usb_ep = container_of(_ep, struct udc_usb_ep, usb_ep);
  1264. ep = udc_usb_ep->pxa_ep;
  1265. if (!ep || is_ep0(ep) || !list_empty(&ep->queue))
  1266. return -EINVAL;
  1267. spin_lock_irqsave(&ep->lock, flags);
  1268. ep->enabled = 0;
  1269. nuke(ep, -ESHUTDOWN);
  1270. spin_unlock_irqrestore(&ep->lock, flags);
  1271. pxa_ep_fifo_flush(_ep);
  1272. udc_usb_ep->pxa_ep = NULL;
  1273. ep_dbg(ep, "disabled\n");
  1274. return 0;
  1275. }
  1276. static struct usb_ep_ops pxa_ep_ops = {
  1277. .enable = pxa_ep_enable,
  1278. .disable = pxa_ep_disable,
  1279. .alloc_request = pxa_ep_alloc_request,
  1280. .free_request = pxa_ep_free_request,
  1281. .queue = pxa_ep_queue,
  1282. .dequeue = pxa_ep_dequeue,
  1283. .set_halt = pxa_ep_set_halt,
  1284. .fifo_status = pxa_ep_fifo_status,
  1285. .fifo_flush = pxa_ep_fifo_flush,
  1286. };
  1287. /**
  1288. * dplus_pullup - Connect or disconnect pullup resistor to D+ pin
  1289. * @udc: udc device
  1290. * @on: 0 if disconnect pullup resistor, 1 otherwise
  1291. * Context: any
  1292. *
  1293. * Handle D+ pullup resistor, make the device visible to the usb bus, and
  1294. * declare it as a full speed usb device
  1295. */
  1296. static void dplus_pullup(struct pxa_udc *udc, int on)
  1297. {
  1298. if (on) {
  1299. if (gpio_is_valid(udc->mach->gpio_pullup))
  1300. gpio_set_value(udc->mach->gpio_pullup,
  1301. !udc->mach->gpio_pullup_inverted);
  1302. if (udc->mach->udc_command)
  1303. udc->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
  1304. } else {
  1305. if (gpio_is_valid(udc->mach->gpio_pullup))
  1306. gpio_set_value(udc->mach->gpio_pullup,
  1307. udc->mach->gpio_pullup_inverted);
  1308. if (udc->mach->udc_command)
  1309. udc->mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  1310. }
  1311. udc->pullup_on = on;
  1312. }
  1313. /**
  1314. * pxa_udc_get_frame - Returns usb frame number
  1315. * @_gadget: usb gadget
  1316. */
  1317. static int pxa_udc_get_frame(struct usb_gadget *_gadget)
  1318. {
  1319. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1320. return (udc_readl(udc, UDCFNR) & 0x7ff);
  1321. }
  1322. /**
  1323. * pxa_udc_wakeup - Force udc device out of suspend
  1324. * @_gadget: usb gadget
  1325. *
  1326. * Returns 0 if succesfull, error code otherwise
  1327. */
  1328. static int pxa_udc_wakeup(struct usb_gadget *_gadget)
  1329. {
  1330. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1331. /* host may not have enabled remote wakeup */
  1332. if ((udc_readl(udc, UDCCR) & UDCCR_DWRE) == 0)
  1333. return -EHOSTUNREACH;
  1334. udc_set_mask_UDCCR(udc, UDCCR_UDR);
  1335. return 0;
  1336. }
  1337. static void udc_enable(struct pxa_udc *udc);
  1338. static void udc_disable(struct pxa_udc *udc);
  1339. /**
  1340. * should_enable_udc - Tells if UDC should be enabled
  1341. * @udc: udc device
  1342. * Context: any
  1343. *
  1344. * The UDC should be enabled if :
  1345. * - the pullup resistor is connected
  1346. * - and a gadget driver is bound
  1347. * - and vbus is sensed (or no vbus sense is available)
  1348. *
  1349. * Returns 1 if UDC should be enabled, 0 otherwise
  1350. */
  1351. static int should_enable_udc(struct pxa_udc *udc)
  1352. {
  1353. int put_on;
  1354. put_on = ((udc->pullup_on) && (udc->driver));
  1355. put_on &= ((udc->vbus_sensed) || (!udc->transceiver));
  1356. return put_on;
  1357. }
  1358. /**
  1359. * should_disable_udc - Tells if UDC should be disabled
  1360. * @udc: udc device
  1361. * Context: any
  1362. *
  1363. * The UDC should be disabled if :
  1364. * - the pullup resistor is not connected
  1365. * - or no gadget driver is bound
  1366. * - or no vbus is sensed (when vbus sesing is available)
  1367. *
  1368. * Returns 1 if UDC should be disabled
  1369. */
  1370. static int should_disable_udc(struct pxa_udc *udc)
  1371. {
  1372. int put_off;
  1373. put_off = ((!udc->pullup_on) || (!udc->driver));
  1374. put_off |= ((!udc->vbus_sensed) && (udc->transceiver));
  1375. return put_off;
  1376. }
  1377. /**
  1378. * pxa_udc_pullup - Offer manual D+ pullup control
  1379. * @_gadget: usb gadget using the control
  1380. * @is_active: 0 if disconnect, else connect D+ pullup resistor
  1381. * Context: !in_interrupt()
  1382. *
  1383. * Returns 0 if OK, -EOPNOTSUPP if udc driver doesn't handle D+ pullup
  1384. */
  1385. static int pxa_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1386. {
  1387. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1388. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  1389. return -EOPNOTSUPP;
  1390. dplus_pullup(udc, is_active);
  1391. if (should_enable_udc(udc))
  1392. udc_enable(udc);
  1393. if (should_disable_udc(udc))
  1394. udc_disable(udc);
  1395. return 0;
  1396. }
  1397. static void udc_enable(struct pxa_udc *udc);
  1398. static void udc_disable(struct pxa_udc *udc);
  1399. /**
  1400. * pxa_udc_vbus_session - Called by external transceiver to enable/disable udc
  1401. * @_gadget: usb gadget
  1402. * @is_active: 0 if should disable the udc, 1 if should enable
  1403. *
  1404. * Enables the udc, and optionnaly activates D+ pullup resistor. Or disables the
  1405. * udc, and deactivates D+ pullup resistor.
  1406. *
  1407. * Returns 0
  1408. */
  1409. static int pxa_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  1410. {
  1411. struct pxa_udc *udc = to_gadget_udc(_gadget);
  1412. udc->vbus_sensed = is_active;
  1413. if (should_enable_udc(udc))
  1414. udc_enable(udc);
  1415. if (should_disable_udc(udc))
  1416. udc_disable(udc);
  1417. return 0;
  1418. }
  1419. /**
  1420. * pxa_udc_vbus_draw - Called by gadget driver after SET_CONFIGURATION completed
  1421. * @_gadget: usb gadget
  1422. * @mA: current drawn
  1423. *
  1424. * Context: !in_interrupt()
  1425. *
  1426. * Called after a configuration was chosen by a USB host, to inform how much
  1427. * current can be drawn by the device from VBus line.
  1428. *
  1429. * Returns 0 or -EOPNOTSUPP if no transceiver is handling the udc
  1430. */
  1431. static int pxa_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1432. {
  1433. struct pxa_udc *udc;
  1434. udc = to_gadget_udc(_gadget);
  1435. if (udc->transceiver)
  1436. return otg_set_power(udc->transceiver, mA);
  1437. return -EOPNOTSUPP;
  1438. }
  1439. static const struct usb_gadget_ops pxa_udc_ops = {
  1440. .get_frame = pxa_udc_get_frame,
  1441. .wakeup = pxa_udc_wakeup,
  1442. .pullup = pxa_udc_pullup,
  1443. .vbus_session = pxa_udc_vbus_session,
  1444. .vbus_draw = pxa_udc_vbus_draw,
  1445. };
  1446. /**
  1447. * udc_disable - disable udc device controller
  1448. * @udc: udc device
  1449. * Context: any
  1450. *
  1451. * Disables the udc device : disables clocks, udc interrupts, control endpoint
  1452. * interrupts.
  1453. */
  1454. static void udc_disable(struct pxa_udc *udc)
  1455. {
  1456. if (!udc->enabled)
  1457. return;
  1458. udc_writel(udc, UDCICR0, 0);
  1459. udc_writel(udc, UDCICR1, 0);
  1460. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1461. clk_disable(udc->clk);
  1462. ep0_idle(udc);
  1463. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1464. udc->enabled = 0;
  1465. }
  1466. /**
  1467. * udc_init_data - Initialize udc device data structures
  1468. * @dev: udc device
  1469. *
  1470. * Initializes gadget endpoint list, endpoints locks. No action is taken
  1471. * on the hardware.
  1472. */
  1473. static __init void udc_init_data(struct pxa_udc *dev)
  1474. {
  1475. int i;
  1476. struct pxa_ep *ep;
  1477. /* device/ep0 records init */
  1478. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1479. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1480. dev->udc_usb_ep[0].pxa_ep = &dev->pxa_ep[0];
  1481. ep0_idle(dev);
  1482. /* PXA endpoints init */
  1483. for (i = 0; i < NR_PXA_ENDPOINTS; i++) {
  1484. ep = &dev->pxa_ep[i];
  1485. ep->enabled = is_ep0(ep);
  1486. INIT_LIST_HEAD(&ep->queue);
  1487. spin_lock_init(&ep->lock);
  1488. }
  1489. /* USB endpoints init */
  1490. for (i = 1; i < NR_USB_ENDPOINTS; i++)
  1491. list_add_tail(&dev->udc_usb_ep[i].usb_ep.ep_list,
  1492. &dev->gadget.ep_list);
  1493. }
  1494. /**
  1495. * udc_enable - Enables the udc device
  1496. * @dev: udc device
  1497. *
  1498. * Enables the udc device : enables clocks, udc interrupts, control endpoint
  1499. * interrupts, sets usb as UDC client and setups endpoints.
  1500. */
  1501. static void udc_enable(struct pxa_udc *udc)
  1502. {
  1503. if (udc->enabled)
  1504. return;
  1505. udc_writel(udc, UDCICR0, 0);
  1506. udc_writel(udc, UDCICR1, 0);
  1507. udc_clear_mask_UDCCR(udc, UDCCR_UDE);
  1508. clk_enable(udc->clk);
  1509. ep0_idle(udc);
  1510. udc->gadget.speed = USB_SPEED_FULL;
  1511. memset(&udc->stats, 0, sizeof(udc->stats));
  1512. udc_set_mask_UDCCR(udc, UDCCR_UDE);
  1513. udelay(2);
  1514. if (udc_readl(udc, UDCCR) & UDCCR_EMCE)
  1515. dev_err(udc->dev, "Configuration errors, udc disabled\n");
  1516. /*
  1517. * Caller must be able to sleep in order to cope with startup transients
  1518. */
  1519. msleep(100);
  1520. /* enable suspend/resume and reset irqs */
  1521. udc_writel(udc, UDCICR1,
  1522. UDCICR1_IECC | UDCICR1_IERU
  1523. | UDCICR1_IESU | UDCICR1_IERS);
  1524. /* enable ep0 irqs */
  1525. pio_irq_enable(&udc->pxa_ep[0]);
  1526. udc->enabled = 1;
  1527. }
  1528. /**
  1529. * usb_gadget_register_driver - Register gadget driver
  1530. * @driver: gadget driver
  1531. *
  1532. * When a driver is successfully registered, it will receive control requests
  1533. * including set_configuration(), which enables non-control requests. Then
  1534. * usb traffic follows until a disconnect is reported. Then a host may connect
  1535. * again, or the driver might get unbound.
  1536. *
  1537. * Note that the udc is not automatically enabled. Check function
  1538. * should_enable_udc().
  1539. *
  1540. * Returns 0 if no error, -EINVAL, -ENODEV, -EBUSY otherwise
  1541. */
  1542. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1543. {
  1544. struct pxa_udc *udc = the_controller;
  1545. int retval;
  1546. if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind
  1547. || !driver->disconnect || !driver->setup)
  1548. return -EINVAL;
  1549. if (!udc)
  1550. return -ENODEV;
  1551. if (udc->driver)
  1552. return -EBUSY;
  1553. /* first hook up the driver ... */
  1554. udc->driver = driver;
  1555. udc->gadget.dev.driver = &driver->driver;
  1556. dplus_pullup(udc, 1);
  1557. retval = device_add(&udc->gadget.dev);
  1558. if (retval) {
  1559. dev_err(udc->dev, "device_add error %d\n", retval);
  1560. goto add_fail;
  1561. }
  1562. retval = driver->bind(&udc->gadget);
  1563. if (retval) {
  1564. dev_err(udc->dev, "bind to driver %s --> error %d\n",
  1565. driver->driver.name, retval);
  1566. goto bind_fail;
  1567. }
  1568. dev_dbg(udc->dev, "registered gadget driver '%s'\n",
  1569. driver->driver.name);
  1570. if (udc->transceiver) {
  1571. retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
  1572. if (retval) {
  1573. dev_err(udc->dev, "can't bind to transceiver\n");
  1574. goto transceiver_fail;
  1575. }
  1576. }
  1577. if (should_enable_udc(udc))
  1578. udc_enable(udc);
  1579. return 0;
  1580. transceiver_fail:
  1581. if (driver->unbind)
  1582. driver->unbind(&udc->gadget);
  1583. bind_fail:
  1584. device_del(&udc->gadget.dev);
  1585. add_fail:
  1586. udc->driver = NULL;
  1587. udc->gadget.dev.driver = NULL;
  1588. return retval;
  1589. }
  1590. EXPORT_SYMBOL(usb_gadget_register_driver);
  1591. /**
  1592. * stop_activity - Stops udc endpoints
  1593. * @udc: udc device
  1594. * @driver: gadget driver
  1595. *
  1596. * Disables all udc endpoints (even control endpoint), report disconnect to
  1597. * the gadget user.
  1598. */
  1599. static void stop_activity(struct pxa_udc *udc, struct usb_gadget_driver *driver)
  1600. {
  1601. int i;
  1602. /* don't disconnect drivers more than once */
  1603. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  1604. driver = NULL;
  1605. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1606. for (i = 0; i < NR_USB_ENDPOINTS; i++)
  1607. pxa_ep_disable(&udc->udc_usb_ep[i].usb_ep);
  1608. if (driver)
  1609. driver->disconnect(&udc->gadget);
  1610. }
  1611. /**
  1612. * usb_gadget_unregister_driver - Unregister the gadget driver
  1613. * @driver: gadget driver
  1614. *
  1615. * Returns 0 if no error, -ENODEV, -EINVAL otherwise
  1616. */
  1617. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1618. {
  1619. struct pxa_udc *udc = the_controller;
  1620. if (!udc)
  1621. return -ENODEV;
  1622. if (!driver || driver != udc->driver || !driver->unbind)
  1623. return -EINVAL;
  1624. stop_activity(udc, driver);
  1625. udc_disable(udc);
  1626. dplus_pullup(udc, 0);
  1627. driver->unbind(&udc->gadget);
  1628. udc->driver = NULL;
  1629. device_del(&udc->gadget.dev);
  1630. dev_info(udc->dev, "unregistered gadget driver '%s'\n",
  1631. driver->driver.name);
  1632. if (udc->transceiver)
  1633. return otg_set_peripheral(udc->transceiver, NULL);
  1634. return 0;
  1635. }
  1636. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1637. /**
  1638. * handle_ep0_ctrl_req - handle control endpoint control request
  1639. * @udc: udc device
  1640. * @req: control request
  1641. */
  1642. static void handle_ep0_ctrl_req(struct pxa_udc *udc,
  1643. struct pxa27x_request *req)
  1644. {
  1645. struct pxa_ep *ep = &udc->pxa_ep[0];
  1646. union {
  1647. struct usb_ctrlrequest r;
  1648. u32 word[2];
  1649. } u;
  1650. int i;
  1651. int have_extrabytes = 0;
  1652. nuke(ep, -EPROTO);
  1653. /* read SETUP packet */
  1654. for (i = 0; i < 2; i++) {
  1655. if (unlikely(ep_is_empty(ep)))
  1656. goto stall;
  1657. u.word[i] = udc_ep_readl(ep, UDCDR);
  1658. }
  1659. have_extrabytes = !ep_is_empty(ep);
  1660. while (!ep_is_empty(ep)) {
  1661. i = udc_ep_readl(ep, UDCDR);
  1662. ep_err(ep, "wrong to have extra bytes for setup : 0x%08x\n", i);
  1663. }
  1664. ep_dbg(ep, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1665. u.r.bRequestType, u.r.bRequest,
  1666. le16_to_cpu(u.r.wValue), le16_to_cpu(u.r.wIndex),
  1667. le16_to_cpu(u.r.wLength));
  1668. if (unlikely(have_extrabytes))
  1669. goto stall;
  1670. if (u.r.bRequestType & USB_DIR_IN)
  1671. set_ep0state(udc, IN_DATA_STAGE);
  1672. else
  1673. set_ep0state(udc, OUT_DATA_STAGE);
  1674. /* Tell UDC to enter Data Stage */
  1675. udc_ep_writel(ep, UDCCSR, UDCCSR0_SA | UDCCSR0_OPC);
  1676. i = udc->driver->setup(&udc->gadget, &u.r);
  1677. if (i < 0)
  1678. goto stall;
  1679. out:
  1680. return;
  1681. stall:
  1682. ep_dbg(ep, "protocol STALL, udccsr0=%03x err %d\n",
  1683. udc_ep_readl(ep, UDCCSR), i);
  1684. udc_ep_writel(ep, UDCCSR, UDCCSR0_FST | UDCCSR0_FTF);
  1685. set_ep0state(udc, STALL);
  1686. goto out;
  1687. }
  1688. /**
  1689. * handle_ep0 - Handle control endpoint data transfers
  1690. * @udc: udc device
  1691. * @fifo_irq: 1 if triggered by fifo service type irq
  1692. * @opc_irq: 1 if triggered by output packet complete type irq
  1693. *
  1694. * Context : when in_interrupt() or with ep->lock held
  1695. *
  1696. * Tries to transfer all pending request data into the endpoint and/or
  1697. * transfer all pending data in the endpoint into usb requests.
  1698. * Handles states of ep0 automata.
  1699. *
  1700. * PXA27x hardware handles several standard usb control requests without
  1701. * driver notification. The requests fully handled by hardware are :
  1702. * SET_ADDRESS, SET_FEATURE, CLEAR_FEATURE, GET_CONFIGURATION, GET_INTERFACE,
  1703. * GET_STATUS
  1704. * The requests handled by hardware, but with irq notification are :
  1705. * SYNCH_FRAME, SET_CONFIGURATION, SET_INTERFACE
  1706. * The remaining standard requests really handled by handle_ep0 are :
  1707. * GET_DESCRIPTOR, SET_DESCRIPTOR, specific requests.
  1708. * Requests standardized outside of USB 2.0 chapter 9 are handled more
  1709. * uniformly, by gadget drivers.
  1710. *
  1711. * The control endpoint state machine is _not_ USB spec compliant, it's even
  1712. * hardly compliant with Intel PXA270 developers guide.
  1713. * The key points which inferred this state machine are :
  1714. * - on every setup token, bit UDCCSR0_SA is raised and held until cleared by
  1715. * software.
  1716. * - on every OUT packet received, UDCCSR0_OPC is raised and held until
  1717. * cleared by software.
  1718. * - clearing UDCCSR0_OPC always flushes ep0. If in setup stage, never do it
  1719. * before reading ep0.
  1720. * - irq can be called on a "packet complete" event (opc_irq=1), while
  1721. * UDCCSR0_OPC is not yet raised (delta can be as big as 100ms
  1722. * from experimentation).
  1723. * - as UDCCSR0_SA can be activated while in irq handling, and clearing
  1724. * UDCCSR0_OPC would flush the setup data, we almost never clear UDCCSR0_OPC
  1725. * => we never actually read the "status stage" packet of an IN data stage
  1726. * => this is not documented in Intel documentation
  1727. * - hardware as no idea of STATUS STAGE, it only handle SETUP STAGE and DATA
  1728. * STAGE. The driver add STATUS STAGE to send last zero length packet in
  1729. * OUT_STATUS_STAGE.
  1730. * - special attention was needed for IN_STATUS_STAGE. If a packet complete
  1731. * event is detected, we terminate the status stage without ackowledging the
  1732. * packet (not to risk to loose a potential SETUP packet)
  1733. */
  1734. static void handle_ep0(struct pxa_udc *udc, int fifo_irq, int opc_irq)
  1735. {
  1736. u32 udccsr0;
  1737. struct pxa_ep *ep = &udc->pxa_ep[0];
  1738. struct pxa27x_request *req = NULL;
  1739. int completed = 0;
  1740. if (!list_empty(&ep->queue))
  1741. req = list_entry(ep->queue.next, struct pxa27x_request, queue);
  1742. udccsr0 = udc_ep_readl(ep, UDCCSR);
  1743. ep_dbg(ep, "state=%s, req=%p, udccsr0=0x%03x, udcbcr=%d, irq_msk=%x\n",
  1744. EP0_STNAME(udc), req, udccsr0, udc_ep_readl(ep, UDCBCR),
  1745. (fifo_irq << 1 | opc_irq));
  1746. if (udccsr0 & UDCCSR0_SST) {
  1747. ep_dbg(ep, "clearing stall status\n");
  1748. nuke(ep, -EPIPE);
  1749. udc_ep_writel(ep, UDCCSR, UDCCSR0_SST);
  1750. ep0_idle(udc);
  1751. }
  1752. if (udccsr0 & UDCCSR0_SA) {
  1753. nuke(ep, 0);
  1754. set_ep0state(udc, SETUP_STAGE);
  1755. }
  1756. switch (udc->ep0state) {
  1757. case WAIT_FOR_SETUP:
  1758. /*
  1759. * Hardware bug : beware, we cannot clear OPC, since we would
  1760. * miss a potential OPC irq for a setup packet.
  1761. * So, we only do ... nothing, and hope for a next irq with
  1762. * UDCCSR0_SA set.
  1763. */
  1764. break;
  1765. case SETUP_STAGE:
  1766. udccsr0 &= UDCCSR0_CTRL_REQ_MASK;
  1767. if (likely(udccsr0 == UDCCSR0_CTRL_REQ_MASK))
  1768. handle_ep0_ctrl_req(udc, req);
  1769. break;
  1770. case IN_DATA_STAGE: /* GET_DESCRIPTOR */
  1771. if (epout_has_pkt(ep))
  1772. udc_ep_writel(ep, UDCCSR, UDCCSR0_OPC);
  1773. if (req && !ep_is_full(ep))
  1774. completed = write_ep0_fifo(ep, req);
  1775. if (completed)
  1776. ep0_end_in_req(ep, req);
  1777. break;
  1778. case OUT_DATA_STAGE: /* SET_DESCRIPTOR */
  1779. if (epout_has_pkt(ep) && req)
  1780. completed = read_ep0_fifo(ep, req);
  1781. if (completed)
  1782. ep0_end_out_req(ep, req);
  1783. break;
  1784. case STALL:
  1785. udc_ep_writel(ep, UDCCSR, UDCCSR0_FST);
  1786. break;
  1787. case IN_STATUS_STAGE:
  1788. /*
  1789. * Hardware bug : beware, we cannot clear OPC, since we would
  1790. * miss a potential PC irq for a setup packet.
  1791. * So, we only put the ep0 into WAIT_FOR_SETUP state.
  1792. */
  1793. if (opc_irq)
  1794. ep0_idle(udc);
  1795. break;
  1796. case OUT_STATUS_STAGE:
  1797. case WAIT_ACK_SET_CONF_INTERF:
  1798. ep_warn(ep, "should never get in %s state here!!!\n",
  1799. EP0_STNAME(ep->dev));
  1800. ep0_idle(udc);
  1801. break;
  1802. }
  1803. }
  1804. /**
  1805. * handle_ep - Handle endpoint data tranfers
  1806. * @ep: pxa physical endpoint
  1807. *
  1808. * Tries to transfer all pending request data into the endpoint and/or
  1809. * transfer all pending data in the endpoint into usb requests.
  1810. *
  1811. * Is always called when in_interrupt() or with ep->lock held.
  1812. */
  1813. static void handle_ep(struct pxa_ep *ep)
  1814. {
  1815. struct pxa27x_request *req;
  1816. int completed;
  1817. u32 udccsr;
  1818. int is_in = ep->dir_in;
  1819. int loop = 0;
  1820. do {
  1821. completed = 0;
  1822. udccsr = udc_ep_readl(ep, UDCCSR);
  1823. if (likely(!list_empty(&ep->queue)))
  1824. req = list_entry(ep->queue.next,
  1825. struct pxa27x_request, queue);
  1826. else
  1827. req = NULL;
  1828. ep_dbg(ep, "req:%p, udccsr 0x%03x loop=%d\n",
  1829. req, udccsr, loop++);
  1830. if (unlikely(udccsr & (UDCCSR_SST | UDCCSR_TRN)))
  1831. udc_ep_writel(ep, UDCCSR,
  1832. udccsr & (UDCCSR_SST | UDCCSR_TRN));
  1833. if (!req)
  1834. break;
  1835. if (unlikely(is_in)) {
  1836. if (likely(!ep_is_full(ep)))
  1837. completed = write_fifo(ep, req);
  1838. if (completed)
  1839. ep_end_in_req(ep, req);
  1840. } else {
  1841. if (likely(epout_has_pkt(ep)))
  1842. completed = read_fifo(ep, req);
  1843. if (completed)
  1844. ep_end_out_req(ep, req);
  1845. }
  1846. } while (completed);
  1847. }
  1848. /**
  1849. * pxa27x_change_configuration - Handle SET_CONF usb request notification
  1850. * @udc: udc device
  1851. * @config: usb configuration
  1852. *
  1853. * Post the request to upper level.
  1854. * Don't use any pxa specific harware configuration capabilities
  1855. */
  1856. static void pxa27x_change_configuration(struct pxa_udc *udc, int config)
  1857. {
  1858. struct usb_ctrlrequest req ;
  1859. dev_dbg(udc->dev, "config=%d\n", config);
  1860. udc->config = config;
  1861. udc->last_interface = 0;
  1862. udc->last_alternate = 0;
  1863. req.bRequestType = 0;
  1864. req.bRequest = USB_REQ_SET_CONFIGURATION;
  1865. req.wValue = config;
  1866. req.wIndex = 0;
  1867. req.wLength = 0;
  1868. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1869. udc->driver->setup(&udc->gadget, &req);
  1870. }
  1871. /**
  1872. * pxa27x_change_interface - Handle SET_INTERF usb request notification
  1873. * @udc: udc device
  1874. * @iface: interface number
  1875. * @alt: alternate setting number
  1876. *
  1877. * Post the request to upper level.
  1878. * Don't use any pxa specific harware configuration capabilities
  1879. */
  1880. static void pxa27x_change_interface(struct pxa_udc *udc, int iface, int alt)
  1881. {
  1882. struct usb_ctrlrequest req;
  1883. dev_dbg(udc->dev, "interface=%d, alternate setting=%d\n", iface, alt);
  1884. udc->last_interface = iface;
  1885. udc->last_alternate = alt;
  1886. req.bRequestType = USB_RECIP_INTERFACE;
  1887. req.bRequest = USB_REQ_SET_INTERFACE;
  1888. req.wValue = alt;
  1889. req.wIndex = iface;
  1890. req.wLength = 0;
  1891. set_ep0state(udc, WAIT_ACK_SET_CONF_INTERF);
  1892. udc->driver->setup(&udc->gadget, &req);
  1893. }
  1894. /*
  1895. * irq_handle_data - Handle data transfer
  1896. * @irq: irq IRQ number
  1897. * @udc: dev pxa_udc device structure
  1898. *
  1899. * Called from irq handler, transferts data to or from endpoint to queue
  1900. */
  1901. static void irq_handle_data(int irq, struct pxa_udc *udc)
  1902. {
  1903. int i;
  1904. struct pxa_ep *ep;
  1905. u32 udcisr0 = udc_readl(udc, UDCISR0) & UDCCISR0_EP_MASK;
  1906. u32 udcisr1 = udc_readl(udc, UDCISR1) & UDCCISR1_EP_MASK;
  1907. if (udcisr0 & UDCISR_INT_MASK) {
  1908. udc->pxa_ep[0].stats.irqs++;
  1909. udc_writel(udc, UDCISR0, UDCISR_INT(0, UDCISR_INT_MASK));
  1910. handle_ep0(udc, !!(udcisr0 & UDCICR_FIFOERR),
  1911. !!(udcisr0 & UDCICR_PKTCOMPL));
  1912. }
  1913. udcisr0 >>= 2;
  1914. for (i = 1; udcisr0 != 0 && i < 16; udcisr0 >>= 2, i++) {
  1915. if (!(udcisr0 & UDCISR_INT_MASK))
  1916. continue;
  1917. udc_writel(udc, UDCISR0, UDCISR_INT(i, UDCISR_INT_MASK));
  1918. ep = &udc->pxa_ep[i];
  1919. ep->stats.irqs++;
  1920. handle_ep(ep);
  1921. }
  1922. for (i = 16; udcisr1 != 0 && i < 24; udcisr1 >>= 2, i++) {
  1923. udc_writel(udc, UDCISR1, UDCISR_INT(i - 16, UDCISR_INT_MASK));
  1924. if (!(udcisr1 & UDCISR_INT_MASK))
  1925. continue;
  1926. ep = &udc->pxa_ep[i];
  1927. ep->stats.irqs++;
  1928. handle_ep(ep);
  1929. }
  1930. }
  1931. /**
  1932. * irq_udc_suspend - Handle IRQ "UDC Suspend"
  1933. * @udc: udc device
  1934. */
  1935. static void irq_udc_suspend(struct pxa_udc *udc)
  1936. {
  1937. udc_writel(udc, UDCISR1, UDCISR1_IRSU);
  1938. udc->stats.irqs_suspend++;
  1939. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1940. && udc->driver && udc->driver->suspend)
  1941. udc->driver->suspend(&udc->gadget);
  1942. ep0_idle(udc);
  1943. }
  1944. /**
  1945. * irq_udc_resume - Handle IRQ "UDC Resume"
  1946. * @udc: udc device
  1947. */
  1948. static void irq_udc_resume(struct pxa_udc *udc)
  1949. {
  1950. udc_writel(udc, UDCISR1, UDCISR1_IRRU);
  1951. udc->stats.irqs_resume++;
  1952. if (udc->gadget.speed != USB_SPEED_UNKNOWN
  1953. && udc->driver && udc->driver->resume)
  1954. udc->driver->resume(&udc->gadget);
  1955. }
  1956. /**
  1957. * irq_udc_reconfig - Handle IRQ "UDC Change Configuration"
  1958. * @udc: udc device
  1959. */
  1960. static void irq_udc_reconfig(struct pxa_udc *udc)
  1961. {
  1962. unsigned config, interface, alternate, config_change;
  1963. u32 udccr = udc_readl(udc, UDCCR);
  1964. udc_writel(udc, UDCISR1, UDCISR1_IRCC);
  1965. udc->stats.irqs_reconfig++;
  1966. config = (udccr & UDCCR_ACN) >> UDCCR_ACN_S;
  1967. config_change = (config != udc->config);
  1968. pxa27x_change_configuration(udc, config);
  1969. interface = (udccr & UDCCR_AIN) >> UDCCR_AIN_S;
  1970. alternate = (udccr & UDCCR_AAISN) >> UDCCR_AAISN_S;
  1971. pxa27x_change_interface(udc, interface, alternate);
  1972. if (config_change)
  1973. update_pxa_ep_matches(udc);
  1974. udc_set_mask_UDCCR(udc, UDCCR_SMAC);
  1975. }
  1976. /**
  1977. * irq_udc_reset - Handle IRQ "UDC Reset"
  1978. * @udc: udc device
  1979. */
  1980. static void irq_udc_reset(struct pxa_udc *udc)
  1981. {
  1982. u32 udccr = udc_readl(udc, UDCCR);
  1983. struct pxa_ep *ep = &udc->pxa_ep[0];
  1984. dev_info(udc->dev, "USB reset\n");
  1985. udc_writel(udc, UDCISR1, UDCISR1_IRRS);
  1986. udc->stats.irqs_reset++;
  1987. if ((udccr & UDCCR_UDA) == 0) {
  1988. dev_dbg(udc->dev, "USB reset start\n");
  1989. stop_activity(udc, udc->driver);
  1990. }
  1991. udc->gadget.speed = USB_SPEED_FULL;
  1992. memset(&udc->stats, 0, sizeof udc->stats);
  1993. nuke(ep, -EPROTO);
  1994. udc_ep_writel(ep, UDCCSR, UDCCSR0_FTF | UDCCSR0_OPC);
  1995. ep0_idle(udc);
  1996. }
  1997. /**
  1998. * pxa_udc_irq - Main irq handler
  1999. * @irq: irq number
  2000. * @_dev: udc device
  2001. *
  2002. * Handles all udc interrupts
  2003. */
  2004. static irqreturn_t pxa_udc_irq(int irq, void *_dev)
  2005. {
  2006. struct pxa_udc *udc = _dev;
  2007. u32 udcisr0 = udc_readl(udc, UDCISR0);
  2008. u32 udcisr1 = udc_readl(udc, UDCISR1);
  2009. u32 udccr = udc_readl(udc, UDCCR);
  2010. u32 udcisr1_spec;
  2011. dev_vdbg(udc->dev, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
  2012. "UDCCR:0x%08x\n", udcisr0, udcisr1, udccr);
  2013. udcisr1_spec = udcisr1 & 0xf8000000;
  2014. if (unlikely(udcisr1_spec & UDCISR1_IRSU))
  2015. irq_udc_suspend(udc);
  2016. if (unlikely(udcisr1_spec & UDCISR1_IRRU))
  2017. irq_udc_resume(udc);
  2018. if (unlikely(udcisr1_spec & UDCISR1_IRCC))
  2019. irq_udc_reconfig(udc);
  2020. if (unlikely(udcisr1_spec & UDCISR1_IRRS))
  2021. irq_udc_reset(udc);
  2022. if ((udcisr0 & UDCCISR0_EP_MASK) | (udcisr1 & UDCCISR1_EP_MASK))
  2023. irq_handle_data(irq, udc);
  2024. return IRQ_HANDLED;
  2025. }
  2026. static struct pxa_udc memory = {
  2027. .gadget = {
  2028. .ops = &pxa_udc_ops,
  2029. .ep0 = &memory.udc_usb_ep[0].usb_ep,
  2030. .name = driver_name,
  2031. .dev = {
  2032. .init_name = "gadget",
  2033. },
  2034. },
  2035. .udc_usb_ep = {
  2036. USB_EP_CTRL,
  2037. USB_EP_OUT_BULK(1),
  2038. USB_EP_IN_BULK(2),
  2039. USB_EP_IN_ISO(3),
  2040. USB_EP_OUT_ISO(4),
  2041. USB_EP_IN_INT(5),
  2042. },
  2043. .pxa_ep = {
  2044. PXA_EP_CTRL,
  2045. /* Endpoints for gadget zero */
  2046. PXA_EP_OUT_BULK(1, 1, 3, 0, 0),
  2047. PXA_EP_IN_BULK(2, 2, 3, 0, 0),
  2048. /* Endpoints for ether gadget, file storage gadget */
  2049. PXA_EP_OUT_BULK(3, 1, 1, 0, 0),
  2050. PXA_EP_IN_BULK(4, 2, 1, 0, 0),
  2051. PXA_EP_IN_ISO(5, 3, 1, 0, 0),
  2052. PXA_EP_OUT_ISO(6, 4, 1, 0, 0),
  2053. PXA_EP_IN_INT(7, 5, 1, 0, 0),
  2054. /* Endpoints for RNDIS, serial */
  2055. PXA_EP_OUT_BULK(8, 1, 2, 0, 0),
  2056. PXA_EP_IN_BULK(9, 2, 2, 0, 0),
  2057. PXA_EP_IN_INT(10, 5, 2, 0, 0),
  2058. /*
  2059. * All the following endpoints are only for completion. They
  2060. * won't never work, as multiple interfaces are really broken on
  2061. * the pxa.
  2062. */
  2063. PXA_EP_OUT_BULK(11, 1, 2, 1, 0),
  2064. PXA_EP_IN_BULK(12, 2, 2, 1, 0),
  2065. /* Endpoint for CDC Ether */
  2066. PXA_EP_OUT_BULK(13, 1, 1, 1, 1),
  2067. PXA_EP_IN_BULK(14, 2, 1, 1, 1),
  2068. }
  2069. };
  2070. /**
  2071. * pxa_udc_probe - probes the udc device
  2072. * @_dev: platform device
  2073. *
  2074. * Perform basic init : allocates udc clock, creates sysfs files, requests
  2075. * irq.
  2076. */
  2077. static int __init pxa_udc_probe(struct platform_device *pdev)
  2078. {
  2079. struct resource *regs;
  2080. struct pxa_udc *udc = &memory;
  2081. int retval = 0, gpio;
  2082. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2083. if (!regs)
  2084. return -ENXIO;
  2085. udc->irq = platform_get_irq(pdev, 0);
  2086. if (udc->irq < 0)
  2087. return udc->irq;
  2088. udc->dev = &pdev->dev;
  2089. udc->mach = pdev->dev.platform_data;
  2090. udc->transceiver = otg_get_transceiver();
  2091. gpio = udc->mach->gpio_pullup;
  2092. if (gpio_is_valid(gpio)) {
  2093. retval = gpio_request(gpio, "USB D+ pullup");
  2094. if (retval == 0)
  2095. gpio_direction_output(gpio,
  2096. udc->mach->gpio_pullup_inverted);
  2097. }
  2098. if (retval) {
  2099. dev_err(&pdev->dev, "Couldn't request gpio %d : %d\n",
  2100. gpio, retval);
  2101. return retval;
  2102. }
  2103. udc->clk = clk_get(&pdev->dev, NULL);
  2104. if (IS_ERR(udc->clk)) {
  2105. retval = PTR_ERR(udc->clk);
  2106. goto err_clk;
  2107. }
  2108. retval = -ENOMEM;
  2109. udc->regs = ioremap(regs->start, regs->end - regs->start + 1);
  2110. if (!udc->regs) {
  2111. dev_err(&pdev->dev, "Unable to map UDC I/O memory\n");
  2112. goto err_map;
  2113. }
  2114. device_initialize(&udc->gadget.dev);
  2115. udc->gadget.dev.parent = &pdev->dev;
  2116. udc->gadget.dev.dma_mask = NULL;
  2117. udc->vbus_sensed = 0;
  2118. the_controller = udc;
  2119. platform_set_drvdata(pdev, udc);
  2120. udc_init_data(udc);
  2121. pxa_eps_setup(udc);
  2122. /* irq setup after old hardware state is cleaned up */
  2123. retval = request_irq(udc->irq, pxa_udc_irq,
  2124. IRQF_SHARED, driver_name, udc);
  2125. if (retval != 0) {
  2126. dev_err(udc->dev, "%s: can't get irq %i, err %d\n",
  2127. driver_name, IRQ_USB, retval);
  2128. goto err_irq;
  2129. }
  2130. pxa_init_debugfs(udc);
  2131. return 0;
  2132. err_irq:
  2133. iounmap(udc->regs);
  2134. err_map:
  2135. clk_put(udc->clk);
  2136. udc->clk = NULL;
  2137. err_clk:
  2138. return retval;
  2139. }
  2140. /**
  2141. * pxa_udc_remove - removes the udc device driver
  2142. * @_dev: platform device
  2143. */
  2144. static int __exit pxa_udc_remove(struct platform_device *_dev)
  2145. {
  2146. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2147. int gpio = udc->mach->gpio_pullup;
  2148. usb_gadget_unregister_driver(udc->driver);
  2149. free_irq(udc->irq, udc);
  2150. pxa_cleanup_debugfs(udc);
  2151. if (gpio_is_valid(gpio))
  2152. gpio_free(gpio);
  2153. otg_put_transceiver(udc->transceiver);
  2154. udc->transceiver = NULL;
  2155. platform_set_drvdata(_dev, NULL);
  2156. the_controller = NULL;
  2157. clk_put(udc->clk);
  2158. iounmap(udc->regs);
  2159. return 0;
  2160. }
  2161. static void pxa_udc_shutdown(struct platform_device *_dev)
  2162. {
  2163. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2164. if (udc_readl(udc, UDCCR) & UDCCR_UDE)
  2165. udc_disable(udc);
  2166. }
  2167. #ifdef CONFIG_PM
  2168. /**
  2169. * pxa_udc_suspend - Suspend udc device
  2170. * @_dev: platform device
  2171. * @state: suspend state
  2172. *
  2173. * Suspends udc : saves configuration registers (UDCCR*), then disables the udc
  2174. * device.
  2175. */
  2176. static int pxa_udc_suspend(struct platform_device *_dev, pm_message_t state)
  2177. {
  2178. int i;
  2179. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2180. struct pxa_ep *ep;
  2181. ep = &udc->pxa_ep[0];
  2182. udc->udccsr0 = udc_ep_readl(ep, UDCCSR);
  2183. for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
  2184. ep = &udc->pxa_ep[i];
  2185. ep->udccsr_value = udc_ep_readl(ep, UDCCSR);
  2186. ep->udccr_value = udc_ep_readl(ep, UDCCR);
  2187. ep_dbg(ep, "udccsr:0x%03x, udccr:0x%x\n",
  2188. ep->udccsr_value, ep->udccr_value);
  2189. }
  2190. udc_disable(udc);
  2191. udc->pullup_resume = udc->pullup_on;
  2192. dplus_pullup(udc, 0);
  2193. return 0;
  2194. }
  2195. /**
  2196. * pxa_udc_resume - Resume udc device
  2197. * @_dev: platform device
  2198. *
  2199. * Resumes udc : restores configuration registers (UDCCR*), then enables the udc
  2200. * device.
  2201. */
  2202. static int pxa_udc_resume(struct platform_device *_dev)
  2203. {
  2204. int i;
  2205. struct pxa_udc *udc = platform_get_drvdata(_dev);
  2206. struct pxa_ep *ep;
  2207. ep = &udc->pxa_ep[0];
  2208. udc_ep_writel(ep, UDCCSR, udc->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME));
  2209. for (i = 1; i < NR_PXA_ENDPOINTS; i++) {
  2210. ep = &udc->pxa_ep[i];
  2211. udc_ep_writel(ep, UDCCSR, ep->udccsr_value);
  2212. udc_ep_writel(ep, UDCCR, ep->udccr_value);
  2213. ep_dbg(ep, "udccsr:0x%03x, udccr:0x%x\n",
  2214. ep->udccsr_value, ep->udccr_value);
  2215. }
  2216. dplus_pullup(udc, udc->pullup_resume);
  2217. if (should_enable_udc(udc))
  2218. udc_enable(udc);
  2219. /*
  2220. * We do not handle OTG yet.
  2221. *
  2222. * OTGPH bit is set when sleep mode is entered.
  2223. * it indicates that OTG pad is retaining its state.
  2224. * Upon exit from sleep mode and before clearing OTGPH,
  2225. * Software must configure the USB OTG pad, UDC, and UHC
  2226. * to the state they were in before entering sleep mode.
  2227. */
  2228. if (cpu_is_pxa27x())
  2229. PSSR |= PSSR_OTGPH;
  2230. return 0;
  2231. }
  2232. #endif
  2233. /* work with hotplug and coldplug */
  2234. MODULE_ALIAS("platform:pxa27x-udc");
  2235. static struct platform_driver udc_driver = {
  2236. .driver = {
  2237. .name = "pxa27x-udc",
  2238. .owner = THIS_MODULE,
  2239. },
  2240. .remove = __exit_p(pxa_udc_remove),
  2241. .shutdown = pxa_udc_shutdown,
  2242. #ifdef CONFIG_PM
  2243. .suspend = pxa_udc_suspend,
  2244. .resume = pxa_udc_resume
  2245. #endif
  2246. };
  2247. static int __init udc_init(void)
  2248. {
  2249. if (!cpu_is_pxa27x())
  2250. return -ENODEV;
  2251. printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
  2252. return platform_driver_probe(&udc_driver, pxa_udc_probe);
  2253. }
  2254. module_init(udc_init);
  2255. static void __exit udc_exit(void)
  2256. {
  2257. platform_driver_unregister(&udc_driver);
  2258. }
  2259. module_exit(udc_exit);
  2260. MODULE_DESCRIPTION(DRIVER_DESC);
  2261. MODULE_AUTHOR("Robert Jarzmik");
  2262. MODULE_LICENSE("GPL");