net2280.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  1. /*
  2. * Driver for the PLX NET2280 USB device controller.
  3. * Specs and errata are available from <http://www.plxtech.com>.
  4. *
  5. * PLX Technology Inc. (formerly NetChip Technology) supported the
  6. * development of this driver.
  7. *
  8. *
  9. * CODE STATUS HIGHLIGHTS
  10. *
  11. * This driver should work well with most "gadget" drivers, including
  12. * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
  13. * as well as Gadget Zero and Gadgetfs.
  14. *
  15. * DMA is enabled by default. Drivers using transfer queues might use
  16. * DMA chaining to remove IRQ latencies between transfers. (Except when
  17. * short OUT transfers happen.) Drivers can use the req->no_interrupt
  18. * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
  19. * and DMA chaining is enabled.
  20. *
  21. * Note that almost all the errata workarounds here are only needed for
  22. * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
  23. */
  24. /*
  25. * Copyright (C) 2003 David Brownell
  26. * Copyright (C) 2003-2005 PLX Technology, Inc.
  27. *
  28. * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
  29. * with 2282 chip
  30. *
  31. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation; either version 2 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software
  43. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  44. */
  45. #undef DEBUG /* messages on error and most fault paths */
  46. #undef VERBOSE /* extra debug messages (success too) */
  47. #include <linux/module.h>
  48. #include <linux/pci.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/kernel.h>
  51. #include <linux/delay.h>
  52. #include <linux/ioport.h>
  53. #include <linux/slab.h>
  54. #include <linux/smp_lock.h>
  55. #include <linux/errno.h>
  56. #include <linux/init.h>
  57. #include <linux/timer.h>
  58. #include <linux/list.h>
  59. #include <linux/interrupt.h>
  60. #include <linux/moduleparam.h>
  61. #include <linux/device.h>
  62. #include <linux/usb/ch9.h>
  63. #include <linux/usb_gadget.h>
  64. #include <asm/byteorder.h>
  65. #include <asm/io.h>
  66. #include <asm/irq.h>
  67. #include <asm/system.h>
  68. #include <asm/unaligned.h>
  69. #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
  70. #define DRIVER_VERSION "2005 Sept 27"
  71. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  72. #define EP_DONTUSE 13 /* nonzero */
  73. #define USE_RDK_LEDS /* GPIO pins control three LEDs */
  74. static const char driver_name [] = "net2280";
  75. static const char driver_desc [] = DRIVER_DESC;
  76. static const char ep0name [] = "ep0";
  77. static const char *const ep_name [] = {
  78. ep0name,
  79. "ep-a", "ep-b", "ep-c", "ep-d",
  80. "ep-e", "ep-f",
  81. };
  82. /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
  83. * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
  84. *
  85. * The net2280 DMA engines are not tightly integrated with their FIFOs;
  86. * not all cases are (yet) handled well in this driver or the silicon.
  87. * Some gadget drivers work better with the dma support here than others.
  88. * These two parameters let you use PIO or more aggressive DMA.
  89. */
  90. static int use_dma = 1;
  91. static int use_dma_chaining = 0;
  92. /* "modprobe net2280 use_dma=n" etc */
  93. module_param (use_dma, bool, S_IRUGO);
  94. module_param (use_dma_chaining, bool, S_IRUGO);
  95. /* mode 0 == ep-{a,b,c,d} 1K fifo each
  96. * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
  97. * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
  98. */
  99. static ushort fifo_mode = 0;
  100. /* "modprobe net2280 fifo_mode=1" etc */
  101. module_param (fifo_mode, ushort, 0644);
  102. /* enable_suspend -- When enabled, the driver will respond to
  103. * USB suspend requests by powering down the NET2280. Otherwise,
  104. * USB suspend requests will be ignored. This is acceptible for
  105. * self-powered devices
  106. */
  107. static int enable_suspend = 0;
  108. /* "modprobe net2280 enable_suspend=1" etc */
  109. module_param (enable_suspend, bool, S_IRUGO);
  110. #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
  111. #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
  112. static char *type_string (u8 bmAttributes)
  113. {
  114. switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
  115. case USB_ENDPOINT_XFER_BULK: return "bulk";
  116. case USB_ENDPOINT_XFER_ISOC: return "iso";
  117. case USB_ENDPOINT_XFER_INT: return "intr";
  118. };
  119. return "control";
  120. }
  121. #endif
  122. #include "net2280.h"
  123. #define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
  124. #define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
  125. /*-------------------------------------------------------------------------*/
  126. static int
  127. net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  128. {
  129. struct net2280 *dev;
  130. struct net2280_ep *ep;
  131. u32 max, tmp;
  132. unsigned long flags;
  133. ep = container_of (_ep, struct net2280_ep, ep);
  134. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  135. || desc->bDescriptorType != USB_DT_ENDPOINT)
  136. return -EINVAL;
  137. dev = ep->dev;
  138. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  139. return -ESHUTDOWN;
  140. /* erratum 0119 workaround ties up an endpoint number */
  141. if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
  142. return -EDOM;
  143. /* sanity check ep-e/ep-f since their fifos are small */
  144. max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
  145. if (ep->num > 4 && max > 64)
  146. return -ERANGE;
  147. spin_lock_irqsave (&dev->lock, flags);
  148. _ep->maxpacket = max & 0x7ff;
  149. ep->desc = desc;
  150. /* ep_reset() has already been called */
  151. ep->stopped = 0;
  152. ep->out_overflow = 0;
  153. /* set speed-dependent max packet; may kick in high bandwidth */
  154. set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
  155. /* FIFO lines can't go to different packets. PIO is ok, so
  156. * use it instead of troublesome (non-bulk) multi-packet DMA.
  157. */
  158. if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
  159. DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
  160. ep->ep.name, ep->ep.maxpacket);
  161. ep->dma = NULL;
  162. }
  163. /* set type, direction, address; reset fifo counters */
  164. writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
  165. tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  166. if (tmp == USB_ENDPOINT_XFER_INT) {
  167. /* erratum 0105 workaround prevents hs NYET */
  168. if (dev->chiprev == 0100
  169. && dev->gadget.speed == USB_SPEED_HIGH
  170. && !(desc->bEndpointAddress & USB_DIR_IN))
  171. writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
  172. &ep->regs->ep_rsp);
  173. } else if (tmp == USB_ENDPOINT_XFER_BULK) {
  174. /* catch some particularly blatant driver bugs */
  175. if ((dev->gadget.speed == USB_SPEED_HIGH
  176. && max != 512)
  177. || (dev->gadget.speed == USB_SPEED_FULL
  178. && max > 64)) {
  179. spin_unlock_irqrestore (&dev->lock, flags);
  180. return -ERANGE;
  181. }
  182. }
  183. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
  184. tmp <<= ENDPOINT_TYPE;
  185. tmp |= desc->bEndpointAddress;
  186. tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
  187. tmp |= 1 << ENDPOINT_ENABLE;
  188. wmb ();
  189. /* for OUT transfers, block the rx fifo until a read is posted */
  190. ep->is_in = (tmp & USB_DIR_IN) != 0;
  191. if (!ep->is_in)
  192. writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  193. else if (dev->pdev->device != 0x2280) {
  194. /* Added for 2282, Don't use nak packets on an in endpoint,
  195. * this was ignored on 2280
  196. */
  197. writel ((1 << CLEAR_NAK_OUT_PACKETS)
  198. | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
  199. }
  200. writel (tmp, &ep->regs->ep_cfg);
  201. /* enable irqs */
  202. if (!ep->dma) { /* pio, per-packet */
  203. tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
  204. writel (tmp, &dev->regs->pciirqenb0);
  205. tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
  206. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
  207. if (dev->pdev->device == 0x2280)
  208. tmp |= readl (&ep->regs->ep_irqenb);
  209. writel (tmp, &ep->regs->ep_irqenb);
  210. } else { /* dma, per-request */
  211. tmp = (1 << (8 + ep->num)); /* completion */
  212. tmp |= readl (&dev->regs->pciirqenb1);
  213. writel (tmp, &dev->regs->pciirqenb1);
  214. /* for short OUT transfers, dma completions can't
  215. * advance the queue; do it pio-style, by hand.
  216. * NOTE erratum 0112 workaround #2
  217. */
  218. if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
  219. tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
  220. writel (tmp, &ep->regs->ep_irqenb);
  221. tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
  222. writel (tmp, &dev->regs->pciirqenb0);
  223. }
  224. }
  225. tmp = desc->bEndpointAddress;
  226. DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
  227. _ep->name, tmp & 0x0f, DIR_STRING (tmp),
  228. type_string (desc->bmAttributes),
  229. ep->dma ? "dma" : "pio", max);
  230. /* pci writes may still be posted */
  231. spin_unlock_irqrestore (&dev->lock, flags);
  232. return 0;
  233. }
  234. static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
  235. {
  236. u32 result;
  237. do {
  238. result = readl (ptr);
  239. if (result == ~(u32)0) /* "device unplugged" */
  240. return -ENODEV;
  241. result &= mask;
  242. if (result == done)
  243. return 0;
  244. udelay (1);
  245. usec--;
  246. } while (usec > 0);
  247. return -ETIMEDOUT;
  248. }
  249. static const struct usb_ep_ops net2280_ep_ops;
  250. static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
  251. {
  252. u32 tmp;
  253. ep->desc = NULL;
  254. INIT_LIST_HEAD (&ep->queue);
  255. ep->ep.maxpacket = ~0;
  256. ep->ep.ops = &net2280_ep_ops;
  257. /* disable the dma, irqs, endpoint... */
  258. if (ep->dma) {
  259. writel (0, &ep->dma->dmactl);
  260. writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
  261. | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
  262. | (1 << DMA_ABORT)
  263. , &ep->dma->dmastat);
  264. tmp = readl (&regs->pciirqenb0);
  265. tmp &= ~(1 << ep->num);
  266. writel (tmp, &regs->pciirqenb0);
  267. } else {
  268. tmp = readl (&regs->pciirqenb1);
  269. tmp &= ~(1 << (8 + ep->num)); /* completion */
  270. writel (tmp, &regs->pciirqenb1);
  271. }
  272. writel (0, &ep->regs->ep_irqenb);
  273. /* init to our chosen defaults, notably so that we NAK OUT
  274. * packets until the driver queues a read (+note erratum 0112)
  275. */
  276. if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
  277. tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
  278. | (1 << SET_NAK_OUT_PACKETS)
  279. | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  280. | (1 << CLEAR_INTERRUPT_MODE);
  281. } else {
  282. /* added for 2282 */
  283. tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
  284. | (1 << CLEAR_NAK_OUT_PACKETS)
  285. | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  286. | (1 << CLEAR_INTERRUPT_MODE);
  287. }
  288. if (ep->num != 0) {
  289. tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
  290. | (1 << CLEAR_ENDPOINT_HALT);
  291. }
  292. writel (tmp, &ep->regs->ep_rsp);
  293. /* scrub most status bits, and flush any fifo state */
  294. if (ep->dev->pdev->device == 0x2280)
  295. tmp = (1 << FIFO_OVERFLOW)
  296. | (1 << FIFO_UNDERFLOW);
  297. else
  298. tmp = 0;
  299. writel (tmp | (1 << TIMEOUT)
  300. | (1 << USB_STALL_SENT)
  301. | (1 << USB_IN_NAK_SENT)
  302. | (1 << USB_IN_ACK_RCVD)
  303. | (1 << USB_OUT_PING_NAK_SENT)
  304. | (1 << USB_OUT_ACK_SENT)
  305. | (1 << FIFO_FLUSH)
  306. | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
  307. | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
  308. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  309. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  310. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  311. | (1 << DATA_IN_TOKEN_INTERRUPT)
  312. , &ep->regs->ep_stat);
  313. /* fifo size is handled separately */
  314. }
  315. static void nuke (struct net2280_ep *);
  316. static int net2280_disable (struct usb_ep *_ep)
  317. {
  318. struct net2280_ep *ep;
  319. unsigned long flags;
  320. ep = container_of (_ep, struct net2280_ep, ep);
  321. if (!_ep || !ep->desc || _ep->name == ep0name)
  322. return -EINVAL;
  323. spin_lock_irqsave (&ep->dev->lock, flags);
  324. nuke (ep);
  325. ep_reset (ep->dev->regs, ep);
  326. VDEBUG (ep->dev, "disabled %s %s\n",
  327. ep->dma ? "dma" : "pio", _ep->name);
  328. /* synch memory views with the device */
  329. (void) readl (&ep->regs->ep_cfg);
  330. if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
  331. ep->dma = &ep->dev->dma [ep->num - 1];
  332. spin_unlock_irqrestore (&ep->dev->lock, flags);
  333. return 0;
  334. }
  335. /*-------------------------------------------------------------------------*/
  336. static struct usb_request *
  337. net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  338. {
  339. struct net2280_ep *ep;
  340. struct net2280_request *req;
  341. if (!_ep)
  342. return NULL;
  343. ep = container_of (_ep, struct net2280_ep, ep);
  344. req = kzalloc(sizeof(*req), gfp_flags);
  345. if (!req)
  346. return NULL;
  347. req->req.dma = DMA_ADDR_INVALID;
  348. INIT_LIST_HEAD (&req->queue);
  349. /* this dma descriptor may be swapped with the previous dummy */
  350. if (ep->dma) {
  351. struct net2280_dma *td;
  352. td = pci_pool_alloc (ep->dev->requests, gfp_flags,
  353. &req->td_dma);
  354. if (!td) {
  355. kfree (req);
  356. return NULL;
  357. }
  358. td->dmacount = 0; /* not VALID */
  359. td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
  360. td->dmadesc = td->dmaaddr;
  361. req->td = td;
  362. }
  363. return &req->req;
  364. }
  365. static void
  366. net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
  367. {
  368. struct net2280_ep *ep;
  369. struct net2280_request *req;
  370. ep = container_of (_ep, struct net2280_ep, ep);
  371. if (!_ep || !_req)
  372. return;
  373. req = container_of (_req, struct net2280_request, req);
  374. WARN_ON (!list_empty (&req->queue));
  375. if (req->td)
  376. pci_pool_free (ep->dev->requests, req->td, req->td_dma);
  377. kfree (req);
  378. }
  379. /*-------------------------------------------------------------------------*/
  380. /*
  381. * dma-coherent memory allocation (for dma-capable endpoints)
  382. *
  383. * NOTE: the dma_*_coherent() API calls suck. Most implementations are
  384. * (a) page-oriented, so small buffers lose big; and (b) asymmetric with
  385. * respect to calls with irqs disabled: alloc is safe, free is not.
  386. * We currently work around (b), but not (a).
  387. */
  388. static void *
  389. net2280_alloc_buffer (
  390. struct usb_ep *_ep,
  391. unsigned bytes,
  392. dma_addr_t *dma,
  393. gfp_t gfp_flags
  394. )
  395. {
  396. void *retval;
  397. struct net2280_ep *ep;
  398. ep = container_of (_ep, struct net2280_ep, ep);
  399. if (!_ep)
  400. return NULL;
  401. *dma = DMA_ADDR_INVALID;
  402. if (ep->dma)
  403. retval = dma_alloc_coherent(&ep->dev->pdev->dev,
  404. bytes, dma, gfp_flags);
  405. else
  406. retval = kmalloc(bytes, gfp_flags);
  407. return retval;
  408. }
  409. static DEFINE_SPINLOCK(buflock);
  410. static LIST_HEAD(buffers);
  411. struct free_record {
  412. struct list_head list;
  413. struct device *dev;
  414. unsigned bytes;
  415. dma_addr_t dma;
  416. };
  417. static void do_free(unsigned long ignored)
  418. {
  419. spin_lock_irq(&buflock);
  420. while (!list_empty(&buffers)) {
  421. struct free_record *buf;
  422. buf = list_entry(buffers.next, struct free_record, list);
  423. list_del(&buf->list);
  424. spin_unlock_irq(&buflock);
  425. dma_free_coherent(buf->dev, buf->bytes, buf, buf->dma);
  426. spin_lock_irq(&buflock);
  427. }
  428. spin_unlock_irq(&buflock);
  429. }
  430. static DECLARE_TASKLET(deferred_free, do_free, 0);
  431. static void
  432. net2280_free_buffer (
  433. struct usb_ep *_ep,
  434. void *address,
  435. dma_addr_t dma,
  436. unsigned bytes
  437. ) {
  438. /* free memory into the right allocator */
  439. if (dma != DMA_ADDR_INVALID) {
  440. struct net2280_ep *ep;
  441. struct free_record *buf = address;
  442. unsigned long flags;
  443. ep = container_of(_ep, struct net2280_ep, ep);
  444. if (!_ep)
  445. return;
  446. ep = container_of (_ep, struct net2280_ep, ep);
  447. buf->dev = &ep->dev->pdev->dev;
  448. buf->bytes = bytes;
  449. buf->dma = dma;
  450. spin_lock_irqsave(&buflock, flags);
  451. list_add_tail(&buf->list, &buffers);
  452. tasklet_schedule(&deferred_free);
  453. spin_unlock_irqrestore(&buflock, flags);
  454. } else
  455. kfree (address);
  456. }
  457. /*-------------------------------------------------------------------------*/
  458. /* load a packet into the fifo we use for usb IN transfers.
  459. * works for all endpoints.
  460. *
  461. * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
  462. * at a time, but this code is simpler because it knows it only writes
  463. * one packet. ep-a..ep-d should use dma instead.
  464. */
  465. static void
  466. write_fifo (struct net2280_ep *ep, struct usb_request *req)
  467. {
  468. struct net2280_ep_regs __iomem *regs = ep->regs;
  469. u8 *buf;
  470. u32 tmp;
  471. unsigned count, total;
  472. /* INVARIANT: fifo is currently empty. (testable) */
  473. if (req) {
  474. buf = req->buf + req->actual;
  475. prefetch (buf);
  476. total = req->length - req->actual;
  477. } else {
  478. total = 0;
  479. buf = NULL;
  480. }
  481. /* write just one packet at a time */
  482. count = ep->ep.maxpacket;
  483. if (count > total) /* min() cannot be used on a bitfield */
  484. count = total;
  485. VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
  486. ep->ep.name, count,
  487. (count != ep->ep.maxpacket) ? " (short)" : "",
  488. req);
  489. while (count >= 4) {
  490. /* NOTE be careful if you try to align these. fifo lines
  491. * should normally be full (4 bytes) and successive partial
  492. * lines are ok only in certain cases.
  493. */
  494. tmp = get_unaligned ((u32 *)buf);
  495. cpu_to_le32s (&tmp);
  496. writel (tmp, &regs->ep_data);
  497. buf += 4;
  498. count -= 4;
  499. }
  500. /* last fifo entry is "short" unless we wrote a full packet.
  501. * also explicitly validate last word in (periodic) transfers
  502. * when maxpacket is not a multiple of 4 bytes.
  503. */
  504. if (count || total < ep->ep.maxpacket) {
  505. tmp = count ? get_unaligned ((u32 *)buf) : count;
  506. cpu_to_le32s (&tmp);
  507. set_fifo_bytecount (ep, count & 0x03);
  508. writel (tmp, &regs->ep_data);
  509. }
  510. /* pci writes may still be posted */
  511. }
  512. /* work around erratum 0106: PCI and USB race over the OUT fifo.
  513. * caller guarantees chiprev 0100, out endpoint is NAKing, and
  514. * there's no real data in the fifo.
  515. *
  516. * NOTE: also used in cases where that erratum doesn't apply:
  517. * where the host wrote "too much" data to us.
  518. */
  519. static void out_flush (struct net2280_ep *ep)
  520. {
  521. u32 __iomem *statp;
  522. u32 tmp;
  523. ASSERT_OUT_NAKING (ep);
  524. statp = &ep->regs->ep_stat;
  525. writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  526. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  527. , statp);
  528. writel ((1 << FIFO_FLUSH), statp);
  529. mb ();
  530. tmp = readl (statp);
  531. if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  532. /* high speed did bulk NYET; fifo isn't filling */
  533. && ep->dev->gadget.speed == USB_SPEED_FULL) {
  534. unsigned usec;
  535. usec = 50; /* 64 byte bulk/interrupt */
  536. handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
  537. (1 << USB_OUT_PING_NAK_SENT), usec);
  538. /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
  539. }
  540. }
  541. /* unload packet(s) from the fifo we use for usb OUT transfers.
  542. * returns true iff the request completed, because of short packet
  543. * or the request buffer having filled with full packets.
  544. *
  545. * for ep-a..ep-d this will read multiple packets out when they
  546. * have been accepted.
  547. */
  548. static int
  549. read_fifo (struct net2280_ep *ep, struct net2280_request *req)
  550. {
  551. struct net2280_ep_regs __iomem *regs = ep->regs;
  552. u8 *buf = req->req.buf + req->req.actual;
  553. unsigned count, tmp, is_short;
  554. unsigned cleanup = 0, prevent = 0;
  555. /* erratum 0106 ... packets coming in during fifo reads might
  556. * be incompletely rejected. not all cases have workarounds.
  557. */
  558. if (ep->dev->chiprev == 0x0100
  559. && ep->dev->gadget.speed == USB_SPEED_FULL) {
  560. udelay (1);
  561. tmp = readl (&ep->regs->ep_stat);
  562. if ((tmp & (1 << NAK_OUT_PACKETS)))
  563. cleanup = 1;
  564. else if ((tmp & (1 << FIFO_FULL))) {
  565. start_out_naking (ep);
  566. prevent = 1;
  567. }
  568. /* else: hope we don't see the problem */
  569. }
  570. /* never overflow the rx buffer. the fifo reads packets until
  571. * it sees a short one; we might not be ready for them all.
  572. */
  573. prefetchw (buf);
  574. count = readl (&regs->ep_avail);
  575. if (unlikely (count == 0)) {
  576. udelay (1);
  577. tmp = readl (&ep->regs->ep_stat);
  578. count = readl (&regs->ep_avail);
  579. /* handled that data already? */
  580. if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
  581. return 0;
  582. }
  583. tmp = req->req.length - req->req.actual;
  584. if (count > tmp) {
  585. /* as with DMA, data overflow gets flushed */
  586. if ((tmp % ep->ep.maxpacket) != 0) {
  587. ERROR (ep->dev,
  588. "%s out fifo %d bytes, expected %d\n",
  589. ep->ep.name, count, tmp);
  590. req->req.status = -EOVERFLOW;
  591. cleanup = 1;
  592. /* NAK_OUT_PACKETS will be set, so flushing is safe;
  593. * the next read will start with the next packet
  594. */
  595. } /* else it's a ZLP, no worries */
  596. count = tmp;
  597. }
  598. req->req.actual += count;
  599. is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
  600. VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
  601. ep->ep.name, count, is_short ? " (short)" : "",
  602. cleanup ? " flush" : "", prevent ? " nak" : "",
  603. req, req->req.actual, req->req.length);
  604. while (count >= 4) {
  605. tmp = readl (&regs->ep_data);
  606. cpu_to_le32s (&tmp);
  607. put_unaligned (tmp, (u32 *)buf);
  608. buf += 4;
  609. count -= 4;
  610. }
  611. if (count) {
  612. tmp = readl (&regs->ep_data);
  613. /* LE conversion is implicit here: */
  614. do {
  615. *buf++ = (u8) tmp;
  616. tmp >>= 8;
  617. } while (--count);
  618. }
  619. if (cleanup)
  620. out_flush (ep);
  621. if (prevent) {
  622. writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  623. (void) readl (&ep->regs->ep_rsp);
  624. }
  625. return is_short || ((req->req.actual == req->req.length)
  626. && !req->req.zero);
  627. }
  628. /* fill out dma descriptor to match a given request */
  629. static void
  630. fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
  631. {
  632. struct net2280_dma *td = req->td;
  633. u32 dmacount = req->req.length;
  634. /* don't let DMA continue after a short OUT packet,
  635. * so overruns can't affect the next transfer.
  636. * in case of overruns on max-size packets, we can't
  637. * stop the fifo from filling but we can flush it.
  638. */
  639. if (ep->is_in)
  640. dmacount |= (1 << DMA_DIRECTION);
  641. if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
  642. || ep->dev->pdev->device != 0x2280)
  643. dmacount |= (1 << END_OF_CHAIN);
  644. req->valid = valid;
  645. if (valid)
  646. dmacount |= (1 << VALID_BIT);
  647. if (likely(!req->req.no_interrupt || !use_dma_chaining))
  648. dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
  649. /* td->dmadesc = previously set by caller */
  650. td->dmaaddr = cpu_to_le32 (req->req.dma);
  651. /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
  652. wmb ();
  653. td->dmacount = cpu_to_le32p (&dmacount);
  654. }
  655. static const u32 dmactl_default =
  656. (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
  657. | (1 << DMA_CLEAR_COUNT_ENABLE)
  658. /* erratum 0116 workaround part 1 (use POLLING) */
  659. | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
  660. | (1 << DMA_VALID_BIT_POLLING_ENABLE)
  661. | (1 << DMA_VALID_BIT_ENABLE)
  662. | (1 << DMA_SCATTER_GATHER_ENABLE)
  663. /* erratum 0116 workaround part 2 (no AUTOSTART) */
  664. | (1 << DMA_ENABLE);
  665. static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
  666. {
  667. handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
  668. }
  669. static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
  670. {
  671. writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
  672. spin_stop_dma (dma);
  673. }
  674. static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
  675. {
  676. struct net2280_dma_regs __iomem *dma = ep->dma;
  677. unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
  678. if (ep->dev->pdev->device != 0x2280)
  679. tmp |= (1 << END_OF_CHAIN);
  680. writel (tmp, &dma->dmacount);
  681. writel (readl (&dma->dmastat), &dma->dmastat);
  682. writel (td_dma, &dma->dmadesc);
  683. writel (dmactl, &dma->dmactl);
  684. /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
  685. (void) readl (&ep->dev->pci->pcimstctl);
  686. writel ((1 << DMA_START), &dma->dmastat);
  687. if (!ep->is_in)
  688. stop_out_naking (ep);
  689. }
  690. static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
  691. {
  692. u32 tmp;
  693. struct net2280_dma_regs __iomem *dma = ep->dma;
  694. /* FIXME can't use DMA for ZLPs */
  695. /* on this path we "know" there's no dma active (yet) */
  696. WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
  697. writel (0, &ep->dma->dmactl);
  698. /* previous OUT packet might have been short */
  699. if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
  700. & (1 << NAK_OUT_PACKETS)) != 0) {
  701. writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
  702. &ep->regs->ep_stat);
  703. tmp = readl (&ep->regs->ep_avail);
  704. if (tmp) {
  705. writel (readl (&dma->dmastat), &dma->dmastat);
  706. /* transfer all/some fifo data */
  707. writel (req->req.dma, &dma->dmaaddr);
  708. tmp = min (tmp, req->req.length);
  709. /* dma irq, faking scatterlist status */
  710. req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
  711. writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
  712. | tmp, &dma->dmacount);
  713. req->td->dmadesc = 0;
  714. req->valid = 1;
  715. writel ((1 << DMA_ENABLE), &dma->dmactl);
  716. writel ((1 << DMA_START), &dma->dmastat);
  717. return;
  718. }
  719. }
  720. tmp = dmactl_default;
  721. /* force packet boundaries between dma requests, but prevent the
  722. * controller from automagically writing a last "short" packet
  723. * (zero length) unless the driver explicitly said to do that.
  724. */
  725. if (ep->is_in) {
  726. if (likely ((req->req.length % ep->ep.maxpacket) != 0
  727. || req->req.zero)) {
  728. tmp |= (1 << DMA_FIFO_VALIDATE);
  729. ep->in_fifo_validate = 1;
  730. } else
  731. ep->in_fifo_validate = 0;
  732. }
  733. /* init req->td, pointing to the current dummy */
  734. req->td->dmadesc = cpu_to_le32 (ep->td_dma);
  735. fill_dma_desc (ep, req, 1);
  736. if (!use_dma_chaining)
  737. req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
  738. start_queue (ep, tmp, req->td_dma);
  739. }
  740. static inline void
  741. queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
  742. {
  743. struct net2280_dma *end;
  744. dma_addr_t tmp;
  745. /* swap new dummy for old, link; fill and maybe activate */
  746. end = ep->dummy;
  747. ep->dummy = req->td;
  748. req->td = end;
  749. tmp = ep->td_dma;
  750. ep->td_dma = req->td_dma;
  751. req->td_dma = tmp;
  752. end->dmadesc = cpu_to_le32 (ep->td_dma);
  753. fill_dma_desc (ep, req, valid);
  754. }
  755. static void
  756. done (struct net2280_ep *ep, struct net2280_request *req, int status)
  757. {
  758. struct net2280 *dev;
  759. unsigned stopped = ep->stopped;
  760. list_del_init (&req->queue);
  761. if (req->req.status == -EINPROGRESS)
  762. req->req.status = status;
  763. else
  764. status = req->req.status;
  765. dev = ep->dev;
  766. if (req->mapped) {
  767. pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
  768. ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  769. req->req.dma = DMA_ADDR_INVALID;
  770. req->mapped = 0;
  771. }
  772. if (status && status != -ESHUTDOWN)
  773. VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
  774. ep->ep.name, &req->req, status,
  775. req->req.actual, req->req.length);
  776. /* don't modify queue heads during completion callback */
  777. ep->stopped = 1;
  778. spin_unlock (&dev->lock);
  779. req->req.complete (&ep->ep, &req->req);
  780. spin_lock (&dev->lock);
  781. ep->stopped = stopped;
  782. }
  783. /*-------------------------------------------------------------------------*/
  784. static int
  785. net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  786. {
  787. struct net2280_request *req;
  788. struct net2280_ep *ep;
  789. struct net2280 *dev;
  790. unsigned long flags;
  791. /* we always require a cpu-view buffer, so that we can
  792. * always use pio (as fallback or whatever).
  793. */
  794. req = container_of (_req, struct net2280_request, req);
  795. if (!_req || !_req->complete || !_req->buf
  796. || !list_empty (&req->queue))
  797. return -EINVAL;
  798. if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
  799. return -EDOM;
  800. ep = container_of (_ep, struct net2280_ep, ep);
  801. if (!_ep || (!ep->desc && ep->num != 0))
  802. return -EINVAL;
  803. dev = ep->dev;
  804. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  805. return -ESHUTDOWN;
  806. /* FIXME implement PIO fallback for ZLPs with DMA */
  807. if (ep->dma && _req->length == 0)
  808. return -EOPNOTSUPP;
  809. /* set up dma mapping in case the caller didn't */
  810. if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
  811. _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
  812. ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
  813. req->mapped = 1;
  814. }
  815. #if 0
  816. VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
  817. _ep->name, _req, _req->length, _req->buf);
  818. #endif
  819. spin_lock_irqsave (&dev->lock, flags);
  820. _req->status = -EINPROGRESS;
  821. _req->actual = 0;
  822. /* kickstart this i/o queue? */
  823. if (list_empty (&ep->queue) && !ep->stopped) {
  824. /* use DMA if the endpoint supports it, else pio */
  825. if (ep->dma)
  826. start_dma (ep, req);
  827. else {
  828. /* maybe there's no control data, just status ack */
  829. if (ep->num == 0 && _req->length == 0) {
  830. allow_status (ep);
  831. done (ep, req, 0);
  832. VDEBUG (dev, "%s status ack\n", ep->ep.name);
  833. goto done;
  834. }
  835. /* PIO ... stuff the fifo, or unblock it. */
  836. if (ep->is_in)
  837. write_fifo (ep, _req);
  838. else if (list_empty (&ep->queue)) {
  839. u32 s;
  840. /* OUT FIFO might have packet(s) buffered */
  841. s = readl (&ep->regs->ep_stat);
  842. if ((s & (1 << FIFO_EMPTY)) == 0) {
  843. /* note: _req->short_not_ok is
  844. * ignored here since PIO _always_
  845. * stops queue advance here, and
  846. * _req->status doesn't change for
  847. * short reads (only _req->actual)
  848. */
  849. if (read_fifo (ep, req)) {
  850. done (ep, req, 0);
  851. if (ep->num == 0)
  852. allow_status (ep);
  853. /* don't queue it */
  854. req = NULL;
  855. } else
  856. s = readl (&ep->regs->ep_stat);
  857. }
  858. /* don't NAK, let the fifo fill */
  859. if (req && (s & (1 << NAK_OUT_PACKETS)))
  860. writel ((1 << CLEAR_NAK_OUT_PACKETS),
  861. &ep->regs->ep_rsp);
  862. }
  863. }
  864. } else if (ep->dma) {
  865. int valid = 1;
  866. if (ep->is_in) {
  867. int expect;
  868. /* preventing magic zlps is per-engine state, not
  869. * per-transfer; irq logic must recover hiccups.
  870. */
  871. expect = likely (req->req.zero
  872. || (req->req.length % ep->ep.maxpacket) != 0);
  873. if (expect != ep->in_fifo_validate)
  874. valid = 0;
  875. }
  876. queue_dma (ep, req, valid);
  877. } /* else the irq handler advances the queue. */
  878. ep->responded = 1;
  879. if (req)
  880. list_add_tail (&req->queue, &ep->queue);
  881. done:
  882. spin_unlock_irqrestore (&dev->lock, flags);
  883. /* pci writes may still be posted */
  884. return 0;
  885. }
  886. static inline void
  887. dma_done (
  888. struct net2280_ep *ep,
  889. struct net2280_request *req,
  890. u32 dmacount,
  891. int status
  892. )
  893. {
  894. req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
  895. done (ep, req, status);
  896. }
  897. static void restart_dma (struct net2280_ep *ep);
  898. static void scan_dma_completions (struct net2280_ep *ep)
  899. {
  900. /* only look at descriptors that were "naturally" retired,
  901. * so fifo and list head state won't matter
  902. */
  903. while (!list_empty (&ep->queue)) {
  904. struct net2280_request *req;
  905. u32 tmp;
  906. req = list_entry (ep->queue.next,
  907. struct net2280_request, queue);
  908. if (!req->valid)
  909. break;
  910. rmb ();
  911. tmp = le32_to_cpup (&req->td->dmacount);
  912. if ((tmp & (1 << VALID_BIT)) != 0)
  913. break;
  914. /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
  915. * cases where DMA must be aborted; this code handles
  916. * all non-abort DMA completions.
  917. */
  918. if (unlikely (req->td->dmadesc == 0)) {
  919. /* paranoia */
  920. tmp = readl (&ep->dma->dmacount);
  921. if (tmp & DMA_BYTE_COUNT_MASK)
  922. break;
  923. /* single transfer mode */
  924. dma_done (ep, req, tmp, 0);
  925. break;
  926. } else if (!ep->is_in
  927. && (req->req.length % ep->ep.maxpacket) != 0) {
  928. tmp = readl (&ep->regs->ep_stat);
  929. /* AVOID TROUBLE HERE by not issuing short reads from
  930. * your gadget driver. That helps avoids errata 0121,
  931. * 0122, and 0124; not all cases trigger the warning.
  932. */
  933. if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
  934. WARN (ep->dev, "%s lost packet sync!\n",
  935. ep->ep.name);
  936. req->req.status = -EOVERFLOW;
  937. } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
  938. /* fifo gets flushed later */
  939. ep->out_overflow = 1;
  940. DEBUG (ep->dev, "%s dma, discard %d len %d\n",
  941. ep->ep.name, tmp,
  942. req->req.length);
  943. req->req.status = -EOVERFLOW;
  944. }
  945. }
  946. dma_done (ep, req, tmp, 0);
  947. }
  948. }
  949. static void restart_dma (struct net2280_ep *ep)
  950. {
  951. struct net2280_request *req;
  952. u32 dmactl = dmactl_default;
  953. if (ep->stopped)
  954. return;
  955. req = list_entry (ep->queue.next, struct net2280_request, queue);
  956. if (!use_dma_chaining) {
  957. start_dma (ep, req);
  958. return;
  959. }
  960. /* the 2280 will be processing the queue unless queue hiccups after
  961. * the previous transfer:
  962. * IN: wanted automagic zlp, head doesn't (or vice versa)
  963. * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
  964. * OUT: was "usb-short", we must restart.
  965. */
  966. if (ep->is_in && !req->valid) {
  967. struct net2280_request *entry, *prev = NULL;
  968. int reqmode, done = 0;
  969. DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
  970. ep->in_fifo_validate = likely (req->req.zero
  971. || (req->req.length % ep->ep.maxpacket) != 0);
  972. if (ep->in_fifo_validate)
  973. dmactl |= (1 << DMA_FIFO_VALIDATE);
  974. list_for_each_entry (entry, &ep->queue, queue) {
  975. __le32 dmacount;
  976. if (entry == req)
  977. continue;
  978. dmacount = entry->td->dmacount;
  979. if (!done) {
  980. reqmode = likely (entry->req.zero
  981. || (entry->req.length
  982. % ep->ep.maxpacket) != 0);
  983. if (reqmode == ep->in_fifo_validate) {
  984. entry->valid = 1;
  985. dmacount |= valid_bit;
  986. entry->td->dmacount = dmacount;
  987. prev = entry;
  988. continue;
  989. } else {
  990. /* force a hiccup */
  991. prev->td->dmacount |= dma_done_ie;
  992. done = 1;
  993. }
  994. }
  995. /* walk the rest of the queue so unlinks behave */
  996. entry->valid = 0;
  997. dmacount &= ~valid_bit;
  998. entry->td->dmacount = dmacount;
  999. prev = entry;
  1000. }
  1001. }
  1002. writel (0, &ep->dma->dmactl);
  1003. start_queue (ep, dmactl, req->td_dma);
  1004. }
  1005. static void abort_dma (struct net2280_ep *ep)
  1006. {
  1007. /* abort the current transfer */
  1008. if (likely (!list_empty (&ep->queue))) {
  1009. /* FIXME work around errata 0121, 0122, 0124 */
  1010. writel ((1 << DMA_ABORT), &ep->dma->dmastat);
  1011. spin_stop_dma (ep->dma);
  1012. } else
  1013. stop_dma (ep->dma);
  1014. scan_dma_completions (ep);
  1015. }
  1016. /* dequeue ALL requests */
  1017. static void nuke (struct net2280_ep *ep)
  1018. {
  1019. struct net2280_request *req;
  1020. /* called with spinlock held */
  1021. ep->stopped = 1;
  1022. if (ep->dma)
  1023. abort_dma (ep);
  1024. while (!list_empty (&ep->queue)) {
  1025. req = list_entry (ep->queue.next,
  1026. struct net2280_request,
  1027. queue);
  1028. done (ep, req, -ESHUTDOWN);
  1029. }
  1030. }
  1031. /* dequeue JUST ONE request */
  1032. static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
  1033. {
  1034. struct net2280_ep *ep;
  1035. struct net2280_request *req;
  1036. unsigned long flags;
  1037. u32 dmactl;
  1038. int stopped;
  1039. ep = container_of (_ep, struct net2280_ep, ep);
  1040. if (!_ep || (!ep->desc && ep->num != 0) || !_req)
  1041. return -EINVAL;
  1042. spin_lock_irqsave (&ep->dev->lock, flags);
  1043. stopped = ep->stopped;
  1044. /* quiesce dma while we patch the queue */
  1045. dmactl = 0;
  1046. ep->stopped = 1;
  1047. if (ep->dma) {
  1048. dmactl = readl (&ep->dma->dmactl);
  1049. /* WARNING erratum 0127 may kick in ... */
  1050. stop_dma (ep->dma);
  1051. scan_dma_completions (ep);
  1052. }
  1053. /* make sure it's still queued on this endpoint */
  1054. list_for_each_entry (req, &ep->queue, queue) {
  1055. if (&req->req == _req)
  1056. break;
  1057. }
  1058. if (&req->req != _req) {
  1059. spin_unlock_irqrestore (&ep->dev->lock, flags);
  1060. return -EINVAL;
  1061. }
  1062. /* queue head may be partially complete. */
  1063. if (ep->queue.next == &req->queue) {
  1064. if (ep->dma) {
  1065. DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
  1066. _req->status = -ECONNRESET;
  1067. abort_dma (ep);
  1068. if (likely (ep->queue.next == &req->queue)) {
  1069. // NOTE: misreports single-transfer mode
  1070. req->td->dmacount = 0; /* invalidate */
  1071. dma_done (ep, req,
  1072. readl (&ep->dma->dmacount),
  1073. -ECONNRESET);
  1074. }
  1075. } else {
  1076. DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
  1077. done (ep, req, -ECONNRESET);
  1078. }
  1079. req = NULL;
  1080. /* patch up hardware chaining data */
  1081. } else if (ep->dma && use_dma_chaining) {
  1082. if (req->queue.prev == ep->queue.next) {
  1083. writel (le32_to_cpu (req->td->dmadesc),
  1084. &ep->dma->dmadesc);
  1085. if (req->td->dmacount & dma_done_ie)
  1086. writel (readl (&ep->dma->dmacount)
  1087. | le32_to_cpu(dma_done_ie),
  1088. &ep->dma->dmacount);
  1089. } else {
  1090. struct net2280_request *prev;
  1091. prev = list_entry (req->queue.prev,
  1092. struct net2280_request, queue);
  1093. prev->td->dmadesc = req->td->dmadesc;
  1094. if (req->td->dmacount & dma_done_ie)
  1095. prev->td->dmacount |= dma_done_ie;
  1096. }
  1097. }
  1098. if (req)
  1099. done (ep, req, -ECONNRESET);
  1100. ep->stopped = stopped;
  1101. if (ep->dma) {
  1102. /* turn off dma on inactive queues */
  1103. if (list_empty (&ep->queue))
  1104. stop_dma (ep->dma);
  1105. else if (!ep->stopped) {
  1106. /* resume current request, or start new one */
  1107. if (req)
  1108. writel (dmactl, &ep->dma->dmactl);
  1109. else
  1110. start_dma (ep, list_entry (ep->queue.next,
  1111. struct net2280_request, queue));
  1112. }
  1113. }
  1114. spin_unlock_irqrestore (&ep->dev->lock, flags);
  1115. return 0;
  1116. }
  1117. /*-------------------------------------------------------------------------*/
  1118. static int net2280_fifo_status (struct usb_ep *_ep);
  1119. static int
  1120. net2280_set_halt (struct usb_ep *_ep, int value)
  1121. {
  1122. struct net2280_ep *ep;
  1123. unsigned long flags;
  1124. int retval = 0;
  1125. ep = container_of (_ep, struct net2280_ep, ep);
  1126. if (!_ep || (!ep->desc && ep->num != 0))
  1127. return -EINVAL;
  1128. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1129. return -ESHUTDOWN;
  1130. if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
  1131. == USB_ENDPOINT_XFER_ISOC)
  1132. return -EINVAL;
  1133. spin_lock_irqsave (&ep->dev->lock, flags);
  1134. if (!list_empty (&ep->queue))
  1135. retval = -EAGAIN;
  1136. else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
  1137. retval = -EAGAIN;
  1138. else {
  1139. VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
  1140. value ? "set" : "clear");
  1141. /* set/clear, then synch memory views with the device */
  1142. if (value) {
  1143. if (ep->num == 0)
  1144. ep->dev->protocol_stall = 1;
  1145. else
  1146. set_halt (ep);
  1147. } else
  1148. clear_halt (ep);
  1149. (void) readl (&ep->regs->ep_rsp);
  1150. }
  1151. spin_unlock_irqrestore (&ep->dev->lock, flags);
  1152. return retval;
  1153. }
  1154. static int
  1155. net2280_fifo_status (struct usb_ep *_ep)
  1156. {
  1157. struct net2280_ep *ep;
  1158. u32 avail;
  1159. ep = container_of (_ep, struct net2280_ep, ep);
  1160. if (!_ep || (!ep->desc && ep->num != 0))
  1161. return -ENODEV;
  1162. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1163. return -ESHUTDOWN;
  1164. avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
  1165. if (avail > ep->fifo_size)
  1166. return -EOVERFLOW;
  1167. if (ep->is_in)
  1168. avail = ep->fifo_size - avail;
  1169. return avail;
  1170. }
  1171. static void
  1172. net2280_fifo_flush (struct usb_ep *_ep)
  1173. {
  1174. struct net2280_ep *ep;
  1175. ep = container_of (_ep, struct net2280_ep, ep);
  1176. if (!_ep || (!ep->desc && ep->num != 0))
  1177. return;
  1178. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1179. return;
  1180. writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
  1181. (void) readl (&ep->regs->ep_rsp);
  1182. }
  1183. static const struct usb_ep_ops net2280_ep_ops = {
  1184. .enable = net2280_enable,
  1185. .disable = net2280_disable,
  1186. .alloc_request = net2280_alloc_request,
  1187. .free_request = net2280_free_request,
  1188. .alloc_buffer = net2280_alloc_buffer,
  1189. .free_buffer = net2280_free_buffer,
  1190. .queue = net2280_queue,
  1191. .dequeue = net2280_dequeue,
  1192. .set_halt = net2280_set_halt,
  1193. .fifo_status = net2280_fifo_status,
  1194. .fifo_flush = net2280_fifo_flush,
  1195. };
  1196. /*-------------------------------------------------------------------------*/
  1197. static int net2280_get_frame (struct usb_gadget *_gadget)
  1198. {
  1199. struct net2280 *dev;
  1200. unsigned long flags;
  1201. u16 retval;
  1202. if (!_gadget)
  1203. return -ENODEV;
  1204. dev = container_of (_gadget, struct net2280, gadget);
  1205. spin_lock_irqsave (&dev->lock, flags);
  1206. retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
  1207. spin_unlock_irqrestore (&dev->lock, flags);
  1208. return retval;
  1209. }
  1210. static int net2280_wakeup (struct usb_gadget *_gadget)
  1211. {
  1212. struct net2280 *dev;
  1213. u32 tmp;
  1214. unsigned long flags;
  1215. if (!_gadget)
  1216. return 0;
  1217. dev = container_of (_gadget, struct net2280, gadget);
  1218. spin_lock_irqsave (&dev->lock, flags);
  1219. tmp = readl (&dev->usb->usbctl);
  1220. if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
  1221. writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
  1222. spin_unlock_irqrestore (&dev->lock, flags);
  1223. /* pci writes may still be posted */
  1224. return 0;
  1225. }
  1226. static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
  1227. {
  1228. struct net2280 *dev;
  1229. u32 tmp;
  1230. unsigned long flags;
  1231. if (!_gadget)
  1232. return 0;
  1233. dev = container_of (_gadget, struct net2280, gadget);
  1234. spin_lock_irqsave (&dev->lock, flags);
  1235. tmp = readl (&dev->usb->usbctl);
  1236. if (value)
  1237. tmp |= (1 << SELF_POWERED_STATUS);
  1238. else
  1239. tmp &= ~(1 << SELF_POWERED_STATUS);
  1240. writel (tmp, &dev->usb->usbctl);
  1241. spin_unlock_irqrestore (&dev->lock, flags);
  1242. return 0;
  1243. }
  1244. static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
  1245. {
  1246. struct net2280 *dev;
  1247. u32 tmp;
  1248. unsigned long flags;
  1249. if (!_gadget)
  1250. return -ENODEV;
  1251. dev = container_of (_gadget, struct net2280, gadget);
  1252. spin_lock_irqsave (&dev->lock, flags);
  1253. tmp = readl (&dev->usb->usbctl);
  1254. dev->softconnect = (is_on != 0);
  1255. if (is_on)
  1256. tmp |= (1 << USB_DETECT_ENABLE);
  1257. else
  1258. tmp &= ~(1 << USB_DETECT_ENABLE);
  1259. writel (tmp, &dev->usb->usbctl);
  1260. spin_unlock_irqrestore (&dev->lock, flags);
  1261. return 0;
  1262. }
  1263. static const struct usb_gadget_ops net2280_ops = {
  1264. .get_frame = net2280_get_frame,
  1265. .wakeup = net2280_wakeup,
  1266. .set_selfpowered = net2280_set_selfpowered,
  1267. .pullup = net2280_pullup,
  1268. };
  1269. /*-------------------------------------------------------------------------*/
  1270. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1271. /* FIXME move these into procfs, and use seq_file.
  1272. * Sysfs _still_ doesn't behave for arbitrarily sized files,
  1273. * and also doesn't help products using this with 2.4 kernels.
  1274. */
  1275. /* "function" sysfs attribute */
  1276. static ssize_t
  1277. show_function (struct device *_dev, struct device_attribute *attr, char *buf)
  1278. {
  1279. struct net2280 *dev = dev_get_drvdata (_dev);
  1280. if (!dev->driver
  1281. || !dev->driver->function
  1282. || strlen (dev->driver->function) > PAGE_SIZE)
  1283. return 0;
  1284. return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
  1285. }
  1286. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  1287. static ssize_t
  1288. show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
  1289. {
  1290. struct net2280 *dev;
  1291. char *next;
  1292. unsigned size, t;
  1293. unsigned long flags;
  1294. int i;
  1295. u32 t1, t2;
  1296. const char *s;
  1297. dev = dev_get_drvdata (_dev);
  1298. next = buf;
  1299. size = PAGE_SIZE;
  1300. spin_lock_irqsave (&dev->lock, flags);
  1301. if (dev->driver)
  1302. s = dev->driver->driver.name;
  1303. else
  1304. s = "(none)";
  1305. /* Main Control Registers */
  1306. t = scnprintf (next, size, "%s version " DRIVER_VERSION
  1307. ", chiprev %04x, dma %s\n\n"
  1308. "devinit %03x fifoctl %08x gadget '%s'\n"
  1309. "pci irqenb0 %02x irqenb1 %08x "
  1310. "irqstat0 %04x irqstat1 %08x\n",
  1311. driver_name, dev->chiprev,
  1312. use_dma
  1313. ? (use_dma_chaining ? "chaining" : "enabled")
  1314. : "disabled",
  1315. readl (&dev->regs->devinit),
  1316. readl (&dev->regs->fifoctl),
  1317. s,
  1318. readl (&dev->regs->pciirqenb0),
  1319. readl (&dev->regs->pciirqenb1),
  1320. readl (&dev->regs->irqstat0),
  1321. readl (&dev->regs->irqstat1));
  1322. size -= t;
  1323. next += t;
  1324. /* USB Control Registers */
  1325. t1 = readl (&dev->usb->usbctl);
  1326. t2 = readl (&dev->usb->usbstat);
  1327. if (t1 & (1 << VBUS_PIN)) {
  1328. if (t2 & (1 << HIGH_SPEED))
  1329. s = "high speed";
  1330. else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1331. s = "powered";
  1332. else
  1333. s = "full speed";
  1334. /* full speed bit (6) not working?? */
  1335. } else
  1336. s = "not attached";
  1337. t = scnprintf (next, size,
  1338. "stdrsp %08x usbctl %08x usbstat %08x "
  1339. "addr 0x%02x (%s)\n",
  1340. readl (&dev->usb->stdrsp), t1, t2,
  1341. readl (&dev->usb->ouraddr), s);
  1342. size -= t;
  1343. next += t;
  1344. /* PCI Master Control Registers */
  1345. /* DMA Control Registers */
  1346. /* Configurable EP Control Registers */
  1347. for (i = 0; i < 7; i++) {
  1348. struct net2280_ep *ep;
  1349. ep = &dev->ep [i];
  1350. if (i && !ep->desc)
  1351. continue;
  1352. t1 = readl (&ep->regs->ep_cfg);
  1353. t2 = readl (&ep->regs->ep_rsp) & 0xff;
  1354. t = scnprintf (next, size,
  1355. "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
  1356. "irqenb %02x\n",
  1357. ep->ep.name, t1, t2,
  1358. (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
  1359. ? "NAK " : "",
  1360. (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
  1361. ? "hide " : "",
  1362. (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
  1363. ? "CRC " : "",
  1364. (t2 & (1 << CLEAR_INTERRUPT_MODE))
  1365. ? "interrupt " : "",
  1366. (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
  1367. ? "status " : "",
  1368. (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
  1369. ? "NAKmode " : "",
  1370. (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
  1371. ? "DATA1 " : "DATA0 ",
  1372. (t2 & (1 << CLEAR_ENDPOINT_HALT))
  1373. ? "HALT " : "",
  1374. readl (&ep->regs->ep_irqenb));
  1375. size -= t;
  1376. next += t;
  1377. t = scnprintf (next, size,
  1378. "\tstat %08x avail %04x "
  1379. "(ep%d%s-%s)%s\n",
  1380. readl (&ep->regs->ep_stat),
  1381. readl (&ep->regs->ep_avail),
  1382. t1 & 0x0f, DIR_STRING (t1),
  1383. type_string (t1 >> 8),
  1384. ep->stopped ? "*" : "");
  1385. size -= t;
  1386. next += t;
  1387. if (!ep->dma)
  1388. continue;
  1389. t = scnprintf (next, size,
  1390. " dma\tctl %08x stat %08x count %08x\n"
  1391. "\taddr %08x desc %08x\n",
  1392. readl (&ep->dma->dmactl),
  1393. readl (&ep->dma->dmastat),
  1394. readl (&ep->dma->dmacount),
  1395. readl (&ep->dma->dmaaddr),
  1396. readl (&ep->dma->dmadesc));
  1397. size -= t;
  1398. next += t;
  1399. }
  1400. /* Indexed Registers */
  1401. // none yet
  1402. /* Statistics */
  1403. t = scnprintf (next, size, "\nirqs: ");
  1404. size -= t;
  1405. next += t;
  1406. for (i = 0; i < 7; i++) {
  1407. struct net2280_ep *ep;
  1408. ep = &dev->ep [i];
  1409. if (i && !ep->irqs)
  1410. continue;
  1411. t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
  1412. size -= t;
  1413. next += t;
  1414. }
  1415. t = scnprintf (next, size, "\n");
  1416. size -= t;
  1417. next += t;
  1418. spin_unlock_irqrestore (&dev->lock, flags);
  1419. return PAGE_SIZE - size;
  1420. }
  1421. static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
  1422. static ssize_t
  1423. show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
  1424. {
  1425. struct net2280 *dev;
  1426. char *next;
  1427. unsigned size;
  1428. unsigned long flags;
  1429. int i;
  1430. dev = dev_get_drvdata (_dev);
  1431. next = buf;
  1432. size = PAGE_SIZE;
  1433. spin_lock_irqsave (&dev->lock, flags);
  1434. for (i = 0; i < 7; i++) {
  1435. struct net2280_ep *ep = &dev->ep [i];
  1436. struct net2280_request *req;
  1437. int t;
  1438. if (i != 0) {
  1439. const struct usb_endpoint_descriptor *d;
  1440. d = ep->desc;
  1441. if (!d)
  1442. continue;
  1443. t = d->bEndpointAddress;
  1444. t = scnprintf (next, size,
  1445. "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
  1446. ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
  1447. (t & USB_DIR_IN) ? "in" : "out",
  1448. ({ char *val;
  1449. switch (d->bmAttributes & 0x03) {
  1450. case USB_ENDPOINT_XFER_BULK:
  1451. val = "bulk"; break;
  1452. case USB_ENDPOINT_XFER_INT:
  1453. val = "intr"; break;
  1454. default:
  1455. val = "iso"; break;
  1456. }; val; }),
  1457. le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
  1458. ep->dma ? "dma" : "pio", ep->fifo_size
  1459. );
  1460. } else /* ep0 should only have one transfer queued */
  1461. t = scnprintf (next, size, "ep0 max 64 pio %s\n",
  1462. ep->is_in ? "in" : "out");
  1463. if (t <= 0 || t > size)
  1464. goto done;
  1465. size -= t;
  1466. next += t;
  1467. if (list_empty (&ep->queue)) {
  1468. t = scnprintf (next, size, "\t(nothing queued)\n");
  1469. if (t <= 0 || t > size)
  1470. goto done;
  1471. size -= t;
  1472. next += t;
  1473. continue;
  1474. }
  1475. list_for_each_entry (req, &ep->queue, queue) {
  1476. if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
  1477. t = scnprintf (next, size,
  1478. "\treq %p len %d/%d "
  1479. "buf %p (dmacount %08x)\n",
  1480. &req->req, req->req.actual,
  1481. req->req.length, req->req.buf,
  1482. readl (&ep->dma->dmacount));
  1483. else
  1484. t = scnprintf (next, size,
  1485. "\treq %p len %d/%d buf %p\n",
  1486. &req->req, req->req.actual,
  1487. req->req.length, req->req.buf);
  1488. if (t <= 0 || t > size)
  1489. goto done;
  1490. size -= t;
  1491. next += t;
  1492. if (ep->dma) {
  1493. struct net2280_dma *td;
  1494. td = req->td;
  1495. t = scnprintf (next, size, "\t td %08x "
  1496. " count %08x buf %08x desc %08x\n",
  1497. (u32) req->td_dma,
  1498. le32_to_cpu (td->dmacount),
  1499. le32_to_cpu (td->dmaaddr),
  1500. le32_to_cpu (td->dmadesc));
  1501. if (t <= 0 || t > size)
  1502. goto done;
  1503. size -= t;
  1504. next += t;
  1505. }
  1506. }
  1507. }
  1508. done:
  1509. spin_unlock_irqrestore (&dev->lock, flags);
  1510. return PAGE_SIZE - size;
  1511. }
  1512. static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
  1513. #else
  1514. #define device_create_file(a,b) (0)
  1515. #define device_remove_file(a,b) do { } while (0)
  1516. #endif
  1517. /*-------------------------------------------------------------------------*/
  1518. /* another driver-specific mode might be a request type doing dma
  1519. * to/from another device fifo instead of to/from memory.
  1520. */
  1521. static void set_fifo_mode (struct net2280 *dev, int mode)
  1522. {
  1523. /* keeping high bits preserves BAR2 */
  1524. writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
  1525. /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
  1526. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1527. list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
  1528. list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
  1529. switch (mode) {
  1530. case 0:
  1531. list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
  1532. list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
  1533. dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
  1534. break;
  1535. case 1:
  1536. dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
  1537. break;
  1538. case 2:
  1539. list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
  1540. dev->ep [1].fifo_size = 2048;
  1541. dev->ep [2].fifo_size = 1024;
  1542. break;
  1543. }
  1544. /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
  1545. list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
  1546. list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
  1547. }
  1548. /* just declare this in any driver that really need it */
  1549. extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
  1550. /**
  1551. * net2280_set_fifo_mode - change allocation of fifo buffers
  1552. * @gadget: access to the net2280 device that will be updated
  1553. * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
  1554. * 1 for two 2kB buffers (ep-a and ep-b only);
  1555. * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
  1556. *
  1557. * returns zero on success, else negative errno. when this succeeds,
  1558. * the contents of gadget->ep_list may have changed.
  1559. *
  1560. * you may only call this function when endpoints a-d are all disabled.
  1561. * use it whenever extra hardware buffering can help performance, such
  1562. * as before enabling "high bandwidth" interrupt endpoints that use
  1563. * maxpacket bigger than 512 (when double buffering would otherwise
  1564. * be unavailable).
  1565. */
  1566. int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
  1567. {
  1568. int i;
  1569. struct net2280 *dev;
  1570. int status = 0;
  1571. unsigned long flags;
  1572. if (!gadget)
  1573. return -ENODEV;
  1574. dev = container_of (gadget, struct net2280, gadget);
  1575. spin_lock_irqsave (&dev->lock, flags);
  1576. for (i = 1; i <= 4; i++)
  1577. if (dev->ep [i].desc) {
  1578. status = -EINVAL;
  1579. break;
  1580. }
  1581. if (mode < 0 || mode > 2)
  1582. status = -EINVAL;
  1583. if (status == 0)
  1584. set_fifo_mode (dev, mode);
  1585. spin_unlock_irqrestore (&dev->lock, flags);
  1586. if (status == 0) {
  1587. if (mode == 1)
  1588. DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
  1589. else if (mode == 2)
  1590. DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
  1591. /* else all are 1K */
  1592. }
  1593. return status;
  1594. }
  1595. EXPORT_SYMBOL (net2280_set_fifo_mode);
  1596. /*-------------------------------------------------------------------------*/
  1597. /* keeping it simple:
  1598. * - one bus driver, initted first;
  1599. * - one function driver, initted second
  1600. *
  1601. * most of the work to support multiple net2280 controllers would
  1602. * be to associate this gadget driver (yes?) with all of them, or
  1603. * perhaps to bind specific drivers to specific devices.
  1604. */
  1605. static struct net2280 *the_controller;
  1606. static void usb_reset (struct net2280 *dev)
  1607. {
  1608. u32 tmp;
  1609. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1610. (void) readl (&dev->usb->usbctl);
  1611. net2280_led_init (dev);
  1612. /* disable automatic responses, and irqs */
  1613. writel (0, &dev->usb->stdrsp);
  1614. writel (0, &dev->regs->pciirqenb0);
  1615. writel (0, &dev->regs->pciirqenb1);
  1616. /* clear old dma and irq state */
  1617. for (tmp = 0; tmp < 4; tmp++) {
  1618. struct net2280_ep *ep = &dev->ep [tmp + 1];
  1619. if (ep->dma)
  1620. abort_dma (ep);
  1621. }
  1622. writel (~0, &dev->regs->irqstat0),
  1623. writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
  1624. /* reset, and enable pci */
  1625. tmp = readl (&dev->regs->devinit)
  1626. | (1 << PCI_ENABLE)
  1627. | (1 << FIFO_SOFT_RESET)
  1628. | (1 << USB_SOFT_RESET)
  1629. | (1 << M8051_RESET);
  1630. writel (tmp, &dev->regs->devinit);
  1631. /* standard fifo and endpoint allocations */
  1632. set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
  1633. }
  1634. static void usb_reinit (struct net2280 *dev)
  1635. {
  1636. u32 tmp;
  1637. int init_dma;
  1638. /* use_dma changes are ignored till next device re-init */
  1639. init_dma = use_dma;
  1640. /* basic endpoint init */
  1641. for (tmp = 0; tmp < 7; tmp++) {
  1642. struct net2280_ep *ep = &dev->ep [tmp];
  1643. ep->ep.name = ep_name [tmp];
  1644. ep->dev = dev;
  1645. ep->num = tmp;
  1646. if (tmp > 0 && tmp <= 4) {
  1647. ep->fifo_size = 1024;
  1648. if (init_dma)
  1649. ep->dma = &dev->dma [tmp - 1];
  1650. } else
  1651. ep->fifo_size = 64;
  1652. ep->regs = &dev->epregs [tmp];
  1653. ep_reset (dev->regs, ep);
  1654. }
  1655. dev->ep [0].ep.maxpacket = 64;
  1656. dev->ep [5].ep.maxpacket = 64;
  1657. dev->ep [6].ep.maxpacket = 64;
  1658. dev->gadget.ep0 = &dev->ep [0].ep;
  1659. dev->ep [0].stopped = 0;
  1660. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1661. /* we want to prevent lowlevel/insecure access from the USB host,
  1662. * but erratum 0119 means this enable bit is ignored
  1663. */
  1664. for (tmp = 0; tmp < 5; tmp++)
  1665. writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
  1666. }
  1667. static void ep0_start (struct net2280 *dev)
  1668. {
  1669. writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  1670. | (1 << CLEAR_NAK_OUT_PACKETS)
  1671. | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  1672. , &dev->epregs [0].ep_rsp);
  1673. /*
  1674. * hardware optionally handles a bunch of standard requests
  1675. * that the API hides from drivers anyway. have it do so.
  1676. * endpoint status/features are handled in software, to
  1677. * help pass tests for some dubious behavior.
  1678. */
  1679. writel ( (1 << SET_TEST_MODE)
  1680. | (1 << SET_ADDRESS)
  1681. | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
  1682. | (1 << GET_DEVICE_STATUS)
  1683. | (1 << GET_INTERFACE_STATUS)
  1684. , &dev->usb->stdrsp);
  1685. writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
  1686. | (1 << SELF_POWERED_USB_DEVICE)
  1687. | (1 << REMOTE_WAKEUP_SUPPORT)
  1688. | (dev->softconnect << USB_DETECT_ENABLE)
  1689. | (1 << SELF_POWERED_STATUS)
  1690. , &dev->usb->usbctl);
  1691. /* enable irqs so we can see ep0 and general operation */
  1692. writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
  1693. | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
  1694. , &dev->regs->pciirqenb0);
  1695. writel ( (1 << PCI_INTERRUPT_ENABLE)
  1696. | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1697. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1698. | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
  1699. | (1 << VBUS_INTERRUPT_ENABLE)
  1700. | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
  1701. | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
  1702. , &dev->regs->pciirqenb1);
  1703. /* don't leave any writes posted */
  1704. (void) readl (&dev->usb->usbctl);
  1705. }
  1706. /* when a driver is successfully registered, it will receive
  1707. * control requests including set_configuration(), which enables
  1708. * non-control requests. then usb traffic follows until a
  1709. * disconnect is reported. then a host may connect again, or
  1710. * the driver might get unbound.
  1711. */
  1712. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1713. {
  1714. struct net2280 *dev = the_controller;
  1715. int retval;
  1716. unsigned i;
  1717. /* insist on high speed support from the driver, since
  1718. * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
  1719. * "must not be used in normal operation"
  1720. */
  1721. if (!driver
  1722. || driver->speed != USB_SPEED_HIGH
  1723. || !driver->bind
  1724. || !driver->setup)
  1725. return -EINVAL;
  1726. if (!dev)
  1727. return -ENODEV;
  1728. if (dev->driver)
  1729. return -EBUSY;
  1730. for (i = 0; i < 7; i++)
  1731. dev->ep [i].irqs = 0;
  1732. /* hook up the driver ... */
  1733. dev->softconnect = 1;
  1734. driver->driver.bus = NULL;
  1735. dev->driver = driver;
  1736. dev->gadget.dev.driver = &driver->driver;
  1737. retval = driver->bind (&dev->gadget);
  1738. if (retval) {
  1739. DEBUG (dev, "bind to driver %s --> %d\n",
  1740. driver->driver.name, retval);
  1741. dev->driver = NULL;
  1742. dev->gadget.dev.driver = NULL;
  1743. return retval;
  1744. }
  1745. retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
  1746. if (retval) goto err_unbind;
  1747. retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
  1748. if (retval) goto err_func;
  1749. /* ... then enable host detection and ep0; and we're ready
  1750. * for set_configuration as well as eventual disconnect.
  1751. */
  1752. net2280_led_active (dev, 1);
  1753. ep0_start (dev);
  1754. DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
  1755. driver->driver.name,
  1756. readl (&dev->usb->usbctl),
  1757. readl (&dev->usb->stdrsp));
  1758. /* pci writes may still be posted */
  1759. return 0;
  1760. err_func:
  1761. device_remove_file (&dev->pdev->dev, &dev_attr_function);
  1762. err_unbind:
  1763. driver->unbind (&dev->gadget);
  1764. dev->gadget.dev.driver = NULL;
  1765. dev->driver = NULL;
  1766. return retval;
  1767. }
  1768. EXPORT_SYMBOL (usb_gadget_register_driver);
  1769. static void
  1770. stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
  1771. {
  1772. int i;
  1773. /* don't disconnect if it's not connected */
  1774. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1775. driver = NULL;
  1776. /* stop hardware; prevent new request submissions;
  1777. * and kill any outstanding requests.
  1778. */
  1779. usb_reset (dev);
  1780. for (i = 0; i < 7; i++)
  1781. nuke (&dev->ep [i]);
  1782. /* report disconnect; the driver is already quiesced */
  1783. if (driver) {
  1784. spin_unlock (&dev->lock);
  1785. driver->disconnect (&dev->gadget);
  1786. spin_lock (&dev->lock);
  1787. }
  1788. usb_reinit (dev);
  1789. }
  1790. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1791. {
  1792. struct net2280 *dev = the_controller;
  1793. unsigned long flags;
  1794. if (!dev)
  1795. return -ENODEV;
  1796. if (!driver || driver != dev->driver || !driver->unbind)
  1797. return -EINVAL;
  1798. spin_lock_irqsave (&dev->lock, flags);
  1799. stop_activity (dev, driver);
  1800. spin_unlock_irqrestore (&dev->lock, flags);
  1801. net2280_pullup (&dev->gadget, 0);
  1802. driver->unbind (&dev->gadget);
  1803. dev->gadget.dev.driver = NULL;
  1804. dev->driver = NULL;
  1805. net2280_led_active (dev, 0);
  1806. device_remove_file (&dev->pdev->dev, &dev_attr_function);
  1807. device_remove_file (&dev->pdev->dev, &dev_attr_queues);
  1808. DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
  1809. return 0;
  1810. }
  1811. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1812. /*-------------------------------------------------------------------------*/
  1813. /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
  1814. * also works for dma-capable endpoints, in pio mode or just
  1815. * to manually advance the queue after short OUT transfers.
  1816. */
  1817. static void handle_ep_small (struct net2280_ep *ep)
  1818. {
  1819. struct net2280_request *req;
  1820. u32 t;
  1821. /* 0 error, 1 mid-data, 2 done */
  1822. int mode = 1;
  1823. if (!list_empty (&ep->queue))
  1824. req = list_entry (ep->queue.next,
  1825. struct net2280_request, queue);
  1826. else
  1827. req = NULL;
  1828. /* ack all, and handle what we care about */
  1829. t = readl (&ep->regs->ep_stat);
  1830. ep->irqs++;
  1831. #if 0
  1832. VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
  1833. ep->ep.name, t, req ? &req->req : 0);
  1834. #endif
  1835. if (!ep->is_in || ep->dev->pdev->device == 0x2280)
  1836. writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
  1837. else
  1838. /* Added for 2282 */
  1839. writel (t, &ep->regs->ep_stat);
  1840. /* for ep0, monitor token irqs to catch data stage length errors
  1841. * and to synchronize on status.
  1842. *
  1843. * also, to defer reporting of protocol stalls ... here's where
  1844. * data or status first appears, handling stalls here should never
  1845. * cause trouble on the host side..
  1846. *
  1847. * control requests could be slightly faster without token synch for
  1848. * status, but status can jam up that way.
  1849. */
  1850. if (unlikely (ep->num == 0)) {
  1851. if (ep->is_in) {
  1852. /* status; stop NAKing */
  1853. if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
  1854. if (ep->dev->protocol_stall) {
  1855. ep->stopped = 1;
  1856. set_halt (ep);
  1857. }
  1858. if (!req)
  1859. allow_status (ep);
  1860. mode = 2;
  1861. /* reply to extra IN data tokens with a zlp */
  1862. } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1863. if (ep->dev->protocol_stall) {
  1864. ep->stopped = 1;
  1865. set_halt (ep);
  1866. mode = 2;
  1867. } else if (ep->responded &&
  1868. !req && !ep->stopped)
  1869. write_fifo (ep, NULL);
  1870. }
  1871. } else {
  1872. /* status; stop NAKing */
  1873. if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1874. if (ep->dev->protocol_stall) {
  1875. ep->stopped = 1;
  1876. set_halt (ep);
  1877. }
  1878. mode = 2;
  1879. /* an extra OUT token is an error */
  1880. } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
  1881. && req
  1882. && req->req.actual == req->req.length)
  1883. || (ep->responded && !req)) {
  1884. ep->dev->protocol_stall = 1;
  1885. set_halt (ep);
  1886. ep->stopped = 1;
  1887. if (req)
  1888. done (ep, req, -EOVERFLOW);
  1889. req = NULL;
  1890. }
  1891. }
  1892. }
  1893. if (unlikely (!req))
  1894. return;
  1895. /* manual DMA queue advance after short OUT */
  1896. if (likely (ep->dma != 0)) {
  1897. if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
  1898. u32 count;
  1899. int stopped = ep->stopped;
  1900. /* TRANSFERRED works around OUT_DONE erratum 0112.
  1901. * we expect (N <= maxpacket) bytes; host wrote M.
  1902. * iff (M < N) we won't ever see a DMA interrupt.
  1903. */
  1904. ep->stopped = 1;
  1905. for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
  1906. /* any preceding dma transfers must finish.
  1907. * dma handles (M >= N), may empty the queue
  1908. */
  1909. scan_dma_completions (ep);
  1910. if (unlikely (list_empty (&ep->queue)
  1911. || ep->out_overflow)) {
  1912. req = NULL;
  1913. break;
  1914. }
  1915. req = list_entry (ep->queue.next,
  1916. struct net2280_request, queue);
  1917. /* here either (M < N), a "real" short rx;
  1918. * or (M == N) and the queue didn't empty
  1919. */
  1920. if (likely (t & (1 << FIFO_EMPTY))) {
  1921. count = readl (&ep->dma->dmacount);
  1922. count &= DMA_BYTE_COUNT_MASK;
  1923. if (readl (&ep->dma->dmadesc)
  1924. != req->td_dma)
  1925. req = NULL;
  1926. break;
  1927. }
  1928. udelay(1);
  1929. }
  1930. /* stop DMA, leave ep NAKing */
  1931. writel ((1 << DMA_ABORT), &ep->dma->dmastat);
  1932. spin_stop_dma (ep->dma);
  1933. if (likely (req)) {
  1934. req->td->dmacount = 0;
  1935. t = readl (&ep->regs->ep_avail);
  1936. dma_done (ep, req, count,
  1937. (ep->out_overflow || t)
  1938. ? -EOVERFLOW : 0);
  1939. }
  1940. /* also flush to prevent erratum 0106 trouble */
  1941. if (unlikely (ep->out_overflow
  1942. || (ep->dev->chiprev == 0x0100
  1943. && ep->dev->gadget.speed
  1944. == USB_SPEED_FULL))) {
  1945. out_flush (ep);
  1946. ep->out_overflow = 0;
  1947. }
  1948. /* (re)start dma if needed, stop NAKing */
  1949. ep->stopped = stopped;
  1950. if (!list_empty (&ep->queue))
  1951. restart_dma (ep);
  1952. } else
  1953. DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
  1954. ep->ep.name, t);
  1955. return;
  1956. /* data packet(s) received (in the fifo, OUT) */
  1957. } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
  1958. if (read_fifo (ep, req) && ep->num != 0)
  1959. mode = 2;
  1960. /* data packet(s) transmitted (IN) */
  1961. } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
  1962. unsigned len;
  1963. len = req->req.length - req->req.actual;
  1964. if (len > ep->ep.maxpacket)
  1965. len = ep->ep.maxpacket;
  1966. req->req.actual += len;
  1967. /* if we wrote it all, we're usually done */
  1968. if (req->req.actual == req->req.length) {
  1969. if (ep->num == 0) {
  1970. /* send zlps until the status stage */
  1971. } else if (!req->req.zero || len != ep->ep.maxpacket)
  1972. mode = 2;
  1973. }
  1974. /* there was nothing to do ... */
  1975. } else if (mode == 1)
  1976. return;
  1977. /* done */
  1978. if (mode == 2) {
  1979. /* stream endpoints often resubmit/unlink in completion */
  1980. done (ep, req, 0);
  1981. /* maybe advance queue to next request */
  1982. if (ep->num == 0) {
  1983. /* NOTE: net2280 could let gadget driver start the
  1984. * status stage later. since not all controllers let
  1985. * them control that, the api doesn't (yet) allow it.
  1986. */
  1987. if (!ep->stopped)
  1988. allow_status (ep);
  1989. req = NULL;
  1990. } else {
  1991. if (!list_empty (&ep->queue) && !ep->stopped)
  1992. req = list_entry (ep->queue.next,
  1993. struct net2280_request, queue);
  1994. else
  1995. req = NULL;
  1996. if (req && !ep->is_in)
  1997. stop_out_naking (ep);
  1998. }
  1999. }
  2000. /* is there a buffer for the next packet?
  2001. * for best streaming performance, make sure there is one.
  2002. */
  2003. if (req && !ep->stopped) {
  2004. /* load IN fifo with next packet (may be zlp) */
  2005. if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
  2006. write_fifo (ep, &req->req);
  2007. }
  2008. }
  2009. static struct net2280_ep *
  2010. get_ep_by_addr (struct net2280 *dev, u16 wIndex)
  2011. {
  2012. struct net2280_ep *ep;
  2013. if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
  2014. return &dev->ep [0];
  2015. list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
  2016. u8 bEndpointAddress;
  2017. if (!ep->desc)
  2018. continue;
  2019. bEndpointAddress = ep->desc->bEndpointAddress;
  2020. if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
  2021. continue;
  2022. if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
  2023. return ep;
  2024. }
  2025. return NULL;
  2026. }
  2027. static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
  2028. {
  2029. struct net2280_ep *ep;
  2030. u32 num, scratch;
  2031. /* most of these don't need individual acks */
  2032. stat &= ~(1 << INTA_ASSERTED);
  2033. if (!stat)
  2034. return;
  2035. // DEBUG (dev, "irqstat0 %04x\n", stat);
  2036. /* starting a control request? */
  2037. if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
  2038. union {
  2039. u32 raw [2];
  2040. struct usb_ctrlrequest r;
  2041. } u;
  2042. int tmp;
  2043. struct net2280_request *req;
  2044. if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
  2045. if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
  2046. dev->gadget.speed = USB_SPEED_HIGH;
  2047. else
  2048. dev->gadget.speed = USB_SPEED_FULL;
  2049. net2280_led_speed (dev, dev->gadget.speed);
  2050. DEBUG (dev, "%s speed\n",
  2051. (dev->gadget.speed == USB_SPEED_HIGH)
  2052. ? "high" : "full");
  2053. }
  2054. ep = &dev->ep [0];
  2055. ep->irqs++;
  2056. /* make sure any leftover request state is cleared */
  2057. stat &= ~(1 << ENDPOINT_0_INTERRUPT);
  2058. while (!list_empty (&ep->queue)) {
  2059. req = list_entry (ep->queue.next,
  2060. struct net2280_request, queue);
  2061. done (ep, req, (req->req.actual == req->req.length)
  2062. ? 0 : -EPROTO);
  2063. }
  2064. ep->stopped = 0;
  2065. dev->protocol_stall = 0;
  2066. if (ep->dev->pdev->device == 0x2280)
  2067. tmp = (1 << FIFO_OVERFLOW)
  2068. | (1 << FIFO_UNDERFLOW);
  2069. else
  2070. tmp = 0;
  2071. writel (tmp | (1 << TIMEOUT)
  2072. | (1 << USB_STALL_SENT)
  2073. | (1 << USB_IN_NAK_SENT)
  2074. | (1 << USB_IN_ACK_RCVD)
  2075. | (1 << USB_OUT_PING_NAK_SENT)
  2076. | (1 << USB_OUT_ACK_SENT)
  2077. | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
  2078. | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
  2079. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  2080. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  2081. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2082. | (1 << DATA_IN_TOKEN_INTERRUPT)
  2083. , &ep->regs->ep_stat);
  2084. u.raw [0] = readl (&dev->usb->setup0123);
  2085. u.raw [1] = readl (&dev->usb->setup4567);
  2086. cpu_to_le32s (&u.raw [0]);
  2087. cpu_to_le32s (&u.raw [1]);
  2088. tmp = 0;
  2089. #define w_value le16_to_cpup (&u.r.wValue)
  2090. #define w_index le16_to_cpup (&u.r.wIndex)
  2091. #define w_length le16_to_cpup (&u.r.wLength)
  2092. /* ack the irq */
  2093. writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
  2094. stat ^= (1 << SETUP_PACKET_INTERRUPT);
  2095. /* watch control traffic at the token level, and force
  2096. * synchronization before letting the status stage happen.
  2097. * FIXME ignore tokens we'll NAK, until driver responds.
  2098. * that'll mean a lot less irqs for some drivers.
  2099. */
  2100. ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  2101. if (ep->is_in) {
  2102. scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  2103. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2104. | (1 << DATA_IN_TOKEN_INTERRUPT);
  2105. stop_out_naking (ep);
  2106. } else
  2107. scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  2108. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2109. | (1 << DATA_IN_TOKEN_INTERRUPT);
  2110. writel (scratch, &dev->epregs [0].ep_irqenb);
  2111. /* we made the hardware handle most lowlevel requests;
  2112. * everything else goes uplevel to the gadget code.
  2113. */
  2114. ep->responded = 1;
  2115. switch (u.r.bRequest) {
  2116. case USB_REQ_GET_STATUS: {
  2117. struct net2280_ep *e;
  2118. __le32 status;
  2119. /* hw handles device and interface status */
  2120. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  2121. goto delegate;
  2122. if ((e = get_ep_by_addr (dev, w_index)) == 0
  2123. || w_length > 2)
  2124. goto do_stall;
  2125. if (readl (&e->regs->ep_rsp)
  2126. & (1 << SET_ENDPOINT_HALT))
  2127. status = __constant_cpu_to_le32 (1);
  2128. else
  2129. status = __constant_cpu_to_le32 (0);
  2130. /* don't bother with a request object! */
  2131. writel (0, &dev->epregs [0].ep_irqenb);
  2132. set_fifo_bytecount (ep, w_length);
  2133. writel ((__force u32)status, &dev->epregs [0].ep_data);
  2134. allow_status (ep);
  2135. VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
  2136. goto next_endpoints;
  2137. }
  2138. break;
  2139. case USB_REQ_CLEAR_FEATURE: {
  2140. struct net2280_ep *e;
  2141. /* hw handles device features */
  2142. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2143. goto delegate;
  2144. if (w_value != USB_ENDPOINT_HALT
  2145. || w_length != 0)
  2146. goto do_stall;
  2147. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2148. goto do_stall;
  2149. clear_halt (e);
  2150. allow_status (ep);
  2151. VDEBUG (dev, "%s clear halt\n", ep->ep.name);
  2152. goto next_endpoints;
  2153. }
  2154. break;
  2155. case USB_REQ_SET_FEATURE: {
  2156. struct net2280_ep *e;
  2157. /* hw handles device features */
  2158. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2159. goto delegate;
  2160. if (w_value != USB_ENDPOINT_HALT
  2161. || w_length != 0)
  2162. goto do_stall;
  2163. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2164. goto do_stall;
  2165. set_halt (e);
  2166. allow_status (ep);
  2167. VDEBUG (dev, "%s set halt\n", ep->ep.name);
  2168. goto next_endpoints;
  2169. }
  2170. break;
  2171. default:
  2172. delegate:
  2173. VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
  2174. "ep_cfg %08x\n",
  2175. u.r.bRequestType, u.r.bRequest,
  2176. w_value, w_index, w_length,
  2177. readl (&ep->regs->ep_cfg));
  2178. ep->responded = 0;
  2179. spin_unlock (&dev->lock);
  2180. tmp = dev->driver->setup (&dev->gadget, &u.r);
  2181. spin_lock (&dev->lock);
  2182. }
  2183. /* stall ep0 on error */
  2184. if (tmp < 0) {
  2185. do_stall:
  2186. VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
  2187. u.r.bRequestType, u.r.bRequest, tmp);
  2188. dev->protocol_stall = 1;
  2189. }
  2190. /* some in/out token irq should follow; maybe stall then.
  2191. * driver must queue a request (even zlp) or halt ep0
  2192. * before the host times out.
  2193. */
  2194. }
  2195. #undef w_value
  2196. #undef w_index
  2197. #undef w_length
  2198. next_endpoints:
  2199. /* endpoint data irq ? */
  2200. scratch = stat & 0x7f;
  2201. stat &= ~0x7f;
  2202. for (num = 0; scratch; num++) {
  2203. u32 t;
  2204. /* do this endpoint's FIFO and queue need tending? */
  2205. t = 1 << num;
  2206. if ((scratch & t) == 0)
  2207. continue;
  2208. scratch ^= t;
  2209. ep = &dev->ep [num];
  2210. handle_ep_small (ep);
  2211. }
  2212. if (stat)
  2213. DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
  2214. }
  2215. #define DMA_INTERRUPTS ( \
  2216. (1 << DMA_D_INTERRUPT) \
  2217. | (1 << DMA_C_INTERRUPT) \
  2218. | (1 << DMA_B_INTERRUPT) \
  2219. | (1 << DMA_A_INTERRUPT))
  2220. #define PCI_ERROR_INTERRUPTS ( \
  2221. (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
  2222. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
  2223. | (1 << PCI_RETRY_ABORT_INTERRUPT))
  2224. static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
  2225. {
  2226. struct net2280_ep *ep;
  2227. u32 tmp, num, mask, scratch;
  2228. /* after disconnect there's nothing else to do! */
  2229. tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
  2230. mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
  2231. /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
  2232. * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
  2233. * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
  2234. * only indicates a change in the reset state).
  2235. */
  2236. if (stat & tmp) {
  2237. writel (tmp, &dev->regs->irqstat1);
  2238. if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
  2239. && ((readl (&dev->usb->usbstat) & mask)
  2240. == 0))
  2241. || ((readl (&dev->usb->usbctl)
  2242. & (1 << VBUS_PIN)) == 0)
  2243. ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
  2244. DEBUG (dev, "disconnect %s\n",
  2245. dev->driver->driver.name);
  2246. stop_activity (dev, dev->driver);
  2247. ep0_start (dev);
  2248. return;
  2249. }
  2250. stat &= ~tmp;
  2251. /* vBUS can bounce ... one of many reasons to ignore the
  2252. * notion of hotplug events on bus connect/disconnect!
  2253. */
  2254. if (!stat)
  2255. return;
  2256. }
  2257. /* NOTE: chip stays in PCI D0 state for now, but it could
  2258. * enter D1 to save more power
  2259. */
  2260. tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
  2261. if (stat & tmp) {
  2262. writel (tmp, &dev->regs->irqstat1);
  2263. if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
  2264. if (dev->driver->suspend)
  2265. dev->driver->suspend (&dev->gadget);
  2266. if (!enable_suspend)
  2267. stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
  2268. } else {
  2269. if (dev->driver->resume)
  2270. dev->driver->resume (&dev->gadget);
  2271. /* at high speed, note erratum 0133 */
  2272. }
  2273. stat &= ~tmp;
  2274. }
  2275. /* clear any other status/irqs */
  2276. if (stat)
  2277. writel (stat, &dev->regs->irqstat1);
  2278. /* some status we can just ignore */
  2279. if (dev->pdev->device == 0x2280)
  2280. stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
  2281. | (1 << SUSPEND_REQUEST_INTERRUPT)
  2282. | (1 << RESUME_INTERRUPT)
  2283. | (1 << SOF_INTERRUPT));
  2284. else
  2285. stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
  2286. | (1 << RESUME_INTERRUPT)
  2287. | (1 << SOF_DOWN_INTERRUPT)
  2288. | (1 << SOF_INTERRUPT));
  2289. if (!stat)
  2290. return;
  2291. // DEBUG (dev, "irqstat1 %08x\n", stat);
  2292. /* DMA status, for ep-{a,b,c,d} */
  2293. scratch = stat & DMA_INTERRUPTS;
  2294. stat &= ~DMA_INTERRUPTS;
  2295. scratch >>= 9;
  2296. for (num = 0; scratch; num++) {
  2297. struct net2280_dma_regs __iomem *dma;
  2298. tmp = 1 << num;
  2299. if ((tmp & scratch) == 0)
  2300. continue;
  2301. scratch ^= tmp;
  2302. ep = &dev->ep [num + 1];
  2303. dma = ep->dma;
  2304. if (!dma)
  2305. continue;
  2306. /* clear ep's dma status */
  2307. tmp = readl (&dma->dmastat);
  2308. writel (tmp, &dma->dmastat);
  2309. /* chaining should stop on abort, short OUT from fifo,
  2310. * or (stat0 codepath) short OUT transfer.
  2311. */
  2312. if (!use_dma_chaining) {
  2313. if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
  2314. == 0) {
  2315. DEBUG (ep->dev, "%s no xact done? %08x\n",
  2316. ep->ep.name, tmp);
  2317. continue;
  2318. }
  2319. stop_dma (ep->dma);
  2320. }
  2321. /* OUT transfers terminate when the data from the
  2322. * host is in our memory. Process whatever's done.
  2323. * On this path, we know transfer's last packet wasn't
  2324. * less than req->length. NAK_OUT_PACKETS may be set,
  2325. * or the FIFO may already be holding new packets.
  2326. *
  2327. * IN transfers can linger in the FIFO for a very
  2328. * long time ... we ignore that for now, accounting
  2329. * precisely (like PIO does) needs per-packet irqs
  2330. */
  2331. scan_dma_completions (ep);
  2332. /* disable dma on inactive queues; else maybe restart */
  2333. if (list_empty (&ep->queue)) {
  2334. if (use_dma_chaining)
  2335. stop_dma (ep->dma);
  2336. } else {
  2337. tmp = readl (&dma->dmactl);
  2338. if (!use_dma_chaining
  2339. || (tmp & (1 << DMA_ENABLE)) == 0)
  2340. restart_dma (ep);
  2341. else if (ep->is_in && use_dma_chaining) {
  2342. struct net2280_request *req;
  2343. __le32 dmacount;
  2344. /* the descriptor at the head of the chain
  2345. * may still have VALID_BIT clear; that's
  2346. * used to trigger changing DMA_FIFO_VALIDATE
  2347. * (affects automagic zlp writes).
  2348. */
  2349. req = list_entry (ep->queue.next,
  2350. struct net2280_request, queue);
  2351. dmacount = req->td->dmacount;
  2352. dmacount &= __constant_cpu_to_le32 (
  2353. (1 << VALID_BIT)
  2354. | DMA_BYTE_COUNT_MASK);
  2355. if (dmacount && (dmacount & valid_bit) == 0)
  2356. restart_dma (ep);
  2357. }
  2358. }
  2359. ep->irqs++;
  2360. }
  2361. /* NOTE: there are other PCI errors we might usefully notice.
  2362. * if they appear very often, here's where to try recovering.
  2363. */
  2364. if (stat & PCI_ERROR_INTERRUPTS) {
  2365. ERROR (dev, "pci dma error; stat %08x\n", stat);
  2366. stat &= ~PCI_ERROR_INTERRUPTS;
  2367. /* these are fatal errors, but "maybe" they won't
  2368. * happen again ...
  2369. */
  2370. stop_activity (dev, dev->driver);
  2371. ep0_start (dev);
  2372. stat = 0;
  2373. }
  2374. if (stat)
  2375. DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
  2376. }
  2377. static irqreturn_t net2280_irq (int irq, void *_dev)
  2378. {
  2379. struct net2280 *dev = _dev;
  2380. /* shared interrupt, not ours */
  2381. if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
  2382. return IRQ_NONE;
  2383. spin_lock (&dev->lock);
  2384. /* handle disconnect, dma, and more */
  2385. handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
  2386. /* control requests and PIO */
  2387. handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
  2388. spin_unlock (&dev->lock);
  2389. return IRQ_HANDLED;
  2390. }
  2391. /*-------------------------------------------------------------------------*/
  2392. static void gadget_release (struct device *_dev)
  2393. {
  2394. struct net2280 *dev = dev_get_drvdata (_dev);
  2395. kfree (dev);
  2396. }
  2397. /* tear down the binding between this driver and the pci device */
  2398. static void net2280_remove (struct pci_dev *pdev)
  2399. {
  2400. struct net2280 *dev = pci_get_drvdata (pdev);
  2401. BUG_ON(dev->driver);
  2402. /* then clean up the resources we allocated during probe() */
  2403. net2280_led_shutdown (dev);
  2404. if (dev->requests) {
  2405. int i;
  2406. for (i = 1; i < 5; i++) {
  2407. if (!dev->ep [i].dummy)
  2408. continue;
  2409. pci_pool_free (dev->requests, dev->ep [i].dummy,
  2410. dev->ep [i].td_dma);
  2411. }
  2412. pci_pool_destroy (dev->requests);
  2413. }
  2414. if (dev->got_irq)
  2415. free_irq (pdev->irq, dev);
  2416. if (dev->regs)
  2417. iounmap (dev->regs);
  2418. if (dev->region)
  2419. release_mem_region (pci_resource_start (pdev, 0),
  2420. pci_resource_len (pdev, 0));
  2421. if (dev->enabled)
  2422. pci_disable_device (pdev);
  2423. device_unregister (&dev->gadget.dev);
  2424. device_remove_file (&pdev->dev, &dev_attr_registers);
  2425. pci_set_drvdata (pdev, NULL);
  2426. INFO (dev, "unbind\n");
  2427. the_controller = NULL;
  2428. }
  2429. /* wrap this driver around the specified device, but
  2430. * don't respond over USB until a gadget driver binds to us.
  2431. */
  2432. static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
  2433. {
  2434. struct net2280 *dev;
  2435. unsigned long resource, len;
  2436. void __iomem *base = NULL;
  2437. int retval, i;
  2438. /* if you want to support more than one controller in a system,
  2439. * usb_gadget_driver_{register,unregister}() must change.
  2440. */
  2441. if (the_controller) {
  2442. dev_warn (&pdev->dev, "ignoring\n");
  2443. return -EBUSY;
  2444. }
  2445. /* alloc, and start init */
  2446. dev = kzalloc (sizeof *dev, GFP_KERNEL);
  2447. if (dev == NULL){
  2448. retval = -ENOMEM;
  2449. goto done;
  2450. }
  2451. pci_set_drvdata (pdev, dev);
  2452. spin_lock_init (&dev->lock);
  2453. dev->pdev = pdev;
  2454. dev->gadget.ops = &net2280_ops;
  2455. dev->gadget.is_dualspeed = 1;
  2456. /* the "gadget" abstracts/virtualizes the controller */
  2457. strcpy (dev->gadget.dev.bus_id, "gadget");
  2458. dev->gadget.dev.parent = &pdev->dev;
  2459. dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
  2460. dev->gadget.dev.release = gadget_release;
  2461. dev->gadget.name = driver_name;
  2462. /* now all the pci goodies ... */
  2463. if (pci_enable_device (pdev) < 0) {
  2464. retval = -ENODEV;
  2465. goto done;
  2466. }
  2467. dev->enabled = 1;
  2468. /* BAR 0 holds all the registers
  2469. * BAR 1 is 8051 memory; unused here (note erratum 0103)
  2470. * BAR 2 is fifo memory; unused here
  2471. */
  2472. resource = pci_resource_start (pdev, 0);
  2473. len = pci_resource_len (pdev, 0);
  2474. if (!request_mem_region (resource, len, driver_name)) {
  2475. DEBUG (dev, "controller already in use\n");
  2476. retval = -EBUSY;
  2477. goto done;
  2478. }
  2479. dev->region = 1;
  2480. /* FIXME provide firmware download interface to put
  2481. * 8051 code into the chip, e.g. to turn on PCI PM.
  2482. */
  2483. base = ioremap_nocache (resource, len);
  2484. if (base == NULL) {
  2485. DEBUG (dev, "can't map memory\n");
  2486. retval = -EFAULT;
  2487. goto done;
  2488. }
  2489. dev->regs = (struct net2280_regs __iomem *) base;
  2490. dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
  2491. dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
  2492. dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
  2493. dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
  2494. dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
  2495. /* put into initial config, link up all endpoints */
  2496. writel (0, &dev->usb->usbctl);
  2497. usb_reset (dev);
  2498. usb_reinit (dev);
  2499. /* irq setup after old hardware is cleaned up */
  2500. if (!pdev->irq) {
  2501. ERROR (dev, "No IRQ. Check PCI setup!\n");
  2502. retval = -ENODEV;
  2503. goto done;
  2504. }
  2505. if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
  2506. != 0) {
  2507. ERROR (dev, "request interrupt %d failed\n", pdev->irq);
  2508. retval = -EBUSY;
  2509. goto done;
  2510. }
  2511. dev->got_irq = 1;
  2512. /* DMA setup */
  2513. /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
  2514. dev->requests = pci_pool_create ("requests", pdev,
  2515. sizeof (struct net2280_dma),
  2516. 0 /* no alignment requirements */,
  2517. 0 /* or page-crossing issues */);
  2518. if (!dev->requests) {
  2519. DEBUG (dev, "can't get request pool\n");
  2520. retval = -ENOMEM;
  2521. goto done;
  2522. }
  2523. for (i = 1; i < 5; i++) {
  2524. struct net2280_dma *td;
  2525. td = pci_pool_alloc (dev->requests, GFP_KERNEL,
  2526. &dev->ep [i].td_dma);
  2527. if (!td) {
  2528. DEBUG (dev, "can't get dummy %d\n", i);
  2529. retval = -ENOMEM;
  2530. goto done;
  2531. }
  2532. td->dmacount = 0; /* not VALID */
  2533. td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
  2534. td->dmadesc = td->dmaaddr;
  2535. dev->ep [i].dummy = td;
  2536. }
  2537. /* enable lower-overhead pci memory bursts during DMA */
  2538. writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
  2539. // 256 write retries may not be enough...
  2540. // | (1 << PCI_RETRY_ABORT_ENABLE)
  2541. | (1 << DMA_READ_MULTIPLE_ENABLE)
  2542. | (1 << DMA_READ_LINE_ENABLE)
  2543. , &dev->pci->pcimstctl);
  2544. /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
  2545. pci_set_master (pdev);
  2546. pci_set_mwi (pdev);
  2547. /* ... also flushes any posted pci writes */
  2548. dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
  2549. /* done */
  2550. INFO (dev, "%s\n", driver_desc);
  2551. INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
  2552. pdev->irq, base, dev->chiprev);
  2553. INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
  2554. use_dma
  2555. ? (use_dma_chaining ? "chaining" : "enabled")
  2556. : "disabled");
  2557. the_controller = dev;
  2558. retval = device_register (&dev->gadget.dev);
  2559. if (retval) goto done;
  2560. retval = device_create_file (&pdev->dev, &dev_attr_registers);
  2561. if (retval) goto done;
  2562. return 0;
  2563. done:
  2564. if (dev)
  2565. net2280_remove (pdev);
  2566. return retval;
  2567. }
  2568. /* make sure the board is quiescent; otherwise it will continue
  2569. * generating IRQs across the upcoming reboot.
  2570. */
  2571. static void net2280_shutdown (struct pci_dev *pdev)
  2572. {
  2573. struct net2280 *dev = pci_get_drvdata (pdev);
  2574. /* disable IRQs */
  2575. writel (0, &dev->regs->pciirqenb0);
  2576. writel (0, &dev->regs->pciirqenb1);
  2577. /* disable the pullup so the host will think we're gone */
  2578. writel (0, &dev->usb->usbctl);
  2579. }
  2580. /*-------------------------------------------------------------------------*/
  2581. static const struct pci_device_id pci_ids [] = { {
  2582. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  2583. .class_mask = ~0,
  2584. .vendor = 0x17cc,
  2585. .device = 0x2280,
  2586. .subvendor = PCI_ANY_ID,
  2587. .subdevice = PCI_ANY_ID,
  2588. }, {
  2589. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  2590. .class_mask = ~0,
  2591. .vendor = 0x17cc,
  2592. .device = 0x2282,
  2593. .subvendor = PCI_ANY_ID,
  2594. .subdevice = PCI_ANY_ID,
  2595. }, { /* end: all zeroes */ }
  2596. };
  2597. MODULE_DEVICE_TABLE (pci, pci_ids);
  2598. /* pci driver glue; this is a "new style" PCI driver module */
  2599. static struct pci_driver net2280_pci_driver = {
  2600. .name = (char *) driver_name,
  2601. .id_table = pci_ids,
  2602. .probe = net2280_probe,
  2603. .remove = net2280_remove,
  2604. .shutdown = net2280_shutdown,
  2605. /* FIXME add power management support */
  2606. };
  2607. MODULE_DESCRIPTION (DRIVER_DESC);
  2608. MODULE_AUTHOR ("David Brownell");
  2609. MODULE_LICENSE ("GPL");
  2610. static int __init init (void)
  2611. {
  2612. if (!use_dma)
  2613. use_dma_chaining = 0;
  2614. return pci_register_driver (&net2280_pci_driver);
  2615. }
  2616. module_init (init);
  2617. static void __exit cleanup (void)
  2618. {
  2619. pci_unregister_driver (&net2280_pci_driver);
  2620. }
  2621. module_exit (cleanup);