net2280.c 78 KB

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