net2280.c 77 KB

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