ci13xxx_udc.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  1. /*
  2. * ci13xxx_udc.c - MIPS USB IP core family device controller
  3. *
  4. * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
  5. *
  6. * Author: David Lopo
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. /*
  13. * Description: MIPS USB IP core family device controller
  14. * Currently it only supports IP part number CI13412
  15. *
  16. * This driver is composed of several blocks:
  17. * - HW: hardware interface
  18. * - DBG: debug facilities (optional)
  19. * - UTIL: utilities
  20. * - ISR: interrupts handling
  21. * - ENDPT: endpoint operations (Gadget API)
  22. * - GADGET: gadget operations (Gadget API)
  23. * - BUS: bus glue code, bus abstraction layer
  24. *
  25. * Compile Options
  26. * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
  27. * - STALL_IN: non-empty bulk-in pipes cannot be halted
  28. * if defined mass storage compliance succeeds but with warnings
  29. * => case 4: Hi > Dn
  30. * => case 5: Hi > Di
  31. * => case 8: Hi <> Do
  32. * if undefined usbtest 13 fails
  33. * - TRACE: enable function tracing (depends on DEBUG)
  34. *
  35. * Main Features
  36. * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
  37. * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
  38. * - Normal & LPM support
  39. *
  40. * USBTEST Report
  41. * - OK: 0-12, 13 (STALL_IN defined) & 14
  42. * - Not Supported: 15 & 16 (ISO)
  43. *
  44. * TODO List
  45. * - OTG
  46. * - Isochronous & Interrupt Traffic
  47. * - Handle requests which spawns into several TDs
  48. * - GET_STATUS(device) - always reports 0
  49. * - Gadget API (majority of optional features)
  50. * - Suspend & Remote Wakeup
  51. */
  52. #include <linux/delay.h>
  53. #include <linux/device.h>
  54. #include <linux/dmapool.h>
  55. #include <linux/dma-mapping.h>
  56. #include <linux/init.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/io.h>
  59. #include <linux/irq.h>
  60. #include <linux/kernel.h>
  61. #include <linux/slab.h>
  62. #include <linux/pm_runtime.h>
  63. #include <linux/usb/ch9.h>
  64. #include <linux/usb/gadget.h>
  65. #include <linux/usb/otg.h>
  66. #include "ci13xxx_udc.h"
  67. /******************************************************************************
  68. * DEFINE
  69. *****************************************************************************/
  70. /* ctrl register bank access */
  71. static DEFINE_SPINLOCK(udc_lock);
  72. /* control endpoint description */
  73. static const struct usb_endpoint_descriptor
  74. ctrl_endpt_out_desc = {
  75. .bLength = USB_DT_ENDPOINT_SIZE,
  76. .bDescriptorType = USB_DT_ENDPOINT,
  77. .bEndpointAddress = USB_DIR_OUT,
  78. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  79. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  80. };
  81. static const struct usb_endpoint_descriptor
  82. ctrl_endpt_in_desc = {
  83. .bLength = USB_DT_ENDPOINT_SIZE,
  84. .bDescriptorType = USB_DT_ENDPOINT,
  85. .bEndpointAddress = USB_DIR_IN,
  86. .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
  87. .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX),
  88. };
  89. /* UDC descriptor */
  90. static struct ci13xxx *_udc;
  91. /* Interrupt statistics */
  92. #define ISR_MASK 0x1F
  93. static struct {
  94. u32 test;
  95. u32 ui;
  96. u32 uei;
  97. u32 pci;
  98. u32 uri;
  99. u32 sli;
  100. u32 none;
  101. struct {
  102. u32 cnt;
  103. u32 buf[ISR_MASK+1];
  104. u32 idx;
  105. } hndl;
  106. } isr_statistics;
  107. /**
  108. * ffs_nr: find first (least significant) bit set
  109. * @x: the word to search
  110. *
  111. * This function returns bit number (instead of position)
  112. */
  113. static int ffs_nr(u32 x)
  114. {
  115. int n = ffs(x);
  116. return n ? n-1 : 32;
  117. }
  118. /******************************************************************************
  119. * HW block
  120. *****************************************************************************/
  121. /* register bank descriptor */
  122. static struct {
  123. unsigned lpm; /* is LPM? */
  124. void __iomem *abs; /* bus map offset */
  125. void __iomem *cap; /* bus map offset + CAP offset + CAP data */
  126. size_t size; /* bank size */
  127. } hw_bank;
  128. /* MSM specific */
  129. #define ABS_AHBBURST (0x0090UL)
  130. #define ABS_AHBMODE (0x0098UL)
  131. /* UDC register map */
  132. #define ABS_CAPLENGTH (0x100UL)
  133. #define ABS_HCCPARAMS (0x108UL)
  134. #define ABS_DCCPARAMS (0x124UL)
  135. #define ABS_TESTMODE (hw_bank.lpm ? 0x0FCUL : 0x138UL)
  136. /* offset to CAPLENTGH (addr + data) */
  137. #define CAP_USBCMD (0x000UL)
  138. #define CAP_USBSTS (0x004UL)
  139. #define CAP_USBINTR (0x008UL)
  140. #define CAP_DEVICEADDR (0x014UL)
  141. #define CAP_ENDPTLISTADDR (0x018UL)
  142. #define CAP_PORTSC (0x044UL)
  143. #define CAP_DEVLC (0x084UL)
  144. #define CAP_USBMODE (hw_bank.lpm ? 0x0C8UL : 0x068UL)
  145. #define CAP_ENDPTSETUPSTAT (hw_bank.lpm ? 0x0D8UL : 0x06CUL)
  146. #define CAP_ENDPTPRIME (hw_bank.lpm ? 0x0DCUL : 0x070UL)
  147. #define CAP_ENDPTFLUSH (hw_bank.lpm ? 0x0E0UL : 0x074UL)
  148. #define CAP_ENDPTSTAT (hw_bank.lpm ? 0x0E4UL : 0x078UL)
  149. #define CAP_ENDPTCOMPLETE (hw_bank.lpm ? 0x0E8UL : 0x07CUL)
  150. #define CAP_ENDPTCTRL (hw_bank.lpm ? 0x0ECUL : 0x080UL)
  151. #define CAP_LAST (hw_bank.lpm ? 0x12CUL : 0x0C0UL)
  152. /* maximum number of enpoints: valid only after hw_device_reset() */
  153. static unsigned hw_ep_max;
  154. /**
  155. * hw_ep_bit: calculates the bit number
  156. * @num: endpoint number
  157. * @dir: endpoint direction
  158. *
  159. * This function returns bit number
  160. */
  161. static inline int hw_ep_bit(int num, int dir)
  162. {
  163. return num + (dir ? 16 : 0);
  164. }
  165. /**
  166. * hw_aread: reads from register bitfield
  167. * @addr: address relative to bus map
  168. * @mask: bitfield mask
  169. *
  170. * This function returns register bitfield data
  171. */
  172. static u32 hw_aread(u32 addr, u32 mask)
  173. {
  174. return ioread32(addr + hw_bank.abs) & mask;
  175. }
  176. /**
  177. * hw_awrite: writes to register bitfield
  178. * @addr: address relative to bus map
  179. * @mask: bitfield mask
  180. * @data: new data
  181. */
  182. static void hw_awrite(u32 addr, u32 mask, u32 data)
  183. {
  184. iowrite32(hw_aread(addr, ~mask) | (data & mask),
  185. addr + hw_bank.abs);
  186. }
  187. /**
  188. * hw_cread: reads from register bitfield
  189. * @addr: address relative to CAP offset plus content
  190. * @mask: bitfield mask
  191. *
  192. * This function returns register bitfield data
  193. */
  194. static u32 hw_cread(u32 addr, u32 mask)
  195. {
  196. return ioread32(addr + hw_bank.cap) & mask;
  197. }
  198. /**
  199. * hw_cwrite: writes to register bitfield
  200. * @addr: address relative to CAP offset plus content
  201. * @mask: bitfield mask
  202. * @data: new data
  203. */
  204. static void hw_cwrite(u32 addr, u32 mask, u32 data)
  205. {
  206. iowrite32(hw_cread(addr, ~mask) | (data & mask),
  207. addr + hw_bank.cap);
  208. }
  209. /**
  210. * hw_ctest_and_clear: tests & clears register bitfield
  211. * @addr: address relative to CAP offset plus content
  212. * @mask: bitfield mask
  213. *
  214. * This function returns register bitfield data
  215. */
  216. static u32 hw_ctest_and_clear(u32 addr, u32 mask)
  217. {
  218. u32 reg = hw_cread(addr, mask);
  219. iowrite32(reg, addr + hw_bank.cap);
  220. return reg;
  221. }
  222. /**
  223. * hw_ctest_and_write: tests & writes register bitfield
  224. * @addr: address relative to CAP offset plus content
  225. * @mask: bitfield mask
  226. * @data: new data
  227. *
  228. * This function returns register bitfield data
  229. */
  230. static u32 hw_ctest_and_write(u32 addr, u32 mask, u32 data)
  231. {
  232. u32 reg = hw_cread(addr, ~0);
  233. iowrite32((reg & ~mask) | (data & mask), addr + hw_bank.cap);
  234. return (reg & mask) >> ffs_nr(mask);
  235. }
  236. static int hw_device_init(void __iomem *base)
  237. {
  238. u32 reg;
  239. /* bank is a module variable */
  240. hw_bank.abs = base;
  241. hw_bank.cap = hw_bank.abs;
  242. hw_bank.cap += ABS_CAPLENGTH;
  243. hw_bank.cap += ioread8(hw_bank.cap);
  244. reg = hw_aread(ABS_HCCPARAMS, HCCPARAMS_LEN) >> ffs_nr(HCCPARAMS_LEN);
  245. hw_bank.lpm = reg;
  246. hw_bank.size = hw_bank.cap - hw_bank.abs;
  247. hw_bank.size += CAP_LAST;
  248. hw_bank.size /= sizeof(u32);
  249. reg = hw_aread(ABS_DCCPARAMS, DCCPARAMS_DEN) >> ffs_nr(DCCPARAMS_DEN);
  250. hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */
  251. if (hw_ep_max == 0 || hw_ep_max > ENDPT_MAX)
  252. return -ENODEV;
  253. /* setup lock mode ? */
  254. /* ENDPTSETUPSTAT is '0' by default */
  255. /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
  256. return 0;
  257. }
  258. /**
  259. * hw_device_reset: resets chip (execute without interruption)
  260. * @base: register base address
  261. *
  262. * This function returns an error code
  263. */
  264. static int hw_device_reset(struct ci13xxx *udc)
  265. {
  266. /* should flush & stop before reset */
  267. hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0);
  268. hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
  269. hw_cwrite(CAP_USBCMD, USBCMD_RST, USBCMD_RST);
  270. while (hw_cread(CAP_USBCMD, USBCMD_RST))
  271. udelay(10); /* not RTOS friendly */
  272. if (udc->udc_driver->notify_event)
  273. udc->udc_driver->notify_event(udc,
  274. CI13XXX_CONTROLLER_RESET_EVENT);
  275. if (udc->udc_driver->flags & CI13XXX_DISABLE_STREAMING)
  276. hw_cwrite(CAP_USBMODE, USBMODE_SDIS, USBMODE_SDIS);
  277. /* USBMODE should be configured step by step */
  278. hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
  279. hw_cwrite(CAP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE);
  280. hw_cwrite(CAP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); /* HW >= 2.3 */
  281. if (hw_cread(CAP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) {
  282. pr_err("cannot enter in device mode");
  283. pr_err("lpm = %i", hw_bank.lpm);
  284. return -ENODEV;
  285. }
  286. return 0;
  287. }
  288. /**
  289. * hw_device_state: enables/disables interrupts & starts/stops device (execute
  290. * without interruption)
  291. * @dma: 0 => disable, !0 => enable and set dma engine
  292. *
  293. * This function returns an error code
  294. */
  295. static int hw_device_state(u32 dma)
  296. {
  297. if (dma) {
  298. hw_cwrite(CAP_ENDPTLISTADDR, ~0, dma);
  299. /* interrupt, error, port change, reset, sleep/suspend */
  300. hw_cwrite(CAP_USBINTR, ~0,
  301. USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI);
  302. hw_cwrite(CAP_USBCMD, USBCMD_RS, USBCMD_RS);
  303. } else {
  304. hw_cwrite(CAP_USBCMD, USBCMD_RS, 0);
  305. hw_cwrite(CAP_USBINTR, ~0, 0);
  306. }
  307. return 0;
  308. }
  309. /**
  310. * hw_ep_flush: flush endpoint fifo (execute without interruption)
  311. * @num: endpoint number
  312. * @dir: endpoint direction
  313. *
  314. * This function returns an error code
  315. */
  316. static int hw_ep_flush(int num, int dir)
  317. {
  318. int n = hw_ep_bit(num, dir);
  319. do {
  320. /* flush any pending transfer */
  321. hw_cwrite(CAP_ENDPTFLUSH, BIT(n), BIT(n));
  322. while (hw_cread(CAP_ENDPTFLUSH, BIT(n)))
  323. cpu_relax();
  324. } while (hw_cread(CAP_ENDPTSTAT, BIT(n)));
  325. return 0;
  326. }
  327. /**
  328. * hw_ep_disable: disables endpoint (execute without interruption)
  329. * @num: endpoint number
  330. * @dir: endpoint direction
  331. *
  332. * This function returns an error code
  333. */
  334. static int hw_ep_disable(int num, int dir)
  335. {
  336. hw_ep_flush(num, dir);
  337. hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32),
  338. dir ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0);
  339. return 0;
  340. }
  341. /**
  342. * hw_ep_enable: enables endpoint (execute without interruption)
  343. * @num: endpoint number
  344. * @dir: endpoint direction
  345. * @type: endpoint type
  346. *
  347. * This function returns an error code
  348. */
  349. static int hw_ep_enable(int num, int dir, int type)
  350. {
  351. u32 mask, data;
  352. if (dir) {
  353. mask = ENDPTCTRL_TXT; /* type */
  354. data = type << ffs_nr(mask);
  355. mask |= ENDPTCTRL_TXS; /* unstall */
  356. mask |= ENDPTCTRL_TXR; /* reset data toggle */
  357. data |= ENDPTCTRL_TXR;
  358. mask |= ENDPTCTRL_TXE; /* enable */
  359. data |= ENDPTCTRL_TXE;
  360. } else {
  361. mask = ENDPTCTRL_RXT; /* type */
  362. data = type << ffs_nr(mask);
  363. mask |= ENDPTCTRL_RXS; /* unstall */
  364. mask |= ENDPTCTRL_RXR; /* reset data toggle */
  365. data |= ENDPTCTRL_RXR;
  366. mask |= ENDPTCTRL_RXE; /* enable */
  367. data |= ENDPTCTRL_RXE;
  368. }
  369. hw_cwrite(CAP_ENDPTCTRL + num * sizeof(u32), mask, data);
  370. return 0;
  371. }
  372. /**
  373. * hw_ep_get_halt: return endpoint halt status
  374. * @num: endpoint number
  375. * @dir: endpoint direction
  376. *
  377. * This function returns 1 if endpoint halted
  378. */
  379. static int hw_ep_get_halt(int num, int dir)
  380. {
  381. u32 mask = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  382. return hw_cread(CAP_ENDPTCTRL + num * sizeof(u32), mask) ? 1 : 0;
  383. }
  384. /**
  385. * hw_test_and_clear_setup_status: test & clear setup status (execute without
  386. * interruption)
  387. * @n: bit number (endpoint)
  388. *
  389. * This function returns setup status
  390. */
  391. static int hw_test_and_clear_setup_status(int n)
  392. {
  393. return hw_ctest_and_clear(CAP_ENDPTSETUPSTAT, BIT(n));
  394. }
  395. /**
  396. * hw_ep_prime: primes endpoint (execute without interruption)
  397. * @num: endpoint number
  398. * @dir: endpoint direction
  399. * @is_ctrl: true if control endpoint
  400. *
  401. * This function returns an error code
  402. */
  403. static int hw_ep_prime(int num, int dir, int is_ctrl)
  404. {
  405. int n = hw_ep_bit(num, dir);
  406. if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
  407. return -EAGAIN;
  408. hw_cwrite(CAP_ENDPTPRIME, BIT(n), BIT(n));
  409. while (hw_cread(CAP_ENDPTPRIME, BIT(n)))
  410. cpu_relax();
  411. if (is_ctrl && dir == RX && hw_cread(CAP_ENDPTSETUPSTAT, BIT(num)))
  412. return -EAGAIN;
  413. /* status shoult be tested according with manual but it doesn't work */
  414. return 0;
  415. }
  416. /**
  417. * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute
  418. * without interruption)
  419. * @num: endpoint number
  420. * @dir: endpoint direction
  421. * @value: true => stall, false => unstall
  422. *
  423. * This function returns an error code
  424. */
  425. static int hw_ep_set_halt(int num, int dir, int value)
  426. {
  427. if (value != 0 && value != 1)
  428. return -EINVAL;
  429. do {
  430. u32 addr = CAP_ENDPTCTRL + num * sizeof(u32);
  431. u32 mask_xs = dir ? ENDPTCTRL_TXS : ENDPTCTRL_RXS;
  432. u32 mask_xr = dir ? ENDPTCTRL_TXR : ENDPTCTRL_RXR;
  433. /* data toggle - reserved for EP0 but it's in ESS */
  434. hw_cwrite(addr, mask_xs|mask_xr, value ? mask_xs : mask_xr);
  435. } while (value != hw_ep_get_halt(num, dir));
  436. return 0;
  437. }
  438. /**
  439. * hw_intr_clear: disables interrupt & clears interrupt status (execute without
  440. * interruption)
  441. * @n: interrupt bit
  442. *
  443. * This function returns an error code
  444. */
  445. static int hw_intr_clear(int n)
  446. {
  447. if (n >= REG_BITS)
  448. return -EINVAL;
  449. hw_cwrite(CAP_USBINTR, BIT(n), 0);
  450. hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
  451. return 0;
  452. }
  453. /**
  454. * hw_intr_force: enables interrupt & forces interrupt status (execute without
  455. * interruption)
  456. * @n: interrupt bit
  457. *
  458. * This function returns an error code
  459. */
  460. static int hw_intr_force(int n)
  461. {
  462. if (n >= REG_BITS)
  463. return -EINVAL;
  464. hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, TESTMODE_FORCE);
  465. hw_cwrite(CAP_USBINTR, BIT(n), BIT(n));
  466. hw_cwrite(CAP_USBSTS, BIT(n), BIT(n));
  467. hw_awrite(ABS_TESTMODE, TESTMODE_FORCE, 0);
  468. return 0;
  469. }
  470. /**
  471. * hw_is_port_high_speed: test if port is high speed
  472. *
  473. * This function returns true if high speed port
  474. */
  475. static int hw_port_is_high_speed(void)
  476. {
  477. return hw_bank.lpm ? hw_cread(CAP_DEVLC, DEVLC_PSPD) :
  478. hw_cread(CAP_PORTSC, PORTSC_HSP);
  479. }
  480. /**
  481. * hw_port_test_get: reads port test mode value
  482. *
  483. * This function returns port test mode value
  484. */
  485. static u8 hw_port_test_get(void)
  486. {
  487. return hw_cread(CAP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC);
  488. }
  489. /**
  490. * hw_port_test_set: writes port test mode (execute without interruption)
  491. * @mode: new value
  492. *
  493. * This function returns an error code
  494. */
  495. static int hw_port_test_set(u8 mode)
  496. {
  497. const u8 TEST_MODE_MAX = 7;
  498. if (mode > TEST_MODE_MAX)
  499. return -EINVAL;
  500. hw_cwrite(CAP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC));
  501. return 0;
  502. }
  503. /**
  504. * hw_read_intr_enable: returns interrupt enable register
  505. *
  506. * This function returns register data
  507. */
  508. static u32 hw_read_intr_enable(void)
  509. {
  510. return hw_cread(CAP_USBINTR, ~0);
  511. }
  512. /**
  513. * hw_read_intr_status: returns interrupt status register
  514. *
  515. * This function returns register data
  516. */
  517. static u32 hw_read_intr_status(void)
  518. {
  519. return hw_cread(CAP_USBSTS, ~0);
  520. }
  521. /**
  522. * hw_register_read: reads all device registers (execute without interruption)
  523. * @buf: destination buffer
  524. * @size: buffer size
  525. *
  526. * This function returns number of registers read
  527. */
  528. static size_t hw_register_read(u32 *buf, size_t size)
  529. {
  530. unsigned i;
  531. if (size > hw_bank.size)
  532. size = hw_bank.size;
  533. for (i = 0; i < size; i++)
  534. buf[i] = hw_aread(i * sizeof(u32), ~0);
  535. return size;
  536. }
  537. /**
  538. * hw_register_write: writes to register
  539. * @addr: register address
  540. * @data: register value
  541. *
  542. * This function returns an error code
  543. */
  544. static int hw_register_write(u16 addr, u32 data)
  545. {
  546. /* align */
  547. addr /= sizeof(u32);
  548. if (addr >= hw_bank.size)
  549. return -EINVAL;
  550. /* align */
  551. addr *= sizeof(u32);
  552. hw_awrite(addr, ~0, data);
  553. return 0;
  554. }
  555. /**
  556. * hw_test_and_clear_complete: test & clear complete status (execute without
  557. * interruption)
  558. * @n: bit number (endpoint)
  559. *
  560. * This function returns complete status
  561. */
  562. static int hw_test_and_clear_complete(int n)
  563. {
  564. return hw_ctest_and_clear(CAP_ENDPTCOMPLETE, BIT(n));
  565. }
  566. /**
  567. * hw_test_and_clear_intr_active: test & clear active interrupts (execute
  568. * without interruption)
  569. *
  570. * This function returns active interrutps
  571. */
  572. static u32 hw_test_and_clear_intr_active(void)
  573. {
  574. u32 reg = hw_read_intr_status() & hw_read_intr_enable();
  575. hw_cwrite(CAP_USBSTS, ~0, reg);
  576. return reg;
  577. }
  578. /**
  579. * hw_test_and_clear_setup_guard: test & clear setup guard (execute without
  580. * interruption)
  581. *
  582. * This function returns guard value
  583. */
  584. static int hw_test_and_clear_setup_guard(void)
  585. {
  586. return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, 0);
  587. }
  588. /**
  589. * hw_test_and_set_setup_guard: test & set setup guard (execute without
  590. * interruption)
  591. *
  592. * This function returns guard value
  593. */
  594. static int hw_test_and_set_setup_guard(void)
  595. {
  596. return hw_ctest_and_write(CAP_USBCMD, USBCMD_SUTW, USBCMD_SUTW);
  597. }
  598. /**
  599. * hw_usb_set_address: configures USB address (execute without interruption)
  600. * @value: new USB address
  601. *
  602. * This function returns an error code
  603. */
  604. static int hw_usb_set_address(u8 value)
  605. {
  606. /* advance */
  607. hw_cwrite(CAP_DEVICEADDR, DEVICEADDR_USBADR | DEVICEADDR_USBADRA,
  608. value << ffs_nr(DEVICEADDR_USBADR) | DEVICEADDR_USBADRA);
  609. return 0;
  610. }
  611. /**
  612. * hw_usb_reset: restart device after a bus reset (execute without
  613. * interruption)
  614. *
  615. * This function returns an error code
  616. */
  617. static int hw_usb_reset(void)
  618. {
  619. hw_usb_set_address(0);
  620. /* ESS flushes only at end?!? */
  621. hw_cwrite(CAP_ENDPTFLUSH, ~0, ~0); /* flush all EPs */
  622. /* clear setup token semaphores */
  623. hw_cwrite(CAP_ENDPTSETUPSTAT, 0, 0); /* writes its content */
  624. /* clear complete status */
  625. hw_cwrite(CAP_ENDPTCOMPLETE, 0, 0); /* writes its content */
  626. /* wait until all bits cleared */
  627. while (hw_cread(CAP_ENDPTPRIME, ~0))
  628. udelay(10); /* not RTOS friendly */
  629. /* reset all endpoints ? */
  630. /* reset internal status and wait for further instructions
  631. no need to verify the port reset status (ESS does it) */
  632. return 0;
  633. }
  634. /******************************************************************************
  635. * DBG block
  636. *****************************************************************************/
  637. /**
  638. * show_device: prints information about device capabilities and status
  639. *
  640. * Check "device.h" for details
  641. */
  642. static ssize_t show_device(struct device *dev, struct device_attribute *attr,
  643. char *buf)
  644. {
  645. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  646. struct usb_gadget *gadget = &udc->gadget;
  647. int n = 0;
  648. dbg_trace("[%s] %p\n", __func__, buf);
  649. if (attr == NULL || buf == NULL) {
  650. dev_err(dev, "[%s] EINVAL\n", __func__);
  651. return 0;
  652. }
  653. n += scnprintf(buf + n, PAGE_SIZE - n, "speed = %d\n",
  654. gadget->speed);
  655. n += scnprintf(buf + n, PAGE_SIZE - n, "is_dualspeed = %d\n",
  656. gadget->is_dualspeed);
  657. n += scnprintf(buf + n, PAGE_SIZE - n, "is_otg = %d\n",
  658. gadget->is_otg);
  659. n += scnprintf(buf + n, PAGE_SIZE - n, "is_a_peripheral = %d\n",
  660. gadget->is_a_peripheral);
  661. n += scnprintf(buf + n, PAGE_SIZE - n, "b_hnp_enable = %d\n",
  662. gadget->b_hnp_enable);
  663. n += scnprintf(buf + n, PAGE_SIZE - n, "a_hnp_support = %d\n",
  664. gadget->a_hnp_support);
  665. n += scnprintf(buf + n, PAGE_SIZE - n, "a_alt_hnp_support = %d\n",
  666. gadget->a_alt_hnp_support);
  667. n += scnprintf(buf + n, PAGE_SIZE - n, "name = %s\n",
  668. (gadget->name ? gadget->name : ""));
  669. return n;
  670. }
  671. static DEVICE_ATTR(device, S_IRUSR, show_device, NULL);
  672. /**
  673. * show_driver: prints information about attached gadget (if any)
  674. *
  675. * Check "device.h" for details
  676. */
  677. static ssize_t show_driver(struct device *dev, struct device_attribute *attr,
  678. char *buf)
  679. {
  680. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  681. struct usb_gadget_driver *driver = udc->driver;
  682. int n = 0;
  683. dbg_trace("[%s] %p\n", __func__, buf);
  684. if (attr == NULL || buf == NULL) {
  685. dev_err(dev, "[%s] EINVAL\n", __func__);
  686. return 0;
  687. }
  688. if (driver == NULL)
  689. return scnprintf(buf, PAGE_SIZE,
  690. "There is no gadget attached!\n");
  691. n += scnprintf(buf + n, PAGE_SIZE - n, "function = %s\n",
  692. (driver->function ? driver->function : ""));
  693. n += scnprintf(buf + n, PAGE_SIZE - n, "max speed = %d\n",
  694. driver->speed);
  695. return n;
  696. }
  697. static DEVICE_ATTR(driver, S_IRUSR, show_driver, NULL);
  698. /* Maximum event message length */
  699. #define DBG_DATA_MSG 64UL
  700. /* Maximum event messages */
  701. #define DBG_DATA_MAX 128UL
  702. /* Event buffer descriptor */
  703. static struct {
  704. char (buf[DBG_DATA_MAX])[DBG_DATA_MSG]; /* buffer */
  705. unsigned idx; /* index */
  706. unsigned tty; /* print to console? */
  707. rwlock_t lck; /* lock */
  708. } dbg_data = {
  709. .idx = 0,
  710. .tty = 0,
  711. .lck = __RW_LOCK_UNLOCKED(lck)
  712. };
  713. /**
  714. * dbg_dec: decrements debug event index
  715. * @idx: buffer index
  716. */
  717. static void dbg_dec(unsigned *idx)
  718. {
  719. *idx = (*idx - 1) & (DBG_DATA_MAX-1);
  720. }
  721. /**
  722. * dbg_inc: increments debug event index
  723. * @idx: buffer index
  724. */
  725. static void dbg_inc(unsigned *idx)
  726. {
  727. *idx = (*idx + 1) & (DBG_DATA_MAX-1);
  728. }
  729. /**
  730. * dbg_print: prints the common part of the event
  731. * @addr: endpoint address
  732. * @name: event name
  733. * @status: status
  734. * @extra: extra information
  735. */
  736. static void dbg_print(u8 addr, const char *name, int status, const char *extra)
  737. {
  738. struct timeval tval;
  739. unsigned int stamp;
  740. unsigned long flags;
  741. write_lock_irqsave(&dbg_data.lck, flags);
  742. do_gettimeofday(&tval);
  743. stamp = tval.tv_sec & 0xFFFF; /* 2^32 = 4294967296. Limit to 4096s */
  744. stamp = stamp * 1000000 + tval.tv_usec;
  745. scnprintf(dbg_data.buf[dbg_data.idx], DBG_DATA_MSG,
  746. "%04X\t? %02X %-7.7s %4i ?\t%s\n",
  747. stamp, addr, name, status, extra);
  748. dbg_inc(&dbg_data.idx);
  749. write_unlock_irqrestore(&dbg_data.lck, flags);
  750. if (dbg_data.tty != 0)
  751. pr_notice("%04X\t? %02X %-7.7s %4i ?\t%s\n",
  752. stamp, addr, name, status, extra);
  753. }
  754. /**
  755. * dbg_done: prints a DONE event
  756. * @addr: endpoint address
  757. * @td: transfer descriptor
  758. * @status: status
  759. */
  760. static void dbg_done(u8 addr, const u32 token, int status)
  761. {
  762. char msg[DBG_DATA_MSG];
  763. scnprintf(msg, sizeof(msg), "%d %02X",
  764. (int)(token & TD_TOTAL_BYTES) >> ffs_nr(TD_TOTAL_BYTES),
  765. (int)(token & TD_STATUS) >> ffs_nr(TD_STATUS));
  766. dbg_print(addr, "DONE", status, msg);
  767. }
  768. /**
  769. * dbg_event: prints a generic event
  770. * @addr: endpoint address
  771. * @name: event name
  772. * @status: status
  773. */
  774. static void dbg_event(u8 addr, const char *name, int status)
  775. {
  776. if (name != NULL)
  777. dbg_print(addr, name, status, "");
  778. }
  779. /*
  780. * dbg_queue: prints a QUEUE event
  781. * @addr: endpoint address
  782. * @req: USB request
  783. * @status: status
  784. */
  785. static void dbg_queue(u8 addr, const struct usb_request *req, int status)
  786. {
  787. char msg[DBG_DATA_MSG];
  788. if (req != NULL) {
  789. scnprintf(msg, sizeof(msg),
  790. "%d %d", !req->no_interrupt, req->length);
  791. dbg_print(addr, "QUEUE", status, msg);
  792. }
  793. }
  794. /**
  795. * dbg_setup: prints a SETUP event
  796. * @addr: endpoint address
  797. * @req: setup request
  798. */
  799. static void dbg_setup(u8 addr, const struct usb_ctrlrequest *req)
  800. {
  801. char msg[DBG_DATA_MSG];
  802. if (req != NULL) {
  803. scnprintf(msg, sizeof(msg),
  804. "%02X %02X %04X %04X %d", req->bRequestType,
  805. req->bRequest, le16_to_cpu(req->wValue),
  806. le16_to_cpu(req->wIndex), le16_to_cpu(req->wLength));
  807. dbg_print(addr, "SETUP", 0, msg);
  808. }
  809. }
  810. /**
  811. * show_events: displays the event buffer
  812. *
  813. * Check "device.h" for details
  814. */
  815. static ssize_t show_events(struct device *dev, struct device_attribute *attr,
  816. char *buf)
  817. {
  818. unsigned long flags;
  819. unsigned i, j, n = 0;
  820. dbg_trace("[%s] %p\n", __func__, buf);
  821. if (attr == NULL || buf == NULL) {
  822. dev_err(dev, "[%s] EINVAL\n", __func__);
  823. return 0;
  824. }
  825. read_lock_irqsave(&dbg_data.lck, flags);
  826. i = dbg_data.idx;
  827. for (dbg_dec(&i); i != dbg_data.idx; dbg_dec(&i)) {
  828. n += strlen(dbg_data.buf[i]);
  829. if (n >= PAGE_SIZE) {
  830. n -= strlen(dbg_data.buf[i]);
  831. break;
  832. }
  833. }
  834. for (j = 0, dbg_inc(&i); j < n; dbg_inc(&i))
  835. j += scnprintf(buf + j, PAGE_SIZE - j,
  836. "%s", dbg_data.buf[i]);
  837. read_unlock_irqrestore(&dbg_data.lck, flags);
  838. return n;
  839. }
  840. /**
  841. * store_events: configure if events are going to be also printed to console
  842. *
  843. * Check "device.h" for details
  844. */
  845. static ssize_t store_events(struct device *dev, struct device_attribute *attr,
  846. const char *buf, size_t count)
  847. {
  848. unsigned tty;
  849. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  850. if (attr == NULL || buf == NULL) {
  851. dev_err(dev, "[%s] EINVAL\n", __func__);
  852. goto done;
  853. }
  854. if (sscanf(buf, "%u", &tty) != 1 || tty > 1) {
  855. dev_err(dev, "<1|0>: enable|disable console log\n");
  856. goto done;
  857. }
  858. dbg_data.tty = tty;
  859. dev_info(dev, "tty = %u", dbg_data.tty);
  860. done:
  861. return count;
  862. }
  863. static DEVICE_ATTR(events, S_IRUSR | S_IWUSR, show_events, store_events);
  864. /**
  865. * show_inters: interrupt status, enable status and historic
  866. *
  867. * Check "device.h" for details
  868. */
  869. static ssize_t show_inters(struct device *dev, struct device_attribute *attr,
  870. char *buf)
  871. {
  872. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  873. unsigned long flags;
  874. u32 intr;
  875. unsigned i, j, n = 0;
  876. dbg_trace("[%s] %p\n", __func__, buf);
  877. if (attr == NULL || buf == NULL) {
  878. dev_err(dev, "[%s] EINVAL\n", __func__);
  879. return 0;
  880. }
  881. spin_lock_irqsave(udc->lock, flags);
  882. n += scnprintf(buf + n, PAGE_SIZE - n,
  883. "status = %08x\n", hw_read_intr_status());
  884. n += scnprintf(buf + n, PAGE_SIZE - n,
  885. "enable = %08x\n", hw_read_intr_enable());
  886. n += scnprintf(buf + n, PAGE_SIZE - n, "*test = %d\n",
  887. isr_statistics.test);
  888. n += scnprintf(buf + n, PAGE_SIZE - n, "? ui = %d\n",
  889. isr_statistics.ui);
  890. n += scnprintf(buf + n, PAGE_SIZE - n, "? uei = %d\n",
  891. isr_statistics.uei);
  892. n += scnprintf(buf + n, PAGE_SIZE - n, "? pci = %d\n",
  893. isr_statistics.pci);
  894. n += scnprintf(buf + n, PAGE_SIZE - n, "? uri = %d\n",
  895. isr_statistics.uri);
  896. n += scnprintf(buf + n, PAGE_SIZE - n, "? sli = %d\n",
  897. isr_statistics.sli);
  898. n += scnprintf(buf + n, PAGE_SIZE - n, "*none = %d\n",
  899. isr_statistics.none);
  900. n += scnprintf(buf + n, PAGE_SIZE - n, "*hndl = %d\n",
  901. isr_statistics.hndl.cnt);
  902. for (i = isr_statistics.hndl.idx, j = 0; j <= ISR_MASK; j++, i++) {
  903. i &= ISR_MASK;
  904. intr = isr_statistics.hndl.buf[i];
  905. if (USBi_UI & intr)
  906. n += scnprintf(buf + n, PAGE_SIZE - n, "ui ");
  907. intr &= ~USBi_UI;
  908. if (USBi_UEI & intr)
  909. n += scnprintf(buf + n, PAGE_SIZE - n, "uei ");
  910. intr &= ~USBi_UEI;
  911. if (USBi_PCI & intr)
  912. n += scnprintf(buf + n, PAGE_SIZE - n, "pci ");
  913. intr &= ~USBi_PCI;
  914. if (USBi_URI & intr)
  915. n += scnprintf(buf + n, PAGE_SIZE - n, "uri ");
  916. intr &= ~USBi_URI;
  917. if (USBi_SLI & intr)
  918. n += scnprintf(buf + n, PAGE_SIZE - n, "sli ");
  919. intr &= ~USBi_SLI;
  920. if (intr)
  921. n += scnprintf(buf + n, PAGE_SIZE - n, "??? ");
  922. if (isr_statistics.hndl.buf[i])
  923. n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
  924. }
  925. spin_unlock_irqrestore(udc->lock, flags);
  926. return n;
  927. }
  928. /**
  929. * store_inters: enable & force or disable an individual interrutps
  930. * (to be used for test purposes only)
  931. *
  932. * Check "device.h" for details
  933. */
  934. static ssize_t store_inters(struct device *dev, struct device_attribute *attr,
  935. const char *buf, size_t count)
  936. {
  937. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  938. unsigned long flags;
  939. unsigned en, bit;
  940. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  941. if (attr == NULL || buf == NULL) {
  942. dev_err(dev, "[%s] EINVAL\n", __func__);
  943. goto done;
  944. }
  945. if (sscanf(buf, "%u %u", &en, &bit) != 2 || en > 1) {
  946. dev_err(dev, "<1|0> <bit>: enable|disable interrupt");
  947. goto done;
  948. }
  949. spin_lock_irqsave(udc->lock, flags);
  950. if (en) {
  951. if (hw_intr_force(bit))
  952. dev_err(dev, "invalid bit number\n");
  953. else
  954. isr_statistics.test++;
  955. } else {
  956. if (hw_intr_clear(bit))
  957. dev_err(dev, "invalid bit number\n");
  958. }
  959. spin_unlock_irqrestore(udc->lock, flags);
  960. done:
  961. return count;
  962. }
  963. static DEVICE_ATTR(inters, S_IRUSR | S_IWUSR, show_inters, store_inters);
  964. /**
  965. * show_port_test: reads port test mode
  966. *
  967. * Check "device.h" for details
  968. */
  969. static ssize_t show_port_test(struct device *dev,
  970. struct device_attribute *attr, char *buf)
  971. {
  972. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  973. unsigned long flags;
  974. unsigned mode;
  975. dbg_trace("[%s] %p\n", __func__, buf);
  976. if (attr == NULL || buf == NULL) {
  977. dev_err(dev, "[%s] EINVAL\n", __func__);
  978. return 0;
  979. }
  980. spin_lock_irqsave(udc->lock, flags);
  981. mode = hw_port_test_get();
  982. spin_unlock_irqrestore(udc->lock, flags);
  983. return scnprintf(buf, PAGE_SIZE, "mode = %u\n", mode);
  984. }
  985. /**
  986. * store_port_test: writes port test mode
  987. *
  988. * Check "device.h" for details
  989. */
  990. static ssize_t store_port_test(struct device *dev,
  991. struct device_attribute *attr,
  992. const char *buf, size_t count)
  993. {
  994. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  995. unsigned long flags;
  996. unsigned mode;
  997. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  998. if (attr == NULL || buf == NULL) {
  999. dev_err(dev, "[%s] EINVAL\n", __func__);
  1000. goto done;
  1001. }
  1002. if (sscanf(buf, "%u", &mode) != 1) {
  1003. dev_err(dev, "<mode>: set port test mode");
  1004. goto done;
  1005. }
  1006. spin_lock_irqsave(udc->lock, flags);
  1007. if (hw_port_test_set(mode))
  1008. dev_err(dev, "invalid mode\n");
  1009. spin_unlock_irqrestore(udc->lock, flags);
  1010. done:
  1011. return count;
  1012. }
  1013. static DEVICE_ATTR(port_test, S_IRUSR | S_IWUSR,
  1014. show_port_test, store_port_test);
  1015. /**
  1016. * show_qheads: DMA contents of all queue heads
  1017. *
  1018. * Check "device.h" for details
  1019. */
  1020. static ssize_t show_qheads(struct device *dev, struct device_attribute *attr,
  1021. char *buf)
  1022. {
  1023. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1024. unsigned long flags;
  1025. unsigned i, j, n = 0;
  1026. dbg_trace("[%s] %p\n", __func__, buf);
  1027. if (attr == NULL || buf == NULL) {
  1028. dev_err(dev, "[%s] EINVAL\n", __func__);
  1029. return 0;
  1030. }
  1031. spin_lock_irqsave(udc->lock, flags);
  1032. for (i = 0; i < hw_ep_max/2; i++) {
  1033. struct ci13xxx_ep *mEpRx = &udc->ci13xxx_ep[i];
  1034. struct ci13xxx_ep *mEpTx = &udc->ci13xxx_ep[i + hw_ep_max/2];
  1035. n += scnprintf(buf + n, PAGE_SIZE - n,
  1036. "EP=%02i: RX=%08X TX=%08X\n",
  1037. i, (u32)mEpRx->qh.dma, (u32)mEpTx->qh.dma);
  1038. for (j = 0; j < (sizeof(struct ci13xxx_qh)/sizeof(u32)); j++) {
  1039. n += scnprintf(buf + n, PAGE_SIZE - n,
  1040. " %04X: %08X %08X\n", j,
  1041. *((u32 *)mEpRx->qh.ptr + j),
  1042. *((u32 *)mEpTx->qh.ptr + j));
  1043. }
  1044. }
  1045. spin_unlock_irqrestore(udc->lock, flags);
  1046. return n;
  1047. }
  1048. static DEVICE_ATTR(qheads, S_IRUSR, show_qheads, NULL);
  1049. /**
  1050. * show_registers: dumps all registers
  1051. *
  1052. * Check "device.h" for details
  1053. */
  1054. #define DUMP_ENTRIES 512
  1055. static ssize_t show_registers(struct device *dev,
  1056. struct device_attribute *attr, char *buf)
  1057. {
  1058. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1059. unsigned long flags;
  1060. u32 *dump;
  1061. unsigned i, k, n = 0;
  1062. dbg_trace("[%s] %p\n", __func__, buf);
  1063. if (attr == NULL || buf == NULL) {
  1064. dev_err(dev, "[%s] EINVAL\n", __func__);
  1065. return 0;
  1066. }
  1067. dump = kmalloc(sizeof(u32) * DUMP_ENTRIES, GFP_KERNEL);
  1068. if (!dump) {
  1069. dev_err(dev, "%s: out of memory\n", __func__);
  1070. return 0;
  1071. }
  1072. spin_lock_irqsave(udc->lock, flags);
  1073. k = hw_register_read(dump, DUMP_ENTRIES);
  1074. spin_unlock_irqrestore(udc->lock, flags);
  1075. for (i = 0; i < k; i++) {
  1076. n += scnprintf(buf + n, PAGE_SIZE - n,
  1077. "reg[0x%04X] = 0x%08X\n",
  1078. i * (unsigned)sizeof(u32), dump[i]);
  1079. }
  1080. kfree(dump);
  1081. return n;
  1082. }
  1083. /**
  1084. * store_registers: writes value to register address
  1085. *
  1086. * Check "device.h" for details
  1087. */
  1088. static ssize_t store_registers(struct device *dev,
  1089. struct device_attribute *attr,
  1090. const char *buf, size_t count)
  1091. {
  1092. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1093. unsigned long addr, data, flags;
  1094. dbg_trace("[%s] %p, %d\n", __func__, buf, count);
  1095. if (attr == NULL || buf == NULL) {
  1096. dev_err(dev, "[%s] EINVAL\n", __func__);
  1097. goto done;
  1098. }
  1099. if (sscanf(buf, "%li %li", &addr, &data) != 2) {
  1100. dev_err(dev, "<addr> <data>: write data to register address");
  1101. goto done;
  1102. }
  1103. spin_lock_irqsave(udc->lock, flags);
  1104. if (hw_register_write(addr, data))
  1105. dev_err(dev, "invalid address range\n");
  1106. spin_unlock_irqrestore(udc->lock, flags);
  1107. done:
  1108. return count;
  1109. }
  1110. static DEVICE_ATTR(registers, S_IRUSR | S_IWUSR,
  1111. show_registers, store_registers);
  1112. /**
  1113. * show_requests: DMA contents of all requests currently queued (all endpts)
  1114. *
  1115. * Check "device.h" for details
  1116. */
  1117. static ssize_t show_requests(struct device *dev, struct device_attribute *attr,
  1118. char *buf)
  1119. {
  1120. struct ci13xxx *udc = container_of(dev, struct ci13xxx, gadget.dev);
  1121. unsigned long flags;
  1122. struct list_head *ptr = NULL;
  1123. struct ci13xxx_req *req = NULL;
  1124. unsigned i, j, n = 0, qSize = sizeof(struct ci13xxx_td)/sizeof(u32);
  1125. dbg_trace("[%s] %p\n", __func__, buf);
  1126. if (attr == NULL || buf == NULL) {
  1127. dev_err(dev, "[%s] EINVAL\n", __func__);
  1128. return 0;
  1129. }
  1130. spin_lock_irqsave(udc->lock, flags);
  1131. for (i = 0; i < hw_ep_max; i++)
  1132. list_for_each(ptr, &udc->ci13xxx_ep[i].qh.queue)
  1133. {
  1134. req = list_entry(ptr, struct ci13xxx_req, queue);
  1135. n += scnprintf(buf + n, PAGE_SIZE - n,
  1136. "EP=%02i: TD=%08X %s\n",
  1137. i % hw_ep_max/2, (u32)req->dma,
  1138. ((i < hw_ep_max/2) ? "RX" : "TX"));
  1139. for (j = 0; j < qSize; j++)
  1140. n += scnprintf(buf + n, PAGE_SIZE - n,
  1141. " %04X: %08X\n", j,
  1142. *((u32 *)req->ptr + j));
  1143. }
  1144. spin_unlock_irqrestore(udc->lock, flags);
  1145. return n;
  1146. }
  1147. static DEVICE_ATTR(requests, S_IRUSR, show_requests, NULL);
  1148. /**
  1149. * dbg_create_files: initializes the attribute interface
  1150. * @dev: device
  1151. *
  1152. * This function returns an error code
  1153. */
  1154. __maybe_unused static int dbg_create_files(struct device *dev)
  1155. {
  1156. int retval = 0;
  1157. if (dev == NULL)
  1158. return -EINVAL;
  1159. retval = device_create_file(dev, &dev_attr_device);
  1160. if (retval)
  1161. goto done;
  1162. retval = device_create_file(dev, &dev_attr_driver);
  1163. if (retval)
  1164. goto rm_device;
  1165. retval = device_create_file(dev, &dev_attr_events);
  1166. if (retval)
  1167. goto rm_driver;
  1168. retval = device_create_file(dev, &dev_attr_inters);
  1169. if (retval)
  1170. goto rm_events;
  1171. retval = device_create_file(dev, &dev_attr_port_test);
  1172. if (retval)
  1173. goto rm_inters;
  1174. retval = device_create_file(dev, &dev_attr_qheads);
  1175. if (retval)
  1176. goto rm_port_test;
  1177. retval = device_create_file(dev, &dev_attr_registers);
  1178. if (retval)
  1179. goto rm_qheads;
  1180. retval = device_create_file(dev, &dev_attr_requests);
  1181. if (retval)
  1182. goto rm_registers;
  1183. return 0;
  1184. rm_registers:
  1185. device_remove_file(dev, &dev_attr_registers);
  1186. rm_qheads:
  1187. device_remove_file(dev, &dev_attr_qheads);
  1188. rm_port_test:
  1189. device_remove_file(dev, &dev_attr_port_test);
  1190. rm_inters:
  1191. device_remove_file(dev, &dev_attr_inters);
  1192. rm_events:
  1193. device_remove_file(dev, &dev_attr_events);
  1194. rm_driver:
  1195. device_remove_file(dev, &dev_attr_driver);
  1196. rm_device:
  1197. device_remove_file(dev, &dev_attr_device);
  1198. done:
  1199. return retval;
  1200. }
  1201. /**
  1202. * dbg_remove_files: destroys the attribute interface
  1203. * @dev: device
  1204. *
  1205. * This function returns an error code
  1206. */
  1207. __maybe_unused static int dbg_remove_files(struct device *dev)
  1208. {
  1209. if (dev == NULL)
  1210. return -EINVAL;
  1211. device_remove_file(dev, &dev_attr_requests);
  1212. device_remove_file(dev, &dev_attr_registers);
  1213. device_remove_file(dev, &dev_attr_qheads);
  1214. device_remove_file(dev, &dev_attr_port_test);
  1215. device_remove_file(dev, &dev_attr_inters);
  1216. device_remove_file(dev, &dev_attr_events);
  1217. device_remove_file(dev, &dev_attr_driver);
  1218. device_remove_file(dev, &dev_attr_device);
  1219. return 0;
  1220. }
  1221. /******************************************************************************
  1222. * UTIL block
  1223. *****************************************************************************/
  1224. /**
  1225. * _usb_addr: calculates endpoint address from direction & number
  1226. * @ep: endpoint
  1227. */
  1228. static inline u8 _usb_addr(struct ci13xxx_ep *ep)
  1229. {
  1230. return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num;
  1231. }
  1232. /**
  1233. * _hardware_queue: configures a request at hardware level
  1234. * @gadget: gadget
  1235. * @mEp: endpoint
  1236. *
  1237. * This function returns an error code
  1238. */
  1239. static int _hardware_enqueue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  1240. {
  1241. unsigned i;
  1242. int ret = 0;
  1243. unsigned length = mReq->req.length;
  1244. trace("%p, %p", mEp, mReq);
  1245. /* don't queue twice */
  1246. if (mReq->req.status == -EALREADY)
  1247. return -EALREADY;
  1248. mReq->req.status = -EALREADY;
  1249. if (length && !mReq->req.dma) {
  1250. mReq->req.dma = \
  1251. dma_map_single(mEp->device, mReq->req.buf,
  1252. length, mEp->dir ? DMA_TO_DEVICE :
  1253. DMA_FROM_DEVICE);
  1254. if (mReq->req.dma == 0)
  1255. return -ENOMEM;
  1256. mReq->map = 1;
  1257. }
  1258. if (mReq->req.zero && length && (length % mEp->ep.maxpacket == 0)) {
  1259. mReq->zptr = dma_pool_alloc(mEp->td_pool, GFP_ATOMIC,
  1260. &mReq->zdma);
  1261. if (mReq->zptr == NULL) {
  1262. if (mReq->map) {
  1263. dma_unmap_single(mEp->device, mReq->req.dma,
  1264. length, mEp->dir ? DMA_TO_DEVICE :
  1265. DMA_FROM_DEVICE);
  1266. mReq->req.dma = 0;
  1267. mReq->map = 0;
  1268. }
  1269. return -ENOMEM;
  1270. }
  1271. memset(mReq->zptr, 0, sizeof(*mReq->zptr));
  1272. mReq->zptr->next = TD_TERMINATE;
  1273. mReq->zptr->token = TD_STATUS_ACTIVE;
  1274. if (!mReq->req.no_interrupt)
  1275. mReq->zptr->token |= TD_IOC;
  1276. }
  1277. /*
  1278. * TD configuration
  1279. * TODO - handle requests which spawns into several TDs
  1280. */
  1281. memset(mReq->ptr, 0, sizeof(*mReq->ptr));
  1282. mReq->ptr->token = length << ffs_nr(TD_TOTAL_BYTES);
  1283. mReq->ptr->token &= TD_TOTAL_BYTES;
  1284. mReq->ptr->token |= TD_STATUS_ACTIVE;
  1285. if (mReq->zptr) {
  1286. mReq->ptr->next = mReq->zdma;
  1287. } else {
  1288. mReq->ptr->next = TD_TERMINATE;
  1289. if (!mReq->req.no_interrupt)
  1290. mReq->ptr->token |= TD_IOC;
  1291. }
  1292. mReq->ptr->page[0] = mReq->req.dma;
  1293. for (i = 1; i < 5; i++)
  1294. mReq->ptr->page[i] =
  1295. (mReq->req.dma + i * CI13XXX_PAGE_SIZE) & ~TD_RESERVED_MASK;
  1296. if (!list_empty(&mEp->qh.queue)) {
  1297. struct ci13xxx_req *mReqPrev;
  1298. int n = hw_ep_bit(mEp->num, mEp->dir);
  1299. int tmp_stat;
  1300. mReqPrev = list_entry(mEp->qh.queue.prev,
  1301. struct ci13xxx_req, queue);
  1302. if (mReqPrev->zptr)
  1303. mReqPrev->zptr->next = mReq->dma & TD_ADDR_MASK;
  1304. else
  1305. mReqPrev->ptr->next = mReq->dma & TD_ADDR_MASK;
  1306. wmb();
  1307. if (hw_cread(CAP_ENDPTPRIME, BIT(n)))
  1308. goto done;
  1309. do {
  1310. hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW);
  1311. tmp_stat = hw_cread(CAP_ENDPTSTAT, BIT(n));
  1312. } while (!hw_cread(CAP_USBCMD, USBCMD_ATDTW));
  1313. hw_cwrite(CAP_USBCMD, USBCMD_ATDTW, 0);
  1314. if (tmp_stat)
  1315. goto done;
  1316. }
  1317. /* QH configuration */
  1318. mEp->qh.ptr->td.next = mReq->dma; /* TERMINATE = 0 */
  1319. mEp->qh.ptr->td.token &= ~TD_STATUS; /* clear status */
  1320. mEp->qh.ptr->cap |= QH_ZLT;
  1321. wmb(); /* synchronize before ep prime */
  1322. ret = hw_ep_prime(mEp->num, mEp->dir,
  1323. mEp->type == USB_ENDPOINT_XFER_CONTROL);
  1324. done:
  1325. return ret;
  1326. }
  1327. /**
  1328. * _hardware_dequeue: handles a request at hardware level
  1329. * @gadget: gadget
  1330. * @mEp: endpoint
  1331. *
  1332. * This function returns an error code
  1333. */
  1334. static int _hardware_dequeue(struct ci13xxx_ep *mEp, struct ci13xxx_req *mReq)
  1335. {
  1336. trace("%p, %p", mEp, mReq);
  1337. if (mReq->req.status != -EALREADY)
  1338. return -EINVAL;
  1339. if ((TD_STATUS_ACTIVE & mReq->ptr->token) != 0)
  1340. return -EBUSY;
  1341. if (mReq->zptr) {
  1342. if ((TD_STATUS_ACTIVE & mReq->zptr->token) != 0)
  1343. return -EBUSY;
  1344. dma_pool_free(mEp->td_pool, mReq->zptr, mReq->zdma);
  1345. mReq->zptr = NULL;
  1346. }
  1347. mReq->req.status = 0;
  1348. if (mReq->map) {
  1349. dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
  1350. mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  1351. mReq->req.dma = 0;
  1352. mReq->map = 0;
  1353. }
  1354. mReq->req.status = mReq->ptr->token & TD_STATUS;
  1355. if ((TD_STATUS_HALTED & mReq->req.status) != 0)
  1356. mReq->req.status = -1;
  1357. else if ((TD_STATUS_DT_ERR & mReq->req.status) != 0)
  1358. mReq->req.status = -1;
  1359. else if ((TD_STATUS_TR_ERR & mReq->req.status) != 0)
  1360. mReq->req.status = -1;
  1361. mReq->req.actual = mReq->ptr->token & TD_TOTAL_BYTES;
  1362. mReq->req.actual >>= ffs_nr(TD_TOTAL_BYTES);
  1363. mReq->req.actual = mReq->req.length - mReq->req.actual;
  1364. mReq->req.actual = mReq->req.status ? 0 : mReq->req.actual;
  1365. return mReq->req.actual;
  1366. }
  1367. /**
  1368. * _ep_nuke: dequeues all endpoint requests
  1369. * @mEp: endpoint
  1370. *
  1371. * This function returns an error code
  1372. * Caller must hold lock
  1373. */
  1374. static int _ep_nuke(struct ci13xxx_ep *mEp)
  1375. __releases(mEp->lock)
  1376. __acquires(mEp->lock)
  1377. {
  1378. trace("%p", mEp);
  1379. if (mEp == NULL)
  1380. return -EINVAL;
  1381. hw_ep_flush(mEp->num, mEp->dir);
  1382. while (!list_empty(&mEp->qh.queue)) {
  1383. /* pop oldest request */
  1384. struct ci13xxx_req *mReq = \
  1385. list_entry(mEp->qh.queue.next,
  1386. struct ci13xxx_req, queue);
  1387. list_del_init(&mReq->queue);
  1388. mReq->req.status = -ESHUTDOWN;
  1389. if (mReq->req.complete != NULL) {
  1390. spin_unlock(mEp->lock);
  1391. mReq->req.complete(&mEp->ep, &mReq->req);
  1392. spin_lock(mEp->lock);
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. /**
  1398. * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts
  1399. * @gadget: gadget
  1400. *
  1401. * This function returns an error code
  1402. * Caller must hold lock
  1403. */
  1404. static int _gadget_stop_activity(struct usb_gadget *gadget)
  1405. {
  1406. struct usb_ep *ep;
  1407. struct ci13xxx *udc = container_of(gadget, struct ci13xxx, gadget);
  1408. unsigned long flags;
  1409. trace("%p", gadget);
  1410. if (gadget == NULL)
  1411. return -EINVAL;
  1412. spin_lock_irqsave(udc->lock, flags);
  1413. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1414. udc->remote_wakeup = 0;
  1415. udc->suspended = 0;
  1416. spin_unlock_irqrestore(udc->lock, flags);
  1417. /* flush all endpoints */
  1418. gadget_for_each_ep(ep, gadget) {
  1419. usb_ep_fifo_flush(ep);
  1420. }
  1421. usb_ep_fifo_flush(&udc->ep0out.ep);
  1422. usb_ep_fifo_flush(&udc->ep0in.ep);
  1423. udc->driver->disconnect(gadget);
  1424. /* make sure to disable all endpoints */
  1425. gadget_for_each_ep(ep, gadget) {
  1426. usb_ep_disable(ep);
  1427. }
  1428. if (udc->status != NULL) {
  1429. usb_ep_free_request(&udc->ep0in.ep, udc->status);
  1430. udc->status = NULL;
  1431. }
  1432. return 0;
  1433. }
  1434. /******************************************************************************
  1435. * ISR block
  1436. *****************************************************************************/
  1437. /**
  1438. * isr_reset_handler: USB reset interrupt handler
  1439. * @udc: UDC device
  1440. *
  1441. * This function resets USB engine after a bus reset occurred
  1442. */
  1443. static void isr_reset_handler(struct ci13xxx *udc)
  1444. __releases(udc->lock)
  1445. __acquires(udc->lock)
  1446. {
  1447. int retval;
  1448. trace("%p", udc);
  1449. if (udc == NULL) {
  1450. err("EINVAL");
  1451. return;
  1452. }
  1453. dbg_event(0xFF, "BUS RST", 0);
  1454. spin_unlock(udc->lock);
  1455. retval = _gadget_stop_activity(&udc->gadget);
  1456. if (retval)
  1457. goto done;
  1458. retval = hw_usb_reset();
  1459. if (retval)
  1460. goto done;
  1461. udc->status = usb_ep_alloc_request(&udc->ep0in.ep, GFP_ATOMIC);
  1462. if (udc->status == NULL)
  1463. retval = -ENOMEM;
  1464. spin_lock(udc->lock);
  1465. done:
  1466. if (retval)
  1467. err("error: %i", retval);
  1468. }
  1469. /**
  1470. * isr_get_status_complete: get_status request complete function
  1471. * @ep: endpoint
  1472. * @req: request handled
  1473. *
  1474. * Caller must release lock
  1475. */
  1476. static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req)
  1477. {
  1478. trace("%p, %p", ep, req);
  1479. if (ep == NULL || req == NULL) {
  1480. err("EINVAL");
  1481. return;
  1482. }
  1483. kfree(req->buf);
  1484. usb_ep_free_request(ep, req);
  1485. }
  1486. /**
  1487. * isr_get_status_response: get_status request response
  1488. * @udc: udc struct
  1489. * @setup: setup request packet
  1490. *
  1491. * This function returns an error code
  1492. */
  1493. static int isr_get_status_response(struct ci13xxx *udc,
  1494. struct usb_ctrlrequest *setup)
  1495. __releases(mEp->lock)
  1496. __acquires(mEp->lock)
  1497. {
  1498. struct ci13xxx_ep *mEp = &udc->ep0in;
  1499. struct usb_request *req = NULL;
  1500. gfp_t gfp_flags = GFP_ATOMIC;
  1501. int dir, num, retval;
  1502. trace("%p, %p", mEp, setup);
  1503. if (mEp == NULL || setup == NULL)
  1504. return -EINVAL;
  1505. spin_unlock(mEp->lock);
  1506. req = usb_ep_alloc_request(&mEp->ep, gfp_flags);
  1507. spin_lock(mEp->lock);
  1508. if (req == NULL)
  1509. return -ENOMEM;
  1510. req->complete = isr_get_status_complete;
  1511. req->length = 2;
  1512. req->buf = kzalloc(req->length, gfp_flags);
  1513. if (req->buf == NULL) {
  1514. retval = -ENOMEM;
  1515. goto err_free_req;
  1516. }
  1517. if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
  1518. /* Assume that device is bus powered for now. */
  1519. *((u16 *)req->buf) = _udc->remote_wakeup << 1;
  1520. retval = 0;
  1521. } else if ((setup->bRequestType & USB_RECIP_MASK) \
  1522. == USB_RECIP_ENDPOINT) {
  1523. dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ?
  1524. TX : RX;
  1525. num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK;
  1526. *((u16 *)req->buf) = hw_ep_get_halt(num, dir);
  1527. }
  1528. /* else do nothing; reserved for future use */
  1529. spin_unlock(mEp->lock);
  1530. retval = usb_ep_queue(&mEp->ep, req, gfp_flags);
  1531. spin_lock(mEp->lock);
  1532. if (retval)
  1533. goto err_free_buf;
  1534. return 0;
  1535. err_free_buf:
  1536. kfree(req->buf);
  1537. err_free_req:
  1538. spin_unlock(mEp->lock);
  1539. usb_ep_free_request(&mEp->ep, req);
  1540. spin_lock(mEp->lock);
  1541. return retval;
  1542. }
  1543. /**
  1544. * isr_setup_status_complete: setup_status request complete function
  1545. * @ep: endpoint
  1546. * @req: request handled
  1547. *
  1548. * Caller must release lock. Put the port in test mode if test mode
  1549. * feature is selected.
  1550. */
  1551. static void
  1552. isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req)
  1553. {
  1554. struct ci13xxx *udc = req->context;
  1555. unsigned long flags;
  1556. trace("%p, %p", ep, req);
  1557. spin_lock_irqsave(udc->lock, flags);
  1558. if (udc->test_mode)
  1559. hw_port_test_set(udc->test_mode);
  1560. spin_unlock_irqrestore(udc->lock, flags);
  1561. }
  1562. /**
  1563. * isr_setup_status_phase: queues the status phase of a setup transation
  1564. * @udc: udc struct
  1565. *
  1566. * This function returns an error code
  1567. */
  1568. static int isr_setup_status_phase(struct ci13xxx *udc)
  1569. __releases(mEp->lock)
  1570. __acquires(mEp->lock)
  1571. {
  1572. int retval;
  1573. struct ci13xxx_ep *mEp;
  1574. trace("%p", udc);
  1575. mEp = (udc->ep0_dir == TX) ? &udc->ep0out : &udc->ep0in;
  1576. udc->status->context = udc;
  1577. udc->status->complete = isr_setup_status_complete;
  1578. spin_unlock(mEp->lock);
  1579. retval = usb_ep_queue(&mEp->ep, udc->status, GFP_ATOMIC);
  1580. spin_lock(mEp->lock);
  1581. return retval;
  1582. }
  1583. /**
  1584. * isr_tr_complete_low: transaction complete low level handler
  1585. * @mEp: endpoint
  1586. *
  1587. * This function returns an error code
  1588. * Caller must hold lock
  1589. */
  1590. static int isr_tr_complete_low(struct ci13xxx_ep *mEp)
  1591. __releases(mEp->lock)
  1592. __acquires(mEp->lock)
  1593. {
  1594. struct ci13xxx_req *mReq, *mReqTemp;
  1595. struct ci13xxx_ep *mEpTemp = mEp;
  1596. int uninitialized_var(retval);
  1597. trace("%p", mEp);
  1598. if (list_empty(&mEp->qh.queue))
  1599. return -EINVAL;
  1600. list_for_each_entry_safe(mReq, mReqTemp, &mEp->qh.queue,
  1601. queue) {
  1602. retval = _hardware_dequeue(mEp, mReq);
  1603. if (retval < 0)
  1604. break;
  1605. list_del_init(&mReq->queue);
  1606. dbg_done(_usb_addr(mEp), mReq->ptr->token, retval);
  1607. if (mReq->req.complete != NULL) {
  1608. spin_unlock(mEp->lock);
  1609. if ((mEp->type == USB_ENDPOINT_XFER_CONTROL) &&
  1610. mReq->req.length)
  1611. mEpTemp = &_udc->ep0in;
  1612. mReq->req.complete(&mEpTemp->ep, &mReq->req);
  1613. spin_lock(mEp->lock);
  1614. }
  1615. }
  1616. if (retval == -EBUSY)
  1617. retval = 0;
  1618. if (retval < 0)
  1619. dbg_event(_usb_addr(mEp), "DONE", retval);
  1620. return retval;
  1621. }
  1622. /**
  1623. * isr_tr_complete_handler: transaction complete interrupt handler
  1624. * @udc: UDC descriptor
  1625. *
  1626. * This function handles traffic events
  1627. */
  1628. static void isr_tr_complete_handler(struct ci13xxx *udc)
  1629. __releases(udc->lock)
  1630. __acquires(udc->lock)
  1631. {
  1632. unsigned i;
  1633. u8 tmode = 0;
  1634. trace("%p", udc);
  1635. if (udc == NULL) {
  1636. err("EINVAL");
  1637. return;
  1638. }
  1639. for (i = 0; i < hw_ep_max; i++) {
  1640. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
  1641. int type, num, dir, err = -EINVAL;
  1642. struct usb_ctrlrequest req;
  1643. if (mEp->desc == NULL)
  1644. continue; /* not configured */
  1645. if (hw_test_and_clear_complete(i)) {
  1646. err = isr_tr_complete_low(mEp);
  1647. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  1648. if (err > 0) /* needs status phase */
  1649. err = isr_setup_status_phase(udc);
  1650. if (err < 0) {
  1651. dbg_event(_usb_addr(mEp),
  1652. "ERROR", err);
  1653. spin_unlock(udc->lock);
  1654. if (usb_ep_set_halt(&mEp->ep))
  1655. err("error: ep_set_halt");
  1656. spin_lock(udc->lock);
  1657. }
  1658. }
  1659. }
  1660. if (mEp->type != USB_ENDPOINT_XFER_CONTROL ||
  1661. !hw_test_and_clear_setup_status(i))
  1662. continue;
  1663. if (i != 0) {
  1664. warn("ctrl traffic received at endpoint");
  1665. continue;
  1666. }
  1667. /*
  1668. * Flush data and handshake transactions of previous
  1669. * setup packet.
  1670. */
  1671. _ep_nuke(&udc->ep0out);
  1672. _ep_nuke(&udc->ep0in);
  1673. /* read_setup_packet */
  1674. do {
  1675. hw_test_and_set_setup_guard();
  1676. memcpy(&req, &mEp->qh.ptr->setup, sizeof(req));
  1677. } while (!hw_test_and_clear_setup_guard());
  1678. type = req.bRequestType;
  1679. udc->ep0_dir = (type & USB_DIR_IN) ? TX : RX;
  1680. dbg_setup(_usb_addr(mEp), &req);
  1681. switch (req.bRequest) {
  1682. case USB_REQ_CLEAR_FEATURE:
  1683. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  1684. le16_to_cpu(req.wValue) ==
  1685. USB_ENDPOINT_HALT) {
  1686. if (req.wLength != 0)
  1687. break;
  1688. num = le16_to_cpu(req.wIndex);
  1689. dir = num & USB_ENDPOINT_DIR_MASK;
  1690. num &= USB_ENDPOINT_NUMBER_MASK;
  1691. if (dir) /* TX */
  1692. num += hw_ep_max/2;
  1693. if (!udc->ci13xxx_ep[num].wedge) {
  1694. spin_unlock(udc->lock);
  1695. err = usb_ep_clear_halt(
  1696. &udc->ci13xxx_ep[num].ep);
  1697. spin_lock(udc->lock);
  1698. if (err)
  1699. break;
  1700. }
  1701. err = isr_setup_status_phase(udc);
  1702. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) &&
  1703. le16_to_cpu(req.wValue) ==
  1704. USB_DEVICE_REMOTE_WAKEUP) {
  1705. if (req.wLength != 0)
  1706. break;
  1707. udc->remote_wakeup = 0;
  1708. err = isr_setup_status_phase(udc);
  1709. } else {
  1710. goto delegate;
  1711. }
  1712. break;
  1713. case USB_REQ_GET_STATUS:
  1714. if (type != (USB_DIR_IN|USB_RECIP_DEVICE) &&
  1715. type != (USB_DIR_IN|USB_RECIP_ENDPOINT) &&
  1716. type != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1717. goto delegate;
  1718. if (le16_to_cpu(req.wLength) != 2 ||
  1719. le16_to_cpu(req.wValue) != 0)
  1720. break;
  1721. err = isr_get_status_response(udc, &req);
  1722. break;
  1723. case USB_REQ_SET_ADDRESS:
  1724. if (type != (USB_DIR_OUT|USB_RECIP_DEVICE))
  1725. goto delegate;
  1726. if (le16_to_cpu(req.wLength) != 0 ||
  1727. le16_to_cpu(req.wIndex) != 0)
  1728. break;
  1729. err = hw_usb_set_address((u8)le16_to_cpu(req.wValue));
  1730. if (err)
  1731. break;
  1732. err = isr_setup_status_phase(udc);
  1733. break;
  1734. case USB_REQ_SET_FEATURE:
  1735. if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) &&
  1736. le16_to_cpu(req.wValue) ==
  1737. USB_ENDPOINT_HALT) {
  1738. if (req.wLength != 0)
  1739. break;
  1740. num = le16_to_cpu(req.wIndex);
  1741. dir = num & USB_ENDPOINT_DIR_MASK;
  1742. num &= USB_ENDPOINT_NUMBER_MASK;
  1743. if (dir) /* TX */
  1744. num += hw_ep_max/2;
  1745. spin_unlock(udc->lock);
  1746. err = usb_ep_set_halt(&udc->ci13xxx_ep[num].ep);
  1747. spin_lock(udc->lock);
  1748. if (!err)
  1749. isr_setup_status_phase(udc);
  1750. } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) {
  1751. if (req.wLength != 0)
  1752. break;
  1753. switch (le16_to_cpu(req.wValue)) {
  1754. case USB_DEVICE_REMOTE_WAKEUP:
  1755. udc->remote_wakeup = 1;
  1756. err = isr_setup_status_phase(udc);
  1757. break;
  1758. case USB_DEVICE_TEST_MODE:
  1759. tmode = le16_to_cpu(req.wIndex) >> 8;
  1760. switch (tmode) {
  1761. case TEST_J:
  1762. case TEST_K:
  1763. case TEST_SE0_NAK:
  1764. case TEST_PACKET:
  1765. case TEST_FORCE_EN:
  1766. udc->test_mode = tmode;
  1767. err = isr_setup_status_phase(
  1768. udc);
  1769. break;
  1770. default:
  1771. break;
  1772. }
  1773. default:
  1774. goto delegate;
  1775. }
  1776. } else {
  1777. goto delegate;
  1778. }
  1779. break;
  1780. default:
  1781. delegate:
  1782. if (req.wLength == 0) /* no data phase */
  1783. udc->ep0_dir = TX;
  1784. spin_unlock(udc->lock);
  1785. err = udc->driver->setup(&udc->gadget, &req);
  1786. spin_lock(udc->lock);
  1787. break;
  1788. }
  1789. if (err < 0) {
  1790. dbg_event(_usb_addr(mEp), "ERROR", err);
  1791. spin_unlock(udc->lock);
  1792. if (usb_ep_set_halt(&mEp->ep))
  1793. err("error: ep_set_halt");
  1794. spin_lock(udc->lock);
  1795. }
  1796. }
  1797. }
  1798. /******************************************************************************
  1799. * ENDPT block
  1800. *****************************************************************************/
  1801. /**
  1802. * ep_enable: configure endpoint, making it usable
  1803. *
  1804. * Check usb_ep_enable() at "usb_gadget.h" for details
  1805. */
  1806. static int ep_enable(struct usb_ep *ep,
  1807. const struct usb_endpoint_descriptor *desc)
  1808. {
  1809. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1810. int retval = 0;
  1811. unsigned long flags;
  1812. trace("%p, %p", ep, desc);
  1813. if (ep == NULL || desc == NULL)
  1814. return -EINVAL;
  1815. spin_lock_irqsave(mEp->lock, flags);
  1816. /* only internal SW should enable ctrl endpts */
  1817. mEp->desc = desc;
  1818. if (!list_empty(&mEp->qh.queue))
  1819. warn("enabling a non-empty endpoint!");
  1820. mEp->dir = usb_endpoint_dir_in(desc) ? TX : RX;
  1821. mEp->num = usb_endpoint_num(desc);
  1822. mEp->type = usb_endpoint_type(desc);
  1823. mEp->ep.maxpacket = __constant_le16_to_cpu(desc->wMaxPacketSize);
  1824. dbg_event(_usb_addr(mEp), "ENABLE", 0);
  1825. mEp->qh.ptr->cap = 0;
  1826. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1827. mEp->qh.ptr->cap |= QH_IOS;
  1828. else if (mEp->type == USB_ENDPOINT_XFER_ISOC)
  1829. mEp->qh.ptr->cap &= ~QH_MULT;
  1830. else
  1831. mEp->qh.ptr->cap &= ~QH_ZLT;
  1832. mEp->qh.ptr->cap |=
  1833. (mEp->ep.maxpacket << ffs_nr(QH_MAX_PKT)) & QH_MAX_PKT;
  1834. mEp->qh.ptr->td.next |= TD_TERMINATE; /* needed? */
  1835. /*
  1836. * Enable endpoints in the HW other than ep0 as ep0
  1837. * is always enabled
  1838. */
  1839. if (mEp->num)
  1840. retval |= hw_ep_enable(mEp->num, mEp->dir, mEp->type);
  1841. spin_unlock_irqrestore(mEp->lock, flags);
  1842. return retval;
  1843. }
  1844. /**
  1845. * ep_disable: endpoint is no longer usable
  1846. *
  1847. * Check usb_ep_disable() at "usb_gadget.h" for details
  1848. */
  1849. static int ep_disable(struct usb_ep *ep)
  1850. {
  1851. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1852. int direction, retval = 0;
  1853. unsigned long flags;
  1854. trace("%p", ep);
  1855. if (ep == NULL)
  1856. return -EINVAL;
  1857. else if (mEp->desc == NULL)
  1858. return -EBUSY;
  1859. spin_lock_irqsave(mEp->lock, flags);
  1860. /* only internal SW should disable ctrl endpts */
  1861. direction = mEp->dir;
  1862. do {
  1863. dbg_event(_usb_addr(mEp), "DISABLE", 0);
  1864. retval |= _ep_nuke(mEp);
  1865. retval |= hw_ep_disable(mEp->num, mEp->dir);
  1866. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  1867. mEp->dir = (mEp->dir == TX) ? RX : TX;
  1868. } while (mEp->dir != direction);
  1869. mEp->desc = NULL;
  1870. spin_unlock_irqrestore(mEp->lock, flags);
  1871. return retval;
  1872. }
  1873. /**
  1874. * ep_alloc_request: allocate a request object to use with this endpoint
  1875. *
  1876. * Check usb_ep_alloc_request() at "usb_gadget.h" for details
  1877. */
  1878. static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
  1879. {
  1880. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1881. struct ci13xxx_req *mReq = NULL;
  1882. trace("%p, %i", ep, gfp_flags);
  1883. if (ep == NULL) {
  1884. err("EINVAL");
  1885. return NULL;
  1886. }
  1887. mReq = kzalloc(sizeof(struct ci13xxx_req), gfp_flags);
  1888. if (mReq != NULL) {
  1889. INIT_LIST_HEAD(&mReq->queue);
  1890. mReq->ptr = dma_pool_alloc(mEp->td_pool, gfp_flags,
  1891. &mReq->dma);
  1892. if (mReq->ptr == NULL) {
  1893. kfree(mReq);
  1894. mReq = NULL;
  1895. }
  1896. }
  1897. dbg_event(_usb_addr(mEp), "ALLOC", mReq == NULL);
  1898. return (mReq == NULL) ? NULL : &mReq->req;
  1899. }
  1900. /**
  1901. * ep_free_request: frees a request object
  1902. *
  1903. * Check usb_ep_free_request() at "usb_gadget.h" for details
  1904. */
  1905. static void ep_free_request(struct usb_ep *ep, struct usb_request *req)
  1906. {
  1907. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1908. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1909. unsigned long flags;
  1910. trace("%p, %p", ep, req);
  1911. if (ep == NULL || req == NULL) {
  1912. err("EINVAL");
  1913. return;
  1914. } else if (!list_empty(&mReq->queue)) {
  1915. err("EBUSY");
  1916. return;
  1917. }
  1918. spin_lock_irqsave(mEp->lock, flags);
  1919. if (mReq->ptr)
  1920. dma_pool_free(mEp->td_pool, mReq->ptr, mReq->dma);
  1921. kfree(mReq);
  1922. dbg_event(_usb_addr(mEp), "FREE", 0);
  1923. spin_unlock_irqrestore(mEp->lock, flags);
  1924. }
  1925. /**
  1926. * ep_queue: queues (submits) an I/O request to an endpoint
  1927. *
  1928. * Check usb_ep_queue()* at usb_gadget.h" for details
  1929. */
  1930. static int ep_queue(struct usb_ep *ep, struct usb_request *req,
  1931. gfp_t __maybe_unused gfp_flags)
  1932. {
  1933. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1934. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1935. int retval = 0;
  1936. unsigned long flags;
  1937. trace("%p, %p, %X", ep, req, gfp_flags);
  1938. if (ep == NULL || req == NULL || mEp->desc == NULL)
  1939. return -EINVAL;
  1940. spin_lock_irqsave(mEp->lock, flags);
  1941. if (mEp->type == USB_ENDPOINT_XFER_CONTROL) {
  1942. if (req->length)
  1943. mEp = (_udc->ep0_dir == RX) ?
  1944. &_udc->ep0out : &_udc->ep0in;
  1945. if (!list_empty(&mEp->qh.queue)) {
  1946. _ep_nuke(mEp);
  1947. retval = -EOVERFLOW;
  1948. warn("endpoint ctrl %X nuked", _usb_addr(mEp));
  1949. }
  1950. }
  1951. /* first nuke then test link, e.g. previous status has not sent */
  1952. if (!list_empty(&mReq->queue)) {
  1953. retval = -EBUSY;
  1954. err("request already in queue");
  1955. goto done;
  1956. }
  1957. if (req->length > (4 * CI13XXX_PAGE_SIZE)) {
  1958. req->length = (4 * CI13XXX_PAGE_SIZE);
  1959. retval = -EMSGSIZE;
  1960. warn("request length truncated");
  1961. }
  1962. dbg_queue(_usb_addr(mEp), req, retval);
  1963. /* push request */
  1964. mReq->req.status = -EINPROGRESS;
  1965. mReq->req.actual = 0;
  1966. retval = _hardware_enqueue(mEp, mReq);
  1967. if (retval == -EALREADY) {
  1968. dbg_event(_usb_addr(mEp), "QUEUE", retval);
  1969. retval = 0;
  1970. }
  1971. if (!retval)
  1972. list_add_tail(&mReq->queue, &mEp->qh.queue);
  1973. done:
  1974. spin_unlock_irqrestore(mEp->lock, flags);
  1975. return retval;
  1976. }
  1977. /**
  1978. * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint
  1979. *
  1980. * Check usb_ep_dequeue() at "usb_gadget.h" for details
  1981. */
  1982. static int ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  1983. {
  1984. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  1985. struct ci13xxx_req *mReq = container_of(req, struct ci13xxx_req, req);
  1986. unsigned long flags;
  1987. trace("%p, %p", ep, req);
  1988. if (ep == NULL || req == NULL || mReq->req.status != -EALREADY ||
  1989. mEp->desc == NULL || list_empty(&mReq->queue) ||
  1990. list_empty(&mEp->qh.queue))
  1991. return -EINVAL;
  1992. spin_lock_irqsave(mEp->lock, flags);
  1993. dbg_event(_usb_addr(mEp), "DEQUEUE", 0);
  1994. hw_ep_flush(mEp->num, mEp->dir);
  1995. /* pop request */
  1996. list_del_init(&mReq->queue);
  1997. if (mReq->map) {
  1998. dma_unmap_single(mEp->device, mReq->req.dma, mReq->req.length,
  1999. mEp->dir ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  2000. mReq->req.dma = 0;
  2001. mReq->map = 0;
  2002. }
  2003. req->status = -ECONNRESET;
  2004. if (mReq->req.complete != NULL) {
  2005. spin_unlock(mEp->lock);
  2006. mReq->req.complete(&mEp->ep, &mReq->req);
  2007. spin_lock(mEp->lock);
  2008. }
  2009. spin_unlock_irqrestore(mEp->lock, flags);
  2010. return 0;
  2011. }
  2012. /**
  2013. * ep_set_halt: sets the endpoint halt feature
  2014. *
  2015. * Check usb_ep_set_halt() at "usb_gadget.h" for details
  2016. */
  2017. static int ep_set_halt(struct usb_ep *ep, int value)
  2018. {
  2019. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2020. int direction, retval = 0;
  2021. unsigned long flags;
  2022. trace("%p, %i", ep, value);
  2023. if (ep == NULL || mEp->desc == NULL)
  2024. return -EINVAL;
  2025. spin_lock_irqsave(mEp->lock, flags);
  2026. #ifndef STALL_IN
  2027. /* g_file_storage MS compliant but g_zero fails chapter 9 compliance */
  2028. if (value && mEp->type == USB_ENDPOINT_XFER_BULK && mEp->dir == TX &&
  2029. !list_empty(&mEp->qh.queue)) {
  2030. spin_unlock_irqrestore(mEp->lock, flags);
  2031. return -EAGAIN;
  2032. }
  2033. #endif
  2034. direction = mEp->dir;
  2035. do {
  2036. dbg_event(_usb_addr(mEp), "HALT", value);
  2037. retval |= hw_ep_set_halt(mEp->num, mEp->dir, value);
  2038. if (!value)
  2039. mEp->wedge = 0;
  2040. if (mEp->type == USB_ENDPOINT_XFER_CONTROL)
  2041. mEp->dir = (mEp->dir == TX) ? RX : TX;
  2042. } while (mEp->dir != direction);
  2043. spin_unlock_irqrestore(mEp->lock, flags);
  2044. return retval;
  2045. }
  2046. /**
  2047. * ep_set_wedge: sets the halt feature and ignores clear requests
  2048. *
  2049. * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  2050. */
  2051. static int ep_set_wedge(struct usb_ep *ep)
  2052. {
  2053. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2054. unsigned long flags;
  2055. trace("%p", ep);
  2056. if (ep == NULL || mEp->desc == NULL)
  2057. return -EINVAL;
  2058. spin_lock_irqsave(mEp->lock, flags);
  2059. dbg_event(_usb_addr(mEp), "WEDGE", 0);
  2060. mEp->wedge = 1;
  2061. spin_unlock_irqrestore(mEp->lock, flags);
  2062. return usb_ep_set_halt(ep);
  2063. }
  2064. /**
  2065. * ep_fifo_flush: flushes contents of a fifo
  2066. *
  2067. * Check usb_ep_fifo_flush() at "usb_gadget.h" for details
  2068. */
  2069. static void ep_fifo_flush(struct usb_ep *ep)
  2070. {
  2071. struct ci13xxx_ep *mEp = container_of(ep, struct ci13xxx_ep, ep);
  2072. unsigned long flags;
  2073. trace("%p", ep);
  2074. if (ep == NULL) {
  2075. err("%02X: -EINVAL", _usb_addr(mEp));
  2076. return;
  2077. }
  2078. spin_lock_irqsave(mEp->lock, flags);
  2079. dbg_event(_usb_addr(mEp), "FFLUSH", 0);
  2080. hw_ep_flush(mEp->num, mEp->dir);
  2081. spin_unlock_irqrestore(mEp->lock, flags);
  2082. }
  2083. /**
  2084. * Endpoint-specific part of the API to the USB controller hardware
  2085. * Check "usb_gadget.h" for details
  2086. */
  2087. static const struct usb_ep_ops usb_ep_ops = {
  2088. .enable = ep_enable,
  2089. .disable = ep_disable,
  2090. .alloc_request = ep_alloc_request,
  2091. .free_request = ep_free_request,
  2092. .queue = ep_queue,
  2093. .dequeue = ep_dequeue,
  2094. .set_halt = ep_set_halt,
  2095. .set_wedge = ep_set_wedge,
  2096. .fifo_flush = ep_fifo_flush,
  2097. };
  2098. /******************************************************************************
  2099. * GADGET block
  2100. *****************************************************************************/
  2101. static int ci13xxx_vbus_session(struct usb_gadget *_gadget, int is_active)
  2102. {
  2103. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2104. unsigned long flags;
  2105. int gadget_ready = 0;
  2106. if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS))
  2107. return -EOPNOTSUPP;
  2108. spin_lock_irqsave(udc->lock, flags);
  2109. udc->vbus_active = is_active;
  2110. if (udc->driver)
  2111. gadget_ready = 1;
  2112. spin_unlock_irqrestore(udc->lock, flags);
  2113. if (gadget_ready) {
  2114. if (is_active) {
  2115. pm_runtime_get_sync(&_gadget->dev);
  2116. hw_device_reset(udc);
  2117. hw_device_state(udc->ep0out.qh.dma);
  2118. } else {
  2119. hw_device_state(0);
  2120. if (udc->udc_driver->notify_event)
  2121. udc->udc_driver->notify_event(udc,
  2122. CI13XXX_CONTROLLER_STOPPED_EVENT);
  2123. _gadget_stop_activity(&udc->gadget);
  2124. pm_runtime_put_sync(&_gadget->dev);
  2125. }
  2126. }
  2127. return 0;
  2128. }
  2129. static int ci13xxx_wakeup(struct usb_gadget *_gadget)
  2130. {
  2131. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2132. unsigned long flags;
  2133. int ret = 0;
  2134. trace();
  2135. spin_lock_irqsave(udc->lock, flags);
  2136. if (!udc->remote_wakeup) {
  2137. ret = -EOPNOTSUPP;
  2138. dbg_trace("remote wakeup feature is not enabled\n");
  2139. goto out;
  2140. }
  2141. if (!hw_cread(CAP_PORTSC, PORTSC_SUSP)) {
  2142. ret = -EINVAL;
  2143. dbg_trace("port is not suspended\n");
  2144. goto out;
  2145. }
  2146. hw_cwrite(CAP_PORTSC, PORTSC_FPR, PORTSC_FPR);
  2147. out:
  2148. spin_unlock_irqrestore(udc->lock, flags);
  2149. return ret;
  2150. }
  2151. static int ci13xxx_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  2152. {
  2153. struct ci13xxx *udc = container_of(_gadget, struct ci13xxx, gadget);
  2154. if (udc->transceiver)
  2155. return otg_set_power(udc->transceiver, mA);
  2156. return -ENOTSUPP;
  2157. }
  2158. static int ci13xxx_start(struct usb_gadget_driver *driver,
  2159. int (*bind)(struct usb_gadget *));
  2160. static int ci13xxx_stop(struct usb_gadget_driver *driver);
  2161. /**
  2162. * Device operations part of the API to the USB controller hardware,
  2163. * which don't involve endpoints (or i/o)
  2164. * Check "usb_gadget.h" for details
  2165. */
  2166. static const struct usb_gadget_ops usb_gadget_ops = {
  2167. .vbus_session = ci13xxx_vbus_session,
  2168. .wakeup = ci13xxx_wakeup,
  2169. .vbus_draw = ci13xxx_vbus_draw,
  2170. .start = ci13xxx_start,
  2171. .stop = ci13xxx_stop,
  2172. };
  2173. /**
  2174. * ci13xxx_start: register a gadget driver
  2175. * @driver: the driver being registered
  2176. * @bind: the driver's bind callback
  2177. *
  2178. * Check ci13xxx_start() at <linux/usb/gadget.h> for details.
  2179. * Interrupts are enabled here.
  2180. */
  2181. static int ci13xxx_start(struct usb_gadget_driver *driver,
  2182. int (*bind)(struct usb_gadget *))
  2183. {
  2184. struct ci13xxx *udc = _udc;
  2185. unsigned long flags;
  2186. int i, j;
  2187. int retval = -ENOMEM;
  2188. trace("%p", driver);
  2189. if (driver == NULL ||
  2190. bind == NULL ||
  2191. driver->setup == NULL ||
  2192. driver->disconnect == NULL ||
  2193. driver->suspend == NULL ||
  2194. driver->resume == NULL)
  2195. return -EINVAL;
  2196. else if (udc == NULL)
  2197. return -ENODEV;
  2198. else if (udc->driver != NULL)
  2199. return -EBUSY;
  2200. /* alloc resources */
  2201. udc->qh_pool = dma_pool_create("ci13xxx_qh", &udc->gadget.dev,
  2202. sizeof(struct ci13xxx_qh),
  2203. 64, CI13XXX_PAGE_SIZE);
  2204. if (udc->qh_pool == NULL)
  2205. return -ENOMEM;
  2206. udc->td_pool = dma_pool_create("ci13xxx_td", &udc->gadget.dev,
  2207. sizeof(struct ci13xxx_td),
  2208. 64, CI13XXX_PAGE_SIZE);
  2209. if (udc->td_pool == NULL) {
  2210. dma_pool_destroy(udc->qh_pool);
  2211. udc->qh_pool = NULL;
  2212. return -ENOMEM;
  2213. }
  2214. spin_lock_irqsave(udc->lock, flags);
  2215. info("hw_ep_max = %d", hw_ep_max);
  2216. udc->gadget.dev.driver = NULL;
  2217. retval = 0;
  2218. for (i = 0; i < hw_ep_max/2; i++) {
  2219. for (j = RX; j <= TX; j++) {
  2220. int k = i + j * hw_ep_max/2;
  2221. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[k];
  2222. scnprintf(mEp->name, sizeof(mEp->name), "ep%i%s", i,
  2223. (j == TX) ? "in" : "out");
  2224. mEp->lock = udc->lock;
  2225. mEp->device = &udc->gadget.dev;
  2226. mEp->td_pool = udc->td_pool;
  2227. mEp->ep.name = mEp->name;
  2228. mEp->ep.ops = &usb_ep_ops;
  2229. mEp->ep.maxpacket = CTRL_PAYLOAD_MAX;
  2230. INIT_LIST_HEAD(&mEp->qh.queue);
  2231. spin_unlock_irqrestore(udc->lock, flags);
  2232. mEp->qh.ptr = dma_pool_alloc(udc->qh_pool, GFP_KERNEL,
  2233. &mEp->qh.dma);
  2234. spin_lock_irqsave(udc->lock, flags);
  2235. if (mEp->qh.ptr == NULL)
  2236. retval = -ENOMEM;
  2237. else
  2238. memset(mEp->qh.ptr, 0, sizeof(*mEp->qh.ptr));
  2239. /* skip ep0 out and in endpoints */
  2240. if (i == 0)
  2241. continue;
  2242. list_add_tail(&mEp->ep.ep_list, &udc->gadget.ep_list);
  2243. }
  2244. }
  2245. if (retval)
  2246. goto done;
  2247. spin_unlock_irqrestore(udc->lock, flags);
  2248. udc->ep0out.ep.desc = &ctrl_endpt_out_desc;
  2249. retval = usb_ep_enable(&udc->ep0out.ep);
  2250. if (retval)
  2251. return retval;
  2252. udc->ep0in.ep.desc = &ctrl_endpt_in_desc;
  2253. retval = usb_ep_enable(&udc->ep0in.ep);
  2254. if (retval)
  2255. return retval;
  2256. spin_lock_irqsave(udc->lock, flags);
  2257. udc->gadget.ep0 = &udc->ep0in.ep;
  2258. /* bind gadget */
  2259. driver->driver.bus = NULL;
  2260. udc->gadget.dev.driver = &driver->driver;
  2261. spin_unlock_irqrestore(udc->lock, flags);
  2262. retval = bind(&udc->gadget); /* MAY SLEEP */
  2263. spin_lock_irqsave(udc->lock, flags);
  2264. if (retval) {
  2265. udc->gadget.dev.driver = NULL;
  2266. goto done;
  2267. }
  2268. udc->driver = driver;
  2269. pm_runtime_get_sync(&udc->gadget.dev);
  2270. if (udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) {
  2271. if (udc->vbus_active) {
  2272. if (udc->udc_driver->flags & CI13XXX_REGS_SHARED)
  2273. hw_device_reset(udc);
  2274. } else {
  2275. pm_runtime_put_sync(&udc->gadget.dev);
  2276. goto done;
  2277. }
  2278. }
  2279. retval = hw_device_state(udc->ep0out.qh.dma);
  2280. if (retval)
  2281. pm_runtime_put_sync(&udc->gadget.dev);
  2282. done:
  2283. spin_unlock_irqrestore(udc->lock, flags);
  2284. return retval;
  2285. }
  2286. /**
  2287. * ci13xxx_stop: unregister a gadget driver
  2288. *
  2289. * Check usb_gadget_unregister_driver() at "usb_gadget.h" for details
  2290. */
  2291. static int ci13xxx_stop(struct usb_gadget_driver *driver)
  2292. {
  2293. struct ci13xxx *udc = _udc;
  2294. unsigned long i, flags;
  2295. trace("%p", driver);
  2296. if (driver == NULL ||
  2297. driver->unbind == NULL ||
  2298. driver->setup == NULL ||
  2299. driver->disconnect == NULL ||
  2300. driver->suspend == NULL ||
  2301. driver->resume == NULL ||
  2302. driver != udc->driver)
  2303. return -EINVAL;
  2304. spin_lock_irqsave(udc->lock, flags);
  2305. if (!(udc->udc_driver->flags & CI13XXX_PULLUP_ON_VBUS) ||
  2306. udc->vbus_active) {
  2307. hw_device_state(0);
  2308. if (udc->udc_driver->notify_event)
  2309. udc->udc_driver->notify_event(udc,
  2310. CI13XXX_CONTROLLER_STOPPED_EVENT);
  2311. _gadget_stop_activity(&udc->gadget);
  2312. pm_runtime_put(&udc->gadget.dev);
  2313. }
  2314. /* unbind gadget */
  2315. spin_unlock_irqrestore(udc->lock, flags);
  2316. driver->unbind(&udc->gadget); /* MAY SLEEP */
  2317. spin_lock_irqsave(udc->lock, flags);
  2318. udc->gadget.dev.driver = NULL;
  2319. /* free resources */
  2320. for (i = 0; i < hw_ep_max; i++) {
  2321. struct ci13xxx_ep *mEp = &udc->ci13xxx_ep[i];
  2322. if (!list_empty(&mEp->ep.ep_list))
  2323. list_del_init(&mEp->ep.ep_list);
  2324. if (mEp->qh.ptr != NULL)
  2325. dma_pool_free(udc->qh_pool, mEp->qh.ptr, mEp->qh.dma);
  2326. }
  2327. udc->gadget.ep0 = NULL;
  2328. udc->driver = NULL;
  2329. spin_unlock_irqrestore(udc->lock, flags);
  2330. if (udc->td_pool != NULL) {
  2331. dma_pool_destroy(udc->td_pool);
  2332. udc->td_pool = NULL;
  2333. }
  2334. if (udc->qh_pool != NULL) {
  2335. dma_pool_destroy(udc->qh_pool);
  2336. udc->qh_pool = NULL;
  2337. }
  2338. return 0;
  2339. }
  2340. /******************************************************************************
  2341. * BUS block
  2342. *****************************************************************************/
  2343. /**
  2344. * udc_irq: global interrupt handler
  2345. *
  2346. * This function returns IRQ_HANDLED if the IRQ has been handled
  2347. * It locks access to registers
  2348. */
  2349. static irqreturn_t udc_irq(void)
  2350. {
  2351. struct ci13xxx *udc = _udc;
  2352. irqreturn_t retval;
  2353. u32 intr;
  2354. trace();
  2355. if (udc == NULL) {
  2356. err("ENODEV");
  2357. return IRQ_HANDLED;
  2358. }
  2359. spin_lock(udc->lock);
  2360. if (udc->udc_driver->flags & CI13XXX_REGS_SHARED) {
  2361. if (hw_cread(CAP_USBMODE, USBMODE_CM) !=
  2362. USBMODE_CM_DEVICE) {
  2363. spin_unlock(udc->lock);
  2364. return IRQ_NONE;
  2365. }
  2366. }
  2367. intr = hw_test_and_clear_intr_active();
  2368. if (intr) {
  2369. isr_statistics.hndl.buf[isr_statistics.hndl.idx++] = intr;
  2370. isr_statistics.hndl.idx &= ISR_MASK;
  2371. isr_statistics.hndl.cnt++;
  2372. /* order defines priority - do NOT change it */
  2373. if (USBi_URI & intr) {
  2374. isr_statistics.uri++;
  2375. isr_reset_handler(udc);
  2376. }
  2377. if (USBi_PCI & intr) {
  2378. isr_statistics.pci++;
  2379. udc->gadget.speed = hw_port_is_high_speed() ?
  2380. USB_SPEED_HIGH : USB_SPEED_FULL;
  2381. if (udc->suspended) {
  2382. spin_unlock(udc->lock);
  2383. udc->driver->resume(&udc->gadget);
  2384. spin_lock(udc->lock);
  2385. udc->suspended = 0;
  2386. }
  2387. }
  2388. if (USBi_UEI & intr)
  2389. isr_statistics.uei++;
  2390. if (USBi_UI & intr) {
  2391. isr_statistics.ui++;
  2392. isr_tr_complete_handler(udc);
  2393. }
  2394. if (USBi_SLI & intr) {
  2395. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  2396. udc->suspended = 1;
  2397. spin_unlock(udc->lock);
  2398. udc->driver->suspend(&udc->gadget);
  2399. spin_lock(udc->lock);
  2400. }
  2401. isr_statistics.sli++;
  2402. }
  2403. retval = IRQ_HANDLED;
  2404. } else {
  2405. isr_statistics.none++;
  2406. retval = IRQ_NONE;
  2407. }
  2408. spin_unlock(udc->lock);
  2409. return retval;
  2410. }
  2411. /**
  2412. * udc_release: driver release function
  2413. * @dev: device
  2414. *
  2415. * Currently does nothing
  2416. */
  2417. static void udc_release(struct device *dev)
  2418. {
  2419. trace("%p", dev);
  2420. if (dev == NULL)
  2421. err("EINVAL");
  2422. }
  2423. /**
  2424. * udc_probe: parent probe must call this to initialize UDC
  2425. * @dev: parent device
  2426. * @regs: registers base address
  2427. * @name: driver name
  2428. *
  2429. * This function returns an error code
  2430. * No interrupts active, the IRQ has not been requested yet
  2431. * Kernel assumes 32-bit DMA operations by default, no need to dma_set_mask
  2432. */
  2433. static int udc_probe(struct ci13xxx_udc_driver *driver, struct device *dev,
  2434. void __iomem *regs)
  2435. {
  2436. struct ci13xxx *udc;
  2437. int retval = 0;
  2438. trace("%p, %p, %p", dev, regs, name);
  2439. if (dev == NULL || regs == NULL || driver == NULL ||
  2440. driver->name == NULL)
  2441. return -EINVAL;
  2442. udc = kzalloc(sizeof(struct ci13xxx), GFP_KERNEL);
  2443. if (udc == NULL)
  2444. return -ENOMEM;
  2445. udc->lock = &udc_lock;
  2446. udc->regs = regs;
  2447. udc->udc_driver = driver;
  2448. udc->gadget.ops = &usb_gadget_ops;
  2449. udc->gadget.speed = USB_SPEED_UNKNOWN;
  2450. udc->gadget.is_dualspeed = 1;
  2451. udc->gadget.is_otg = 0;
  2452. udc->gadget.name = driver->name;
  2453. INIT_LIST_HEAD(&udc->gadget.ep_list);
  2454. udc->gadget.ep0 = NULL;
  2455. dev_set_name(&udc->gadget.dev, "gadget");
  2456. udc->gadget.dev.dma_mask = dev->dma_mask;
  2457. udc->gadget.dev.coherent_dma_mask = dev->coherent_dma_mask;
  2458. udc->gadget.dev.parent = dev;
  2459. udc->gadget.dev.release = udc_release;
  2460. retval = hw_device_init(regs);
  2461. if (retval < 0)
  2462. goto free_udc;
  2463. udc->transceiver = otg_get_transceiver();
  2464. if (udc->udc_driver->flags & CI13XXX_REQUIRE_TRANSCEIVER) {
  2465. if (udc->transceiver == NULL) {
  2466. retval = -ENODEV;
  2467. goto free_udc;
  2468. }
  2469. }
  2470. if (!(udc->udc_driver->flags & CI13XXX_REGS_SHARED)) {
  2471. retval = hw_device_reset(udc);
  2472. if (retval)
  2473. goto put_transceiver;
  2474. }
  2475. retval = device_register(&udc->gadget.dev);
  2476. if (retval) {
  2477. put_device(&udc->gadget.dev);
  2478. goto put_transceiver;
  2479. }
  2480. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2481. retval = dbg_create_files(&udc->gadget.dev);
  2482. #endif
  2483. if (retval)
  2484. goto unreg_device;
  2485. if (udc->transceiver) {
  2486. retval = otg_set_peripheral(udc->transceiver, &udc->gadget);
  2487. if (retval)
  2488. goto remove_dbg;
  2489. }
  2490. retval = usb_add_gadget_udc(dev, &udc->gadget);
  2491. if (retval)
  2492. goto remove_trans;
  2493. pm_runtime_no_callbacks(&udc->gadget.dev);
  2494. pm_runtime_enable(&udc->gadget.dev);
  2495. _udc = udc;
  2496. return retval;
  2497. remove_trans:
  2498. if (udc->transceiver) {
  2499. otg_set_peripheral(udc->transceiver, &udc->gadget);
  2500. otg_put_transceiver(udc->transceiver);
  2501. }
  2502. err("error = %i", retval);
  2503. remove_dbg:
  2504. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2505. dbg_remove_files(&udc->gadget.dev);
  2506. #endif
  2507. unreg_device:
  2508. device_unregister(&udc->gadget.dev);
  2509. put_transceiver:
  2510. if (udc->transceiver)
  2511. otg_put_transceiver(udc->transceiver);
  2512. free_udc:
  2513. kfree(udc);
  2514. _udc = NULL;
  2515. return retval;
  2516. }
  2517. /**
  2518. * udc_remove: parent remove must call this to remove UDC
  2519. *
  2520. * No interrupts active, the IRQ has been released
  2521. */
  2522. static void udc_remove(void)
  2523. {
  2524. struct ci13xxx *udc = _udc;
  2525. if (udc == NULL) {
  2526. err("EINVAL");
  2527. return;
  2528. }
  2529. usb_del_gadget_udc(&udc->gadget);
  2530. if (udc->transceiver) {
  2531. otg_set_peripheral(udc->transceiver, &udc->gadget);
  2532. otg_put_transceiver(udc->transceiver);
  2533. }
  2534. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2535. dbg_remove_files(&udc->gadget.dev);
  2536. #endif
  2537. device_unregister(&udc->gadget.dev);
  2538. kfree(udc);
  2539. _udc = NULL;
  2540. }