net2280.c 78 KB

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