net2280.c 79 KB

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