ci13xxx_udc.c 68 KB

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