ci13xxx_udc.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. /*
  2. * ci13xxx_udc.c - MIPS USB IP core family device controller
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * Description: MIPS USB IP core family device controller
  14. * Currently it only supports IP part number CI13412
  15. *
  16. * This driver is composed of several blocks:
  17. * - HW: hardware interface
  18. * - DBG: debug facilities (optional)
  19. * - UTIL: utilities
  20. * - ISR: interrupts handling
  21. * - ENDPT: endpoint operations (Gadget API)
  22. * - GADGET: gadget operations (Gadget API)
  23. * - BUS: bus glue code, bus abstraction layer
  24. *
  25. * Compile Options
  26. * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
  27. * - STALL_IN: non-empty bulk-in pipes cannot be halted
  28. * if defined mass storage compliance succeeds but with warnings
  29. * => case 4: Hi > Dn
  30. * => case 5: Hi > Di
  31. * => case 8: Hi <> Do
  32. * if undefined usbtest 13 fails
  33. * - TRACE: enable function tracing (depends on DEBUG)
  34. *
  35. * Main Features
  36. * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
  37. * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
  38. * - Normal & LPM support
  39. *
  40. * USBTEST Report
  41. * - OK: 0-12, 13 (STALL_IN defined) & 14
  42. * - Not Supported: 15 & 16 (ISO)
  43. *
  44. * TODO List
  45. * - OTG
  46. * - Isochronous & Interrupt Traffic
  47. * - Handle requests which spawns into several TDs
  48. * - GET_STATUS(device) - always reports 0
  49. * - Gadget API (majority of optional features)
  50. * - Suspend & Remote Wakeup
  51. */
  52. #include <linux/delay.h>
  53. #include <linux/device.h>
  54. #include <linux/dmapool.h>
  55. #include <linux/dma-mapping.h>
  56. #include <linux/init.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/io.h>
  59. #include <linux/irq.h>
  60. #include <linux/kernel.h>
  61. #include <linux/slab.h>
  62. #include <linux/pm_runtime.h>
  63. #include <linux/usb/ch9.h>
  64. #include <linux/usb/gadget.h>
  65. #include <linux/usb/otg.h>
  66. #include "ci13xxx_udc.h"
  67. /******************************************************************************
  68. * DEFINE
  69. *****************************************************************************/
  70. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  71. /* ctrl register bank access */
  72. static DEFINE_SPINLOCK(udc_lock);
  73. /* control endpoint description */
  74. static const struct usb_endpoint_descriptor
  75. ctrl_endpt_out_desc = {
  76. .bLength = USB_DT_ENDPOINT_SIZE,
  77. .bDescriptorType = USB_DT_ENDPOINT,
  78. .bEndpointAddress = USB_DIR_OUT,
  79. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  80. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  81. };
  82. static const struct usb_endpoint_descriptor
  83. ctrl_endpt_in_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. .bEndpointAddress = USB_DIR_IN,
  87. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  88. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  89. };
  90. /* UDC descriptor */
  91. static struct ci13xxx *_udc;
  92. /* Interrupt statistics */
  93. #define ISR_MASK 0x1F
  94. static struct {
  95. u32 test;
  96. u32 ui;
  97. u32 uei;
  98. u32 pci;
  99. u32 uri;
  100. u32 sli;
  101. u32 none;
  102. struct {
  103. u32 cnt;
  104. u32 buf[ISR_MASK+1];
  105. u32 idx;
  106. } hndl;
  107. } isr_statistics;
  108. /**
  109. * ffs_nr: find first (least significant) bit set
  110. * @x: the word to search
  111. *
  112. * This function returns bit number (instead of position)
  113. */
  114. static int ffs_nr(u32 x)
  115. {
  116. int n = ffs(x);
  117. return n ? n-1 : 32;
  118. }
  119. /******************************************************************************
  120. * HW block
  121. *****************************************************************************/
  122. /* register bank descriptor */
  123. static struct {
  124. unsigned lpm; /* is LPM? */
  125. void __iomem *abs; /* bus map offset */
  126. void __iomem *cap; /* bus map offset + CAP offset + CAP data */
  127. size_t size; /* bank size */
  128. } hw_bank;
  129. /* MSM specific */
  130. #define ABS_AHBBURST (0x0090UL)
  131. #define ABS_AHBMODE (0x0098UL)
  132. /* UDC register map */
  133. #define ABS_CAPLENGTH (0x100UL)
  134. #define ABS_HCCPARAMS (0x108UL)
  135. #define ABS_DCCPARAMS (0x124UL)
  136. #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
  137. /* offset to CAPLENTGH (addr + data) */
  138. #define CAP_USBCMD (0x000UL)
  139. #define CAP_USBSTS (0x004UL)
  140. #define CAP_USBINTR (0x008UL)
  141. #define CAP_DEVICEADDR (0x014UL)
  142. #define CAP_ENDPTLISTADDR (0x018UL)
  143. #define CAP_PORTSC (0x044UL)
  144. #define CAP_DEVLC (0x084UL)
  145. #define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
  146. #define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
  147. #define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
  148. #define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
  149. #define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
  150. #define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
  151. #define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
  152. #define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
  153. /* maximum number of enpoints: valid only after hw_device_reset() */
  154. static unsigned hw_ep_max;
  155. /**
  156. * hw_ep_bit: calculates the bit number
  157. * @num: endpoint number
  158. * @dir: endpoint direction
  159. *
  160. * This function returns bit number
  161. */
  162. static inline int hw_ep_bit(int num, int dir)
  163. {
  164. return num + (dir ? 16 : 0);
  165. }
  166. static int ep_to_bit(int n)
  167. {
  168. int fill = 16 - hw_ep_max / 2;
  169. if (n >= hw_ep_max / 2)
  170. n += fill;
  171. return n;
  172. }
  173. /**
  174. * hw_aread: reads from register bitfield
  175. * @addr: address relative to bus map
  176. * @mask: bitfield mask
  177. *
  178. * This function returns register bitfield data
  179. */
  180. static u32 hw_aread(u32 addr, u32 mask)
  181. {
  182. return ioread32(addr + hw_bank.abs) & mask;
  183. }
  184. /**
  185. * hw_awrite: writes to register bitfield
  186. * @addr: address relative to bus map
  187. * @mask: bitfield mask
  188. * @data: new data
  189. */
  190. static void hw_awrite(u32 addr, u32 mask, u32 data)
  191. {
  192. iowrite32(hw_aread(addr, ~mask) | (data & mask),
  193. addr + hw_bank.abs);
  194. }
  195. /**
  196. * hw_cread: reads from register bitfield
  197. * @addr: address relative to CAP offset plus content
  198. * @mask: bitfield mask
  199. *
  200. * This function returns register bitfield data
  201. */
  202. static u32 hw_cread(u32 addr, u32 mask)
  203. {
  204. return ioread32(addr + hw_bank.cap) & mask;
  205. }
  206. /**
  207. * hw_cwrite: writes to register bitfield
  208. * @addr: address relative to CAP offset plus content
  209. * @mask: bitfield mask
  210. * @data: new data
  211. */
  212. static void hw_cwrite(u32 addr, u32 mask, u32 data)
  213. {
  214. iowrite32(hw_cread(addr, ~mask) | (data & mask),
  215. addr + hw_bank.cap);
  216. }
  217. /**
  218. * hw_ctest_and_clear: tests & clears register bitfield
  219. * @addr: address relative to CAP offset plus content
  220. * @mask: bitfield mask
  221. *
  222. * This function returns register bitfield data
  223. */
  224. static u32 hw_ctest_and_clear(u32 addr, u32 mask)
  225. {
  226. u32 reg = hw_cread(addr, mask);
  227. iowrite32(reg, addr + hw_bank.cap);
  228. return reg;
  229. }
  230. /**
  231. * hw_ctest_and_write: tests & writes register bitfield
  232. * @addr: address relative to CAP offset plus content
  233. * @mask: bitfield mask
  234. * @data: new data
  235. *
  236. * This function returns register bitfield data
  237. */
  238. static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
  239. {
  240. u32 reg = hw_cread(addr, ~0);
  241. iowrite32((reg & ~mask) | (data & mask), addr + hw_bank.cap);
  242. return (reg & mask) >> ffs_nr(mask);
  243. }
  244. static int hw_device_init(void __iomem *base)
  245. {
  246. u32 reg;
  247. /* bank is a module variable */
  248. hw_bank.abs = base;
  249. hw_bank.cap = hw_bank.abs;
  250. hw_bank.cap += ABS_CAPLENGTH;
  251. hw_bank.cap += ioread8(hw_bank.cap);
  252. reg = hw_aread(ABS_HCCPARAMS, HCCPARAMS_LEN) >> ffs_nr(HCCPARAMS_LEN);
  253. hw_bank.lpm = reg;
  254. hw_bank.size = hw_bank.cap - hw_bank.abs;
  255. hw_bank.size += CAP_LAST;
  256. hw_bank.size /= sizeof(u32);
  257. reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
  258. hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  259. if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX)
  260. return -ENODEV;
  261. /* setup lock mode ? */
  262. /* ENDPTSETUPSTAT is '0' by default */
  263. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  264. return 0;
  265. }
  266. /**
  267. * hw_device_reset: resets chip (execute without interruption)
  268. * @base: register base address
  269. *
  270. * This function returns an error code
  271. */
  272. static int hw_device_reset(struct ci13xxx *udc)
  273. {
  274. /* should flush & stop before reset */
  275. hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
  276. hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
  277. hw_cwrite(CAP_USBCMD, USBCMD_RST, USBCMD_RST);
  278. while (hw_cread(CAP_USBCMD, USBCMD_RST))
  279. udelay(10); /* not RTOS friendly */
  280. if (udc->udc_driver->notify_event)
  281. udc->udc_driver->notify_event(udc,
  282. CI13XXX_CONTROLLER_RESET_EVENT);
  283. if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
  284. hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
  285. /* USBMODE should be configured step by step */
  286. hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  287. hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
  288. hw_cwrite(CAP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); /* HW >= 2.3 */
  289. if (hw_cread(CAP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
  290. pr_err("cannot enter in device mode");
  291. pr_err("lpm = %i", hw_bank.lpm);
  292. return -ENODEV;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * hw_device_state: enables/disables interrupts & starts/stops device (execute
  298. * without interruption)
  299. * @dma: 0 => disable, !0 => enable and set dma engine
  300. *
  301. * This function returns an error code
  302. */
  303. static int hw_device_state(u32 dma)
  304. {
  305. if (dma) {
  306. hw_cwrite(CAP_ENDPTLISTADDR, ~0, dma);
  307. /* interrupt, error, port change, reset, sleep/suspend */
  308. hw_cwrite(CAP_USBINTR, ~0,
  309. USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
  310. hw_cwrite(CAP_USBCMD, USBCMD_RS, USBCMD_RS);
  311. } else {
  312. hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
  313. hw_cwrite(CAP_USBINTR, ~0, 0);
  314. }
  315. return 0;
  316. }
  317. /**
  318. * hw_ep_flush: flush endpoint fifo (execute without interruption)
  319. * @num: endpoint number
  320. * @dir: endpoint direction
  321. *
  322. * This function returns an error code
  323. */
  324. static int hw_ep_flush(int num, int dir)
  325. {
  326. int n = hw_ep_bit(num, dir);
  327. do {
  328. /* flush any pending transfer */
  329. hw_cwrite(CAP_ENDPTFLUSH, BIT(n), BIT(n));
  330. while (hw_cread(CAP_ENDPTFLUSH, BIT(n)))
  331. cpu_relax();
  332. } while (hw_cread(CAP_ENDPTSTAT, BIT(n)));
  333. return 0;
  334. }
  335. /**
  336. * hw_ep_disable: disables endpoint (execute without interruption)
  337. * @num: endpoint number
  338. * @dir: endpoint direction
  339. *
  340. * This function returns an error code
  341. */
  342. static int hw_ep_disable(int num, int dir)
  343. {
  344. hw_ep_flush(num, dir);
  345. hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32),
  346. dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
  347. return 0;
  348. }
  349. /**
  350. * hw_ep_enable: enables endpoint (execute without interruption)
  351. * @num: endpoint number
  352. * @dir: endpoint direction
  353. * @type: endpoint type
  354. *
  355. * This function returns an error code
  356. */
  357. static int hw_ep_enable(int num, int dir, int type)
  358. {
  359. u32 mask, data;
  360. if (dir) {
  361. mask = ENDPTCTRL_TXT; /* type */
  362. data = type << ffs_nr(mask);
  363. mask |= ENDPTCTRL_TXS; /* unstall */
  364. mask |= ENDPTCTRL_TXR; /* reset data toggle */
  365. data |= ENDPTCTRL_TXR;
  366. mask |= ENDPTCTRL_TXE; /* enable */
  367. data |= ENDPTCTRL_TXE;
  368. } else {
  369. mask = ENDPTCTRL_RXT; /* type */
  370. data = type << ffs_nr(mask);
  371. mask |= ENDPTCTRL_RXS; /* unstall */
  372. mask |= ENDPTCTRL_RXR; /* reset data toggle */
  373. data |= ENDPTCTRL_RXR;
  374. mask |= ENDPTCTRL_RXE; /* enable */
  375. data |= ENDPTCTRL_RXE;
  376. }
  377. hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), mask, data);
  378. return 0;
  379. }
  380. /**
  381. * hw_ep_get_halt: return endpoint halt status
  382. * @num: endpoint number
  383. * @dir: endpoint direction
  384. *
  385. * This function returns 1 if endpoint halted
  386. */
  387. static int hw_ep_get_halt(int num, int dir)
  388. {
  389. u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  390. return hw_cread(CAP_ENDPTCTRL + num * sizeof(u32), mask) ? 1 : 0;
  391. }
  392. /**
  393. * hw_test_and_clear_setup_status: test & clear setup status (execute without
  394. * interruption)
  395. * @n: endpoint number
  396. *
  397. * This function returns setup status
  398. */
  399. static int hw_test_and_clear_setup_status(int n)
  400. {
  401. n = ep_to_bit(n);
  402. return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT, BIT(n));
  403. }
  404. /**
  405. * hw_ep_prime: primes endpoint (execute without interruption)
  406. * @num: endpoint number
  407. * @dir: endpoint direction
  408. * @is_ctrl: true if control endpoint
  409. *
  410. * This function returns an error code
  411. */
  412. static int hw_ep_prime(int num, int dir, int is_ctrl)
  413. {
  414. int n = hw_ep_bit(num, dir);
  415. if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
  416. return -EAGAIN;
  417. hw_cwrite(CAP_ENDPTPRIME, BIT(n), BIT(n));
  418. while (hw_cread(CAP_ENDPTPRIME, BIT(n)))
  419. cpu_relax();
  420. if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
  421. return -EAGAIN;
  422. /* status shoult be tested according with manual but it doesn't work */
  423. return 0;
  424. }
  425. /**
  426. * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
  427. * without interruption)
  428. * @num: endpoint number
  429. * @dir: endpoint direction
  430. * @value: true => stall, false => unstall
  431. *
  432. * This function returns an error code
  433. */
  434. static int hw_ep_set_halt(int num, int dir, int value)
  435. {
  436. if (value != 0 && value != 1)
  437. return -EINVAL;
  438. do {
  439. u32 addr = CAP_ENDPTCTRL + num * sizeof(u32);
  440. u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  441. u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
  442. /* data toggle - reserved for EP0 but it's in ESS */
  443. hw_cwrite(addr, mask_xs|mask_xr, value ? mask_xs : mask_xr);
  444. } while (value != hw_ep_get_halt(num, dir));
  445. return 0;
  446. }
  447. /**
  448. * hw_intr_clear: disables interrupt & clears interrupt status (execute without
  449. * interruption)
  450. * @n: interrupt bit
  451. *
  452. * This function returns an error code
  453. */
  454. static int hw_intr_clear(int n)
  455. {
  456. if (n >= REG_BITS)
  457. return -EINVAL;
  458. hw_cwrite(CAP_USBINTR, BIT(n), 0);
  459. hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
  460. return 0;
  461. }
  462. /**
  463. * hw_intr_force: enables interrupt & forces interrupt status (execute without
  464. * interruption)
  465. * @n: interrupt bit
  466. *
  467. * This function returns an error code
  468. */
  469. static int hw_intr_force(int n)
  470. {
  471. if (n >= REG_BITS)
  472. return -EINVAL;
  473. hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
  474. hw_cwrite(CAP_USBINTR, BIT(n), BIT(n));
  475. hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
  476. hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, 0);
  477. return 0;
  478. }
  479. /**
  480. * hw_is_port_high_speed: test if port is high speed
  481. *
  482. * This function returns true if high speed port
  483. */
  484. static int hw_port_is_high_speed(void)
  485. {
  486. return hw_bank.lpm ? hw_cread(CAP_DEVLC, DEVLC_PSPD) :
  487. hw_cread(CAP_PORTSC, PORTSC_HSP);
  488. }
  489. /**
  490. * hw_port_test_get: reads port test mode value
  491. *
  492. * This function returns port test mode value
  493. */
  494. static u8 hw_port_test_get(void)
  495. {
  496. return hw_cread(CAP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
  497. }
  498. /**
  499. * hw_port_test_set: writes port test mode (execute without interruption)
  500. * @mode: new value
  501. *
  502. * This function returns an error code
  503. */
  504. static int hw_port_test_set(u8 mode)
  505. {
  506. const u8 TEST_MODE_MAX = 7;
  507. if (mode > TEST_MODE_MAX)
  508. return -EINVAL;
  509. hw_cwrite(CAP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
  510. return 0;
  511. }
  512. /**
  513. * hw_read_intr_enable: returns interrupt enable register
  514. *
  515. * This function returns register data
  516. */
  517. static u32 hw_read_intr_enable(void)
  518. {
  519. return hw_cread(CAP_USBINTR, ~0);
  520. }
  521. /**
  522. * hw_read_intr_status: returns interrupt status register
  523. *
  524. * This function returns register data
  525. */
  526. static u32 hw_read_intr_status(void)
  527. {
  528. return hw_cread(CAP_USBSTS, ~0);
  529. }
  530. /**
  531. * hw_register_read: reads all device registers (execute without interruption)
  532. * @buf: destination buffer
  533. * @size: buffer size
  534. *
  535. * This function returns number of registers read
  536. */
  537. static size_t hw_register_read(u32 *buf, size_t size)
  538. {
  539. unsigned i;
  540. if (size > hw_bank.size)
  541. size = hw_bank.size;
  542. for (i = 0; i < size; i++)
  543. buf[i] = hw_aread(i * sizeof(u32), ~0);
  544. return size;
  545. }
  546. /**
  547. * hw_register_write: writes to register
  548. * @addr: register address
  549. * @data: register value
  550. *
  551. * This function returns an error code
  552. */
  553. static int hw_register_write(u16 addr, u32 data)
  554. {
  555. /* align */
  556. addr /= sizeof(u32);
  557. if (addr >= hw_bank.size)
  558. return -EINVAL;
  559. /* align */
  560. addr *= sizeof(u32);
  561. hw_awrite(addr, ~0, data);
  562. return 0;
  563. }
  564. /**
  565. * hw_test_and_clear_complete: test & clear complete status (execute without
  566. * interruption)
  567. * @n: endpoint number
  568. *
  569. * This function returns complete status
  570. */
  571. static int hw_test_and_clear_complete(int n)
  572. {
  573. n = ep_to_bit(n);
  574. return hw_ctest_and_clear(CAP_ENDPTCOMPLETE, BIT(n));
  575. }
  576. /**
  577. * hw_test_and_clear_intr_active: test & clear active interrupts (execute
  578. * without interruption)
  579. *
  580. * This function returns active interrutps
  581. */
  582. static u32 hw_test_and_clear_intr_active(void)
  583. {
  584. u32 reg = hw_read_intr_status() & hw_read_intr_enable();
  585. hw_cwrite(CAP_USBSTS, ~0, reg);
  586. return reg;
  587. }
  588. /**
  589. * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
  590. * interruption)
  591. *
  592. * This function returns guard value
  593. */
  594. static int hw_test_and_clear_setup_guard(void)
  595. {
  596. return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 0);
  597. }
  598. /**
  599. * hw_test_and_set_setup_guard: test & set setup guard (execute without
  600. * interruption)
  601. *
  602. * This function returns guard value
  603. */
  604. static int hw_test_and_set_setup_guard(void)
  605. {
  606. return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
  607. }
  608. /**
  609. * hw_usb_set_address: configures USB address (execute without interruption)
  610. * @value: new USB address
  611. *
  612. * This function returns an error code
  613. */
  614. static int hw_usb_set_address(u8 value)
  615. {
  616. /* advance */
  617. hw_cwrite(CAP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
  618. value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
  619. return 0;
  620. }
  621. /**
  622. * hw_usb_reset: restart device after a bus reset (execute without
  623. * interruption)
  624. *
  625. * This function returns an error code
  626. */
  627. static int hw_usb_reset(void)
  628. {
  629. hw_usb_set_address(0);
  630. /* ESS flushes only at end?!? */
  631. hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); /* flush all EPs */
  632. /* clear setup token semaphores */
  633. hw_cwrite(CAP_ENDPTSETUPSTAT, 0, 0); /* writes its content */
  634. /* clear complete status */
  635. hw_cwrite(CAP_ENDPTCOMPLETE, 0, 0); /* writes its content */
  636. /* wait until all bits cleared */
  637. while (hw_cread(CAP_ENDPTPRIME, ~0))
  638. udelay(10); /* not RTOS friendly */
  639. /* reset all endpoints ? */
  640. /* reset internal status and wait for further instructions
  641. no need to verify the port reset status (ESS does it) */
  642. return 0;
  643. }
  644. /******************************************************************************
  645. * DBG block
  646. *****************************************************************************/
  647. /**
  648. * show_device: prints information about device capabilities and status
  649. *
  650. * Check "device.h" for details
  651. */
  652. static ssize_t show_device(struct device *dev, struct device_attribute *attr,
  653. char *buf)
  654. {
  655. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  656. struct usb_gadget *gadget = &udc->gadget;
  657. int n = 0;
  658. dbg_trace("[%s] %p\n", __func__, buf);
  659. if (attr == NULL || buf == NULL) {
  660. dev_err(dev, "[%s] EINVAL\n", __func__);
  661. return 0;
  662. }
  663. n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
  664. gadget->speed);
  665. n += scnprintf(buf + n, PAGE_SIZE - n, "max_speed = %d\n",
  666. gadget->max_speed);
  667. /* TODO: Scheduled for removal in 3.8. */
  668. n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
  669. gadget_is_dualspeed(gadget));
  670. n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
  671. gadget->is_otg);
  672. n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
  673. gadget->is_a_peripheral);
  674. n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
  675. gadget->b_hnp_enable);
  676. n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
  677. gadget->a_hnp_support);
  678. n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
  679. gadget->a_alt_hnp_support);
  680. n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
  681. (gadget->name ? gadget->name : ""));
  682. return n;
  683. }
  684. static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
  685. /**
  686. * show_driver: prints information about attached gadget (if any)
  687. *
  688. * Check "device.h" for details
  689. */
  690. static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
  691. char *buf)
  692. {
  693. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  694. struct usb_gadget_driver *driver = udc->driver;
  695. int n = 0;
  696. dbg_trace("[%s] %p\n", __func__, buf);
  697. if (attr == NULL || buf == NULL) {
  698. dev_err(dev, "[%s] EINVAL\n", __func__);
  699. return 0;
  700. }
  701. if (driver == NULL)
  702. return scnprintf(buf, PAGE_SIZE,
  703. "There is no gadget attached!\n");
  704. n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
  705. (driver->function ? driver->function : ""));
  706. n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
  707. driver->max_speed);
  708. return n;
  709. }
  710. static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
  711. /* Maximum event message length */
  712. #define DBG_DATA_MSG 64UL
  713. /* Maximum event messages */
  714. #define DBG_DATA_MAX 128UL
  715. /* Event buffer descriptor */
  716. static struct {
  717. char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
  718. unsigned idx; /* index */
  719. unsigned tty; /* print to console? */
  720. rwlock_t lck; /* lock */
  721. } dbg_data = {
  722. .idx = 0,
  723. .tty = 0,
  724. .lck = __RW_LOCK_UNLOCKED(lck)
  725. };
  726. /**
  727. * dbg_dec: decrements debug event index
  728. * @idx: buffer index
  729. */
  730. static void dbg_dec(unsigned *idx)
  731. {
  732. *idx = (*idx - 1) & (DBG_DATA_MAX-1);
  733. }
  734. /**
  735. * dbg_inc: increments debug event index
  736. * @idx: buffer index
  737. */
  738. static void dbg_inc(unsigned *idx)
  739. {
  740. *idx = (*idx + 1) & (DBG_DATA_MAX-1);
  741. }
  742. /**
  743. * dbg_print: prints the common part of the event
  744. * @addr: endpoint address
  745. * @name: event name
  746. * @status: status
  747. * @extra: extra information
  748. */
  749. static void dbg_print(u8 addr, const char *name, int status, const char *extra)
  750. {
  751. struct timeval tval;
  752. unsigned int stamp;
  753. unsigned long flags;
  754. write_lock_irqsave(&dbg_data.lck, flags);
  755. do_gettimeofday(&tval);
  756. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
  757. stamp = stamp * 1000000 + tval.tv_usec;
  758. scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
  759. "%04X\t? %02X %-7.7s %4i ?\t%s\n",
  760. stamp, addr, name, status, extra);
  761. dbg_inc(&dbg_data.idx);
  762. write_unlock_irqrestore(&dbg_data.lck, flags);
  763. if (dbg_data.tty != 0)
  764. pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
  765. stamp, addr, name, status, extra);
  766. }
  767. /**
  768. * dbg_done: prints a DONE event
  769. * @addr: endpoint address
  770. * @td: transfer descriptor
  771. * @status: status
  772. */
  773. static void dbg_done(u8 addr, const u32 token, int status)
  774. {
  775. char msg[DBG_DATA_MSG];
  776. scnprintf(msg, sizeof(msg), "%d %02X",
  777. (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
  778. (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
  779. dbg_print(addr, "DONE", status, msg);
  780. }
  781. /**
  782. * dbg_event: prints a generic event
  783. * @addr: endpoint address
  784. * @name: event name
  785. * @status: status
  786. */
  787. static void dbg_event(u8 addr, const char *name, int status)
  788. {
  789. if (name != NULL)
  790. dbg_print(addr, name, status, "");
  791. }
  792. /*
  793. * dbg_queue: prints a QUEUE event
  794. * @addr: endpoint address
  795. * @req: USB request
  796. * @status: status
  797. */
  798. static void dbg_queue(u8 addr, const struct usb_request *req, int status)
  799. {
  800. char msg[DBG_DATA_MSG];
  801. if (req != NULL) {
  802. scnprintf(msg, sizeof(msg),
  803. "%d %d", !req->no_interrupt, req->length);
  804. dbg_print(addr, "QUEUE", status, msg);
  805. }
  806. }
  807. /**
  808. * dbg_setup: prints a SETUP event
  809. * @addr: endpoint address
  810. * @req: setup request
  811. */
  812. static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
  813. {
  814. char msg[DBG_DATA_MSG];
  815. if (req != NULL) {
  816. scnprintf(msg, sizeof(msg),
  817. "%02X %02X %04X %04X %d", req->bRequestType,
  818. req->bRequest, le16_to_cpu(req->wValue),
  819. le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
  820. dbg_print(addr, "SETUP", 0, msg);
  821. }
  822. }
  823. /**
  824. * show_events: displays the event buffer
  825. *
  826. * Check "device.h" for details
  827. */
  828. static ssize_t show_events(struct device *dev, struct device_attribute *attr,
  829. char *buf)
  830. {
  831. unsigned long flags;
  832. unsigned i, j, n = 0;
  833. dbg_trace("[%s] %p\n", __func__, buf);
  834. if (attr == NULL || buf == NULL) {
  835. dev_err(dev, "[%s] EINVAL\n", __func__);
  836. return 0;
  837. }
  838. read_lock_irqsave(&dbg_data.lck, flags);
  839. i = dbg_data.idx;
  840. for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
  841. n += strlen(dbg_data.buf[i]);
  842. if (n >= PAGE_SIZE) {
  843. n -= strlen(dbg_data.buf[i]);
  844. break;
  845. }
  846. }
  847. for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
  848. j += scnprintf(buf + j, PAGE_SIZE - j,
  849. "%s", dbg_data.buf[i]);
  850. read_unlock_irqrestore(&dbg_data.lck, flags);
  851. return n;
  852. }
  853. /**
  854. * store_events: configure if events are going to be also printed to console
  855. *
  856. * Check "device.h" for details
  857. */
  858. static ssize_t store_events(struct device *dev, struct device_attribute *attr,
  859. const char *buf, size_t count)
  860. {
  861. unsigned tty;
  862. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  863. if (attr == NULL || buf == NULL) {
  864. dev_err(dev, "[%s] EINVAL\n", __func__);
  865. goto done;
  866. }
  867. if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
  868. dev_err(dev, "<1|0>: enable|disable console log\n");
  869. goto done;
  870. }
  871. dbg_data.tty = tty;
  872. dev_info(dev, "tty = %u", dbg_data.tty);
  873. done:
  874. return count;
  875. }
  876. static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
  877. /**
  878. * show_inters: interrupt status, enable status and historic
  879. *
  880. * Check "device.h" for details
  881. */
  882. static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
  883. char *buf)
  884. {
  885. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  886. unsigned long flags;
  887. u32 intr;
  888. unsigned i, j, n = 0;
  889. dbg_trace("[%s] %p\n", __func__, buf);
  890. if (attr == NULL || buf == NULL) {
  891. dev_err(dev, "[%s] EINVAL\n", __func__);
  892. return 0;
  893. }
  894. spin_lock_irqsave(udc->lock, flags);
  895. n += scnprintf(buf + n, PAGE_SIZE - n,
  896. "status = %08x\n", hw_read_intr_status());
  897. n += scnprintf(buf + n, PAGE_SIZE - n,
  898. "enable = %08x\n", hw_read_intr_enable());
  899. n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
  900. isr_statistics.test);
  901. n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
  902. isr_statistics.ui);
  903. n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
  904. isr_statistics.uei);
  905. n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
  906. isr_statistics.pci);
  907. n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
  908. isr_statistics.uri);
  909. n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
  910. isr_statistics.sli);
  911. n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
  912. isr_statistics.none);
  913. n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
  914. isr_statistics.hndl.cnt);
  915. for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
  916. i &= ISR_MASK;
  917. intr = isr_statistics.hndl.buf[i];
  918. if (USBi_UI & intr)
  919. n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
  920. intr &= ~USBi_UI;
  921. if (USBi_UEI & intr)
  922. n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
  923. intr &= ~USBi_UEI;
  924. if (USBi_PCI & intr)
  925. n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
  926. intr &= ~USBi_PCI;
  927. if (USBi_URI & intr)
  928. n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
  929. intr &= ~USBi_URI;
  930. if (USBi_SLI & intr)
  931. n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
  932. intr &= ~USBi_SLI;
  933. if (intr)
  934. n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
  935. if (isr_statistics.hndl.buf[i])
  936. n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
  937. }
  938. spin_unlock_irqrestore(udc->lock, flags);
  939. return n;
  940. }
  941. /**
  942. * store_inters: enable & force or disable an individual interrutps
  943. * (to be used for test purposes only)
  944. *
  945. * Check "device.h" for details
  946. */
  947. static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
  948. const char *buf, size_t count)
  949. {
  950. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  951. unsigned long flags;
  952. unsigned en, bit;
  953. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  954. if (attr == NULL || buf == NULL) {
  955. dev_err(dev, "[%s] EINVAL\n", __func__);
  956. goto done;
  957. }
  958. if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
  959. dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
  960. goto done;
  961. }
  962. spin_lock_irqsave(udc->lock, flags);
  963. if (en) {
  964. if (hw_intr_force(bit))
  965. dev_err(dev, "invalid bit number\n");
  966. else
  967. isr_statistics.test++;
  968. } else {
  969. if (hw_intr_clear(bit))
  970. dev_err(dev, "invalid bit number\n");
  971. }
  972. spin_unlock_irqrestore(udc->lock, flags);
  973. done:
  974. return count;
  975. }
  976. static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
  977. /**
  978. * show_port_test: reads port test mode
  979. *
  980. * Check "device.h" for details
  981. */
  982. static ssize_t show_port_test(struct device *dev,
  983. struct device_attribute *attr, char *buf)
  984. {
  985. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  986. unsigned long flags;
  987. unsigned mode;
  988. dbg_trace("[%s] %p\n", __func__, buf);
  989. if (attr == NULL || buf == NULL) {
  990. dev_err(dev, "[%s] EINVAL\n", __func__);
  991. return 0;
  992. }
  993. spin_lock_irqsave(udc->lock, flags);
  994. mode = hw_port_test_get();
  995. spin_unlock_irqrestore(udc->lock, flags);
  996. return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
  997. }
  998. /**
  999. * store_port_test: writes port test mode
  1000. *
  1001. * Check "device.h" for details
  1002. */
  1003. static ssize_t store_port_test(struct device *dev,
  1004. struct device_attribute *attr,
  1005. const char *buf, size_t count)
  1006. {
  1007. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1008. unsigned long flags;
  1009. unsigned mode;
  1010. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  1011. if (attr == NULL || buf == NULL) {
  1012. dev_err(dev, "[%s] EINVAL\n", __func__);
  1013. goto done;
  1014. }
  1015. if (sscanf(buf, "%u", &mode) != 1) {
  1016. dev_err(dev, "<mode>: set port test mode");
  1017. goto done;
  1018. }
  1019. spin_lock_irqsave(udc->lock, flags);
  1020. if (hw_port_test_set(mode))
  1021. dev_err(dev, "invalid mode\n");
  1022. spin_unlock_irqrestore(udc->lock, flags);
  1023. done:
  1024. return count;
  1025. }
  1026. static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
  1027. show_port_test, store_port_test);
  1028. /**
  1029. * show_qheads: DMA contents of all queue heads
  1030. *
  1031. * Check "device.h" for details
  1032. */
  1033. static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
  1034. char *buf)
  1035. {
  1036. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1037. unsigned long flags;
  1038. unsigned i, j, n = 0;
  1039. dbg_trace("[%s] %p\n", __func__, buf);
  1040. if (attr == NULL || buf == NULL) {
  1041. dev_err(dev, "[%s] EINVAL\n", __func__);
  1042. return 0;
  1043. }
  1044. spin_lock_irqsave(udc->lock, flags);
  1045. for (i = 0; i < hw_ep_max/2; i++) {
  1046. struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
  1047. struct ci13xxx_ep *mEpTx = &udc->ci13xxx_ep[i + hw_ep_max/2];
  1048. n += scnprintf(buf + n, PAGE_SIZE - n,
  1049. "EP=%02i: RX=%08X TX=%08X\n",
  1050. i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
  1051. for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
  1052. n += scnprintf(buf + n, PAGE_SIZE - n,
  1053. " %04X: %08X %08X\n", j,
  1054. *((u32 *)mEpRx->qh.ptr + j),
  1055. *((u32 *)mEpTx->qh.ptr + j));
  1056. }
  1057. }
  1058. spin_unlock_irqrestore(udc->lock, flags);
  1059. return n;
  1060. }
  1061. static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
  1062. /**
  1063. * show_registers: dumps all registers
  1064. *
  1065. * Check "device.h" for details
  1066. */
  1067. #define DUMP_ENTRIES 512
  1068. static ssize_t show_registers(struct device *dev,
  1069. struct device_attribute *attr, char *buf)
  1070. {
  1071. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1072. unsigned long flags;
  1073. u32 *dump;
  1074. unsigned i, k, n = 0;
  1075. dbg_trace("[%s] %p\n", __func__, buf);
  1076. if (attr == NULL || buf == NULL) {
  1077. dev_err(dev, "[%s] EINVAL\n", __func__);
  1078. return 0;
  1079. }
  1080. dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
  1081. if (!dump) {
  1082. dev_err(dev, "%s: out of memory\n", __func__);
  1083. return 0;
  1084. }
  1085. spin_lock_irqsave(udc->lock, flags);
  1086. k = hw_register_read(dump, DUMP_ENTRIES);
  1087. spin_unlock_irqrestore(udc->lock, flags);
  1088. for (i = 0; i < k; i++) {
  1089. n += scnprintf(buf + n, PAGE_SIZE - n,
  1090. "reg[0x%04X] = 0x%08X\n",
  1091. i * (unsigned)sizeof(u32), dump[i]);
  1092. }
  1093. kfree(dump);
  1094. return n;
  1095. }
  1096. /**
  1097. * store_registers: writes value to register address
  1098. *
  1099. * Check "device.h" for details
  1100. */
  1101. static ssize_t store_registers(struct device *dev,
  1102. struct device_attribute *attr,
  1103. const char *buf, size_t count)
  1104. {
  1105. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1106. unsigned long addr, data, flags;
  1107. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  1108. if (attr == NULL || buf == NULL) {
  1109. dev_err(dev, "[%s] EINVAL\n", __func__);
  1110. goto done;
  1111. }
  1112. if (sscanf(buf, "%li %li", &addr, &data) != 2) {
  1113. dev_err(dev, "<addr> <data>: write data to register address");
  1114. goto done;
  1115. }
  1116. spin_lock_irqsave(udc->lock, flags);
  1117. if (hw_register_write(addr, data))
  1118. dev_err(dev, "invalid address range\n");
  1119. spin_unlock_irqrestore(udc->lock, flags);
  1120. done:
  1121. return count;
  1122. }
  1123. static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
  1124. show_registers, store_registers);
  1125. /**
  1126. * show_requests: DMA contents of all requests currently queued (all endpts)
  1127. *
  1128. * Check "device.h" for details
  1129. */
  1130. static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
  1131. char *buf)
  1132. {
  1133. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1134. unsigned long flags;
  1135. struct list_head *ptr = NULL;
  1136. struct ci13xxx_req *req = NULL;
  1137. unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
  1138. dbg_trace("[%s] %p\n", __func__, buf);
  1139. if (attr == NULL || buf == NULL) {
  1140. dev_err(dev, "[%s] EINVAL\n", __func__);
  1141. return 0;
  1142. }
  1143. spin_lock_irqsave(udc->lock, flags);
  1144. for (i = 0; i < hw_ep_max; i++)
  1145. list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
  1146. {
  1147. req = list_entry(ptr, struct ci13xxx_req, queue);
  1148. n += scnprintf(buf + n, PAGE_SIZE - n,
  1149. "EP=%02i: TD=%08X %s\n",
  1150. i % hw_ep_max/2, (u32)req->dma,
  1151. ((i < hw_ep_max/2) ? "RX" : "TX"));
  1152. for (j = 0; j < qSize; j++)
  1153. n += scnprintf(buf + n, PAGE_SIZE - n,
  1154. " %04X: %08X\n", j,
  1155. *((u32 *)req->ptr + j));
  1156. }
  1157. spin_unlock_irqrestore(udc->lock, flags);
  1158. return n;
  1159. }
  1160. static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
  1161. /**
  1162. * dbg_create_files: initializes the attribute interface
  1163. * @dev: device
  1164. *
  1165. * This function returns an error code
  1166. */
  1167. __maybe_unused static int dbg_create_files(struct device *dev)
  1168. {
  1169. int retval = 0;
  1170. if (dev == NULL)
  1171. return -EINVAL;
  1172. retval = device_create_file(dev, &dev_attr_device);
  1173. if (retval)
  1174. goto done;
  1175. retval = device_create_file(dev, &dev_attr_driver);
  1176. if (retval)
  1177. goto rm_device;
  1178. retval = device_create_file(dev, &dev_attr_events);
  1179. if (retval)
  1180. goto rm_driver;
  1181. retval = device_create_file(dev, &dev_attr_inters);
  1182. if (retval)
  1183. goto rm_events;
  1184. retval = device_create_file(dev, &dev_attr_port_test);
  1185. if (retval)
  1186. goto rm_inters;
  1187. retval = device_create_file(dev, &dev_attr_qheads);
  1188. if (retval)
  1189. goto rm_port_test;
  1190. retval = device_create_file(dev, &dev_attr_registers);
  1191. if (retval)
  1192. goto rm_qheads;
  1193. retval = device_create_file(dev, &dev_attr_requests);
  1194. if (retval)
  1195. goto rm_registers;
  1196. return 0;
  1197. rm_registers:
  1198. device_remove_file(dev, &dev_attr_registers);
  1199. rm_qheads:
  1200. device_remove_file(dev, &dev_attr_qheads);
  1201. rm_port_test:
  1202. device_remove_file(dev, &dev_attr_port_test);
  1203. rm_inters:
  1204. device_remove_file(dev, &dev_attr_inters);
  1205. rm_events:
  1206. device_remove_file(dev, &dev_attr_events);
  1207. rm_driver:
  1208. device_remove_file(dev, &dev_attr_driver);
  1209. rm_device:
  1210. device_remove_file(dev, &dev_attr_device);
  1211. done:
  1212. return retval;
  1213. }
  1214. /**
  1215. * dbg_remove_files: destroys the attribute interface
  1216. * @dev: device
  1217. *
  1218. * This function returns an error code
  1219. */
  1220. __maybe_unused static int dbg_remove_files(struct device *dev)
  1221. {
  1222. if (dev == NULL)
  1223. return -EINVAL;
  1224. device_remove_file(dev, &dev_attr_requests);
  1225. device_remove_file(dev, &dev_attr_registers);
  1226. device_remove_file(dev, &dev_attr_qheads);
  1227. device_remove_file(dev, &dev_attr_port_test);
  1228. device_remove_file(dev, &dev_attr_inters);
  1229. device_remove_file(dev, &dev_attr_events);
  1230. device_remove_file(dev, &dev_attr_driver);
  1231. device_remove_file(dev, &dev_attr_device);
  1232. return 0;
  1233. }
  1234. /******************************************************************************
  1235. * UTIL block
  1236. *****************************************************************************/
  1237. /**
  1238. * _usb_addr: calculates endpoint address from direction & number
  1239. * @ep: endpoint
  1240. */
  1241. static inline u8 _usb_addr(struct ci13xxx_ep *ep)
  1242. {
  1243. return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
  1244. }
  1245. /**
  1246. * _hardware_queue: configures a request at hardware level
  1247. * @gadget: gadget
  1248. * @mEp: endpoint
  1249. *
  1250. * This function returns an error code
  1251. */
  1252. static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  1253. {
  1254. unsigned i;
  1255. int ret = 0;
  1256. unsigned length = mReq->req.length;
  1257. trace("%p, %p", mEp, mReq);
  1258. /* don't queue twice */
  1259. if (mReq->req.status == -EALREADY)
  1260. return -EALREADY;
  1261. mReq->req.status = -EALREADY;
  1262. if (length && mReq->req.dma == DMA_ADDR_INVALID) {
  1263. mReq->req.dma = \
  1264. dma_map_single(mEp->device, mReq->req.buf,
  1265. length, mEp->dir ? DMA_TO_DEVICE :
  1266. DMA_FROM_DEVICE);
  1267. if (mReq->req.dma == 0)
  1268. return -ENOMEM;
  1269. mReq->map = 1;
  1270. }
  1271. if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
  1272. mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
  1273. &mReq->zdma);
  1274. if (mReq->zptr == NULL) {
  1275. if (mReq->map) {
  1276. dma_unmap_single(mEp->device, mReq->req.dma,
  1277. length, mEp->dir ? DMA_TO_DEVICE :
  1278. DMA_FROM_DEVICE);
  1279. mReq->req.dma = DMA_ADDR_INVALID;
  1280. mReq->map = 0;
  1281. }
  1282. return -ENOMEM;
  1283. }
  1284. memset(mReq->zptr, 0, sizeof(*mReq->zptr));
  1285. mReq->zptr->next = TD_TERMINATE;
  1286. mReq->zptr->token = TD_STATUS_ACTIVE;
  1287. if (!mReq->req.no_interrupt)
  1288. mReq->zptr->token |= TD_IOC;
  1289. }
  1290. /*
  1291. * TD configuration
  1292. * TODO - handle requests which spawns into several TDs
  1293. */
  1294. memset(mReq->ptr, 0, sizeof(*mReq->ptr));
  1295. mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
  1296. mReq->ptr->token &= TD_TOTAL_BYTES;
  1297. mReq->ptr->token |= TD_STATUS_ACTIVE;
  1298. if (mReq->zptr) {
  1299. mReq->ptr->next = mReq->zdma;
  1300. } else {
  1301. mReq->ptr->next = TD_TERMINATE;
  1302. if (!mReq->req.no_interrupt)
  1303. mReq->ptr->token |= TD_IOC;
  1304. }
  1305. mReq->ptr->page[0] = mReq->req.dma;
  1306. for (i = 1; i < 5; i++)
  1307. mReq->ptr->page[i] =
  1308. (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
  1309. if (!list_empty(&mEp->qh.queue)) {
  1310. struct ci13xxx_req *mReqPrev;
  1311. int n = hw_ep_bit(mEp->num, mEp->dir);
  1312. int tmp_stat;
  1313. mReqPrev = list_entry(mEp->qh.queue.prev,
  1314. struct ci13xxx_req, queue);
  1315. if (mReqPrev->zptr)
  1316. mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
  1317. else
  1318. mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
  1319. wmb();
  1320. if (hw_cread(CAP_ENDPTPRIME, BIT(n)))
  1321. goto done;
  1322. do {
  1323. hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
  1324. tmp_stat = hw_cread(CAP_ENDPTSTAT, BIT(n));
  1325. } while (!hw_cread(CAP_USBCMD, USBCMD_ATDTW));
  1326. hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, 0);
  1327. if (tmp_stat)
  1328. goto done;
  1329. }
  1330. /* QH configuration */
  1331. mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
  1332. mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
  1333. mEp->qh.ptr->cap |= QH_ZLT;
  1334. wmb(); /* synchronize before ep prime */
  1335. ret = hw_ep_prime(mEp->num, mEp->dir,
  1336. mEp->type == USB_ENDPOINT_XFER_CONTROL);
  1337. done:
  1338. return ret;
  1339. }
  1340. /**
  1341. * _hardware_dequeue: handles a request at hardware level
  1342. * @gadget: gadget
  1343. * @mEp: endpoint
  1344. *
  1345. * This function returns an error code
  1346. */
  1347. static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  1348. {
  1349. trace("%p, %p", mEp, mReq);
  1350. if (mReq->req.status != -EALREADY)
  1351. return -EINVAL;
  1352. if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
  1353. return -EBUSY;
  1354. if (mReq->zptr) {
  1355. if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
  1356. return -EBUSY;
  1357. dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
  1358. mReq->zptr = NULL;
  1359. }
  1360. mReq->req.status = 0;
  1361. if (mReq->map) {
  1362. dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
  1363. mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1364. mReq->req.dma = DMA_ADDR_INVALID;
  1365. mReq->map = 0;
  1366. }
  1367. mReq->req.status = mReq->ptr->token & TD_STATUS;
  1368. if ((TD_STATUS_HALTED & mReq->req.status) != 0)
  1369. mReq->req.status = -1;
  1370. else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
  1371. mReq->req.status = -1;
  1372. else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
  1373. mReq->req.status = -1;
  1374. mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
  1375. mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
  1376. mReq->req.actual = mReq->req.length - mReq->req.actual;
  1377. mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
  1378. return mReq->req.actual;
  1379. }
  1380. /**
  1381. * _ep_nuke: dequeues all endpoint requests
  1382. * @mEp: endpoint
  1383. *
  1384. * This function returns an error code
  1385. * Caller must hold lock
  1386. */
  1387. static int _ep_nuke(struct ci13xxx_ep *mEp)
  1388. __releases(mEp->lock)
  1389. __acquires(mEp->lock)
  1390. {
  1391. trace("%p", mEp);
  1392. if (mEp == NULL)
  1393. return -EINVAL;
  1394. hw_ep_flush(mEp->num, mEp->dir);
  1395. while (!list_empty(&mEp->qh.queue)) {
  1396. /* pop oldest request */
  1397. struct ci13xxx_req *mReq = \
  1398. list_entry(mEp->qh.queue.next,
  1399. struct ci13xxx_req, queue);
  1400. list_del_init(&mReq->queue);
  1401. mReq->req.status = -ESHUTDOWN;
  1402. if (mReq->req.complete != NULL) {
  1403. spin_unlock(mEp->lock);
  1404. mReq->req.complete(&mEp->ep, &mReq->req);
  1405. spin_lock(mEp->lock);
  1406. }
  1407. }
  1408. return 0;
  1409. }
  1410. /**
  1411. * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
  1412. * @gadget: gadget
  1413. *
  1414. * This function returns an error code
  1415. */
  1416. static int _gadget_stop_activity(struct usb_gadget *gadget)
  1417. {
  1418. struct usb_ep *ep;
  1419. struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
  1420. unsigned long flags;
  1421. trace("%p", gadget);
  1422. if (gadget == NULL)
  1423. return -EINVAL;
  1424. spin_lock_irqsave(udc->lock, flags);
  1425. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1426. udc->remote_wakeup = 0;
  1427. udc->suspended = 0;
  1428. spin_unlock_irqrestore(udc->lock, flags);
  1429. /* flush all endpoints */
  1430. gadget_for_each_ep(ep, gadget) {
  1431. usb_ep_fifo_flush(ep);
  1432. }
  1433. usb_ep_fifo_flush(&udc->ep0out.ep);
  1434. usb_ep_fifo_flush(&udc->ep0in.ep);
  1435. udc->driver->disconnect(gadget);
  1436. /* make sure to disable all endpoints */
  1437. gadget_for_each_ep(ep, gadget) {
  1438. usb_ep_disable(ep);
  1439. }
  1440. if (udc->status != NULL) {
  1441. usb_ep_free_request(&udc->ep0in.ep, udc->status);
  1442. udc->status = NULL;
  1443. }
  1444. return 0;
  1445. }
  1446. /******************************************************************************
  1447. * ISR block
  1448. *****************************************************************************/
  1449. /**
  1450. * isr_reset_handler: USB reset interrupt handler
  1451. * @udc: UDC device
  1452. *
  1453. * This function resets USB engine after a bus reset occurred
  1454. */
  1455. static void isr_reset_handler(struct ci13xxx *udc)
  1456. __releases(udc->lock)
  1457. __acquires(udc->lock)
  1458. {
  1459. int retval;
  1460. trace("%p", udc);
  1461. if (udc == NULL) {
  1462. err("EINVAL");
  1463. return;
  1464. }
  1465. dbg_event(0xFF, "BUS RST", 0);
  1466. spin_unlock(udc->lock);
  1467. retval = _gadget_stop_activity(&udc->gadget);
  1468. if (retval)
  1469. goto done;
  1470. retval = hw_usb_reset();
  1471. if (retval)
  1472. goto done;
  1473. udc->status = usb_ep_alloc_request(&udc->ep0in.ep, GFP_ATOMIC);
  1474. if (udc->status == NULL)
  1475. retval = -ENOMEM;
  1476. spin_lock(udc->lock);
  1477. done:
  1478. if (retval)
  1479. err("error: %i", retval);
  1480. }
  1481. /**
  1482. * isr_get_status_complete: get_status request complete function
  1483. * @ep: endpoint
  1484. * @req: request handled
  1485. *
  1486. * Caller must release lock
  1487. */
  1488. static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
  1489. {
  1490. trace("%p, %p", ep, req);
  1491. if (ep == NULL || req == NULL) {
  1492. err("EINVAL");
  1493. return;
  1494. }
  1495. kfree(req->buf);
  1496. usb_ep_free_request(ep, req);
  1497. }
  1498. /**
  1499. * isr_get_status_response: get_status request response
  1500. * @udc: udc struct
  1501. * @setup: setup request packet
  1502. *
  1503. * This function returns an error code
  1504. */
  1505. static int isr_get_status_response(struct ci13xxx *udc,
  1506. struct usb_ctrlrequest *setup)
  1507. __releases(mEp->lock)
  1508. __acquires(mEp->lock)
  1509. {
  1510. struct ci13xxx_ep *mEp = &udc->ep0in;
  1511. struct usb_request *req = NULL;
  1512. gfp_t gfp_flags = GFP_ATOMIC;
  1513. int dir, num, retval;
  1514. trace("%p, %p", mEp, setup);
  1515. if (mEp == NULL || setup == NULL)
  1516. return -EINVAL;
  1517. spin_unlock(mEp->lock);
  1518. req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
  1519. spin_lock(mEp->lock);
  1520. if (req == NULL)
  1521. return -ENOMEM;
  1522. req->complete = isr_get_status_complete;
  1523. req->length = 2;
  1524. req->buf = kzalloc(req->length, gfp_flags);
  1525. if (req->buf == NULL) {
  1526. retval = -ENOMEM;
  1527. goto err_free_req;
  1528. }
  1529. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1530. /* Assume that device is bus powered for now. */
  1531. *((u16 *)req->buf) = _udc->remote_wakeup << 1;
  1532. retval = 0;
  1533. } else if ((setup->bRequestType & USB_RECIP_MASK) \
  1534. == USB_RECIP_ENDPOINT) {
  1535. dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
  1536. TX : RX;
  1537. num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  1538. *((u16 *)req->buf) = hw_ep_get_halt(num, dir);
  1539. }
  1540. /* else do nothing; reserved for future use */
  1541. spin_unlock(mEp->lock);
  1542. retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
  1543. spin_lock(mEp->lock);
  1544. if (retval)
  1545. goto err_free_buf;
  1546. return 0;
  1547. err_free_buf:
  1548. kfree(req->buf);
  1549. err_free_req:
  1550. spin_unlock(mEp->lock);
  1551. usb_ep_free_request(&mEp->ep, req);
  1552. spin_lock(mEp->lock);
  1553. return retval;
  1554. }
  1555. /**
  1556. * isr_setup_status_complete: setup_status request complete function
  1557. * @ep: endpoint
  1558. * @req: request handled
  1559. *
  1560. * Caller must release lock. Put the port in test mode if test mode
  1561. * feature is selected.
  1562. */
  1563. static void
  1564. isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
  1565. {
  1566. struct ci13xxx *udc = req->context;
  1567. unsigned long flags;
  1568. trace("%p, %p", ep, req);
  1569. spin_lock_irqsave(udc->lock, flags);
  1570. if (udc->test_mode)
  1571. hw_port_test_set(udc->test_mode);
  1572. spin_unlock_irqrestore(udc->lock, flags);
  1573. }
  1574. /**
  1575. * isr_setup_status_phase: queues the status phase of a setup transation
  1576. * @udc: udc struct
  1577. *
  1578. * This function returns an error code
  1579. */
  1580. static int isr_setup_status_phase(struct ci13xxx *udc)
  1581. __releases(mEp->lock)
  1582. __acquires(mEp->lock)
  1583. {
  1584. int retval;
  1585. struct ci13xxx_ep *mEp;
  1586. trace("%p", udc);
  1587. mEp = (udc->ep0_dir == TX) ? &udc->ep0out : &udc->ep0in;
  1588. udc->status->context = udc;
  1589. udc->status->complete = isr_setup_status_complete;
  1590. spin_unlock(mEp->lock);
  1591. retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
  1592. spin_lock(mEp->lock);
  1593. return retval;
  1594. }
  1595. /**
  1596. * isr_tr_complete_low: transaction complete low level handler
  1597. * @mEp: endpoint
  1598. *
  1599. * This function returns an error code
  1600. * Caller must hold lock
  1601. */
  1602. static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
  1603. __releases(mEp->lock)
  1604. __acquires(mEp->lock)
  1605. {
  1606. struct ci13xxx_req *mReq, *mReqTemp;
  1607. struct ci13xxx_ep *mEpTemp = mEp;
  1608. int uninitialized_var(retval);
  1609. trace("%p", mEp);
  1610. if (list_empty(&mEp->qh.queue))
  1611. return -EINVAL;
  1612. list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
  1613. queue) {
  1614. retval = _hardware_dequeue(mEp, mReq);
  1615. if (retval < 0)
  1616. break;
  1617. list_del_init(&mReq->queue);
  1618. dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
  1619. if (mReq->req.complete != NULL) {
  1620. spin_unlock(mEp->lock);
  1621. if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
  1622. mReq->req.length)
  1623. mEpTemp = &_udc->ep0in;
  1624. mReq->req.complete(&mEpTemp->ep, &mReq->req);
  1625. spin_lock(mEp->lock);
  1626. }
  1627. }
  1628. if (retval == -EBUSY)
  1629. retval = 0;
  1630. if (retval < 0)
  1631. dbg_event(_usb_addr(mEp), "DONE", retval);
  1632. return retval;
  1633. }
  1634. /**
  1635. * isr_tr_complete_handler: transaction complete interrupt handler
  1636. * @udc: UDC descriptor
  1637. *
  1638. * This function handles traffic events
  1639. */
  1640. static void isr_tr_complete_handler(struct ci13xxx *udc)
  1641. __releases(udc->lock)
  1642. __acquires(udc->lock)
  1643. {
  1644. unsigned i;
  1645. u8 tmode = 0;
  1646. trace("%p", udc);
  1647. if (udc == NULL) {
  1648. err("EINVAL");
  1649. return;
  1650. }
  1651. for (i = 0; i < hw_ep_max; i++) {
  1652. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
  1653. int type, num, dir, err = -EINVAL;
  1654. struct usb_ctrlrequest req;
  1655. if (mEp->desc == NULL)
  1656. continue; /* not configured */
  1657. if (hw_test_and_clear_complete(i)) {
  1658. err = isr_tr_complete_low(mEp);
  1659. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  1660. if (err > 0) /* needs status phase */
  1661. err = isr_setup_status_phase(udc);
  1662. if (err < 0) {
  1663. dbg_event(_usb_addr(mEp),
  1664. "ERROR", err);
  1665. spin_unlock(udc->lock);
  1666. if (usb_ep_set_halt(&mEp->ep))
  1667. err("error: ep_set_halt");
  1668. spin_lock(udc->lock);
  1669. }
  1670. }
  1671. }
  1672. if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
  1673. !hw_test_and_clear_setup_status(i))
  1674. continue;
  1675. if (i != 0) {
  1676. warn("ctrl traffic received at endpoint");
  1677. continue;
  1678. }
  1679. /*
  1680. * Flush data and handshake transactions of previous
  1681. * setup packet.
  1682. */
  1683. _ep_nuke(&udc->ep0out);
  1684. _ep_nuke(&udc->ep0in);
  1685. /* read_setup_packet */
  1686. do {
  1687. hw_test_and_set_setup_guard();
  1688. memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
  1689. } while (!hw_test_and_clear_setup_guard());
  1690. type = req.bRequestType;
  1691. udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
  1692. dbg_setup(_usb_addr(mEp), &req);
  1693. switch (req.bRequest) {
  1694. case USB_REQ_CLEAR_FEATURE:
  1695. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  1696. le16_to_cpu(req.wValue) ==
  1697. USB_ENDPOINT_HALT) {
  1698. if (req.wLength != 0)
  1699. break;
  1700. num = le16_to_cpu(req.wIndex);
  1701. dir = num & USB_ENDPOINT_DIR_MASK;
  1702. num &= USB_ENDPOINT_NUMBER_MASK;
  1703. if (dir) /* TX */
  1704. num += hw_ep_max/2;
  1705. if (!udc->ci13xxx_ep[num].wedge) {
  1706. spin_unlock(udc->lock);
  1707. err = usb_ep_clear_halt(
  1708. &udc->ci13xxx_ep[num].ep);
  1709. spin_lock(udc->lock);
  1710. if (err)
  1711. break;
  1712. }
  1713. err = isr_setup_status_phase(udc);
  1714. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
  1715. le16_to_cpu(req.wValue) ==
  1716. USB_DEVICE_REMOTE_WAKEUP) {
  1717. if (req.wLength != 0)
  1718. break;
  1719. udc->remote_wakeup = 0;
  1720. err = isr_setup_status_phase(udc);
  1721. } else {
  1722. goto delegate;
  1723. }
  1724. break;
  1725. case USB_REQ_GET_STATUS:
  1726. if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
  1727. type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
  1728. type != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1729. goto delegate;
  1730. if (le16_to_cpu(req.wLength) != 2 ||
  1731. le16_to_cpu(req.wValue) != 0)
  1732. break;
  1733. err = isr_get_status_response(udc, &req);
  1734. break;
  1735. case USB_REQ_SET_ADDRESS:
  1736. if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
  1737. goto delegate;
  1738. if (le16_to_cpu(req.wLength) != 0 ||
  1739. le16_to_cpu(req.wIndex) != 0)
  1740. break;
  1741. err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
  1742. if (err)
  1743. break;
  1744. err = isr_setup_status_phase(udc);
  1745. break;
  1746. case USB_REQ_SET_FEATURE:
  1747. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  1748. le16_to_cpu(req.wValue) ==
  1749. USB_ENDPOINT_HALT) {
  1750. if (req.wLength != 0)
  1751. break;
  1752. num = le16_to_cpu(req.wIndex);
  1753. dir = num & USB_ENDPOINT_DIR_MASK;
  1754. num &= USB_ENDPOINT_NUMBER_MASK;
  1755. if (dir) /* TX */
  1756. num += hw_ep_max/2;
  1757. spin_unlock(udc->lock);
  1758. err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
  1759. spin_lock(udc->lock);
  1760. if (!err)
  1761. isr_setup_status_phase(udc);
  1762. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
  1763. if (req.wLength != 0)
  1764. break;
  1765. switch (le16_to_cpu(req.wValue)) {
  1766. case USB_DEVICE_REMOTE_WAKEUP:
  1767. udc->remote_wakeup = 1;
  1768. err = isr_setup_status_phase(udc);
  1769. break;
  1770. case USB_DEVICE_TEST_MODE:
  1771. tmode = le16_to_cpu(req.wIndex) >> 8;
  1772. switch (tmode) {
  1773. case TEST_J:
  1774. case TEST_K:
  1775. case TEST_SE0_NAK:
  1776. case TEST_PACKET:
  1777. case TEST_FORCE_EN:
  1778. udc->test_mode = tmode;
  1779. err = isr_setup_status_phase(
  1780. udc);
  1781. break;
  1782. default:
  1783. break;
  1784. }
  1785. default:
  1786. goto delegate;
  1787. }
  1788. } else {
  1789. goto delegate;
  1790. }
  1791. break;
  1792. default:
  1793. delegate:
  1794. if (req.wLength == 0) /* no data phase */
  1795. udc->ep0_dir = TX;
  1796. spin_unlock(udc->lock);
  1797. err = udc->driver->setup(&udc->gadget, &req);
  1798. spin_lock(udc->lock);
  1799. break;
  1800. }
  1801. if (err < 0) {
  1802. dbg_event(_usb_addr(mEp), "ERROR", err);
  1803. spin_unlock(udc->lock);
  1804. if (usb_ep_set_halt(&mEp->ep))
  1805. err("error: ep_set_halt");
  1806. spin_lock(udc->lock);
  1807. }
  1808. }
  1809. }
  1810. /******************************************************************************
  1811. * ENDPT block
  1812. *****************************************************************************/
  1813. /**
  1814. * ep_enable: configure endpoint, making it usable
  1815. *
  1816. * Check usb_ep_enable() at "usb_gadget.h" for details
  1817. */
  1818. static int ep_enable(struct usb_ep *ep,
  1819. const struct usb_endpoint_descriptor *desc)
  1820. {
  1821. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1822. int retval = 0;
  1823. unsigned long flags;
  1824. trace("%p, %p", ep, desc);
  1825. if (ep == NULL || desc == NULL)
  1826. return -EINVAL;
  1827. spin_lock_irqsave(mEp->lock, flags);
  1828. /* only internal SW should enable ctrl endpts */
  1829. mEp->desc = desc;
  1830. if (!list_empty(&mEp->qh.queue))
  1831. warn("enabling a non-empty endpoint!");
  1832. mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
  1833. mEp->num = usb_endpoint_num(desc);
  1834. mEp->type = usb_endpoint_type(desc);
  1835. mEp->ep.maxpacket = usb_endpoint_maxp(desc);
  1836. dbg_event(_usb_addr(mEp), "ENABLE", 0);
  1837. mEp->qh.ptr->cap = 0;
  1838. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1839. mEp->qh.ptr->cap |= QH_IOS;
  1840. else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
  1841. mEp->qh.ptr->cap &= ~QH_MULT;
  1842. else
  1843. mEp->qh.ptr->cap &= ~QH_ZLT;
  1844. mEp->qh.ptr->cap |=
  1845. (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
  1846. mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
  1847. /*
  1848. * Enable endpoints in the HW other than ep0 as ep0
  1849. * is always enabled
  1850. */
  1851. if (mEp->num)
  1852. retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
  1853. spin_unlock_irqrestore(mEp->lock, flags);
  1854. return retval;
  1855. }
  1856. /**
  1857. * ep_disable: endpoint is no longer usable
  1858. *
  1859. * Check usb_ep_disable() at "usb_gadget.h" for details
  1860. */
  1861. static int ep_disable(struct usb_ep *ep)
  1862. {
  1863. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1864. int direction, retval = 0;
  1865. unsigned long flags;
  1866. trace("%p", ep);
  1867. if (ep == NULL)
  1868. return -EINVAL;
  1869. else if (mEp->desc == NULL)
  1870. return -EBUSY;
  1871. spin_lock_irqsave(mEp->lock, flags);
  1872. /* only internal SW should disable ctrl endpts */
  1873. direction = mEp->dir;
  1874. do {
  1875. dbg_event(_usb_addr(mEp), "DISABLE", 0);
  1876. retval |= _ep_nuke(mEp);
  1877. retval |= hw_ep_disable(mEp->num, mEp->dir);
  1878. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1879. mEp->dir = (mEp->dir == TX) ? RX : TX;
  1880. } while (mEp->dir != direction);
  1881. mEp->desc = NULL;
  1882. mEp->ep.desc = NULL;
  1883. spin_unlock_irqrestore(mEp->lock, flags);
  1884. return retval;
  1885. }
  1886. /**
  1887. * ep_alloc_request: allocate a request object to use with this endpoint
  1888. *
  1889. * Check usb_ep_alloc_request() at "usb_gadget.h" for details
  1890. */
  1891. static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  1892. {
  1893. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1894. struct ci13xxx_req *mReq = NULL;
  1895. trace("%p, %i", ep, gfp_flags);
  1896. if (ep == NULL) {
  1897. err("EINVAL");
  1898. return NULL;
  1899. }
  1900. mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
  1901. if (mReq != NULL) {
  1902. INIT_LIST_HEAD(&mReq->queue);
  1903. mReq->req.dma = DMA_ADDR_INVALID;
  1904. mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
  1905. &mReq->dma);
  1906. if (mReq->ptr == NULL) {
  1907. kfree(mReq);
  1908. mReq = NULL;
  1909. }
  1910. }
  1911. dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
  1912. return (mReq == NULL) ? NULL : &mReq->req;
  1913. }
  1914. /**
  1915. * ep_free_request: frees a request object
  1916. *
  1917. * Check usb_ep_free_request() at "usb_gadget.h" for details
  1918. */
  1919. static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
  1920. {
  1921. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1922. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1923. unsigned long flags;
  1924. trace("%p, %p", ep, req);
  1925. if (ep == NULL || req == NULL) {
  1926. err("EINVAL");
  1927. return;
  1928. } else if (!list_empty(&mReq->queue)) {
  1929. err("EBUSY");
  1930. return;
  1931. }
  1932. spin_lock_irqsave(mEp->lock, flags);
  1933. if (mReq->ptr)
  1934. dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
  1935. kfree(mReq);
  1936. dbg_event(_usb_addr(mEp), "FREE", 0);
  1937. spin_unlock_irqrestore(mEp->lock, flags);
  1938. }
  1939. /**
  1940. * ep_queue: queues (submits) an I/O request to an endpoint
  1941. *
  1942. * Check usb_ep_queue()* at usb_gadget.h" for details
  1943. */
  1944. static int ep_queue(struct usb_ep *ep, struct usb_request *req,
  1945. gfp_t __maybe_unused gfp_flags)
  1946. {
  1947. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1948. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1949. int retval = 0;
  1950. unsigned long flags;
  1951. trace("%p, %p, %X", ep, req, gfp_flags);
  1952. if (ep == NULL || req == NULL || mEp->desc == NULL)
  1953. return -EINVAL;
  1954. spin_lock_irqsave(mEp->lock, flags);
  1955. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  1956. if (req->length)
  1957. mEp = (_udc->ep0_dir == RX) ?
  1958. &_udc->ep0out : &_udc->ep0in;
  1959. if (!list_empty(&mEp->qh.queue)) {
  1960. _ep_nuke(mEp);
  1961. retval = -EOVERFLOW;
  1962. warn("endpoint ctrl %X nuked", _usb_addr(mEp));
  1963. }
  1964. }
  1965. /* first nuke then test link, e.g. previous status has not sent */
  1966. if (!list_empty(&mReq->queue)) {
  1967. retval = -EBUSY;
  1968. err("request already in queue");
  1969. goto done;
  1970. }
  1971. if (req->length > (4 * CI13XXX_PAGE_SIZE)) {
  1972. req->length = (4 * CI13XXX_PAGE_SIZE);
  1973. retval = -EMSGSIZE;
  1974. warn("request length truncated");
  1975. }
  1976. dbg_queue(_usb_addr(mEp), req, retval);
  1977. /* push request */
  1978. mReq->req.status = -EINPROGRESS;
  1979. mReq->req.actual = 0;
  1980. retval = _hardware_enqueue(mEp, mReq);
  1981. if (retval == -EALREADY) {
  1982. dbg_event(_usb_addr(mEp), "QUEUE", retval);
  1983. retval = 0;
  1984. }
  1985. if (!retval)
  1986. list_add_tail(&mReq->queue, &mEp->qh.queue);
  1987. done:
  1988. spin_unlock_irqrestore(mEp->lock, flags);
  1989. return retval;
  1990. }
  1991. /**
  1992. * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
  1993. *
  1994. * Check usb_ep_dequeue() at "usb_gadget.h" for details
  1995. */
  1996. static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  1997. {
  1998. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1999. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  2000. unsigned long flags;
  2001. trace("%p, %p", ep, req);
  2002. if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
  2003. mEp->desc == NULL || list_empty(&mReq->queue) ||
  2004. list_empty(&mEp->qh.queue))
  2005. return -EINVAL;
  2006. spin_lock_irqsave(mEp->lock, flags);
  2007. dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
  2008. hw_ep_flush(mEp->num, mEp->dir);
  2009. /* pop request */
  2010. list_del_init(&mReq->queue);
  2011. if (mReq->map) {
  2012. dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
  2013. mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  2014. mReq->req.dma = DMA_ADDR_INVALID;
  2015. mReq->map = 0;
  2016. }
  2017. req->status = -ECONNRESET;
  2018. if (mReq->req.complete != NULL) {
  2019. spin_unlock(mEp->lock);
  2020. mReq->req.complete(&mEp->ep, &mReq->req);
  2021. spin_lock(mEp->lock);
  2022. }
  2023. spin_unlock_irqrestore(mEp->lock, flags);
  2024. return 0;
  2025. }
  2026. /**
  2027. * ep_set_halt: sets the endpoint halt feature
  2028. *
  2029. * Check usb_ep_set_halt() at "usb_gadget.h" for details
  2030. */
  2031. static int ep_set_halt(struct usb_ep *ep, int value)
  2032. {
  2033. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2034. int direction, retval = 0;
  2035. unsigned long flags;
  2036. trace("%p, %i", ep, value);
  2037. if (ep == NULL || mEp->desc == NULL)
  2038. return -EINVAL;
  2039. spin_lock_irqsave(mEp->lock, flags);
  2040. #ifndef STALL_IN
  2041. /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
  2042. if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
  2043. !list_empty(&mEp->qh.queue)) {
  2044. spin_unlock_irqrestore(mEp->lock, flags);
  2045. return -EAGAIN;
  2046. }
  2047. #endif
  2048. direction = mEp->dir;
  2049. do {
  2050. dbg_event(_usb_addr(mEp), "HALT", value);
  2051. retval |= hw_ep_set_halt(mEp->num, mEp->dir, value);
  2052. if (!value)
  2053. mEp->wedge = 0;
  2054. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  2055. mEp->dir = (mEp->dir == TX) ? RX : TX;
  2056. } while (mEp->dir != direction);
  2057. spin_unlock_irqrestore(mEp->lock, flags);
  2058. return retval;
  2059. }
  2060. /**
  2061. * ep_set_wedge: sets the halt feature and ignores clear requests
  2062. *
  2063. * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  2064. */
  2065. static int ep_set_wedge(struct usb_ep *ep)
  2066. {
  2067. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2068. unsigned long flags;
  2069. trace("%p", ep);
  2070. if (ep == NULL || mEp->desc == NULL)
  2071. return -EINVAL;
  2072. spin_lock_irqsave(mEp->lock, flags);
  2073. dbg_event(_usb_addr(mEp), "WEDGE", 0);
  2074. mEp->wedge = 1;
  2075. spin_unlock_irqrestore(mEp->lock, flags);
  2076. return usb_ep_set_halt(ep);
  2077. }
  2078. /**
  2079. * ep_fifo_flush: flushes contents of a fifo
  2080. *
  2081. * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
  2082. */
  2083. static void ep_fifo_flush(struct usb_ep *ep)
  2084. {
  2085. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2086. unsigned long flags;
  2087. trace("%p", ep);
  2088. if (ep == NULL) {
  2089. err("%02X: -EINVAL", _usb_addr(mEp));
  2090. return;
  2091. }
  2092. spin_lock_irqsave(mEp->lock, flags);
  2093. dbg_event(_usb_addr(mEp), "FFLUSH", 0);
  2094. hw_ep_flush(mEp->num, mEp->dir);
  2095. spin_unlock_irqrestore(mEp->lock, flags);
  2096. }
  2097. /**
  2098. * Endpoint-specific part of the API to the USB controller hardware
  2099. * Check "usb_gadget.h" for details
  2100. */
  2101. static const struct usb_ep_ops usb_ep_ops = {
  2102. .enable = ep_enable,
  2103. .disable = ep_disable,
  2104. .alloc_request = ep_alloc_request,
  2105. .free_request = ep_free_request,
  2106. .queue = ep_queue,
  2107. .dequeue = ep_dequeue,
  2108. .set_halt = ep_set_halt,
  2109. .set_wedge = ep_set_wedge,
  2110. .fifo_flush = ep_fifo_flush,
  2111. };
  2112. /******************************************************************************
  2113. * GADGET block
  2114. *****************************************************************************/
  2115. static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
  2116. {
  2117. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2118. unsigned long flags;
  2119. int gadget_ready = 0;
  2120. if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
  2121. return -EOPNOTSUPP;
  2122. spin_lock_irqsave(udc->lock, flags);
  2123. udc->vbus_active = is_active;
  2124. if (udc->driver)
  2125. gadget_ready = 1;
  2126. spin_unlock_irqrestore(udc->lock, flags);
  2127. if (gadget_ready) {
  2128. if (is_active) {
  2129. pm_runtime_get_sync(&_gadget->dev);
  2130. hw_device_reset(udc);
  2131. hw_device_state(udc->ep0out.qh.dma);
  2132. } else {
  2133. hw_device_state(0);
  2134. if (udc->udc_driver->notify_event)
  2135. udc->udc_driver->notify_event(udc,
  2136. CI13XXX_CONTROLLER_STOPPED_EVENT);
  2137. _gadget_stop_activity(&udc->gadget);
  2138. pm_runtime_put_sync(&_gadget->dev);
  2139. }
  2140. }
  2141. return 0;
  2142. }
  2143. static int ci13xxx_wakeup(struct usb_gadget *_gadget)
  2144. {
  2145. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2146. unsigned long flags;
  2147. int ret = 0;
  2148. trace();
  2149. spin_lock_irqsave(udc->lock, flags);
  2150. if (!udc->remote_wakeup) {
  2151. ret = -EOPNOTSUPP;
  2152. trace("remote wakeup feature is not enabled\n");
  2153. goto out;
  2154. }
  2155. if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) {
  2156. ret = -EINVAL;
  2157. trace("port is not suspended\n");
  2158. goto out;
  2159. }
  2160. hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR);
  2161. out:
  2162. spin_unlock_irqrestore(udc->lock, flags);
  2163. return ret;
  2164. }
  2165. static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  2166. {
  2167. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2168. if (udc->transceiver)
  2169. return usb_phy_set_power(udc->transceiver, mA);
  2170. return -ENOTSUPP;
  2171. }
  2172. static int ci13xxx_start(struct usb_gadget_driver *driver,
  2173. int (*bind)(struct usb_gadget *));
  2174. static int ci13xxx_stop(struct usb_gadget_driver *driver);
  2175. /**
  2176. * Device operations part of the API to the USB controller hardware,
  2177. * which don't involve endpoints (or i/o)
  2178. * Check "usb_gadget.h" for details
  2179. */
  2180. static const struct usb_gadget_ops usb_gadget_ops = {
  2181. .vbus_session = ci13xxx_vbus_session,
  2182. .wakeup = ci13xxx_wakeup,
  2183. .vbus_draw = ci13xxx_vbus_draw,
  2184. .start = ci13xxx_start,
  2185. .stop = ci13xxx_stop,
  2186. };
  2187. /**
  2188. * ci13xxx_start: register a gadget driver
  2189. * @driver: the driver being registered
  2190. * @bind: the driver's bind callback
  2191. *
  2192. * Check ci13xxx_start() at <linux/usb/gadget.h> for details.
  2193. * Interrupts are enabled here.
  2194. */
  2195. static int ci13xxx_start(struct usb_gadget_driver *driver,
  2196. int (*bind)(struct usb_gadget *))
  2197. {
  2198. struct ci13xxx *udc = _udc;
  2199. unsigned long flags;
  2200. int i, j;
  2201. int retval = -ENOMEM;
  2202. trace("%p", driver);
  2203. if (driver == NULL ||
  2204. bind == NULL ||
  2205. driver->setup == NULL ||
  2206. driver->disconnect == NULL)
  2207. return -EINVAL;
  2208. else if (udc == NULL)
  2209. return -ENODEV;
  2210. else if (udc->driver != NULL)
  2211. return -EBUSY;
  2212. /* alloc resources */
  2213. udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
  2214. sizeof(struct ci13xxx_qh),
  2215. 64, CI13XXX_PAGE_SIZE);
  2216. if (udc->qh_pool == NULL)
  2217. return -ENOMEM;
  2218. udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
  2219. sizeof(struct ci13xxx_td),
  2220. 64, CI13XXX_PAGE_SIZE);
  2221. if (udc->td_pool == NULL) {
  2222. dma_pool_destroy(udc->qh_pool);
  2223. udc->qh_pool = NULL;
  2224. return -ENOMEM;
  2225. }
  2226. spin_lock_irqsave(udc->lock, flags);
  2227. info("hw_ep_max = %d", hw_ep_max);
  2228. udc->gadget.dev.driver = NULL;
  2229. retval = 0;
  2230. for (i = 0; i < hw_ep_max/2; i++) {
  2231. for (j = RX; j <= TX; j++) {
  2232. int k = i + j * hw_ep_max/2;
  2233. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
  2234. scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
  2235. (j == TX) ? "in" : "out");
  2236. mEp->lock = udc->lock;
  2237. mEp->device = &udc->gadget.dev;
  2238. mEp->td_pool = udc->td_pool;
  2239. mEp->ep.name = mEp->name;
  2240. mEp->ep.ops = &usb_ep_ops;
  2241. mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
  2242. INIT_LIST_HEAD(&mEp->qh.queue);
  2243. spin_unlock_irqrestore(udc->lock, flags);
  2244. mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
  2245. &mEp->qh.dma);
  2246. spin_lock_irqsave(udc->lock, flags);
  2247. if (mEp->qh.ptr == NULL)
  2248. retval = -ENOMEM;
  2249. else
  2250. memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
  2251. /* skip ep0 out and in endpoints */
  2252. if (i == 0)
  2253. continue;
  2254. list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
  2255. }
  2256. }
  2257. if (retval)
  2258. goto done;
  2259. spin_unlock_irqrestore(udc->lock, flags);
  2260. udc->ep0out.ep.desc = &ctrl_endpt_out_desc;
  2261. retval = usb_ep_enable(&udc->ep0out.ep);
  2262. if (retval)
  2263. return retval;
  2264. udc->ep0in.ep.desc = &ctrl_endpt_in_desc;
  2265. retval = usb_ep_enable(&udc->ep0in.ep);
  2266. if (retval)
  2267. return retval;
  2268. spin_lock_irqsave(udc->lock, flags);
  2269. udc->gadget.ep0 = &udc->ep0in.ep;
  2270. /* bind gadget */
  2271. driver->driver.bus = NULL;
  2272. udc->gadget.dev.driver = &driver->driver;
  2273. spin_unlock_irqrestore(udc->lock, flags);
  2274. retval = bind(&udc->gadget); /* MAY SLEEP */
  2275. spin_lock_irqsave(udc->lock, flags);
  2276. if (retval) {
  2277. udc->gadget.dev.driver = NULL;
  2278. goto done;
  2279. }
  2280. udc->driver = driver;
  2281. pm_runtime_get_sync(&udc->gadget.dev);
  2282. if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
  2283. if (udc->vbus_active) {
  2284. if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
  2285. hw_device_reset(udc);
  2286. } else {
  2287. pm_runtime_put_sync(&udc->gadget.dev);
  2288. goto done;
  2289. }
  2290. }
  2291. retval = hw_device_state(udc->ep0out.qh.dma);
  2292. if (retval)
  2293. pm_runtime_put_sync(&udc->gadget.dev);
  2294. done:
  2295. spin_unlock_irqrestore(udc->lock, flags);
  2296. return retval;
  2297. }
  2298. /**
  2299. * ci13xxx_stop: unregister a gadget driver
  2300. *
  2301. * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
  2302. */
  2303. static int ci13xxx_stop(struct usb_gadget_driver *driver)
  2304. {
  2305. struct ci13xxx *udc = _udc;
  2306. unsigned long i, flags;
  2307. trace("%p", driver);
  2308. if (driver == NULL ||
  2309. driver->unbind == NULL ||
  2310. driver->setup == NULL ||
  2311. driver->disconnect == NULL ||
  2312. driver != udc->driver)
  2313. return -EINVAL;
  2314. spin_lock_irqsave(udc->lock, flags);
  2315. if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
  2316. udc->vbus_active) {
  2317. hw_device_state(0);
  2318. if (udc->udc_driver->notify_event)
  2319. udc->udc_driver->notify_event(udc,
  2320. CI13XXX_CONTROLLER_STOPPED_EVENT);
  2321. spin_unlock_irqrestore(udc->lock, flags);
  2322. _gadget_stop_activity(&udc->gadget);
  2323. spin_lock_irqsave(udc->lock, flags);
  2324. pm_runtime_put(&udc->gadget.dev);
  2325. }
  2326. /* unbind gadget */
  2327. spin_unlock_irqrestore(udc->lock, flags);
  2328. driver->unbind(&udc->gadget); /* MAY SLEEP */
  2329. spin_lock_irqsave(udc->lock, flags);
  2330. udc->gadget.dev.driver = NULL;
  2331. /* free resources */
  2332. for (i = 0; i < hw_ep_max; i++) {
  2333. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
  2334. if (!list_empty(&mEp->ep.ep_list))
  2335. list_del_init(&mEp->ep.ep_list);
  2336. if (mEp->qh.ptr != NULL)
  2337. dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
  2338. }
  2339. udc->gadget.ep0 = NULL;
  2340. udc->driver = NULL;
  2341. spin_unlock_irqrestore(udc->lock, flags);
  2342. if (udc->td_pool != NULL) {
  2343. dma_pool_destroy(udc->td_pool);
  2344. udc->td_pool = NULL;
  2345. }
  2346. if (udc->qh_pool != NULL) {
  2347. dma_pool_destroy(udc->qh_pool);
  2348. udc->qh_pool = NULL;
  2349. }
  2350. return 0;
  2351. }
  2352. /******************************************************************************
  2353. * BUS block
  2354. *****************************************************************************/
  2355. /**
  2356. * udc_irq: global interrupt handler
  2357. *
  2358. * This function returns IRQ_HANDLED if the IRQ has been handled
  2359. * It locks access to registers
  2360. */
  2361. static irqreturn_t udc_irq(void)
  2362. {
  2363. struct ci13xxx *udc = _udc;
  2364. irqreturn_t retval;
  2365. u32 intr;
  2366. trace();
  2367. if (udc == NULL) {
  2368. err("ENODEV");
  2369. return IRQ_HANDLED;
  2370. }
  2371. spin_lock(udc->lock);
  2372. if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
  2373. if (hw_cread(CAP_USBMODE, USBMODE_CM) !=
  2374. USBMODE_CM_DEVICE) {
  2375. spin_unlock(udc->lock);
  2376. return IRQ_NONE;
  2377. }
  2378. }
  2379. intr = hw_test_and_clear_intr_active();
  2380. if (intr) {
  2381. isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
  2382. isr_statistics.hndl.idx &= ISR_MASK;
  2383. isr_statistics.hndl.cnt++;
  2384. /* order defines priority - do NOT change it */
  2385. if (USBi_URI & intr) {
  2386. isr_statistics.uri++;
  2387. isr_reset_handler(udc);
  2388. }
  2389. if (USBi_PCI & intr) {
  2390. isr_statistics.pci++;
  2391. udc->gadget.speed = hw_port_is_high_speed() ?
  2392. USB_SPEED_HIGH : USB_SPEED_FULL;
  2393. if (udc->suspended && udc->driver->resume) {
  2394. spin_unlock(udc->lock);
  2395. udc->driver->resume(&udc->gadget);
  2396. spin_lock(udc->lock);
  2397. udc->suspended = 0;
  2398. }
  2399. }
  2400. if (USBi_UEI & intr)
  2401. isr_statistics.uei++;
  2402. if (USBi_UI & intr) {
  2403. isr_statistics.ui++;
  2404. isr_tr_complete_handler(udc);
  2405. }
  2406. if (USBi_SLI & intr) {
  2407. if (udc->gadget.speed != USB_SPEED_UNKNOWN &&
  2408. udc->driver->suspend) {
  2409. udc->suspended = 1;
  2410. spin_unlock(udc->lock);
  2411. udc->driver->suspend(&udc->gadget);
  2412. spin_lock(udc->lock);
  2413. }
  2414. isr_statistics.sli++;
  2415. }
  2416. retval = IRQ_HANDLED;
  2417. } else {
  2418. isr_statistics.none++;
  2419. retval = IRQ_NONE;
  2420. }
  2421. spin_unlock(udc->lock);
  2422. return retval;
  2423. }
  2424. /**
  2425. * udc_release: driver release function
  2426. * @dev: device
  2427. *
  2428. * Currently does nothing
  2429. */
  2430. static void udc_release(struct device *dev)
  2431. {
  2432. trace("%p", dev);
  2433. if (dev == NULL)
  2434. err("EINVAL");
  2435. }
  2436. /**
  2437. * udc_probe: parent probe must call this to initialize UDC
  2438. * @dev: parent device
  2439. * @regs: registers base address
  2440. * @name: driver name
  2441. *
  2442. * This function returns an error code
  2443. * No interrupts active, the IRQ has not been requested yet
  2444. * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
  2445. */
  2446. static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
  2447. void __iomem *regs)
  2448. {
  2449. struct ci13xxx *udc;
  2450. int retval = 0;
  2451. trace("%p, %p, %p", dev, regs, driver->name);
  2452. if (dev == NULL || regs == NULL || driver == NULL ||
  2453. driver->name == NULL)
  2454. return -EINVAL;
  2455. udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
  2456. if (udc == NULL)
  2457. return -ENOMEM;
  2458. udc->lock = &udc_lock;
  2459. udc->regs = regs;
  2460. udc->udc_driver = driver;
  2461. udc->gadget.ops = &usb_gadget_ops;
  2462. udc->gadget.speed = USB_SPEED_UNKNOWN;
  2463. udc->gadget.max_speed = USB_SPEED_HIGH;
  2464. udc->gadget.is_otg = 0;
  2465. udc->gadget.name = driver->name;
  2466. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2467. udc->gadget.ep0 = NULL;
  2468. dev_set_name(&udc->gadget.dev, "gadget");
  2469. udc->gadget.dev.dma_mask = dev->dma_mask;
  2470. udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
  2471. udc->gadget.dev.parent = dev;
  2472. udc->gadget.dev.release = udc_release;
  2473. retval = hw_device_init(regs);
  2474. if (retval < 0)
  2475. goto free_udc;
  2476. udc->transceiver = usb_get_transceiver();
  2477. if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
  2478. if (udc->transceiver == NULL) {
  2479. retval = -ENODEV;
  2480. goto free_udc;
  2481. }
  2482. }
  2483. if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
  2484. retval = hw_device_reset(udc);
  2485. if (retval)
  2486. goto put_transceiver;
  2487. }
  2488. retval = device_register(&udc->gadget.dev);
  2489. if (retval) {
  2490. put_device(&udc->gadget.dev);
  2491. goto put_transceiver;
  2492. }
  2493. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2494. retval = dbg_create_files(&udc->gadget.dev);
  2495. #endif
  2496. if (retval)
  2497. goto unreg_device;
  2498. if (udc->transceiver) {
  2499. retval = otg_set_peripheral(udc->transceiver->otg,
  2500. &udc->gadget);
  2501. if (retval)
  2502. goto remove_dbg;
  2503. }
  2504. retval = usb_add_gadget_udc(dev, &udc->gadget);
  2505. if (retval)
  2506. goto remove_trans;
  2507. pm_runtime_no_callbacks(&udc->gadget.dev);
  2508. pm_runtime_enable(&udc->gadget.dev);
  2509. _udc = udc;
  2510. return retval;
  2511. remove_trans:
  2512. if (udc->transceiver) {
  2513. otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
  2514. usb_put_transceiver(udc->transceiver);
  2515. }
  2516. err("error = %i", retval);
  2517. remove_dbg:
  2518. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2519. dbg_remove_files(&udc->gadget.dev);
  2520. #endif
  2521. unreg_device:
  2522. device_unregister(&udc->gadget.dev);
  2523. put_transceiver:
  2524. if (udc->transceiver)
  2525. usb_put_transceiver(udc->transceiver);
  2526. free_udc:
  2527. kfree(udc);
  2528. _udc = NULL;
  2529. return retval;
  2530. }
  2531. /**
  2532. * udc_remove: parent remove must call this to remove UDC
  2533. *
  2534. * No interrupts active, the IRQ has been released
  2535. */
  2536. static void udc_remove(void)
  2537. {
  2538. struct ci13xxx *udc = _udc;
  2539. if (udc == NULL) {
  2540. err("EINVAL");
  2541. return;
  2542. }
  2543. usb_del_gadget_udc(&udc->gadget);
  2544. if (udc->transceiver) {
  2545. otg_set_peripheral(udc->transceiver->otg, &udc->gadget);
  2546. usb_put_transceiver(udc->transceiver);
  2547. }
  2548. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2549. dbg_remove_files(&udc->gadget.dev);
  2550. #endif
  2551. device_unregister(&udc->gadget.dev);
  2552. kfree(udc);
  2553. _udc = NULL;
  2554. }