ci13xxx_udc.c 68 KB

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