2
0

usb-ehci.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  1. /*
  2. * QEMU USB EHCI Emulation
  3. *
  4. * Copyright(c) 2008 Emutex Ltd. (address@hidden)
  5. *
  6. * EHCI project was started by Mark Burkley, with contributions by
  7. * Niels de Vos. David S. Ahern continued working on it. Kevin Wolf,
  8. * Jan Kiszka and Vincent Palatin contributed bugfixes.
  9. *
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or(at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "hw.h"
  25. #include "qemu-timer.h"
  26. #include "usb.h"
  27. #include "pci.h"
  28. #include "monitor.h"
  29. #include "trace.h"
  30. #include "dma.h"
  31. #define EHCI_DEBUG 0
  32. #if EHCI_DEBUG
  33. #define DPRINTF printf
  34. #else
  35. #define DPRINTF(...)
  36. #endif
  37. /* internal processing - reset HC to try and recover */
  38. #define USB_RET_PROCERR (-99)
  39. #define MMIO_SIZE 0x1000
  40. /* Capability Registers Base Address - section 2.2 */
  41. #define CAPREGBASE 0x0000
  42. #define CAPLENGTH CAPREGBASE + 0x0000 // 1-byte, 0x0001 reserved
  43. #define HCIVERSION CAPREGBASE + 0x0002 // 2-bytes, i/f version #
  44. #define HCSPARAMS CAPREGBASE + 0x0004 // 4-bytes, structural params
  45. #define HCCPARAMS CAPREGBASE + 0x0008 // 4-bytes, capability params
  46. #define EECP HCCPARAMS + 1
  47. #define HCSPPORTROUTE1 CAPREGBASE + 0x000c
  48. #define HCSPPORTROUTE2 CAPREGBASE + 0x0010
  49. #define OPREGBASE 0x0020 // Operational Registers Base Address
  50. #define USBCMD OPREGBASE + 0x0000
  51. #define USBCMD_RUNSTOP (1 << 0) // run / Stop
  52. #define USBCMD_HCRESET (1 << 1) // HC Reset
  53. #define USBCMD_FLS (3 << 2) // Frame List Size
  54. #define USBCMD_FLS_SH 2 // Frame List Size Shift
  55. #define USBCMD_PSE (1 << 4) // Periodic Schedule Enable
  56. #define USBCMD_ASE (1 << 5) // Asynch Schedule Enable
  57. #define USBCMD_IAAD (1 << 6) // Int Asynch Advance Doorbell
  58. #define USBCMD_LHCR (1 << 7) // Light Host Controller Reset
  59. #define USBCMD_ASPMC (3 << 8) // Async Sched Park Mode Count
  60. #define USBCMD_ASPME (1 << 11) // Async Sched Park Mode Enable
  61. #define USBCMD_ITC (0x7f << 16) // Int Threshold Control
  62. #define USBCMD_ITC_SH 16 // Int Threshold Control Shift
  63. #define USBSTS OPREGBASE + 0x0004
  64. #define USBSTS_RO_MASK 0x0000003f
  65. #define USBSTS_INT (1 << 0) // USB Interrupt
  66. #define USBSTS_ERRINT (1 << 1) // Error Interrupt
  67. #define USBSTS_PCD (1 << 2) // Port Change Detect
  68. #define USBSTS_FLR (1 << 3) // Frame List Rollover
  69. #define USBSTS_HSE (1 << 4) // Host System Error
  70. #define USBSTS_IAA (1 << 5) // Interrupt on Async Advance
  71. #define USBSTS_HALT (1 << 12) // HC Halted
  72. #define USBSTS_REC (1 << 13) // Reclamation
  73. #define USBSTS_PSS (1 << 14) // Periodic Schedule Status
  74. #define USBSTS_ASS (1 << 15) // Asynchronous Schedule Status
  75. /*
  76. * Interrupt enable bits correspond to the interrupt active bits in USBSTS
  77. * so no need to redefine here.
  78. */
  79. #define USBINTR OPREGBASE + 0x0008
  80. #define USBINTR_MASK 0x0000003f
  81. #define FRINDEX OPREGBASE + 0x000c
  82. #define CTRLDSSEGMENT OPREGBASE + 0x0010
  83. #define PERIODICLISTBASE OPREGBASE + 0x0014
  84. #define ASYNCLISTADDR OPREGBASE + 0x0018
  85. #define ASYNCLISTADDR_MASK 0xffffffe0
  86. #define CONFIGFLAG OPREGBASE + 0x0040
  87. #define PORTSC (OPREGBASE + 0x0044)
  88. #define PORTSC_BEGIN PORTSC
  89. #define PORTSC_END (PORTSC + 4 * NB_PORTS)
  90. /*
  91. * Bits that are reserved or are read-only are masked out of values
  92. * written to us by software
  93. */
  94. #define PORTSC_RO_MASK 0x007001c0
  95. #define PORTSC_RWC_MASK 0x0000002a
  96. #define PORTSC_WKOC_E (1 << 22) // Wake on Over Current Enable
  97. #define PORTSC_WKDS_E (1 << 21) // Wake on Disconnect Enable
  98. #define PORTSC_WKCN_E (1 << 20) // Wake on Connect Enable
  99. #define PORTSC_PTC (15 << 16) // Port Test Control
  100. #define PORTSC_PTC_SH 16 // Port Test Control shift
  101. #define PORTSC_PIC (3 << 14) // Port Indicator Control
  102. #define PORTSC_PIC_SH 14 // Port Indicator Control Shift
  103. #define PORTSC_POWNER (1 << 13) // Port Owner
  104. #define PORTSC_PPOWER (1 << 12) // Port Power
  105. #define PORTSC_LINESTAT (3 << 10) // Port Line Status
  106. #define PORTSC_LINESTAT_SH 10 // Port Line Status Shift
  107. #define PORTSC_PRESET (1 << 8) // Port Reset
  108. #define PORTSC_SUSPEND (1 << 7) // Port Suspend
  109. #define PORTSC_FPRES (1 << 6) // Force Port Resume
  110. #define PORTSC_OCC (1 << 5) // Over Current Change
  111. #define PORTSC_OCA (1 << 4) // Over Current Active
  112. #define PORTSC_PEDC (1 << 3) // Port Enable/Disable Change
  113. #define PORTSC_PED (1 << 2) // Port Enable/Disable
  114. #define PORTSC_CSC (1 << 1) // Connect Status Change
  115. #define PORTSC_CONNECT (1 << 0) // Current Connect Status
  116. #define FRAME_TIMER_FREQ 1000
  117. #define FRAME_TIMER_NS (1000000000 / FRAME_TIMER_FREQ)
  118. #define NB_MAXINTRATE 8 // Max rate at which controller issues ints
  119. #define NB_PORTS 6 // Number of downstream ports
  120. #define BUFF_SIZE 5*4096 // Max bytes to transfer per transaction
  121. #define MAX_ITERATIONS 20 // Max number of QH before we break the loop
  122. #define MAX_QH 100 // Max allowable queue heads in a chain
  123. /* Internal periodic / asynchronous schedule state machine states
  124. */
  125. typedef enum {
  126. EST_INACTIVE = 1000,
  127. EST_ACTIVE,
  128. EST_EXECUTING,
  129. EST_SLEEPING,
  130. /* The following states are internal to the state machine function
  131. */
  132. EST_WAITLISTHEAD,
  133. EST_FETCHENTRY,
  134. EST_FETCHQH,
  135. EST_FETCHITD,
  136. EST_FETCHSITD,
  137. EST_ADVANCEQUEUE,
  138. EST_FETCHQTD,
  139. EST_EXECUTE,
  140. EST_WRITEBACK,
  141. EST_HORIZONTALQH
  142. } EHCI_STATES;
  143. /* macros for accessing fields within next link pointer entry */
  144. #define NLPTR_GET(x) ((x) & 0xffffffe0)
  145. #define NLPTR_TYPE_GET(x) (((x) >> 1) & 3)
  146. #define NLPTR_TBIT(x) ((x) & 1) // 1=invalid, 0=valid
  147. /* link pointer types */
  148. #define NLPTR_TYPE_ITD 0 // isoc xfer descriptor
  149. #define NLPTR_TYPE_QH 1 // queue head
  150. #define NLPTR_TYPE_STITD 2 // split xaction, isoc xfer descriptor
  151. #define NLPTR_TYPE_FSTN 3 // frame span traversal node
  152. /* EHCI spec version 1.0 Section 3.3
  153. */
  154. typedef struct EHCIitd {
  155. uint32_t next;
  156. uint32_t transact[8];
  157. #define ITD_XACT_ACTIVE (1 << 31)
  158. #define ITD_XACT_DBERROR (1 << 30)
  159. #define ITD_XACT_BABBLE (1 << 29)
  160. #define ITD_XACT_XACTERR (1 << 28)
  161. #define ITD_XACT_LENGTH_MASK 0x0fff0000
  162. #define ITD_XACT_LENGTH_SH 16
  163. #define ITD_XACT_IOC (1 << 15)
  164. #define ITD_XACT_PGSEL_MASK 0x00007000
  165. #define ITD_XACT_PGSEL_SH 12
  166. #define ITD_XACT_OFFSET_MASK 0x00000fff
  167. uint32_t bufptr[7];
  168. #define ITD_BUFPTR_MASK 0xfffff000
  169. #define ITD_BUFPTR_SH 12
  170. #define ITD_BUFPTR_EP_MASK 0x00000f00
  171. #define ITD_BUFPTR_EP_SH 8
  172. #define ITD_BUFPTR_DEVADDR_MASK 0x0000007f
  173. #define ITD_BUFPTR_DEVADDR_SH 0
  174. #define ITD_BUFPTR_DIRECTION (1 << 11)
  175. #define ITD_BUFPTR_MAXPKT_MASK 0x000007ff
  176. #define ITD_BUFPTR_MAXPKT_SH 0
  177. #define ITD_BUFPTR_MULT_MASK 0x00000003
  178. #define ITD_BUFPTR_MULT_SH 0
  179. } EHCIitd;
  180. /* EHCI spec version 1.0 Section 3.4
  181. */
  182. typedef struct EHCIsitd {
  183. uint32_t next; // Standard next link pointer
  184. uint32_t epchar;
  185. #define SITD_EPCHAR_IO (1 << 31)
  186. #define SITD_EPCHAR_PORTNUM_MASK 0x7f000000
  187. #define SITD_EPCHAR_PORTNUM_SH 24
  188. #define SITD_EPCHAR_HUBADD_MASK 0x007f0000
  189. #define SITD_EPCHAR_HUBADDR_SH 16
  190. #define SITD_EPCHAR_EPNUM_MASK 0x00000f00
  191. #define SITD_EPCHAR_EPNUM_SH 8
  192. #define SITD_EPCHAR_DEVADDR_MASK 0x0000007f
  193. uint32_t uframe;
  194. #define SITD_UFRAME_CMASK_MASK 0x0000ff00
  195. #define SITD_UFRAME_CMASK_SH 8
  196. #define SITD_UFRAME_SMASK_MASK 0x000000ff
  197. uint32_t results;
  198. #define SITD_RESULTS_IOC (1 << 31)
  199. #define SITD_RESULTS_PGSEL (1 << 30)
  200. #define SITD_RESULTS_TBYTES_MASK 0x03ff0000
  201. #define SITD_RESULTS_TYBYTES_SH 16
  202. #define SITD_RESULTS_CPROGMASK_MASK 0x0000ff00
  203. #define SITD_RESULTS_CPROGMASK_SH 8
  204. #define SITD_RESULTS_ACTIVE (1 << 7)
  205. #define SITD_RESULTS_ERR (1 << 6)
  206. #define SITD_RESULTS_DBERR (1 << 5)
  207. #define SITD_RESULTS_BABBLE (1 << 4)
  208. #define SITD_RESULTS_XACTERR (1 << 3)
  209. #define SITD_RESULTS_MISSEDUF (1 << 2)
  210. #define SITD_RESULTS_SPLITXSTATE (1 << 1)
  211. uint32_t bufptr[2];
  212. #define SITD_BUFPTR_MASK 0xfffff000
  213. #define SITD_BUFPTR_CURROFF_MASK 0x00000fff
  214. #define SITD_BUFPTR_TPOS_MASK 0x00000018
  215. #define SITD_BUFPTR_TPOS_SH 3
  216. #define SITD_BUFPTR_TCNT_MASK 0x00000007
  217. uint32_t backptr; // Standard next link pointer
  218. } EHCIsitd;
  219. /* EHCI spec version 1.0 Section 3.5
  220. */
  221. typedef struct EHCIqtd {
  222. uint32_t next; // Standard next link pointer
  223. uint32_t altnext; // Standard next link pointer
  224. uint32_t token;
  225. #define QTD_TOKEN_DTOGGLE (1 << 31)
  226. #define QTD_TOKEN_TBYTES_MASK 0x7fff0000
  227. #define QTD_TOKEN_TBYTES_SH 16
  228. #define QTD_TOKEN_IOC (1 << 15)
  229. #define QTD_TOKEN_CPAGE_MASK 0x00007000
  230. #define QTD_TOKEN_CPAGE_SH 12
  231. #define QTD_TOKEN_CERR_MASK 0x00000c00
  232. #define QTD_TOKEN_CERR_SH 10
  233. #define QTD_TOKEN_PID_MASK 0x00000300
  234. #define QTD_TOKEN_PID_SH 8
  235. #define QTD_TOKEN_ACTIVE (1 << 7)
  236. #define QTD_TOKEN_HALT (1 << 6)
  237. #define QTD_TOKEN_DBERR (1 << 5)
  238. #define QTD_TOKEN_BABBLE (1 << 4)
  239. #define QTD_TOKEN_XACTERR (1 << 3)
  240. #define QTD_TOKEN_MISSEDUF (1 << 2)
  241. #define QTD_TOKEN_SPLITXSTATE (1 << 1)
  242. #define QTD_TOKEN_PING (1 << 0)
  243. uint32_t bufptr[5]; // Standard buffer pointer
  244. #define QTD_BUFPTR_MASK 0xfffff000
  245. #define QTD_BUFPTR_SH 12
  246. } EHCIqtd;
  247. /* EHCI spec version 1.0 Section 3.6
  248. */
  249. typedef struct EHCIqh {
  250. uint32_t next; // Standard next link pointer
  251. /* endpoint characteristics */
  252. uint32_t epchar;
  253. #define QH_EPCHAR_RL_MASK 0xf0000000
  254. #define QH_EPCHAR_RL_SH 28
  255. #define QH_EPCHAR_C (1 << 27)
  256. #define QH_EPCHAR_MPLEN_MASK 0x07FF0000
  257. #define QH_EPCHAR_MPLEN_SH 16
  258. #define QH_EPCHAR_H (1 << 15)
  259. #define QH_EPCHAR_DTC (1 << 14)
  260. #define QH_EPCHAR_EPS_MASK 0x00003000
  261. #define QH_EPCHAR_EPS_SH 12
  262. #define EHCI_QH_EPS_FULL 0
  263. #define EHCI_QH_EPS_LOW 1
  264. #define EHCI_QH_EPS_HIGH 2
  265. #define EHCI_QH_EPS_RESERVED 3
  266. #define QH_EPCHAR_EP_MASK 0x00000f00
  267. #define QH_EPCHAR_EP_SH 8
  268. #define QH_EPCHAR_I (1 << 7)
  269. #define QH_EPCHAR_DEVADDR_MASK 0x0000007f
  270. #define QH_EPCHAR_DEVADDR_SH 0
  271. /* endpoint capabilities */
  272. uint32_t epcap;
  273. #define QH_EPCAP_MULT_MASK 0xc0000000
  274. #define QH_EPCAP_MULT_SH 30
  275. #define QH_EPCAP_PORTNUM_MASK 0x3f800000
  276. #define QH_EPCAP_PORTNUM_SH 23
  277. #define QH_EPCAP_HUBADDR_MASK 0x007f0000
  278. #define QH_EPCAP_HUBADDR_SH 16
  279. #define QH_EPCAP_CMASK_MASK 0x0000ff00
  280. #define QH_EPCAP_CMASK_SH 8
  281. #define QH_EPCAP_SMASK_MASK 0x000000ff
  282. #define QH_EPCAP_SMASK_SH 0
  283. uint32_t current_qtd; // Standard next link pointer
  284. uint32_t next_qtd; // Standard next link pointer
  285. uint32_t altnext_qtd;
  286. #define QH_ALTNEXT_NAKCNT_MASK 0x0000001e
  287. #define QH_ALTNEXT_NAKCNT_SH 1
  288. uint32_t token; // Same as QTD token
  289. uint32_t bufptr[5]; // Standard buffer pointer
  290. #define BUFPTR_CPROGMASK_MASK 0x000000ff
  291. #define BUFPTR_FRAMETAG_MASK 0x0000001f
  292. #define BUFPTR_SBYTES_MASK 0x00000fe0
  293. #define BUFPTR_SBYTES_SH 5
  294. } EHCIqh;
  295. /* EHCI spec version 1.0 Section 3.7
  296. */
  297. typedef struct EHCIfstn {
  298. uint32_t next; // Standard next link pointer
  299. uint32_t backptr; // Standard next link pointer
  300. } EHCIfstn;
  301. typedef struct EHCIQueue EHCIQueue;
  302. typedef struct EHCIState EHCIState;
  303. enum async_state {
  304. EHCI_ASYNC_NONE = 0,
  305. EHCI_ASYNC_INFLIGHT,
  306. EHCI_ASYNC_FINISHED,
  307. };
  308. struct EHCIQueue {
  309. EHCIState *ehci;
  310. QTAILQ_ENTRY(EHCIQueue) next;
  311. bool async_schedule;
  312. uint32_t seen;
  313. uint64_t ts;
  314. /* cached data from guest - needs to be flushed
  315. * when guest removes an entry (doorbell, handshake sequence)
  316. */
  317. EHCIqh qh; // copy of current QH (being worked on)
  318. uint32_t qhaddr; // address QH read from
  319. EHCIqtd qtd; // copy of current QTD (being worked on)
  320. uint32_t qtdaddr; // address QTD read from
  321. USBPacket packet;
  322. QEMUSGList sgl;
  323. int pid;
  324. uint32_t tbytes;
  325. enum async_state async;
  326. int usb_status;
  327. };
  328. struct EHCIState {
  329. PCIDevice dev;
  330. USBBus bus;
  331. qemu_irq irq;
  332. MemoryRegion mem;
  333. int companion_count;
  334. /* properties */
  335. uint32_t freq;
  336. uint32_t maxframes;
  337. /*
  338. * EHCI spec version 1.0 Section 2.3
  339. * Host Controller Operational Registers
  340. */
  341. union {
  342. uint8_t mmio[MMIO_SIZE];
  343. struct {
  344. uint8_t cap[OPREGBASE];
  345. uint32_t usbcmd;
  346. uint32_t usbsts;
  347. uint32_t usbintr;
  348. uint32_t frindex;
  349. uint32_t ctrldssegment;
  350. uint32_t periodiclistbase;
  351. uint32_t asynclistaddr;
  352. uint32_t notused[9];
  353. uint32_t configflag;
  354. uint32_t portsc[NB_PORTS];
  355. };
  356. };
  357. /*
  358. * Internal states, shadow registers, etc
  359. */
  360. uint32_t sofv;
  361. QEMUTimer *frame_timer;
  362. int attach_poll_counter;
  363. int astate; // Current state in asynchronous schedule
  364. int pstate; // Current state in periodic schedule
  365. USBPort ports[NB_PORTS];
  366. USBPort *companion_ports[NB_PORTS];
  367. uint32_t usbsts_pending;
  368. QTAILQ_HEAD(, EHCIQueue) queues;
  369. uint32_t a_fetch_addr; // which address to look at next
  370. uint32_t p_fetch_addr; // which address to look at next
  371. USBPacket ipacket;
  372. QEMUSGList isgl;
  373. int isoch_pause;
  374. uint64_t last_run_ns;
  375. };
  376. #define SET_LAST_RUN_CLOCK(s) \
  377. (s)->last_run_ns = qemu_get_clock_ns(vm_clock);
  378. /* nifty macros from Arnon's EHCI version */
  379. #define get_field(data, field) \
  380. (((data) & field##_MASK) >> field##_SH)
  381. #define set_field(data, newval, field) do { \
  382. uint32_t val = *data; \
  383. val &= ~ field##_MASK; \
  384. val |= ((newval) << field##_SH) & field##_MASK; \
  385. *data = val; \
  386. } while(0)
  387. static const char *ehci_state_names[] = {
  388. [EST_INACTIVE] = "INACTIVE",
  389. [EST_ACTIVE] = "ACTIVE",
  390. [EST_EXECUTING] = "EXECUTING",
  391. [EST_SLEEPING] = "SLEEPING",
  392. [EST_WAITLISTHEAD] = "WAITLISTHEAD",
  393. [EST_FETCHENTRY] = "FETCH ENTRY",
  394. [EST_FETCHQH] = "FETCH QH",
  395. [EST_FETCHITD] = "FETCH ITD",
  396. [EST_ADVANCEQUEUE] = "ADVANCEQUEUE",
  397. [EST_FETCHQTD] = "FETCH QTD",
  398. [EST_EXECUTE] = "EXECUTE",
  399. [EST_WRITEBACK] = "WRITEBACK",
  400. [EST_HORIZONTALQH] = "HORIZONTALQH",
  401. };
  402. static const char *ehci_mmio_names[] = {
  403. [CAPLENGTH] = "CAPLENGTH",
  404. [HCIVERSION] = "HCIVERSION",
  405. [HCSPARAMS] = "HCSPARAMS",
  406. [HCCPARAMS] = "HCCPARAMS",
  407. [USBCMD] = "USBCMD",
  408. [USBSTS] = "USBSTS",
  409. [USBINTR] = "USBINTR",
  410. [FRINDEX] = "FRINDEX",
  411. [PERIODICLISTBASE] = "P-LIST BASE",
  412. [ASYNCLISTADDR] = "A-LIST ADDR",
  413. [PORTSC_BEGIN] = "PORTSC #0",
  414. [PORTSC_BEGIN + 4] = "PORTSC #1",
  415. [PORTSC_BEGIN + 8] = "PORTSC #2",
  416. [PORTSC_BEGIN + 12] = "PORTSC #3",
  417. [PORTSC_BEGIN + 16] = "PORTSC #4",
  418. [PORTSC_BEGIN + 20] = "PORTSC #5",
  419. [CONFIGFLAG] = "CONFIGFLAG",
  420. };
  421. static const char *nr2str(const char **n, size_t len, uint32_t nr)
  422. {
  423. if (nr < len && n[nr] != NULL) {
  424. return n[nr];
  425. } else {
  426. return "unknown";
  427. }
  428. }
  429. static const char *state2str(uint32_t state)
  430. {
  431. return nr2str(ehci_state_names, ARRAY_SIZE(ehci_state_names), state);
  432. }
  433. static const char *addr2str(target_phys_addr_t addr)
  434. {
  435. return nr2str(ehci_mmio_names, ARRAY_SIZE(ehci_mmio_names), addr);
  436. }
  437. static void ehci_trace_usbsts(uint32_t mask, int state)
  438. {
  439. /* interrupts */
  440. if (mask & USBSTS_INT) {
  441. trace_usb_ehci_usbsts("INT", state);
  442. }
  443. if (mask & USBSTS_ERRINT) {
  444. trace_usb_ehci_usbsts("ERRINT", state);
  445. }
  446. if (mask & USBSTS_PCD) {
  447. trace_usb_ehci_usbsts("PCD", state);
  448. }
  449. if (mask & USBSTS_FLR) {
  450. trace_usb_ehci_usbsts("FLR", state);
  451. }
  452. if (mask & USBSTS_HSE) {
  453. trace_usb_ehci_usbsts("HSE", state);
  454. }
  455. if (mask & USBSTS_IAA) {
  456. trace_usb_ehci_usbsts("IAA", state);
  457. }
  458. /* status */
  459. if (mask & USBSTS_HALT) {
  460. trace_usb_ehci_usbsts("HALT", state);
  461. }
  462. if (mask & USBSTS_REC) {
  463. trace_usb_ehci_usbsts("REC", state);
  464. }
  465. if (mask & USBSTS_PSS) {
  466. trace_usb_ehci_usbsts("PSS", state);
  467. }
  468. if (mask & USBSTS_ASS) {
  469. trace_usb_ehci_usbsts("ASS", state);
  470. }
  471. }
  472. static inline void ehci_set_usbsts(EHCIState *s, int mask)
  473. {
  474. if ((s->usbsts & mask) == mask) {
  475. return;
  476. }
  477. ehci_trace_usbsts(mask, 1);
  478. s->usbsts |= mask;
  479. }
  480. static inline void ehci_clear_usbsts(EHCIState *s, int mask)
  481. {
  482. if ((s->usbsts & mask) == 0) {
  483. return;
  484. }
  485. ehci_trace_usbsts(mask, 0);
  486. s->usbsts &= ~mask;
  487. }
  488. static inline void ehci_set_interrupt(EHCIState *s, int intr)
  489. {
  490. int level = 0;
  491. // TODO honour interrupt threshold requests
  492. ehci_set_usbsts(s, intr);
  493. if ((s->usbsts & USBINTR_MASK) & s->usbintr) {
  494. level = 1;
  495. }
  496. qemu_set_irq(s->irq, level);
  497. }
  498. static inline void ehci_record_interrupt(EHCIState *s, int intr)
  499. {
  500. s->usbsts_pending |= intr;
  501. }
  502. static inline void ehci_commit_interrupt(EHCIState *s)
  503. {
  504. if (!s->usbsts_pending) {
  505. return;
  506. }
  507. ehci_set_interrupt(s, s->usbsts_pending);
  508. s->usbsts_pending = 0;
  509. }
  510. static void ehci_set_state(EHCIState *s, int async, int state)
  511. {
  512. if (async) {
  513. trace_usb_ehci_state("async", state2str(state));
  514. s->astate = state;
  515. } else {
  516. trace_usb_ehci_state("periodic", state2str(state));
  517. s->pstate = state;
  518. }
  519. }
  520. static int ehci_get_state(EHCIState *s, int async)
  521. {
  522. return async ? s->astate : s->pstate;
  523. }
  524. static void ehci_set_fetch_addr(EHCIState *s, int async, uint32_t addr)
  525. {
  526. if (async) {
  527. s->a_fetch_addr = addr;
  528. } else {
  529. s->p_fetch_addr = addr;
  530. }
  531. }
  532. static int ehci_get_fetch_addr(EHCIState *s, int async)
  533. {
  534. return async ? s->a_fetch_addr : s->p_fetch_addr;
  535. }
  536. static void ehci_trace_qh(EHCIQueue *q, target_phys_addr_t addr, EHCIqh *qh)
  537. {
  538. /* need three here due to argument count limits */
  539. trace_usb_ehci_qh_ptrs(q, addr, qh->next,
  540. qh->current_qtd, qh->next_qtd, qh->altnext_qtd);
  541. trace_usb_ehci_qh_fields(addr,
  542. get_field(qh->epchar, QH_EPCHAR_RL),
  543. get_field(qh->epchar, QH_EPCHAR_MPLEN),
  544. get_field(qh->epchar, QH_EPCHAR_EPS),
  545. get_field(qh->epchar, QH_EPCHAR_EP),
  546. get_field(qh->epchar, QH_EPCHAR_DEVADDR));
  547. trace_usb_ehci_qh_bits(addr,
  548. (bool)(qh->epchar & QH_EPCHAR_C),
  549. (bool)(qh->epchar & QH_EPCHAR_H),
  550. (bool)(qh->epchar & QH_EPCHAR_DTC),
  551. (bool)(qh->epchar & QH_EPCHAR_I));
  552. }
  553. static void ehci_trace_qtd(EHCIQueue *q, target_phys_addr_t addr, EHCIqtd *qtd)
  554. {
  555. /* need three here due to argument count limits */
  556. trace_usb_ehci_qtd_ptrs(q, addr, qtd->next, qtd->altnext);
  557. trace_usb_ehci_qtd_fields(addr,
  558. get_field(qtd->token, QTD_TOKEN_TBYTES),
  559. get_field(qtd->token, QTD_TOKEN_CPAGE),
  560. get_field(qtd->token, QTD_TOKEN_CERR),
  561. get_field(qtd->token, QTD_TOKEN_PID));
  562. trace_usb_ehci_qtd_bits(addr,
  563. (bool)(qtd->token & QTD_TOKEN_IOC),
  564. (bool)(qtd->token & QTD_TOKEN_ACTIVE),
  565. (bool)(qtd->token & QTD_TOKEN_HALT),
  566. (bool)(qtd->token & QTD_TOKEN_BABBLE),
  567. (bool)(qtd->token & QTD_TOKEN_XACTERR));
  568. }
  569. static void ehci_trace_itd(EHCIState *s, target_phys_addr_t addr, EHCIitd *itd)
  570. {
  571. trace_usb_ehci_itd(addr, itd->next,
  572. get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT),
  573. get_field(itd->bufptr[2], ITD_BUFPTR_MULT),
  574. get_field(itd->bufptr[0], ITD_BUFPTR_EP),
  575. get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR));
  576. }
  577. static void ehci_trace_sitd(EHCIState *s, target_phys_addr_t addr,
  578. EHCIsitd *sitd)
  579. {
  580. trace_usb_ehci_sitd(addr, sitd->next,
  581. (bool)(sitd->results & SITD_RESULTS_ACTIVE));
  582. }
  583. /* queue management */
  584. static EHCIQueue *ehci_alloc_queue(EHCIState *ehci, int async)
  585. {
  586. EHCIQueue *q;
  587. q = g_malloc0(sizeof(*q));
  588. q->ehci = ehci;
  589. q->async_schedule = async;
  590. QTAILQ_INSERT_HEAD(&ehci->queues, q, next);
  591. trace_usb_ehci_queue_action(q, "alloc");
  592. return q;
  593. }
  594. static void ehci_free_queue(EHCIQueue *q)
  595. {
  596. trace_usb_ehci_queue_action(q, "free");
  597. if (q->async == EHCI_ASYNC_INFLIGHT) {
  598. usb_cancel_packet(&q->packet);
  599. }
  600. QTAILQ_REMOVE(&q->ehci->queues, q, next);
  601. g_free(q);
  602. }
  603. static EHCIQueue *ehci_find_queue_by_qh(EHCIState *ehci, uint32_t addr)
  604. {
  605. EHCIQueue *q;
  606. QTAILQ_FOREACH(q, &ehci->queues, next) {
  607. if (addr == q->qhaddr) {
  608. return q;
  609. }
  610. }
  611. return NULL;
  612. }
  613. static void ehci_queues_rip_unused(EHCIState *ehci)
  614. {
  615. EHCIQueue *q, *tmp;
  616. QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) {
  617. if (q->seen) {
  618. q->seen = 0;
  619. q->ts = ehci->last_run_ns;
  620. continue;
  621. }
  622. if (ehci->last_run_ns < q->ts + 250000000) {
  623. /* allow 0.25 sec idle */
  624. continue;
  625. }
  626. ehci_free_queue(q);
  627. }
  628. }
  629. static void ehci_queues_rip_device(EHCIState *ehci, USBDevice *dev)
  630. {
  631. EHCIQueue *q, *tmp;
  632. QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) {
  633. if (q->packet.owner != dev) {
  634. continue;
  635. }
  636. ehci_free_queue(q);
  637. }
  638. }
  639. static void ehci_queues_rip_all(EHCIState *ehci)
  640. {
  641. EHCIQueue *q, *tmp;
  642. QTAILQ_FOREACH_SAFE(q, &ehci->queues, next, tmp) {
  643. ehci_free_queue(q);
  644. }
  645. }
  646. /* Attach or detach a device on root hub */
  647. static void ehci_attach(USBPort *port)
  648. {
  649. EHCIState *s = port->opaque;
  650. uint32_t *portsc = &s->portsc[port->index];
  651. trace_usb_ehci_port_attach(port->index, port->dev->product_desc);
  652. if (*portsc & PORTSC_POWNER) {
  653. USBPort *companion = s->companion_ports[port->index];
  654. companion->dev = port->dev;
  655. companion->ops->attach(companion);
  656. return;
  657. }
  658. *portsc |= PORTSC_CONNECT;
  659. *portsc |= PORTSC_CSC;
  660. ehci_set_interrupt(s, USBSTS_PCD);
  661. }
  662. static void ehci_detach(USBPort *port)
  663. {
  664. EHCIState *s = port->opaque;
  665. uint32_t *portsc = &s->portsc[port->index];
  666. trace_usb_ehci_port_detach(port->index);
  667. if (*portsc & PORTSC_POWNER) {
  668. USBPort *companion = s->companion_ports[port->index];
  669. companion->ops->detach(companion);
  670. companion->dev = NULL;
  671. return;
  672. }
  673. ehci_queues_rip_device(s, port->dev);
  674. *portsc &= ~(PORTSC_CONNECT|PORTSC_PED);
  675. *portsc |= PORTSC_CSC;
  676. ehci_set_interrupt(s, USBSTS_PCD);
  677. }
  678. static void ehci_child_detach(USBPort *port, USBDevice *child)
  679. {
  680. EHCIState *s = port->opaque;
  681. uint32_t portsc = s->portsc[port->index];
  682. if (portsc & PORTSC_POWNER) {
  683. USBPort *companion = s->companion_ports[port->index];
  684. companion->ops->child_detach(companion, child);
  685. companion->dev = NULL;
  686. return;
  687. }
  688. ehci_queues_rip_device(s, child);
  689. }
  690. static void ehci_wakeup(USBPort *port)
  691. {
  692. EHCIState *s = port->opaque;
  693. uint32_t portsc = s->portsc[port->index];
  694. if (portsc & PORTSC_POWNER) {
  695. USBPort *companion = s->companion_ports[port->index];
  696. if (companion->ops->wakeup) {
  697. companion->ops->wakeup(companion);
  698. }
  699. }
  700. }
  701. static int ehci_register_companion(USBBus *bus, USBPort *ports[],
  702. uint32_t portcount, uint32_t firstport)
  703. {
  704. EHCIState *s = container_of(bus, EHCIState, bus);
  705. uint32_t i;
  706. if (firstport + portcount > NB_PORTS) {
  707. qerror_report(QERR_INVALID_PARAMETER_VALUE, "firstport",
  708. "firstport on masterbus");
  709. error_printf_unless_qmp(
  710. "firstport value of %u makes companion take ports %u - %u, which "
  711. "is outside of the valid range of 0 - %u\n", firstport, firstport,
  712. firstport + portcount - 1, NB_PORTS - 1);
  713. return -1;
  714. }
  715. for (i = 0; i < portcount; i++) {
  716. if (s->companion_ports[firstport + i]) {
  717. qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
  718. "an USB masterbus");
  719. error_printf_unless_qmp(
  720. "port %u on masterbus %s already has a companion assigned\n",
  721. firstport + i, bus->qbus.name);
  722. return -1;
  723. }
  724. }
  725. for (i = 0; i < portcount; i++) {
  726. s->companion_ports[firstport + i] = ports[i];
  727. s->ports[firstport + i].speedmask |=
  728. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL;
  729. /* Ensure devs attached before the initial reset go to the companion */
  730. s->portsc[firstport + i] = PORTSC_POWNER;
  731. }
  732. s->companion_count++;
  733. s->mmio[0x05] = (s->companion_count << 4) | portcount;
  734. return 0;
  735. }
  736. /* 4.1 host controller initialization */
  737. static void ehci_reset(void *opaque)
  738. {
  739. EHCIState *s = opaque;
  740. int i;
  741. USBDevice *devs[NB_PORTS];
  742. trace_usb_ehci_reset();
  743. /*
  744. * Do the detach before touching portsc, so that it correctly gets send to
  745. * us or to our companion based on PORTSC_POWNER before the reset.
  746. */
  747. for(i = 0; i < NB_PORTS; i++) {
  748. devs[i] = s->ports[i].dev;
  749. if (devs[i] && devs[i]->attached) {
  750. usb_detach(&s->ports[i]);
  751. }
  752. }
  753. memset(&s->mmio[OPREGBASE], 0x00, MMIO_SIZE - OPREGBASE);
  754. s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
  755. s->usbsts = USBSTS_HALT;
  756. s->astate = EST_INACTIVE;
  757. s->pstate = EST_INACTIVE;
  758. s->isoch_pause = -1;
  759. s->attach_poll_counter = 0;
  760. for(i = 0; i < NB_PORTS; i++) {
  761. if (s->companion_ports[i]) {
  762. s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
  763. } else {
  764. s->portsc[i] = PORTSC_PPOWER;
  765. }
  766. if (devs[i] && devs[i]->attached) {
  767. usb_attach(&s->ports[i]);
  768. usb_send_msg(devs[i], USB_MSG_RESET);
  769. }
  770. }
  771. ehci_queues_rip_all(s);
  772. }
  773. static uint32_t ehci_mem_readb(void *ptr, target_phys_addr_t addr)
  774. {
  775. EHCIState *s = ptr;
  776. uint32_t val;
  777. val = s->mmio[addr];
  778. return val;
  779. }
  780. static uint32_t ehci_mem_readw(void *ptr, target_phys_addr_t addr)
  781. {
  782. EHCIState *s = ptr;
  783. uint32_t val;
  784. val = s->mmio[addr] | (s->mmio[addr+1] << 8);
  785. return val;
  786. }
  787. static uint32_t ehci_mem_readl(void *ptr, target_phys_addr_t addr)
  788. {
  789. EHCIState *s = ptr;
  790. uint32_t val;
  791. val = s->mmio[addr] | (s->mmio[addr+1] << 8) |
  792. (s->mmio[addr+2] << 16) | (s->mmio[addr+3] << 24);
  793. trace_usb_ehci_mmio_readl(addr, addr2str(addr), val);
  794. return val;
  795. }
  796. static void ehci_mem_writeb(void *ptr, target_phys_addr_t addr, uint32_t val)
  797. {
  798. fprintf(stderr, "EHCI doesn't handle byte writes to MMIO\n");
  799. exit(1);
  800. }
  801. static void ehci_mem_writew(void *ptr, target_phys_addr_t addr, uint32_t val)
  802. {
  803. fprintf(stderr, "EHCI doesn't handle 16-bit writes to MMIO\n");
  804. exit(1);
  805. }
  806. static void handle_port_owner_write(EHCIState *s, int port, uint32_t owner)
  807. {
  808. USBDevice *dev = s->ports[port].dev;
  809. uint32_t *portsc = &s->portsc[port];
  810. uint32_t orig;
  811. if (s->companion_ports[port] == NULL)
  812. return;
  813. owner = owner & PORTSC_POWNER;
  814. orig = *portsc & PORTSC_POWNER;
  815. if (!(owner ^ orig)) {
  816. return;
  817. }
  818. if (dev && dev->attached) {
  819. usb_detach(&s->ports[port]);
  820. }
  821. *portsc &= ~PORTSC_POWNER;
  822. *portsc |= owner;
  823. if (dev && dev->attached) {
  824. usb_attach(&s->ports[port]);
  825. }
  826. }
  827. static void handle_port_status_write(EHCIState *s, int port, uint32_t val)
  828. {
  829. uint32_t *portsc = &s->portsc[port];
  830. USBDevice *dev = s->ports[port].dev;
  831. /* Clear rwc bits */
  832. *portsc &= ~(val & PORTSC_RWC_MASK);
  833. /* The guest may clear, but not set the PED bit */
  834. *portsc &= val | ~PORTSC_PED;
  835. /* POWNER is masked out by RO_MASK as it is RO when we've no companion */
  836. handle_port_owner_write(s, port, val);
  837. /* And finally apply RO_MASK */
  838. val &= PORTSC_RO_MASK;
  839. if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
  840. trace_usb_ehci_port_reset(port, 1);
  841. }
  842. if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
  843. trace_usb_ehci_port_reset(port, 0);
  844. if (dev && dev->attached) {
  845. usb_reset(&s->ports[port]);
  846. *portsc &= ~PORTSC_CSC;
  847. }
  848. /*
  849. * Table 2.16 Set the enable bit(and enable bit change) to indicate
  850. * to SW that this port has a high speed device attached
  851. */
  852. if (dev && dev->attached && (dev->speedmask & USB_SPEED_MASK_HIGH)) {
  853. val |= PORTSC_PED;
  854. }
  855. }
  856. *portsc &= ~PORTSC_RO_MASK;
  857. *portsc |= val;
  858. }
  859. static void ehci_mem_writel(void *ptr, target_phys_addr_t addr, uint32_t val)
  860. {
  861. EHCIState *s = ptr;
  862. uint32_t *mmio = (uint32_t *)(&s->mmio[addr]);
  863. uint32_t old = *mmio;
  864. int i;
  865. trace_usb_ehci_mmio_writel(addr, addr2str(addr), val);
  866. /* Only aligned reads are allowed on OHCI */
  867. if (addr & 3) {
  868. fprintf(stderr, "usb-ehci: Mis-aligned write to addr 0x"
  869. TARGET_FMT_plx "\n", addr);
  870. return;
  871. }
  872. if (addr >= PORTSC && addr < PORTSC + 4 * NB_PORTS) {
  873. handle_port_status_write(s, (addr-PORTSC)/4, val);
  874. trace_usb_ehci_mmio_change(addr, addr2str(addr), *mmio, old);
  875. return;
  876. }
  877. if (addr < OPREGBASE) {
  878. fprintf(stderr, "usb-ehci: write attempt to read-only register"
  879. TARGET_FMT_plx "\n", addr);
  880. return;
  881. }
  882. /* Do any register specific pre-write processing here. */
  883. switch(addr) {
  884. case USBCMD:
  885. if ((val & USBCMD_RUNSTOP) && !(s->usbcmd & USBCMD_RUNSTOP)) {
  886. qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
  887. SET_LAST_RUN_CLOCK(s);
  888. ehci_clear_usbsts(s, USBSTS_HALT);
  889. }
  890. if (!(val & USBCMD_RUNSTOP) && (s->usbcmd & USBCMD_RUNSTOP)) {
  891. qemu_del_timer(s->frame_timer);
  892. // TODO - should finish out some stuff before setting halt
  893. ehci_set_usbsts(s, USBSTS_HALT);
  894. }
  895. if (val & USBCMD_HCRESET) {
  896. ehci_reset(s);
  897. val &= ~USBCMD_HCRESET;
  898. }
  899. /* not supporting dynamic frame list size at the moment */
  900. if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
  901. fprintf(stderr, "attempt to set frame list size -- value %d\n",
  902. val & USBCMD_FLS);
  903. val &= ~USBCMD_FLS;
  904. }
  905. break;
  906. case USBSTS:
  907. val &= USBSTS_RO_MASK; // bits 6 thru 31 are RO
  908. ehci_clear_usbsts(s, val); // bits 0 thru 5 are R/WC
  909. val = s->usbsts;
  910. ehci_set_interrupt(s, 0);
  911. break;
  912. case USBINTR:
  913. val &= USBINTR_MASK;
  914. break;
  915. case FRINDEX:
  916. s->sofv = val >> 3;
  917. break;
  918. case CONFIGFLAG:
  919. val &= 0x1;
  920. if (val) {
  921. for(i = 0; i < NB_PORTS; i++)
  922. handle_port_owner_write(s, i, 0);
  923. }
  924. break;
  925. case PERIODICLISTBASE:
  926. if ((s->usbcmd & USBCMD_PSE) && (s->usbcmd & USBCMD_RUNSTOP)) {
  927. fprintf(stderr,
  928. "ehci: PERIODIC list base register set while periodic schedule\n"
  929. " is enabled and HC is enabled\n");
  930. }
  931. break;
  932. case ASYNCLISTADDR:
  933. if ((s->usbcmd & USBCMD_ASE) && (s->usbcmd & USBCMD_RUNSTOP)) {
  934. fprintf(stderr,
  935. "ehci: ASYNC list address register set while async schedule\n"
  936. " is enabled and HC is enabled\n");
  937. }
  938. break;
  939. }
  940. *mmio = val;
  941. trace_usb_ehci_mmio_change(addr, addr2str(addr), *mmio, old);
  942. }
  943. // TODO : Put in common header file, duplication from usb-ohci.c
  944. /* Get an array of dwords from main memory */
  945. static inline int get_dwords(EHCIState *ehci, uint32_t addr,
  946. uint32_t *buf, int num)
  947. {
  948. int i;
  949. for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  950. pci_dma_read(&ehci->dev, addr, (uint8_t *)buf, sizeof(*buf));
  951. *buf = le32_to_cpu(*buf);
  952. }
  953. return 1;
  954. }
  955. /* Put an array of dwords in to main memory */
  956. static inline int put_dwords(EHCIState *ehci, uint32_t addr,
  957. uint32_t *buf, int num)
  958. {
  959. int i;
  960. for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  961. uint32_t tmp = cpu_to_le32(*buf);
  962. pci_dma_write(&ehci->dev, addr, (uint8_t *)&tmp, sizeof(tmp));
  963. }
  964. return 1;
  965. }
  966. // 4.10.2
  967. static int ehci_qh_do_overlay(EHCIQueue *q)
  968. {
  969. int i;
  970. int dtoggle;
  971. int ping;
  972. int eps;
  973. int reload;
  974. // remember values in fields to preserve in qh after overlay
  975. dtoggle = q->qh.token & QTD_TOKEN_DTOGGLE;
  976. ping = q->qh.token & QTD_TOKEN_PING;
  977. q->qh.current_qtd = q->qtdaddr;
  978. q->qh.next_qtd = q->qtd.next;
  979. q->qh.altnext_qtd = q->qtd.altnext;
  980. q->qh.token = q->qtd.token;
  981. eps = get_field(q->qh.epchar, QH_EPCHAR_EPS);
  982. if (eps == EHCI_QH_EPS_HIGH) {
  983. q->qh.token &= ~QTD_TOKEN_PING;
  984. q->qh.token |= ping;
  985. }
  986. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  987. set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
  988. for (i = 0; i < 5; i++) {
  989. q->qh.bufptr[i] = q->qtd.bufptr[i];
  990. }
  991. if (!(q->qh.epchar & QH_EPCHAR_DTC)) {
  992. // preserve QH DT bit
  993. q->qh.token &= ~QTD_TOKEN_DTOGGLE;
  994. q->qh.token |= dtoggle;
  995. }
  996. q->qh.bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
  997. q->qh.bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
  998. put_dwords(q->ehci, NLPTR_GET(q->qhaddr), (uint32_t *) &q->qh,
  999. sizeof(EHCIqh) >> 2);
  1000. return 0;
  1001. }
  1002. static int ehci_init_transfer(EHCIQueue *q)
  1003. {
  1004. uint32_t cpage, offset, bytes, plen;
  1005. dma_addr_t page;
  1006. cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
  1007. bytes = get_field(q->qh.token, QTD_TOKEN_TBYTES);
  1008. offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
  1009. pci_dma_sglist_init(&q->sgl, &q->ehci->dev, 5);
  1010. while (bytes > 0) {
  1011. if (cpage > 4) {
  1012. fprintf(stderr, "cpage out of range (%d)\n", cpage);
  1013. return USB_RET_PROCERR;
  1014. }
  1015. page = q->qh.bufptr[cpage] & QTD_BUFPTR_MASK;
  1016. page += offset;
  1017. plen = bytes;
  1018. if (plen > 4096 - offset) {
  1019. plen = 4096 - offset;
  1020. offset = 0;
  1021. cpage++;
  1022. }
  1023. qemu_sglist_add(&q->sgl, page, plen);
  1024. bytes -= plen;
  1025. }
  1026. return 0;
  1027. }
  1028. static void ehci_finish_transfer(EHCIQueue *q, int status)
  1029. {
  1030. uint32_t cpage, offset;
  1031. qemu_sglist_destroy(&q->sgl);
  1032. if (status > 0) {
  1033. /* update cpage & offset */
  1034. cpage = get_field(q->qh.token, QTD_TOKEN_CPAGE);
  1035. offset = q->qh.bufptr[0] & ~QTD_BUFPTR_MASK;
  1036. offset += status;
  1037. cpage += offset >> QTD_BUFPTR_SH;
  1038. offset &= ~QTD_BUFPTR_MASK;
  1039. set_field(&q->qh.token, cpage, QTD_TOKEN_CPAGE);
  1040. q->qh.bufptr[0] &= QTD_BUFPTR_MASK;
  1041. q->qh.bufptr[0] |= offset;
  1042. }
  1043. }
  1044. static void ehci_async_complete_packet(USBPort *port, USBPacket *packet)
  1045. {
  1046. EHCIQueue *q;
  1047. EHCIState *s = port->opaque;
  1048. uint32_t portsc = s->portsc[port->index];
  1049. if (portsc & PORTSC_POWNER) {
  1050. USBPort *companion = s->companion_ports[port->index];
  1051. companion->ops->complete(companion, packet);
  1052. return;
  1053. }
  1054. q = container_of(packet, EHCIQueue, packet);
  1055. trace_usb_ehci_queue_action(q, "wakeup");
  1056. assert(q->async == EHCI_ASYNC_INFLIGHT);
  1057. q->async = EHCI_ASYNC_FINISHED;
  1058. q->usb_status = packet->result;
  1059. }
  1060. static void ehci_execute_complete(EHCIQueue *q)
  1061. {
  1062. int c_err, reload;
  1063. assert(q->async != EHCI_ASYNC_INFLIGHT);
  1064. q->async = EHCI_ASYNC_NONE;
  1065. DPRINTF("execute_complete: qhaddr 0x%x, next %x, qtdaddr 0x%x, status %d\n",
  1066. q->qhaddr, q->qh.next, q->qtdaddr, q->usb_status);
  1067. if (q->usb_status < 0) {
  1068. err:
  1069. /* TO-DO: put this is in a function that can be invoked below as well */
  1070. c_err = get_field(q->qh.token, QTD_TOKEN_CERR);
  1071. c_err--;
  1072. set_field(&q->qh.token, c_err, QTD_TOKEN_CERR);
  1073. switch(q->usb_status) {
  1074. case USB_RET_NODEV:
  1075. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_XACTERR);
  1076. ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
  1077. break;
  1078. case USB_RET_STALL:
  1079. q->qh.token |= QTD_TOKEN_HALT;
  1080. ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
  1081. break;
  1082. case USB_RET_NAK:
  1083. /* 4.10.3 */
  1084. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  1085. if ((q->pid == USB_TOKEN_IN) && reload) {
  1086. int nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
  1087. nakcnt--;
  1088. set_field(&q->qh.altnext_qtd, nakcnt, QH_ALTNEXT_NAKCNT);
  1089. } else if (!reload) {
  1090. return;
  1091. }
  1092. break;
  1093. case USB_RET_BABBLE:
  1094. q->qh.token |= (QTD_TOKEN_HALT | QTD_TOKEN_BABBLE);
  1095. ehci_record_interrupt(q->ehci, USBSTS_ERRINT);
  1096. break;
  1097. default:
  1098. /* should not be triggerable */
  1099. fprintf(stderr, "USB invalid response %d to handle\n", q->usb_status);
  1100. assert(0);
  1101. break;
  1102. }
  1103. } else {
  1104. // DPRINTF("Short packet condition\n");
  1105. // TODO check 4.12 for splits
  1106. if ((q->usb_status > q->tbytes) && (q->pid == USB_TOKEN_IN)) {
  1107. q->usb_status = USB_RET_BABBLE;
  1108. goto err;
  1109. }
  1110. if (q->tbytes && q->pid == USB_TOKEN_IN) {
  1111. q->tbytes -= q->usb_status;
  1112. } else {
  1113. q->tbytes = 0;
  1114. }
  1115. DPRINTF("updating tbytes to %d\n", q->tbytes);
  1116. set_field(&q->qh.token, q->tbytes, QTD_TOKEN_TBYTES);
  1117. }
  1118. ehci_finish_transfer(q, q->usb_status);
  1119. usb_packet_unmap(&q->packet);
  1120. q->qh.token ^= QTD_TOKEN_DTOGGLE;
  1121. q->qh.token &= ~QTD_TOKEN_ACTIVE;
  1122. if ((q->usb_status >= 0) && (q->qh.token & QTD_TOKEN_IOC)) {
  1123. ehci_record_interrupt(q->ehci, USBSTS_INT);
  1124. }
  1125. }
  1126. // 4.10.3
  1127. static int ehci_execute(EHCIQueue *q)
  1128. {
  1129. USBPort *port;
  1130. USBDevice *dev;
  1131. int ret;
  1132. int i;
  1133. int endp;
  1134. int devadr;
  1135. if ( !(q->qh.token & QTD_TOKEN_ACTIVE)) {
  1136. fprintf(stderr, "Attempting to execute inactive QH\n");
  1137. return USB_RET_PROCERR;
  1138. }
  1139. q->tbytes = (q->qh.token & QTD_TOKEN_TBYTES_MASK) >> QTD_TOKEN_TBYTES_SH;
  1140. if (q->tbytes > BUFF_SIZE) {
  1141. fprintf(stderr, "Request for more bytes than allowed\n");
  1142. return USB_RET_PROCERR;
  1143. }
  1144. q->pid = (q->qh.token & QTD_TOKEN_PID_MASK) >> QTD_TOKEN_PID_SH;
  1145. switch(q->pid) {
  1146. case 0: q->pid = USB_TOKEN_OUT; break;
  1147. case 1: q->pid = USB_TOKEN_IN; break;
  1148. case 2: q->pid = USB_TOKEN_SETUP; break;
  1149. default: fprintf(stderr, "bad token\n"); break;
  1150. }
  1151. if (ehci_init_transfer(q) != 0) {
  1152. return USB_RET_PROCERR;
  1153. }
  1154. endp = get_field(q->qh.epchar, QH_EPCHAR_EP);
  1155. devadr = get_field(q->qh.epchar, QH_EPCHAR_DEVADDR);
  1156. ret = USB_RET_NODEV;
  1157. usb_packet_setup(&q->packet, q->pid, devadr, endp);
  1158. usb_packet_map(&q->packet, &q->sgl);
  1159. // TO-DO: associating device with ehci port
  1160. for(i = 0; i < NB_PORTS; i++) {
  1161. port = &q->ehci->ports[i];
  1162. dev = port->dev;
  1163. if (!(q->ehci->portsc[i] &(PORTSC_CONNECT))) {
  1164. DPRINTF("Port %d, no exec, not connected(%08X)\n",
  1165. i, q->ehci->portsc[i]);
  1166. continue;
  1167. }
  1168. ret = usb_handle_packet(dev, &q->packet);
  1169. DPRINTF("submit: qh %x next %x qtd %x pid %x len %zd "
  1170. "(total %d) endp %x ret %d\n",
  1171. q->qhaddr, q->qh.next, q->qtdaddr, q->pid,
  1172. q->packet.iov.size, q->tbytes, endp, ret);
  1173. if (ret != USB_RET_NODEV) {
  1174. break;
  1175. }
  1176. }
  1177. if (ret > BUFF_SIZE) {
  1178. fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
  1179. return USB_RET_PROCERR;
  1180. }
  1181. return ret;
  1182. }
  1183. /* 4.7.2
  1184. */
  1185. static int ehci_process_itd(EHCIState *ehci,
  1186. EHCIitd *itd)
  1187. {
  1188. USBPort *port;
  1189. USBDevice *dev;
  1190. int ret;
  1191. uint32_t i, j, len, pid, dir, devaddr, endp;
  1192. uint32_t pg, off, ptr1, ptr2, max, mult;
  1193. dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
  1194. devaddr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
  1195. endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
  1196. max = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
  1197. mult = get_field(itd->bufptr[2], ITD_BUFPTR_MULT);
  1198. for(i = 0; i < 8; i++) {
  1199. if (itd->transact[i] & ITD_XACT_ACTIVE) {
  1200. pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
  1201. off = itd->transact[i] & ITD_XACT_OFFSET_MASK;
  1202. ptr1 = (itd->bufptr[pg] & ITD_BUFPTR_MASK);
  1203. ptr2 = (itd->bufptr[pg+1] & ITD_BUFPTR_MASK);
  1204. len = get_field(itd->transact[i], ITD_XACT_LENGTH);
  1205. if (len > max * mult) {
  1206. len = max * mult;
  1207. }
  1208. if (len > BUFF_SIZE) {
  1209. return USB_RET_PROCERR;
  1210. }
  1211. pci_dma_sglist_init(&ehci->isgl, &ehci->dev, 2);
  1212. if (off + len > 4096) {
  1213. /* transfer crosses page border */
  1214. uint32_t len2 = off + len - 4096;
  1215. uint32_t len1 = len - len2;
  1216. qemu_sglist_add(&ehci->isgl, ptr1 + off, len1);
  1217. qemu_sglist_add(&ehci->isgl, ptr2, len2);
  1218. } else {
  1219. qemu_sglist_add(&ehci->isgl, ptr1 + off, len);
  1220. }
  1221. pid = dir ? USB_TOKEN_IN : USB_TOKEN_OUT;
  1222. usb_packet_setup(&ehci->ipacket, pid, devaddr, endp);
  1223. usb_packet_map(&ehci->ipacket, &ehci->isgl);
  1224. ret = USB_RET_NODEV;
  1225. for (j = 0; j < NB_PORTS; j++) {
  1226. port = &ehci->ports[j];
  1227. dev = port->dev;
  1228. if (!(ehci->portsc[j] &(PORTSC_CONNECT))) {
  1229. continue;
  1230. }
  1231. ret = usb_handle_packet(dev, &ehci->ipacket);
  1232. if (ret != USB_RET_NODEV) {
  1233. break;
  1234. }
  1235. }
  1236. usb_packet_unmap(&ehci->ipacket);
  1237. qemu_sglist_destroy(&ehci->isgl);
  1238. #if 0
  1239. /* In isoch, there is no facility to indicate a NAK so let's
  1240. * instead just complete a zero-byte transaction. Setting
  1241. * DBERR seems too draconian.
  1242. */
  1243. if (ret == USB_RET_NAK) {
  1244. if (ehci->isoch_pause > 0) {
  1245. DPRINTF("ISOCH: received a NAK but paused so returning\n");
  1246. ehci->isoch_pause--;
  1247. return 0;
  1248. } else if (ehci->isoch_pause == -1) {
  1249. DPRINTF("ISOCH: recv NAK & isoch pause inactive, setting\n");
  1250. // Pause frindex for up to 50 msec waiting for data from
  1251. // remote
  1252. ehci->isoch_pause = 50;
  1253. return 0;
  1254. } else {
  1255. DPRINTF("ISOCH: isoch pause timeout! return 0\n");
  1256. ret = 0;
  1257. }
  1258. } else {
  1259. DPRINTF("ISOCH: received ACK, clearing pause\n");
  1260. ehci->isoch_pause = -1;
  1261. }
  1262. #else
  1263. if (ret == USB_RET_NAK) {
  1264. ret = 0;
  1265. }
  1266. #endif
  1267. if (ret >= 0) {
  1268. if (!dir) {
  1269. /* OUT */
  1270. set_field(&itd->transact[i], len - ret, ITD_XACT_LENGTH);
  1271. } else {
  1272. /* IN */
  1273. set_field(&itd->transact[i], ret, ITD_XACT_LENGTH);
  1274. }
  1275. if (itd->transact[i] & ITD_XACT_IOC) {
  1276. ehci_record_interrupt(ehci, USBSTS_INT);
  1277. }
  1278. }
  1279. itd->transact[i] &= ~ITD_XACT_ACTIVE;
  1280. }
  1281. }
  1282. return 0;
  1283. }
  1284. /* This state is the entry point for asynchronous schedule
  1285. * processing. Entry here consitutes a EHCI start event state (4.8.5)
  1286. */
  1287. static int ehci_state_waitlisthead(EHCIState *ehci, int async)
  1288. {
  1289. EHCIqh qh;
  1290. int i = 0;
  1291. int again = 0;
  1292. uint32_t entry = ehci->asynclistaddr;
  1293. /* set reclamation flag at start event (4.8.6) */
  1294. if (async) {
  1295. ehci_set_usbsts(ehci, USBSTS_REC);
  1296. }
  1297. ehci_queues_rip_unused(ehci);
  1298. /* Find the head of the list (4.9.1.1) */
  1299. for(i = 0; i < MAX_QH; i++) {
  1300. get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &qh,
  1301. sizeof(EHCIqh) >> 2);
  1302. ehci_trace_qh(NULL, NLPTR_GET(entry), &qh);
  1303. if (qh.epchar & QH_EPCHAR_H) {
  1304. if (async) {
  1305. entry |= (NLPTR_TYPE_QH << 1);
  1306. }
  1307. ehci_set_fetch_addr(ehci, async, entry);
  1308. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1309. again = 1;
  1310. goto out;
  1311. }
  1312. entry = qh.next;
  1313. if (entry == ehci->asynclistaddr) {
  1314. break;
  1315. }
  1316. }
  1317. /* no head found for list. */
  1318. ehci_set_state(ehci, async, EST_ACTIVE);
  1319. out:
  1320. return again;
  1321. }
  1322. /* This state is the entry point for periodic schedule processing as
  1323. * well as being a continuation state for async processing.
  1324. */
  1325. static int ehci_state_fetchentry(EHCIState *ehci, int async)
  1326. {
  1327. int again = 0;
  1328. uint32_t entry = ehci_get_fetch_addr(ehci, async);
  1329. if (entry < 0x1000) {
  1330. DPRINTF("fetchentry: entry invalid (0x%08x)\n", entry);
  1331. ehci_set_state(ehci, async, EST_ACTIVE);
  1332. goto out;
  1333. }
  1334. /* section 4.8, only QH in async schedule */
  1335. if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
  1336. fprintf(stderr, "non queue head request in async schedule\n");
  1337. return -1;
  1338. }
  1339. switch (NLPTR_TYPE_GET(entry)) {
  1340. case NLPTR_TYPE_QH:
  1341. ehci_set_state(ehci, async, EST_FETCHQH);
  1342. again = 1;
  1343. break;
  1344. case NLPTR_TYPE_ITD:
  1345. ehci_set_state(ehci, async, EST_FETCHITD);
  1346. again = 1;
  1347. break;
  1348. case NLPTR_TYPE_STITD:
  1349. ehci_set_state(ehci, async, EST_FETCHSITD);
  1350. again = 1;
  1351. break;
  1352. default:
  1353. /* TODO: handle FSTN type */
  1354. fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
  1355. "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
  1356. return -1;
  1357. }
  1358. out:
  1359. return again;
  1360. }
  1361. static EHCIQueue *ehci_state_fetchqh(EHCIState *ehci, int async)
  1362. {
  1363. uint32_t entry;
  1364. EHCIQueue *q;
  1365. int reload;
  1366. entry = ehci_get_fetch_addr(ehci, async);
  1367. q = ehci_find_queue_by_qh(ehci, entry);
  1368. if (NULL == q) {
  1369. q = ehci_alloc_queue(ehci, async);
  1370. }
  1371. q->qhaddr = entry;
  1372. q->seen++;
  1373. if (q->seen > 1) {
  1374. /* we are going in circles -- stop processing */
  1375. ehci_set_state(ehci, async, EST_ACTIVE);
  1376. q = NULL;
  1377. goto out;
  1378. }
  1379. get_dwords(ehci, NLPTR_GET(q->qhaddr),
  1380. (uint32_t *) &q->qh, sizeof(EHCIqh) >> 2);
  1381. ehci_trace_qh(q, NLPTR_GET(q->qhaddr), &q->qh);
  1382. if (q->async == EHCI_ASYNC_INFLIGHT) {
  1383. /* I/O still in progress -- skip queue */
  1384. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1385. goto out;
  1386. }
  1387. if (q->async == EHCI_ASYNC_FINISHED) {
  1388. /* I/O finished -- continue processing queue */
  1389. trace_usb_ehci_queue_action(q, "resume");
  1390. ehci_set_state(ehci, async, EST_EXECUTING);
  1391. goto out;
  1392. }
  1393. if (async && (q->qh.epchar & QH_EPCHAR_H)) {
  1394. /* EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
  1395. if (ehci->usbsts & USBSTS_REC) {
  1396. ehci_clear_usbsts(ehci, USBSTS_REC);
  1397. } else {
  1398. DPRINTF("FETCHQH: QH 0x%08x. H-bit set, reclamation status reset"
  1399. " - done processing\n", q->qhaddr);
  1400. ehci_set_state(ehci, async, EST_ACTIVE);
  1401. q = NULL;
  1402. goto out;
  1403. }
  1404. }
  1405. #if EHCI_DEBUG
  1406. if (q->qhaddr != q->qh.next) {
  1407. DPRINTF("FETCHQH: QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
  1408. q->qhaddr,
  1409. q->qh.epchar & QH_EPCHAR_H,
  1410. q->qh.token & QTD_TOKEN_HALT,
  1411. q->qh.token & QTD_TOKEN_ACTIVE,
  1412. q->qh.next);
  1413. }
  1414. #endif
  1415. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  1416. if (reload) {
  1417. set_field(&q->qh.altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
  1418. }
  1419. if (q->qh.token & QTD_TOKEN_HALT) {
  1420. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1421. } else if ((q->qh.token & QTD_TOKEN_ACTIVE) && (q->qh.current_qtd > 0x1000)) {
  1422. q->qtdaddr = q->qh.current_qtd;
  1423. ehci_set_state(ehci, async, EST_FETCHQTD);
  1424. } else {
  1425. /* EHCI spec version 1.0 Section 4.10.2 */
  1426. ehci_set_state(ehci, async, EST_ADVANCEQUEUE);
  1427. }
  1428. out:
  1429. return q;
  1430. }
  1431. static int ehci_state_fetchitd(EHCIState *ehci, int async)
  1432. {
  1433. uint32_t entry;
  1434. EHCIitd itd;
  1435. assert(!async);
  1436. entry = ehci_get_fetch_addr(ehci, async);
  1437. get_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1438. sizeof(EHCIitd) >> 2);
  1439. ehci_trace_itd(ehci, entry, &itd);
  1440. if (ehci_process_itd(ehci, &itd) != 0) {
  1441. return -1;
  1442. }
  1443. put_dwords(ehci, NLPTR_GET(entry), (uint32_t *) &itd,
  1444. sizeof(EHCIitd) >> 2);
  1445. ehci_set_fetch_addr(ehci, async, itd.next);
  1446. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1447. return 1;
  1448. }
  1449. static int ehci_state_fetchsitd(EHCIState *ehci, int async)
  1450. {
  1451. uint32_t entry;
  1452. EHCIsitd sitd;
  1453. assert(!async);
  1454. entry = ehci_get_fetch_addr(ehci, async);
  1455. get_dwords(ehci, NLPTR_GET(entry), (uint32_t *)&sitd,
  1456. sizeof(EHCIsitd) >> 2);
  1457. ehci_trace_sitd(ehci, entry, &sitd);
  1458. if (!(sitd.results & SITD_RESULTS_ACTIVE)) {
  1459. /* siTD is not active, nothing to do */;
  1460. } else {
  1461. /* TODO: split transfers are not implemented */
  1462. fprintf(stderr, "WARNING: Skipping active siTD\n");
  1463. }
  1464. ehci_set_fetch_addr(ehci, async, sitd.next);
  1465. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1466. return 1;
  1467. }
  1468. /* Section 4.10.2 - paragraph 3 */
  1469. static int ehci_state_advqueue(EHCIQueue *q, int async)
  1470. {
  1471. #if 0
  1472. /* TO-DO: 4.10.2 - paragraph 2
  1473. * if I-bit is set to 1 and QH is not active
  1474. * go to horizontal QH
  1475. */
  1476. if (I-bit set) {
  1477. ehci_set_state(ehci, async, EST_HORIZONTALQH);
  1478. goto out;
  1479. }
  1480. #endif
  1481. /*
  1482. * want data and alt-next qTD is valid
  1483. */
  1484. if (((q->qh.token & QTD_TOKEN_TBYTES_MASK) != 0) &&
  1485. (q->qh.altnext_qtd > 0x1000) &&
  1486. (NLPTR_TBIT(q->qh.altnext_qtd) == 0)) {
  1487. q->qtdaddr = q->qh.altnext_qtd;
  1488. ehci_set_state(q->ehci, async, EST_FETCHQTD);
  1489. /*
  1490. * next qTD is valid
  1491. */
  1492. } else if ((q->qh.next_qtd > 0x1000) &&
  1493. (NLPTR_TBIT(q->qh.next_qtd) == 0)) {
  1494. q->qtdaddr = q->qh.next_qtd;
  1495. ehci_set_state(q->ehci, async, EST_FETCHQTD);
  1496. /*
  1497. * no valid qTD, try next QH
  1498. */
  1499. } else {
  1500. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1501. }
  1502. return 1;
  1503. }
  1504. /* Section 4.10.2 - paragraph 4 */
  1505. static int ehci_state_fetchqtd(EHCIQueue *q, int async)
  1506. {
  1507. int again = 0;
  1508. get_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &q->qtd,
  1509. sizeof(EHCIqtd) >> 2);
  1510. ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), &q->qtd);
  1511. if (q->qtd.token & QTD_TOKEN_ACTIVE) {
  1512. ehci_set_state(q->ehci, async, EST_EXECUTE);
  1513. again = 1;
  1514. } else {
  1515. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1516. again = 1;
  1517. }
  1518. return again;
  1519. }
  1520. static int ehci_state_horizqh(EHCIQueue *q, int async)
  1521. {
  1522. int again = 0;
  1523. if (ehci_get_fetch_addr(q->ehci, async) != q->qh.next) {
  1524. ehci_set_fetch_addr(q->ehci, async, q->qh.next);
  1525. ehci_set_state(q->ehci, async, EST_FETCHENTRY);
  1526. again = 1;
  1527. } else {
  1528. ehci_set_state(q->ehci, async, EST_ACTIVE);
  1529. }
  1530. return again;
  1531. }
  1532. /*
  1533. * Write the qh back to guest physical memory. This step isn't
  1534. * in the EHCI spec but we need to do it since we don't share
  1535. * physical memory with our guest VM.
  1536. *
  1537. * The first three dwords are read-only for the EHCI, so skip them
  1538. * when writing back the qh.
  1539. */
  1540. static void ehci_flush_qh(EHCIQueue *q)
  1541. {
  1542. uint32_t *qh = (uint32_t *) &q->qh;
  1543. uint32_t dwords = sizeof(EHCIqh) >> 2;
  1544. uint32_t addr = NLPTR_GET(q->qhaddr);
  1545. put_dwords(q->ehci, addr + 3 * sizeof(uint32_t), qh + 3, dwords - 3);
  1546. }
  1547. static int ehci_state_execute(EHCIQueue *q, int async)
  1548. {
  1549. int again = 0;
  1550. int reload, nakcnt;
  1551. int smask;
  1552. if (ehci_qh_do_overlay(q) != 0) {
  1553. return -1;
  1554. }
  1555. smask = get_field(q->qh.epcap, QH_EPCAP_SMASK);
  1556. if (!smask) {
  1557. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  1558. nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
  1559. if (reload && !nakcnt) {
  1560. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1561. again = 1;
  1562. goto out;
  1563. }
  1564. }
  1565. // TODO verify enough time remains in the uframe as in 4.4.1.1
  1566. // TODO write back ptr to async list when done or out of time
  1567. // TODO Windows does not seem to ever set the MULT field
  1568. if (!async) {
  1569. int transactCtr = get_field(q->qh.epcap, QH_EPCAP_MULT);
  1570. if (!transactCtr) {
  1571. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1572. again = 1;
  1573. goto out;
  1574. }
  1575. }
  1576. if (async) {
  1577. ehci_set_usbsts(q->ehci, USBSTS_REC);
  1578. }
  1579. q->usb_status = ehci_execute(q);
  1580. if (q->usb_status == USB_RET_PROCERR) {
  1581. again = -1;
  1582. goto out;
  1583. }
  1584. if (q->usb_status == USB_RET_ASYNC) {
  1585. ehci_flush_qh(q);
  1586. trace_usb_ehci_queue_action(q, "suspend");
  1587. q->async = EHCI_ASYNC_INFLIGHT;
  1588. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1589. again = 1;
  1590. goto out;
  1591. }
  1592. ehci_set_state(q->ehci, async, EST_EXECUTING);
  1593. again = 1;
  1594. out:
  1595. return again;
  1596. }
  1597. static int ehci_state_executing(EHCIQueue *q, int async)
  1598. {
  1599. int again = 0;
  1600. int reload, nakcnt;
  1601. ehci_execute_complete(q);
  1602. if (q->usb_status == USB_RET_ASYNC) {
  1603. goto out;
  1604. }
  1605. if (q->usb_status == USB_RET_PROCERR) {
  1606. again = -1;
  1607. goto out;
  1608. }
  1609. // 4.10.3
  1610. if (!async) {
  1611. int transactCtr = get_field(q->qh.epcap, QH_EPCAP_MULT);
  1612. transactCtr--;
  1613. set_field(&q->qh.epcap, transactCtr, QH_EPCAP_MULT);
  1614. // 4.10.3, bottom of page 82, should exit this state when transaction
  1615. // counter decrements to 0
  1616. }
  1617. reload = get_field(q->qh.epchar, QH_EPCHAR_RL);
  1618. if (reload) {
  1619. nakcnt = get_field(q->qh.altnext_qtd, QH_ALTNEXT_NAKCNT);
  1620. if (q->usb_status == USB_RET_NAK) {
  1621. if (nakcnt) {
  1622. nakcnt--;
  1623. }
  1624. } else {
  1625. nakcnt = reload;
  1626. }
  1627. set_field(&q->qh.altnext_qtd, nakcnt, QH_ALTNEXT_NAKCNT);
  1628. }
  1629. /* 4.10.5 */
  1630. if ((q->usb_status == USB_RET_NAK) || (q->qh.token & QTD_TOKEN_ACTIVE)) {
  1631. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1632. } else {
  1633. ehci_set_state(q->ehci, async, EST_WRITEBACK);
  1634. }
  1635. again = 1;
  1636. out:
  1637. ehci_flush_qh(q);
  1638. return again;
  1639. }
  1640. static int ehci_state_writeback(EHCIQueue *q, int async)
  1641. {
  1642. int again = 0;
  1643. /* Write back the QTD from the QH area */
  1644. ehci_trace_qtd(q, NLPTR_GET(q->qtdaddr), (EHCIqtd*) &q->qh.next_qtd);
  1645. put_dwords(q->ehci, NLPTR_GET(q->qtdaddr), (uint32_t *) &q->qh.next_qtd,
  1646. sizeof(EHCIqtd) >> 2);
  1647. /*
  1648. * EHCI specs say go horizontal here.
  1649. *
  1650. * We can also advance the queue here for performance reasons. We
  1651. * need to take care to only take that shortcut in case we've
  1652. * processed the qtd just written back without errors, i.e. halt
  1653. * bit is clear.
  1654. */
  1655. if (q->qh.token & QTD_TOKEN_HALT) {
  1656. ehci_set_state(q->ehci, async, EST_HORIZONTALQH);
  1657. again = 1;
  1658. } else {
  1659. ehci_set_state(q->ehci, async, EST_ADVANCEQUEUE);
  1660. again = 1;
  1661. }
  1662. return again;
  1663. }
  1664. /*
  1665. * This is the state machine that is common to both async and periodic
  1666. */
  1667. static void ehci_advance_state(EHCIState *ehci,
  1668. int async)
  1669. {
  1670. EHCIQueue *q = NULL;
  1671. int again;
  1672. int iter = 0;
  1673. do {
  1674. if (ehci_get_state(ehci, async) == EST_FETCHQH) {
  1675. iter++;
  1676. /* if we are roaming a lot of QH without executing a qTD
  1677. * something is wrong with the linked list. TO-DO: why is
  1678. * this hack needed?
  1679. */
  1680. assert(iter < MAX_ITERATIONS);
  1681. #if 0
  1682. if (iter > MAX_ITERATIONS) {
  1683. DPRINTF("\n*** advance_state: bailing on MAX ITERATIONS***\n");
  1684. ehci_set_state(ehci, async, EST_ACTIVE);
  1685. break;
  1686. }
  1687. #endif
  1688. }
  1689. switch(ehci_get_state(ehci, async)) {
  1690. case EST_WAITLISTHEAD:
  1691. again = ehci_state_waitlisthead(ehci, async);
  1692. break;
  1693. case EST_FETCHENTRY:
  1694. again = ehci_state_fetchentry(ehci, async);
  1695. break;
  1696. case EST_FETCHQH:
  1697. q = ehci_state_fetchqh(ehci, async);
  1698. again = q ? 1 : 0;
  1699. break;
  1700. case EST_FETCHITD:
  1701. again = ehci_state_fetchitd(ehci, async);
  1702. break;
  1703. case EST_FETCHSITD:
  1704. again = ehci_state_fetchsitd(ehci, async);
  1705. break;
  1706. case EST_ADVANCEQUEUE:
  1707. again = ehci_state_advqueue(q, async);
  1708. break;
  1709. case EST_FETCHQTD:
  1710. again = ehci_state_fetchqtd(q, async);
  1711. break;
  1712. case EST_HORIZONTALQH:
  1713. again = ehci_state_horizqh(q, async);
  1714. break;
  1715. case EST_EXECUTE:
  1716. iter = 0;
  1717. again = ehci_state_execute(q, async);
  1718. break;
  1719. case EST_EXECUTING:
  1720. assert(q != NULL);
  1721. again = ehci_state_executing(q, async);
  1722. break;
  1723. case EST_WRITEBACK:
  1724. assert(q != NULL);
  1725. again = ehci_state_writeback(q, async);
  1726. break;
  1727. default:
  1728. fprintf(stderr, "Bad state!\n");
  1729. again = -1;
  1730. assert(0);
  1731. break;
  1732. }
  1733. if (again < 0) {
  1734. fprintf(stderr, "processing error - resetting ehci HC\n");
  1735. ehci_reset(ehci);
  1736. again = 0;
  1737. assert(0);
  1738. }
  1739. }
  1740. while (again);
  1741. ehci_commit_interrupt(ehci);
  1742. }
  1743. static void ehci_advance_async_state(EHCIState *ehci)
  1744. {
  1745. int async = 1;
  1746. switch(ehci_get_state(ehci, async)) {
  1747. case EST_INACTIVE:
  1748. if (!(ehci->usbcmd & USBCMD_ASE)) {
  1749. break;
  1750. }
  1751. ehci_set_usbsts(ehci, USBSTS_ASS);
  1752. ehci_set_state(ehci, async, EST_ACTIVE);
  1753. // No break, fall through to ACTIVE
  1754. case EST_ACTIVE:
  1755. if ( !(ehci->usbcmd & USBCMD_ASE)) {
  1756. ehci_clear_usbsts(ehci, USBSTS_ASS);
  1757. ehci_set_state(ehci, async, EST_INACTIVE);
  1758. break;
  1759. }
  1760. /* If the doorbell is set, the guest wants to make a change to the
  1761. * schedule. The host controller needs to release cached data.
  1762. * (section 4.8.2)
  1763. */
  1764. if (ehci->usbcmd & USBCMD_IAAD) {
  1765. DPRINTF("ASYNC: doorbell request acknowledged\n");
  1766. ehci->usbcmd &= ~USBCMD_IAAD;
  1767. ehci_set_interrupt(ehci, USBSTS_IAA);
  1768. break;
  1769. }
  1770. /* make sure guest has acknowledged */
  1771. /* TO-DO: is this really needed? */
  1772. if (ehci->usbsts & USBSTS_IAA) {
  1773. DPRINTF("IAA status bit still set.\n");
  1774. break;
  1775. }
  1776. /* check that address register has been set */
  1777. if (ehci->asynclistaddr == 0) {
  1778. break;
  1779. }
  1780. ehci_set_state(ehci, async, EST_WAITLISTHEAD);
  1781. ehci_advance_state(ehci, async);
  1782. break;
  1783. default:
  1784. /* this should only be due to a developer mistake */
  1785. fprintf(stderr, "ehci: Bad asynchronous state %d. "
  1786. "Resetting to active\n", ehci->astate);
  1787. assert(0);
  1788. }
  1789. }
  1790. static void ehci_advance_periodic_state(EHCIState *ehci)
  1791. {
  1792. uint32_t entry;
  1793. uint32_t list;
  1794. int async = 0;
  1795. // 4.6
  1796. switch(ehci_get_state(ehci, async)) {
  1797. case EST_INACTIVE:
  1798. if ( !(ehci->frindex & 7) && (ehci->usbcmd & USBCMD_PSE)) {
  1799. ehci_set_usbsts(ehci, USBSTS_PSS);
  1800. ehci_set_state(ehci, async, EST_ACTIVE);
  1801. // No break, fall through to ACTIVE
  1802. } else
  1803. break;
  1804. case EST_ACTIVE:
  1805. if ( !(ehci->frindex & 7) && !(ehci->usbcmd & USBCMD_PSE)) {
  1806. ehci_clear_usbsts(ehci, USBSTS_PSS);
  1807. ehci_set_state(ehci, async, EST_INACTIVE);
  1808. break;
  1809. }
  1810. list = ehci->periodiclistbase & 0xfffff000;
  1811. /* check that register has been set */
  1812. if (list == 0) {
  1813. break;
  1814. }
  1815. list |= ((ehci->frindex & 0x1ff8) >> 1);
  1816. pci_dma_read(&ehci->dev, list, (uint8_t *) &entry, sizeof entry);
  1817. entry = le32_to_cpu(entry);
  1818. DPRINTF("PERIODIC state adv fr=%d. [%08X] -> %08X\n",
  1819. ehci->frindex / 8, list, entry);
  1820. ehci_set_fetch_addr(ehci, async,entry);
  1821. ehci_set_state(ehci, async, EST_FETCHENTRY);
  1822. ehci_advance_state(ehci, async);
  1823. break;
  1824. default:
  1825. /* this should only be due to a developer mistake */
  1826. fprintf(stderr, "ehci: Bad periodic state %d. "
  1827. "Resetting to active\n", ehci->pstate);
  1828. assert(0);
  1829. }
  1830. }
  1831. static void ehci_frame_timer(void *opaque)
  1832. {
  1833. EHCIState *ehci = opaque;
  1834. int64_t expire_time, t_now;
  1835. uint64_t ns_elapsed;
  1836. int frames;
  1837. int i;
  1838. int skipped_frames = 0;
  1839. t_now = qemu_get_clock_ns(vm_clock);
  1840. expire_time = t_now + (get_ticks_per_sec() / ehci->freq);
  1841. ns_elapsed = t_now - ehci->last_run_ns;
  1842. frames = ns_elapsed / FRAME_TIMER_NS;
  1843. for (i = 0; i < frames; i++) {
  1844. if ( !(ehci->usbsts & USBSTS_HALT)) {
  1845. if (ehci->isoch_pause <= 0) {
  1846. ehci->frindex += 8;
  1847. }
  1848. if (ehci->frindex > 0x00001fff) {
  1849. ehci->frindex = 0;
  1850. ehci_set_interrupt(ehci, USBSTS_FLR);
  1851. }
  1852. ehci->sofv = (ehci->frindex - 1) >> 3;
  1853. ehci->sofv &= 0x000003ff;
  1854. }
  1855. if (frames - i > ehci->maxframes) {
  1856. skipped_frames++;
  1857. } else {
  1858. ehci_advance_periodic_state(ehci);
  1859. }
  1860. ehci->last_run_ns += FRAME_TIMER_NS;
  1861. }
  1862. #if 0
  1863. if (skipped_frames) {
  1864. DPRINTF("WARNING - EHCI skipped %d frames\n", skipped_frames);
  1865. }
  1866. #endif
  1867. /* Async is not inside loop since it executes everything it can once
  1868. * called
  1869. */
  1870. ehci_advance_async_state(ehci);
  1871. qemu_mod_timer(ehci->frame_timer, expire_time);
  1872. }
  1873. static const MemoryRegionOps ehci_mem_ops = {
  1874. .old_mmio = {
  1875. .read = { ehci_mem_readb, ehci_mem_readw, ehci_mem_readl },
  1876. .write = { ehci_mem_writeb, ehci_mem_writew, ehci_mem_writel },
  1877. },
  1878. .endianness = DEVICE_LITTLE_ENDIAN,
  1879. };
  1880. static int usb_ehci_initfn(PCIDevice *dev);
  1881. static USBPortOps ehci_port_ops = {
  1882. .attach = ehci_attach,
  1883. .detach = ehci_detach,
  1884. .child_detach = ehci_child_detach,
  1885. .wakeup = ehci_wakeup,
  1886. .complete = ehci_async_complete_packet,
  1887. };
  1888. static USBBusOps ehci_bus_ops = {
  1889. .register_companion = ehci_register_companion,
  1890. };
  1891. static const VMStateDescription vmstate_ehci = {
  1892. .name = "ehci",
  1893. .unmigratable = 1,
  1894. };
  1895. static Property ehci_properties[] = {
  1896. DEFINE_PROP_UINT32("freq", EHCIState, freq, FRAME_TIMER_FREQ),
  1897. DEFINE_PROP_UINT32("maxframes", EHCIState, maxframes, 128),
  1898. DEFINE_PROP_END_OF_LIST(),
  1899. };
  1900. static PCIDeviceInfo ehci_info[] = {
  1901. {
  1902. .qdev.name = "usb-ehci",
  1903. .qdev.size = sizeof(EHCIState),
  1904. .qdev.vmsd = &vmstate_ehci,
  1905. .init = usb_ehci_initfn,
  1906. .vendor_id = PCI_VENDOR_ID_INTEL,
  1907. .device_id = PCI_DEVICE_ID_INTEL_82801D, /* ich4 */
  1908. .revision = 0x10,
  1909. .class_id = PCI_CLASS_SERIAL_USB,
  1910. .qdev.props = ehci_properties,
  1911. },{
  1912. .qdev.name = "ich9-usb-ehci1",
  1913. .qdev.size = sizeof(EHCIState),
  1914. .qdev.vmsd = &vmstate_ehci,
  1915. .init = usb_ehci_initfn,
  1916. .vendor_id = PCI_VENDOR_ID_INTEL,
  1917. .device_id = PCI_DEVICE_ID_INTEL_82801I_EHCI1,
  1918. .revision = 0x03,
  1919. .class_id = PCI_CLASS_SERIAL_USB,
  1920. .qdev.props = ehci_properties,
  1921. },{
  1922. /* end of list */
  1923. }
  1924. };
  1925. static int usb_ehci_initfn(PCIDevice *dev)
  1926. {
  1927. EHCIState *s = DO_UPCAST(EHCIState, dev, dev);
  1928. uint8_t *pci_conf = s->dev.config;
  1929. int i;
  1930. pci_set_byte(&pci_conf[PCI_CLASS_PROG], 0x20);
  1931. /* capabilities pointer */
  1932. pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x00);
  1933. //pci_set_byte(&pci_conf[PCI_CAPABILITY_LIST], 0x50);
  1934. pci_set_byte(&pci_conf[PCI_INTERRUPT_PIN], 4); /* interrupt pin D */
  1935. pci_set_byte(&pci_conf[PCI_MIN_GNT], 0);
  1936. pci_set_byte(&pci_conf[PCI_MAX_LAT], 0);
  1937. // pci_conf[0x50] = 0x01; // power management caps
  1938. pci_set_byte(&pci_conf[USB_SBRN], USB_RELEASE_2); // release number (2.1.4)
  1939. pci_set_byte(&pci_conf[0x61], 0x20); // frame length adjustment (2.1.5)
  1940. pci_set_word(&pci_conf[0x62], 0x00); // port wake up capability (2.1.6)
  1941. pci_conf[0x64] = 0x00;
  1942. pci_conf[0x65] = 0x00;
  1943. pci_conf[0x66] = 0x00;
  1944. pci_conf[0x67] = 0x00;
  1945. pci_conf[0x68] = 0x01;
  1946. pci_conf[0x69] = 0x00;
  1947. pci_conf[0x6a] = 0x00;
  1948. pci_conf[0x6b] = 0x00; // USBLEGSUP
  1949. pci_conf[0x6c] = 0x00;
  1950. pci_conf[0x6d] = 0x00;
  1951. pci_conf[0x6e] = 0x00;
  1952. pci_conf[0x6f] = 0xc0; // USBLEFCTLSTS
  1953. // 2.2 host controller interface version
  1954. s->mmio[0x00] = (uint8_t) OPREGBASE;
  1955. s->mmio[0x01] = 0x00;
  1956. s->mmio[0x02] = 0x00;
  1957. s->mmio[0x03] = 0x01; // HC version
  1958. s->mmio[0x04] = NB_PORTS; // Number of downstream ports
  1959. s->mmio[0x05] = 0x00; // No companion ports at present
  1960. s->mmio[0x06] = 0x00;
  1961. s->mmio[0x07] = 0x00;
  1962. s->mmio[0x08] = 0x80; // We can cache whole frame, not 64-bit capable
  1963. s->mmio[0x09] = 0x68; // EECP
  1964. s->mmio[0x0a] = 0x00;
  1965. s->mmio[0x0b] = 0x00;
  1966. s->irq = s->dev.irq[3];
  1967. usb_bus_new(&s->bus, &ehci_bus_ops, &s->dev.qdev);
  1968. for(i = 0; i < NB_PORTS; i++) {
  1969. usb_register_port(&s->bus, &s->ports[i], s, i, &ehci_port_ops,
  1970. USB_SPEED_MASK_HIGH);
  1971. s->ports[i].dev = 0;
  1972. }
  1973. s->frame_timer = qemu_new_timer_ns(vm_clock, ehci_frame_timer, s);
  1974. QTAILQ_INIT(&s->queues);
  1975. qemu_register_reset(ehci_reset, s);
  1976. memory_region_init_io(&s->mem, &ehci_mem_ops, s, "ehci", MMIO_SIZE);
  1977. pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->mem);
  1978. fprintf(stderr, "*** EHCI support is under development ***\n");
  1979. return 0;
  1980. }
  1981. static void ehci_register(void)
  1982. {
  1983. pci_qdev_register_many(ehci_info);
  1984. }
  1985. device_init(ehci_register);
  1986. /*
  1987. * vim: expandtab ts=4
  1988. */