ci13xxx_udc.c 68 KB

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