ci13xxx_udc.c 68 KB

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