usb-ehci.c 68 KB

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