net2280.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974
  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, unsigned 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_DMA_NONCOHERENT)
  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. unsigned 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, unsigned 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. __le32 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. | le32_to_cpu(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. const 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. /* just declare this in any driver that really need it */
  1521. extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
  1522. /**
  1523. * net2280_set_fifo_mode - change allocation of fifo buffers
  1524. * @gadget: access to the net2280 device that will be updated
  1525. * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
  1526. * 1 for two 2kB buffers (ep-a and ep-b only);
  1527. * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
  1528. *
  1529. * returns zero on success, else negative errno. when this succeeds,
  1530. * the contents of gadget->ep_list may have changed.
  1531. *
  1532. * you may only call this function when endpoints a-d are all disabled.
  1533. * use it whenever extra hardware buffering can help performance, such
  1534. * as before enabling "high bandwidth" interrupt endpoints that use
  1535. * maxpacket bigger than 512 (when double buffering would otherwise
  1536. * be unavailable).
  1537. */
  1538. int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
  1539. {
  1540. int i;
  1541. struct net2280 *dev;
  1542. int status = 0;
  1543. unsigned long flags;
  1544. if (!gadget)
  1545. return -ENODEV;
  1546. dev = container_of (gadget, struct net2280, gadget);
  1547. spin_lock_irqsave (&dev->lock, flags);
  1548. for (i = 1; i <= 4; i++)
  1549. if (dev->ep [i].desc) {
  1550. status = -EINVAL;
  1551. break;
  1552. }
  1553. if (mode < 0 || mode > 2)
  1554. status = -EINVAL;
  1555. if (status == 0)
  1556. set_fifo_mode (dev, mode);
  1557. spin_unlock_irqrestore (&dev->lock, flags);
  1558. if (status == 0) {
  1559. if (mode == 1)
  1560. DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
  1561. else if (mode == 2)
  1562. DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
  1563. /* else all are 1K */
  1564. }
  1565. return status;
  1566. }
  1567. EXPORT_SYMBOL (net2280_set_fifo_mode);
  1568. /*-------------------------------------------------------------------------*/
  1569. /* keeping it simple:
  1570. * - one bus driver, initted first;
  1571. * - one function driver, initted second
  1572. *
  1573. * most of the work to support multiple net2280 controllers would
  1574. * be to associate this gadget driver (yes?) with all of them, or
  1575. * perhaps to bind specific drivers to specific devices.
  1576. */
  1577. static struct net2280 *the_controller;
  1578. static void usb_reset (struct net2280 *dev)
  1579. {
  1580. u32 tmp;
  1581. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1582. (void) readl (&dev->usb->usbctl);
  1583. net2280_led_init (dev);
  1584. /* disable automatic responses, and irqs */
  1585. writel (0, &dev->usb->stdrsp);
  1586. writel (0, &dev->regs->pciirqenb0);
  1587. writel (0, &dev->regs->pciirqenb1);
  1588. /* clear old dma and irq state */
  1589. for (tmp = 0; tmp < 4; tmp++) {
  1590. struct net2280_ep *ep = &dev->ep [tmp + 1];
  1591. if (ep->dma)
  1592. abort_dma (ep);
  1593. }
  1594. writel (~0, &dev->regs->irqstat0),
  1595. writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
  1596. /* reset, and enable pci */
  1597. tmp = readl (&dev->regs->devinit)
  1598. | (1 << PCI_ENABLE)
  1599. | (1 << FIFO_SOFT_RESET)
  1600. | (1 << USB_SOFT_RESET)
  1601. | (1 << M8051_RESET);
  1602. writel (tmp, &dev->regs->devinit);
  1603. /* standard fifo and endpoint allocations */
  1604. set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
  1605. }
  1606. static void usb_reinit (struct net2280 *dev)
  1607. {
  1608. u32 tmp;
  1609. int init_dma;
  1610. /* use_dma changes are ignored till next device re-init */
  1611. init_dma = use_dma;
  1612. /* basic endpoint init */
  1613. for (tmp = 0; tmp < 7; tmp++) {
  1614. struct net2280_ep *ep = &dev->ep [tmp];
  1615. ep->ep.name = ep_name [tmp];
  1616. ep->dev = dev;
  1617. ep->num = tmp;
  1618. if (tmp > 0 && tmp <= 4) {
  1619. ep->fifo_size = 1024;
  1620. if (init_dma)
  1621. ep->dma = &dev->dma [tmp - 1];
  1622. } else
  1623. ep->fifo_size = 64;
  1624. ep->regs = &dev->epregs [tmp];
  1625. ep_reset (dev->regs, ep);
  1626. }
  1627. dev->ep [0].ep.maxpacket = 64;
  1628. dev->ep [5].ep.maxpacket = 64;
  1629. dev->ep [6].ep.maxpacket = 64;
  1630. dev->gadget.ep0 = &dev->ep [0].ep;
  1631. dev->ep [0].stopped = 0;
  1632. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1633. /* we want to prevent lowlevel/insecure access from the USB host,
  1634. * but erratum 0119 means this enable bit is ignored
  1635. */
  1636. for (tmp = 0; tmp < 5; tmp++)
  1637. writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
  1638. }
  1639. static void ep0_start (struct net2280 *dev)
  1640. {
  1641. writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  1642. | (1 << CLEAR_NAK_OUT_PACKETS)
  1643. | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  1644. , &dev->epregs [0].ep_rsp);
  1645. /*
  1646. * hardware optionally handles a bunch of standard requests
  1647. * that the API hides from drivers anyway. have it do so.
  1648. * endpoint status/features are handled in software, to
  1649. * help pass tests for some dubious behavior.
  1650. */
  1651. writel ( (1 << SET_TEST_MODE)
  1652. | (1 << SET_ADDRESS)
  1653. | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
  1654. | (1 << GET_DEVICE_STATUS)
  1655. | (1 << GET_INTERFACE_STATUS)
  1656. , &dev->usb->stdrsp);
  1657. writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
  1658. | (1 << SELF_POWERED_USB_DEVICE)
  1659. | (1 << REMOTE_WAKEUP_SUPPORT)
  1660. | (dev->softconnect << USB_DETECT_ENABLE)
  1661. | (1 << SELF_POWERED_STATUS)
  1662. , &dev->usb->usbctl);
  1663. /* enable irqs so we can see ep0 and general operation */
  1664. writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
  1665. | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
  1666. , &dev->regs->pciirqenb0);
  1667. writel ( (1 << PCI_INTERRUPT_ENABLE)
  1668. | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1669. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1670. | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
  1671. | (1 << VBUS_INTERRUPT_ENABLE)
  1672. | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
  1673. | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
  1674. , &dev->regs->pciirqenb1);
  1675. /* don't leave any writes posted */
  1676. (void) readl (&dev->usb->usbctl);
  1677. }
  1678. /* when a driver is successfully registered, it will receive
  1679. * control requests including set_configuration(), which enables
  1680. * non-control requests. then usb traffic follows until a
  1681. * disconnect is reported. then a host may connect again, or
  1682. * the driver might get unbound.
  1683. */
  1684. int usb_gadget_register_driver (struct usb_gadget_driver *driver)
  1685. {
  1686. struct net2280 *dev = the_controller;
  1687. int retval;
  1688. unsigned i;
  1689. /* insist on high speed support from the driver, since
  1690. * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
  1691. * "must not be used in normal operation"
  1692. */
  1693. if (!driver
  1694. || driver->speed != USB_SPEED_HIGH
  1695. || !driver->bind
  1696. || !driver->unbind
  1697. || !driver->setup)
  1698. return -EINVAL;
  1699. if (!dev)
  1700. return -ENODEV;
  1701. if (dev->driver)
  1702. return -EBUSY;
  1703. for (i = 0; i < 7; i++)
  1704. dev->ep [i].irqs = 0;
  1705. /* hook up the driver ... */
  1706. dev->softconnect = 1;
  1707. driver->driver.bus = NULL;
  1708. dev->driver = driver;
  1709. dev->gadget.dev.driver = &driver->driver;
  1710. retval = driver->bind (&dev->gadget);
  1711. if (retval) {
  1712. DEBUG (dev, "bind to driver %s --> %d\n",
  1713. driver->driver.name, retval);
  1714. dev->driver = NULL;
  1715. dev->gadget.dev.driver = NULL;
  1716. return retval;
  1717. }
  1718. device_create_file (&dev->pdev->dev, &dev_attr_function);
  1719. device_create_file (&dev->pdev->dev, &dev_attr_queues);
  1720. /* ... then enable host detection and ep0; and we're ready
  1721. * for set_configuration as well as eventual disconnect.
  1722. */
  1723. net2280_led_active (dev, 1);
  1724. ep0_start (dev);
  1725. DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
  1726. driver->driver.name,
  1727. readl (&dev->usb->usbctl),
  1728. readl (&dev->usb->stdrsp));
  1729. /* pci writes may still be posted */
  1730. return 0;
  1731. }
  1732. EXPORT_SYMBOL (usb_gadget_register_driver);
  1733. static void
  1734. stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
  1735. {
  1736. int i;
  1737. /* don't disconnect if it's not connected */
  1738. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1739. driver = NULL;
  1740. /* stop hardware; prevent new request submissions;
  1741. * and kill any outstanding requests.
  1742. */
  1743. usb_reset (dev);
  1744. for (i = 0; i < 7; i++)
  1745. nuke (&dev->ep [i]);
  1746. /* report disconnect; the driver is already quiesced */
  1747. if (driver) {
  1748. spin_unlock (&dev->lock);
  1749. driver->disconnect (&dev->gadget);
  1750. spin_lock (&dev->lock);
  1751. }
  1752. usb_reinit (dev);
  1753. }
  1754. int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
  1755. {
  1756. struct net2280 *dev = the_controller;
  1757. unsigned long flags;
  1758. if (!dev)
  1759. return -ENODEV;
  1760. if (!driver || driver != dev->driver)
  1761. return -EINVAL;
  1762. spin_lock_irqsave (&dev->lock, flags);
  1763. stop_activity (dev, driver);
  1764. spin_unlock_irqrestore (&dev->lock, flags);
  1765. net2280_pullup (&dev->gadget, 0);
  1766. driver->unbind (&dev->gadget);
  1767. dev->gadget.dev.driver = NULL;
  1768. dev->driver = NULL;
  1769. net2280_led_active (dev, 0);
  1770. device_remove_file (&dev->pdev->dev, &dev_attr_function);
  1771. device_remove_file (&dev->pdev->dev, &dev_attr_queues);
  1772. DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
  1773. return 0;
  1774. }
  1775. EXPORT_SYMBOL (usb_gadget_unregister_driver);
  1776. /*-------------------------------------------------------------------------*/
  1777. /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
  1778. * also works for dma-capable endpoints, in pio mode or just
  1779. * to manually advance the queue after short OUT transfers.
  1780. */
  1781. static void handle_ep_small (struct net2280_ep *ep)
  1782. {
  1783. struct net2280_request *req;
  1784. u32 t;
  1785. /* 0 error, 1 mid-data, 2 done */
  1786. int mode = 1;
  1787. if (!list_empty (&ep->queue))
  1788. req = list_entry (ep->queue.next,
  1789. struct net2280_request, queue);
  1790. else
  1791. req = NULL;
  1792. /* ack all, and handle what we care about */
  1793. t = readl (&ep->regs->ep_stat);
  1794. ep->irqs++;
  1795. #if 0
  1796. VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
  1797. ep->ep.name, t, req ? &req->req : 0);
  1798. #endif
  1799. writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
  1800. /* for ep0, monitor token irqs to catch data stage length errors
  1801. * and to synchronize on status.
  1802. *
  1803. * also, to defer reporting of protocol stalls ... here's where
  1804. * data or status first appears, handling stalls here should never
  1805. * cause trouble on the host side..
  1806. *
  1807. * control requests could be slightly faster without token synch for
  1808. * status, but status can jam up that way.
  1809. */
  1810. if (unlikely (ep->num == 0)) {
  1811. if (ep->is_in) {
  1812. /* status; stop NAKing */
  1813. if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
  1814. if (ep->dev->protocol_stall) {
  1815. ep->stopped = 1;
  1816. set_halt (ep);
  1817. }
  1818. if (!req)
  1819. allow_status (ep);
  1820. mode = 2;
  1821. /* reply to extra IN data tokens with a zlp */
  1822. } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1823. if (ep->dev->protocol_stall) {
  1824. ep->stopped = 1;
  1825. set_halt (ep);
  1826. mode = 2;
  1827. } else if (!req && ep->stopped)
  1828. write_fifo (ep, NULL);
  1829. }
  1830. } else {
  1831. /* status; stop NAKing */
  1832. if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1833. if (ep->dev->protocol_stall) {
  1834. ep->stopped = 1;
  1835. set_halt (ep);
  1836. }
  1837. mode = 2;
  1838. /* an extra OUT token is an error */
  1839. } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
  1840. && req
  1841. && req->req.actual == req->req.length)
  1842. || !req) {
  1843. ep->dev->protocol_stall = 1;
  1844. set_halt (ep);
  1845. ep->stopped = 1;
  1846. if (req)
  1847. done (ep, req, -EOVERFLOW);
  1848. req = NULL;
  1849. }
  1850. }
  1851. }
  1852. if (unlikely (!req))
  1853. return;
  1854. /* manual DMA queue advance after short OUT */
  1855. if (likely (ep->dma != 0)) {
  1856. if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
  1857. u32 count;
  1858. int stopped = ep->stopped;
  1859. /* TRANSFERRED works around OUT_DONE erratum 0112.
  1860. * we expect (N <= maxpacket) bytes; host wrote M.
  1861. * iff (M < N) we won't ever see a DMA interrupt.
  1862. */
  1863. ep->stopped = 1;
  1864. for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
  1865. /* any preceding dma transfers must finish.
  1866. * dma handles (M >= N), may empty the queue
  1867. */
  1868. scan_dma_completions (ep);
  1869. if (unlikely (list_empty (&ep->queue)
  1870. || ep->out_overflow)) {
  1871. req = NULL;
  1872. break;
  1873. }
  1874. req = list_entry (ep->queue.next,
  1875. struct net2280_request, queue);
  1876. /* here either (M < N), a "real" short rx;
  1877. * or (M == N) and the queue didn't empty
  1878. */
  1879. if (likely (t & (1 << FIFO_EMPTY))) {
  1880. count = readl (&ep->dma->dmacount);
  1881. count &= DMA_BYTE_COUNT_MASK;
  1882. if (readl (&ep->dma->dmadesc)
  1883. != req->td_dma)
  1884. req = NULL;
  1885. break;
  1886. }
  1887. udelay(1);
  1888. }
  1889. /* stop DMA, leave ep NAKing */
  1890. writel ((1 << DMA_ABORT), &ep->dma->dmastat);
  1891. spin_stop_dma (ep->dma);
  1892. if (likely (req)) {
  1893. req->td->dmacount = 0;
  1894. t = readl (&ep->regs->ep_avail);
  1895. dma_done (ep, req, count, t);
  1896. }
  1897. /* also flush to prevent erratum 0106 trouble */
  1898. if (unlikely (ep->out_overflow
  1899. || (ep->dev->chiprev == 0x0100
  1900. && ep->dev->gadget.speed
  1901. == USB_SPEED_FULL))) {
  1902. out_flush (ep);
  1903. ep->out_overflow = 0;
  1904. }
  1905. /* (re)start dma if needed, stop NAKing */
  1906. ep->stopped = stopped;
  1907. if (!list_empty (&ep->queue))
  1908. restart_dma (ep);
  1909. } else
  1910. DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
  1911. ep->ep.name, t);
  1912. return;
  1913. /* data packet(s) received (in the fifo, OUT) */
  1914. } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
  1915. if (read_fifo (ep, req) && ep->num != 0)
  1916. mode = 2;
  1917. /* data packet(s) transmitted (IN) */
  1918. } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
  1919. unsigned len;
  1920. len = req->req.length - req->req.actual;
  1921. if (len > ep->ep.maxpacket)
  1922. len = ep->ep.maxpacket;
  1923. req->req.actual += len;
  1924. /* if we wrote it all, we're usually done */
  1925. if (req->req.actual == req->req.length) {
  1926. if (ep->num == 0) {
  1927. /* wait for control status */
  1928. if (mode != 2)
  1929. req = NULL;
  1930. } else if (!req->req.zero || len != ep->ep.maxpacket)
  1931. mode = 2;
  1932. }
  1933. /* there was nothing to do ... */
  1934. } else if (mode == 1)
  1935. return;
  1936. /* done */
  1937. if (mode == 2) {
  1938. /* stream endpoints often resubmit/unlink in completion */
  1939. done (ep, req, 0);
  1940. /* maybe advance queue to next request */
  1941. if (ep->num == 0) {
  1942. /* NOTE: net2280 could let gadget driver start the
  1943. * status stage later. since not all controllers let
  1944. * them control that, the api doesn't (yet) allow it.
  1945. */
  1946. if (!ep->stopped)
  1947. allow_status (ep);
  1948. req = NULL;
  1949. } else {
  1950. if (!list_empty (&ep->queue) && !ep->stopped)
  1951. req = list_entry (ep->queue.next,
  1952. struct net2280_request, queue);
  1953. else
  1954. req = NULL;
  1955. if (req && !ep->is_in)
  1956. stop_out_naking (ep);
  1957. }
  1958. }
  1959. /* is there a buffer for the next packet?
  1960. * for best streaming performance, make sure there is one.
  1961. */
  1962. if (req && !ep->stopped) {
  1963. /* load IN fifo with next packet (may be zlp) */
  1964. if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
  1965. write_fifo (ep, &req->req);
  1966. }
  1967. }
  1968. static struct net2280_ep *
  1969. get_ep_by_addr (struct net2280 *dev, u16 wIndex)
  1970. {
  1971. struct net2280_ep *ep;
  1972. if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
  1973. return &dev->ep [0];
  1974. list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
  1975. u8 bEndpointAddress;
  1976. if (!ep->desc)
  1977. continue;
  1978. bEndpointAddress = ep->desc->bEndpointAddress;
  1979. if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
  1980. continue;
  1981. if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
  1982. return ep;
  1983. }
  1984. return NULL;
  1985. }
  1986. static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
  1987. {
  1988. struct net2280_ep *ep;
  1989. u32 num, scratch;
  1990. /* most of these don't need individual acks */
  1991. stat &= ~(1 << INTA_ASSERTED);
  1992. if (!stat)
  1993. return;
  1994. // DEBUG (dev, "irqstat0 %04x\n", stat);
  1995. /* starting a control request? */
  1996. if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
  1997. union {
  1998. u32 raw [2];
  1999. struct usb_ctrlrequest r;
  2000. } u;
  2001. int tmp = 0;
  2002. struct net2280_request *req;
  2003. if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
  2004. if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
  2005. dev->gadget.speed = USB_SPEED_HIGH;
  2006. else
  2007. dev->gadget.speed = USB_SPEED_FULL;
  2008. net2280_led_speed (dev, dev->gadget.speed);
  2009. DEBUG (dev, "%s speed\n",
  2010. (dev->gadget.speed == USB_SPEED_HIGH)
  2011. ? "high" : "full");
  2012. }
  2013. ep = &dev->ep [0];
  2014. ep->irqs++;
  2015. /* make sure any leftover request state is cleared */
  2016. stat &= ~(1 << ENDPOINT_0_INTERRUPT);
  2017. while (!list_empty (&ep->queue)) {
  2018. req = list_entry (ep->queue.next,
  2019. struct net2280_request, queue);
  2020. done (ep, req, (req->req.actual == req->req.length)
  2021. ? 0 : -EPROTO);
  2022. }
  2023. ep->stopped = 0;
  2024. dev->protocol_stall = 0;
  2025. writel ( (1 << TIMEOUT)
  2026. | (1 << USB_STALL_SENT)
  2027. | (1 << USB_IN_NAK_SENT)
  2028. | (1 << USB_IN_ACK_RCVD)
  2029. | (1 << USB_OUT_PING_NAK_SENT)
  2030. | (1 << USB_OUT_ACK_SENT)
  2031. | (1 << FIFO_OVERFLOW)
  2032. | (1 << FIFO_UNDERFLOW)
  2033. | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
  2034. | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
  2035. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  2036. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  2037. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2038. | (1 << DATA_IN_TOKEN_INTERRUPT)
  2039. , &ep->regs->ep_stat);
  2040. u.raw [0] = readl (&dev->usb->setup0123);
  2041. u.raw [1] = readl (&dev->usb->setup4567);
  2042. cpu_to_le32s (&u.raw [0]);
  2043. cpu_to_le32s (&u.raw [1]);
  2044. #define w_value le16_to_cpup (&u.r.wValue)
  2045. #define w_index le16_to_cpup (&u.r.wIndex)
  2046. #define w_length le16_to_cpup (&u.r.wLength)
  2047. /* ack the irq */
  2048. writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
  2049. stat ^= (1 << SETUP_PACKET_INTERRUPT);
  2050. /* watch control traffic at the token level, and force
  2051. * synchronization before letting the status stage happen.
  2052. * FIXME ignore tokens we'll NAK, until driver responds.
  2053. * that'll mean a lot less irqs for some drivers.
  2054. */
  2055. ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  2056. if (ep->is_in) {
  2057. scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  2058. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2059. | (1 << DATA_IN_TOKEN_INTERRUPT);
  2060. stop_out_naking (ep);
  2061. } else
  2062. scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  2063. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  2064. | (1 << DATA_IN_TOKEN_INTERRUPT);
  2065. writel (scratch, &dev->epregs [0].ep_irqenb);
  2066. /* we made the hardware handle most lowlevel requests;
  2067. * everything else goes uplevel to the gadget code.
  2068. */
  2069. switch (u.r.bRequest) {
  2070. case USB_REQ_GET_STATUS: {
  2071. struct net2280_ep *e;
  2072. __le32 status;
  2073. /* hw handles device and interface status */
  2074. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  2075. goto delegate;
  2076. if ((e = get_ep_by_addr (dev, w_index)) == 0
  2077. || w_length > 2)
  2078. goto do_stall;
  2079. if (readl (&e->regs->ep_rsp)
  2080. & (1 << SET_ENDPOINT_HALT))
  2081. status = __constant_cpu_to_le32 (1);
  2082. else
  2083. status = __constant_cpu_to_le32 (0);
  2084. /* don't bother with a request object! */
  2085. writel (0, &dev->epregs [0].ep_irqenb);
  2086. set_fifo_bytecount (ep, w_length);
  2087. writel ((__force u32)status, &dev->epregs [0].ep_data);
  2088. allow_status (ep);
  2089. VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
  2090. goto next_endpoints;
  2091. }
  2092. break;
  2093. case USB_REQ_CLEAR_FEATURE: {
  2094. struct net2280_ep *e;
  2095. /* hw handles device features */
  2096. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2097. goto delegate;
  2098. if (w_value != USB_ENDPOINT_HALT
  2099. || w_length != 0)
  2100. goto do_stall;
  2101. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2102. goto do_stall;
  2103. clear_halt (e);
  2104. allow_status (ep);
  2105. VDEBUG (dev, "%s clear halt\n", ep->ep.name);
  2106. goto next_endpoints;
  2107. }
  2108. break;
  2109. case USB_REQ_SET_FEATURE: {
  2110. struct net2280_ep *e;
  2111. /* hw handles device features */
  2112. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2113. goto delegate;
  2114. if (w_value != USB_ENDPOINT_HALT
  2115. || w_length != 0)
  2116. goto do_stall;
  2117. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2118. goto do_stall;
  2119. set_halt (e);
  2120. allow_status (ep);
  2121. VDEBUG (dev, "%s set halt\n", ep->ep.name);
  2122. goto next_endpoints;
  2123. }
  2124. break;
  2125. default:
  2126. delegate:
  2127. VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
  2128. "ep_cfg %08x\n",
  2129. u.r.bRequestType, u.r.bRequest,
  2130. w_value, w_index, w_length,
  2131. readl (&ep->regs->ep_cfg));
  2132. spin_unlock (&dev->lock);
  2133. tmp = dev->driver->setup (&dev->gadget, &u.r);
  2134. spin_lock (&dev->lock);
  2135. }
  2136. /* stall ep0 on error */
  2137. if (tmp < 0) {
  2138. do_stall:
  2139. VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
  2140. u.r.bRequestType, u.r.bRequest, tmp);
  2141. dev->protocol_stall = 1;
  2142. }
  2143. /* some in/out token irq should follow; maybe stall then.
  2144. * driver must queue a request (even zlp) or halt ep0
  2145. * before the host times out.
  2146. */
  2147. }
  2148. #undef w_value
  2149. #undef w_index
  2150. #undef w_length
  2151. next_endpoints:
  2152. /* endpoint data irq ? */
  2153. scratch = stat & 0x7f;
  2154. stat &= ~0x7f;
  2155. for (num = 0; scratch; num++) {
  2156. u32 t;
  2157. /* do this endpoint's FIFO and queue need tending? */
  2158. t = 1 << num;
  2159. if ((scratch & t) == 0)
  2160. continue;
  2161. scratch ^= t;
  2162. ep = &dev->ep [num];
  2163. handle_ep_small (ep);
  2164. }
  2165. if (stat)
  2166. DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
  2167. }
  2168. #define DMA_INTERRUPTS ( \
  2169. (1 << DMA_D_INTERRUPT) \
  2170. | (1 << DMA_C_INTERRUPT) \
  2171. | (1 << DMA_B_INTERRUPT) \
  2172. | (1 << DMA_A_INTERRUPT))
  2173. #define PCI_ERROR_INTERRUPTS ( \
  2174. (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
  2175. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
  2176. | (1 << PCI_RETRY_ABORT_INTERRUPT))
  2177. static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
  2178. {
  2179. struct net2280_ep *ep;
  2180. u32 tmp, num, mask, scratch;
  2181. /* after disconnect there's nothing else to do! */
  2182. tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
  2183. mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
  2184. /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
  2185. * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
  2186. * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
  2187. * only indicates a change in the reset state).
  2188. */
  2189. if (stat & tmp) {
  2190. writel (tmp, &dev->regs->irqstat1);
  2191. if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
  2192. ((readl (&dev->usb->usbstat) & mask) == 0))
  2193. || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
  2194. ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
  2195. DEBUG (dev, "disconnect %s\n",
  2196. dev->driver->driver.name);
  2197. stop_activity (dev, dev->driver);
  2198. ep0_start (dev);
  2199. return;
  2200. }
  2201. stat &= ~tmp;
  2202. /* vBUS can bounce ... one of many reasons to ignore the
  2203. * notion of hotplug events on bus connect/disconnect!
  2204. */
  2205. if (!stat)
  2206. return;
  2207. }
  2208. /* NOTE: chip stays in PCI D0 state for now, but it could
  2209. * enter D1 to save more power
  2210. */
  2211. tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
  2212. if (stat & tmp) {
  2213. writel (tmp, &dev->regs->irqstat1);
  2214. if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
  2215. if (dev->driver->suspend)
  2216. dev->driver->suspend (&dev->gadget);
  2217. if (!enable_suspend)
  2218. stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
  2219. } else {
  2220. if (dev->driver->resume)
  2221. dev->driver->resume (&dev->gadget);
  2222. /* at high speed, note erratum 0133 */
  2223. }
  2224. stat &= ~tmp;
  2225. }
  2226. /* clear any other status/irqs */
  2227. if (stat)
  2228. writel (stat, &dev->regs->irqstat1);
  2229. /* some status we can just ignore */
  2230. stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
  2231. | (1 << SUSPEND_REQUEST_INTERRUPT)
  2232. | (1 << RESUME_INTERRUPT)
  2233. | (1 << SOF_INTERRUPT));
  2234. if (!stat)
  2235. return;
  2236. // DEBUG (dev, "irqstat1 %08x\n", stat);
  2237. /* DMA status, for ep-{a,b,c,d} */
  2238. scratch = stat & DMA_INTERRUPTS;
  2239. stat &= ~DMA_INTERRUPTS;
  2240. scratch >>= 9;
  2241. for (num = 0; scratch; num++) {
  2242. struct net2280_dma_regs __iomem *dma;
  2243. tmp = 1 << num;
  2244. if ((tmp & scratch) == 0)
  2245. continue;
  2246. scratch ^= tmp;
  2247. ep = &dev->ep [num + 1];
  2248. dma = ep->dma;
  2249. if (!dma)
  2250. continue;
  2251. /* clear ep's dma status */
  2252. tmp = readl (&dma->dmastat);
  2253. writel (tmp, &dma->dmastat);
  2254. /* chaining should stop on abort, short OUT from fifo,
  2255. * or (stat0 codepath) short OUT transfer.
  2256. */
  2257. if (!use_dma_chaining) {
  2258. if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
  2259. == 0) {
  2260. DEBUG (ep->dev, "%s no xact done? %08x\n",
  2261. ep->ep.name, tmp);
  2262. continue;
  2263. }
  2264. stop_dma (ep->dma);
  2265. }
  2266. /* OUT transfers terminate when the data from the
  2267. * host is in our memory. Process whatever's done.
  2268. * On this path, we know transfer's last packet wasn't
  2269. * less than req->length. NAK_OUT_PACKETS may be set,
  2270. * or the FIFO may already be holding new packets.
  2271. *
  2272. * IN transfers can linger in the FIFO for a very
  2273. * long time ... we ignore that for now, accounting
  2274. * precisely (like PIO does) needs per-packet irqs
  2275. */
  2276. scan_dma_completions (ep);
  2277. /* disable dma on inactive queues; else maybe restart */
  2278. if (list_empty (&ep->queue)) {
  2279. if (use_dma_chaining)
  2280. stop_dma (ep->dma);
  2281. } else {
  2282. tmp = readl (&dma->dmactl);
  2283. if (!use_dma_chaining
  2284. || (tmp & (1 << DMA_ENABLE)) == 0)
  2285. restart_dma (ep);
  2286. else if (ep->is_in && use_dma_chaining) {
  2287. struct net2280_request *req;
  2288. __le32 dmacount;
  2289. /* the descriptor at the head of the chain
  2290. * may still have VALID_BIT clear; that's
  2291. * used to trigger changing DMA_FIFO_VALIDATE
  2292. * (affects automagic zlp writes).
  2293. */
  2294. req = list_entry (ep->queue.next,
  2295. struct net2280_request, queue);
  2296. dmacount = req->td->dmacount;
  2297. dmacount &= __constant_cpu_to_le32 (
  2298. (1 << VALID_BIT)
  2299. | DMA_BYTE_COUNT_MASK);
  2300. if (dmacount && (dmacount & valid_bit) == 0)
  2301. restart_dma (ep);
  2302. }
  2303. }
  2304. ep->irqs++;
  2305. }
  2306. /* NOTE: there are other PCI errors we might usefully notice.
  2307. * if they appear very often, here's where to try recovering.
  2308. */
  2309. if (stat & PCI_ERROR_INTERRUPTS) {
  2310. ERROR (dev, "pci dma error; stat %08x\n", stat);
  2311. stat &= ~PCI_ERROR_INTERRUPTS;
  2312. /* these are fatal errors, but "maybe" they won't
  2313. * happen again ...
  2314. */
  2315. stop_activity (dev, dev->driver);
  2316. ep0_start (dev);
  2317. stat = 0;
  2318. }
  2319. if (stat)
  2320. DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
  2321. }
  2322. static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
  2323. {
  2324. struct net2280 *dev = _dev;
  2325. spin_lock (&dev->lock);
  2326. /* handle disconnect, dma, and more */
  2327. handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
  2328. /* control requests and PIO */
  2329. handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
  2330. spin_unlock (&dev->lock);
  2331. return IRQ_HANDLED;
  2332. }
  2333. /*-------------------------------------------------------------------------*/
  2334. static void gadget_release (struct device *_dev)
  2335. {
  2336. struct net2280 *dev = dev_get_drvdata (_dev);
  2337. kfree (dev);
  2338. }
  2339. /* tear down the binding between this driver and the pci device */
  2340. static void net2280_remove (struct pci_dev *pdev)
  2341. {
  2342. struct net2280 *dev = pci_get_drvdata (pdev);
  2343. /* start with the driver above us */
  2344. if (dev->driver) {
  2345. /* should have been done already by driver model core */
  2346. WARN (dev, "pci remove, driver '%s' is still registered\n",
  2347. dev->driver->driver.name);
  2348. usb_gadget_unregister_driver (dev->driver);
  2349. }
  2350. /* then clean up the resources we allocated during probe() */
  2351. net2280_led_shutdown (dev);
  2352. if (dev->requests) {
  2353. int i;
  2354. for (i = 1; i < 5; i++) {
  2355. if (!dev->ep [i].dummy)
  2356. continue;
  2357. pci_pool_free (dev->requests, dev->ep [i].dummy,
  2358. dev->ep [i].td_dma);
  2359. }
  2360. pci_pool_destroy (dev->requests);
  2361. }
  2362. if (dev->got_irq)
  2363. free_irq (pdev->irq, dev);
  2364. if (dev->regs)
  2365. iounmap (dev->regs);
  2366. if (dev->region)
  2367. release_mem_region (pci_resource_start (pdev, 0),
  2368. pci_resource_len (pdev, 0));
  2369. if (dev->enabled)
  2370. pci_disable_device (pdev);
  2371. device_unregister (&dev->gadget.dev);
  2372. device_remove_file (&pdev->dev, &dev_attr_registers);
  2373. pci_set_drvdata (pdev, NULL);
  2374. INFO (dev, "unbind\n");
  2375. the_controller = NULL;
  2376. }
  2377. /* wrap this driver around the specified device, but
  2378. * don't respond over USB until a gadget driver binds to us.
  2379. */
  2380. static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
  2381. {
  2382. struct net2280 *dev;
  2383. unsigned long resource, len;
  2384. void __iomem *base = NULL;
  2385. int retval, i;
  2386. char buf [8], *bufp;
  2387. /* if you want to support more than one controller in a system,
  2388. * usb_gadget_driver_{register,unregister}() must change.
  2389. */
  2390. if (the_controller) {
  2391. dev_warn (&pdev->dev, "ignoring\n");
  2392. return -EBUSY;
  2393. }
  2394. /* alloc, and start init */
  2395. dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  2396. if (dev == NULL){
  2397. retval = -ENOMEM;
  2398. goto done;
  2399. }
  2400. memset (dev, 0, sizeof *dev);
  2401. spin_lock_init (&dev->lock);
  2402. dev->pdev = pdev;
  2403. dev->gadget.ops = &net2280_ops;
  2404. dev->gadget.is_dualspeed = 1;
  2405. /* the "gadget" abstracts/virtualizes the controller */
  2406. strcpy (dev->gadget.dev.bus_id, "gadget");
  2407. dev->gadget.dev.parent = &pdev->dev;
  2408. dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
  2409. dev->gadget.dev.release = gadget_release;
  2410. dev->gadget.name = driver_name;
  2411. /* now all the pci goodies ... */
  2412. if (pci_enable_device (pdev) < 0) {
  2413. retval = -ENODEV;
  2414. goto done;
  2415. }
  2416. dev->enabled = 1;
  2417. /* BAR 0 holds all the registers
  2418. * BAR 1 is 8051 memory; unused here (note erratum 0103)
  2419. * BAR 2 is fifo memory; unused here
  2420. */
  2421. resource = pci_resource_start (pdev, 0);
  2422. len = pci_resource_len (pdev, 0);
  2423. if (!request_mem_region (resource, len, driver_name)) {
  2424. DEBUG (dev, "controller already in use\n");
  2425. retval = -EBUSY;
  2426. goto done;
  2427. }
  2428. dev->region = 1;
  2429. base = ioremap_nocache (resource, len);
  2430. if (base == NULL) {
  2431. DEBUG (dev, "can't map memory\n");
  2432. retval = -EFAULT;
  2433. goto done;
  2434. }
  2435. dev->regs = (struct net2280_regs __iomem *) base;
  2436. dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
  2437. dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
  2438. dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
  2439. dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
  2440. dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
  2441. /* put into initial config, link up all endpoints */
  2442. writel (0, &dev->usb->usbctl);
  2443. usb_reset (dev);
  2444. usb_reinit (dev);
  2445. /* irq setup after old hardware is cleaned up */
  2446. if (!pdev->irq) {
  2447. ERROR (dev, "No IRQ. Check PCI setup!\n");
  2448. retval = -ENODEV;
  2449. goto done;
  2450. }
  2451. #ifndef __sparc__
  2452. scnprintf (buf, sizeof buf, "%d", pdev->irq);
  2453. bufp = buf;
  2454. #else
  2455. bufp = __irq_itoa(pdev->irq);
  2456. #endif
  2457. if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
  2458. != 0) {
  2459. ERROR (dev, "request interrupt %s failed\n", bufp);
  2460. retval = -EBUSY;
  2461. goto done;
  2462. }
  2463. dev->got_irq = 1;
  2464. /* DMA setup */
  2465. /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
  2466. dev->requests = pci_pool_create ("requests", pdev,
  2467. sizeof (struct net2280_dma),
  2468. 0 /* no alignment requirements */,
  2469. 0 /* or page-crossing issues */);
  2470. if (!dev->requests) {
  2471. DEBUG (dev, "can't get request pool\n");
  2472. retval = -ENOMEM;
  2473. goto done;
  2474. }
  2475. for (i = 1; i < 5; i++) {
  2476. struct net2280_dma *td;
  2477. td = pci_pool_alloc (dev->requests, GFP_KERNEL,
  2478. &dev->ep [i].td_dma);
  2479. if (!td) {
  2480. DEBUG (dev, "can't get dummy %d\n", i);
  2481. retval = -ENOMEM;
  2482. goto done;
  2483. }
  2484. td->dmacount = 0; /* not VALID */
  2485. td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
  2486. td->dmadesc = td->dmaaddr;
  2487. dev->ep [i].dummy = td;
  2488. }
  2489. /* enable lower-overhead pci memory bursts during DMA */
  2490. writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
  2491. // 256 write retries may not be enough...
  2492. // | (1 << PCI_RETRY_ABORT_ENABLE)
  2493. | (1 << DMA_READ_MULTIPLE_ENABLE)
  2494. | (1 << DMA_READ_LINE_ENABLE)
  2495. , &dev->pci->pcimstctl);
  2496. /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
  2497. pci_set_master (pdev);
  2498. pci_set_mwi (pdev);
  2499. /* ... also flushes any posted pci writes */
  2500. dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
  2501. /* done */
  2502. pci_set_drvdata (pdev, dev);
  2503. INFO (dev, "%s\n", driver_desc);
  2504. INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
  2505. bufp, base, dev->chiprev);
  2506. INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
  2507. use_dma
  2508. ? (use_dma_chaining ? "chaining" : "enabled")
  2509. : "disabled");
  2510. the_controller = dev;
  2511. device_register (&dev->gadget.dev);
  2512. device_create_file (&pdev->dev, &dev_attr_registers);
  2513. return 0;
  2514. done:
  2515. if (dev)
  2516. net2280_remove (pdev);
  2517. return retval;
  2518. }
  2519. /*-------------------------------------------------------------------------*/
  2520. static struct pci_device_id pci_ids [] = { {
  2521. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  2522. .class_mask = ~0,
  2523. .vendor = 0x17cc,
  2524. .device = 0x2280,
  2525. .subvendor = PCI_ANY_ID,
  2526. .subdevice = PCI_ANY_ID,
  2527. }, { /* end: all zeroes */ }
  2528. };
  2529. MODULE_DEVICE_TABLE (pci, pci_ids);
  2530. /* pci driver glue; this is a "new style" PCI driver module */
  2531. static struct pci_driver net2280_pci_driver = {
  2532. .name = (char *) driver_name,
  2533. .id_table = pci_ids,
  2534. .probe = net2280_probe,
  2535. .remove = net2280_remove,
  2536. /* FIXME add power management support */
  2537. };
  2538. MODULE_DESCRIPTION (DRIVER_DESC);
  2539. MODULE_AUTHOR ("David Brownell");
  2540. MODULE_LICENSE ("GPL");
  2541. static int __init init (void)
  2542. {
  2543. if (!use_dma)
  2544. use_dma_chaining = 0;
  2545. return pci_register_driver (&net2280_pci_driver);
  2546. }
  2547. module_init (init);
  2548. static void __exit cleanup (void)
  2549. {
  2550. pci_unregister_driver (&net2280_pci_driver);
  2551. }
  2552. module_exit (cleanup);