ci13xxx_udc.c 64 KB

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