net2280.c 79 KB

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