net2280.c 79 KB

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