net2280.c 79 KB

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