net2280.c 78 KB

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