net2280.c 80 KB

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