hcd-ohci.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. * QEMU USB OHCI Emulation
  3. * Copyright (c) 2004 Gianni Tedesco
  4. * Copyright (c) 2006 CodeSourcery
  5. * Copyright (c) 2006 Openedhand Ltd.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * TODO:
  21. * o Isochronous transfers
  22. * o Allocate bandwidth in frames properly
  23. * o Disable timers when nothing needs to be done, or remove timer usage
  24. * all together.
  25. * o BIOS work to boot from USB storage
  26. */
  27. #include "qemu/osdep.h"
  28. #include "hw/irq.h"
  29. #include "qapi/error.h"
  30. #include "qemu/module.h"
  31. #include "qemu/timer.h"
  32. #include "hw/usb.h"
  33. #include "migration/vmstate.h"
  34. #include "hw/sysbus.h"
  35. #include "hw/qdev-dma.h"
  36. #include "hw/qdev-properties.h"
  37. #include "trace.h"
  38. #include "hcd-ohci.h"
  39. /* This causes frames to occur 1000x slower */
  40. //#define OHCI_TIME_WARP 1
  41. #define ED_LINK_LIMIT 32
  42. static int64_t usb_frame_time;
  43. static int64_t usb_bit_time;
  44. /* Host Controller Communications Area */
  45. struct ohci_hcca {
  46. uint32_t intr[32];
  47. uint16_t frame, pad;
  48. uint32_t done;
  49. };
  50. #define HCCA_WRITEBACK_OFFSET offsetof(struct ohci_hcca, frame)
  51. #define HCCA_WRITEBACK_SIZE 8 /* frame, pad, done */
  52. #define ED_WBACK_OFFSET offsetof(struct ohci_ed, head)
  53. #define ED_WBACK_SIZE 4
  54. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev);
  55. /* Bitfields for the first word of an Endpoint Desciptor. */
  56. #define OHCI_ED_FA_SHIFT 0
  57. #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
  58. #define OHCI_ED_EN_SHIFT 7
  59. #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
  60. #define OHCI_ED_D_SHIFT 11
  61. #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
  62. #define OHCI_ED_S (1<<13)
  63. #define OHCI_ED_K (1<<14)
  64. #define OHCI_ED_F (1<<15)
  65. #define OHCI_ED_MPS_SHIFT 16
  66. #define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
  67. /* Flags in the head field of an Endpoint Desciptor. */
  68. #define OHCI_ED_H 1
  69. #define OHCI_ED_C 2
  70. /* Bitfields for the first word of a Transfer Desciptor. */
  71. #define OHCI_TD_R (1<<18)
  72. #define OHCI_TD_DP_SHIFT 19
  73. #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
  74. #define OHCI_TD_DI_SHIFT 21
  75. #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
  76. #define OHCI_TD_T0 (1<<24)
  77. #define OHCI_TD_T1 (1<<25)
  78. #define OHCI_TD_EC_SHIFT 26
  79. #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
  80. #define OHCI_TD_CC_SHIFT 28
  81. #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
  82. /* Bitfields for the first word of an Isochronous Transfer Desciptor. */
  83. /* CC & DI - same as in the General Transfer Desciptor */
  84. #define OHCI_TD_SF_SHIFT 0
  85. #define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
  86. #define OHCI_TD_FC_SHIFT 24
  87. #define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
  88. /* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
  89. #define OHCI_TD_PSW_CC_SHIFT 12
  90. #define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
  91. #define OHCI_TD_PSW_SIZE_SHIFT 0
  92. #define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
  93. #define OHCI_PAGE_MASK 0xfffff000
  94. #define OHCI_OFFSET_MASK 0xfff
  95. #define OHCI_DPTR_MASK 0xfffffff0
  96. #define OHCI_BM(val, field) \
  97. (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
  98. #define OHCI_SET_BM(val, field, newval) do { \
  99. val &= ~OHCI_##field##_MASK; \
  100. val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
  101. } while(0)
  102. /* endpoint descriptor */
  103. struct ohci_ed {
  104. uint32_t flags;
  105. uint32_t tail;
  106. uint32_t head;
  107. uint32_t next;
  108. };
  109. /* General transfer descriptor */
  110. struct ohci_td {
  111. uint32_t flags;
  112. uint32_t cbp;
  113. uint32_t next;
  114. uint32_t be;
  115. };
  116. /* Isochronous transfer descriptor */
  117. struct ohci_iso_td {
  118. uint32_t flags;
  119. uint32_t bp;
  120. uint32_t next;
  121. uint32_t be;
  122. uint16_t offset[8];
  123. };
  124. #define USB_HZ 12000000
  125. /* OHCI Local stuff */
  126. #define OHCI_CTL_CBSR ((1<<0)|(1<<1))
  127. #define OHCI_CTL_PLE (1<<2)
  128. #define OHCI_CTL_IE (1<<3)
  129. #define OHCI_CTL_CLE (1<<4)
  130. #define OHCI_CTL_BLE (1<<5)
  131. #define OHCI_CTL_HCFS ((1<<6)|(1<<7))
  132. #define OHCI_USB_RESET 0x00
  133. #define OHCI_USB_RESUME 0x40
  134. #define OHCI_USB_OPERATIONAL 0x80
  135. #define OHCI_USB_SUSPEND 0xc0
  136. #define OHCI_CTL_IR (1<<8)
  137. #define OHCI_CTL_RWC (1<<9)
  138. #define OHCI_CTL_RWE (1<<10)
  139. #define OHCI_STATUS_HCR (1<<0)
  140. #define OHCI_STATUS_CLF (1<<1)
  141. #define OHCI_STATUS_BLF (1<<2)
  142. #define OHCI_STATUS_OCR (1<<3)
  143. #define OHCI_STATUS_SOC ((1<<6)|(1<<7))
  144. #define OHCI_INTR_SO (1U<<0) /* Scheduling overrun */
  145. #define OHCI_INTR_WD (1U<<1) /* HcDoneHead writeback */
  146. #define OHCI_INTR_SF (1U<<2) /* Start of frame */
  147. #define OHCI_INTR_RD (1U<<3) /* Resume detect */
  148. #define OHCI_INTR_UE (1U<<4) /* Unrecoverable error */
  149. #define OHCI_INTR_FNO (1U<<5) /* Frame number overflow */
  150. #define OHCI_INTR_RHSC (1U<<6) /* Root hub status change */
  151. #define OHCI_INTR_OC (1U<<30) /* Ownership change */
  152. #define OHCI_INTR_MIE (1U<<31) /* Master Interrupt Enable */
  153. #define OHCI_HCCA_SIZE 0x100
  154. #define OHCI_HCCA_MASK 0xffffff00
  155. #define OHCI_EDPTR_MASK 0xfffffff0
  156. #define OHCI_FMI_FI 0x00003fff
  157. #define OHCI_FMI_FSMPS 0xffff0000
  158. #define OHCI_FMI_FIT 0x80000000
  159. #define OHCI_FR_RT (1U<<31)
  160. #define OHCI_LS_THRESH 0x628
  161. #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
  162. #define OHCI_RHA_PSM (1<<8)
  163. #define OHCI_RHA_NPS (1<<9)
  164. #define OHCI_RHA_DT (1<<10)
  165. #define OHCI_RHA_OCPM (1<<11)
  166. #define OHCI_RHA_NOCP (1<<12)
  167. #define OHCI_RHA_POTPGT_MASK 0xff000000
  168. #define OHCI_RHS_LPS (1U<<0)
  169. #define OHCI_RHS_OCI (1U<<1)
  170. #define OHCI_RHS_DRWE (1U<<15)
  171. #define OHCI_RHS_LPSC (1U<<16)
  172. #define OHCI_RHS_OCIC (1U<<17)
  173. #define OHCI_RHS_CRWE (1U<<31)
  174. #define OHCI_PORT_CCS (1<<0)
  175. #define OHCI_PORT_PES (1<<1)
  176. #define OHCI_PORT_PSS (1<<2)
  177. #define OHCI_PORT_POCI (1<<3)
  178. #define OHCI_PORT_PRS (1<<4)
  179. #define OHCI_PORT_PPS (1<<8)
  180. #define OHCI_PORT_LSDA (1<<9)
  181. #define OHCI_PORT_CSC (1<<16)
  182. #define OHCI_PORT_PESC (1<<17)
  183. #define OHCI_PORT_PSSC (1<<18)
  184. #define OHCI_PORT_OCIC (1<<19)
  185. #define OHCI_PORT_PRSC (1<<20)
  186. #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
  187. |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
  188. #define OHCI_TD_DIR_SETUP 0x0
  189. #define OHCI_TD_DIR_OUT 0x1
  190. #define OHCI_TD_DIR_IN 0x2
  191. #define OHCI_TD_DIR_RESERVED 0x3
  192. #define OHCI_CC_NOERROR 0x0
  193. #define OHCI_CC_CRC 0x1
  194. #define OHCI_CC_BITSTUFFING 0x2
  195. #define OHCI_CC_DATATOGGLEMISMATCH 0x3
  196. #define OHCI_CC_STALL 0x4
  197. #define OHCI_CC_DEVICENOTRESPONDING 0x5
  198. #define OHCI_CC_PIDCHECKFAILURE 0x6
  199. #define OHCI_CC_UNDEXPETEDPID 0x7
  200. #define OHCI_CC_DATAOVERRUN 0x8
  201. #define OHCI_CC_DATAUNDERRUN 0x9
  202. #define OHCI_CC_BUFFEROVERRUN 0xc
  203. #define OHCI_CC_BUFFERUNDERRUN 0xd
  204. #define OHCI_HRESET_FSBIR (1 << 0)
  205. static void ohci_die(OHCIState *ohci)
  206. {
  207. ohci->ohci_die(ohci);
  208. }
  209. /* Update IRQ levels */
  210. static inline void ohci_intr_update(OHCIState *ohci)
  211. {
  212. int level = 0;
  213. if ((ohci->intr & OHCI_INTR_MIE) &&
  214. (ohci->intr_status & ohci->intr))
  215. level = 1;
  216. qemu_set_irq(ohci->irq, level);
  217. }
  218. /* Set an interrupt */
  219. static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr)
  220. {
  221. ohci->intr_status |= intr;
  222. ohci_intr_update(ohci);
  223. }
  224. /* Attach or detach a device on a root hub port. */
  225. static void ohci_attach(USBPort *port1)
  226. {
  227. OHCIState *s = port1->opaque;
  228. OHCIPort *port = &s->rhport[port1->index];
  229. uint32_t old_state = port->ctrl;
  230. /* set connect status */
  231. port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC;
  232. /* update speed */
  233. if (port->port.dev->speed == USB_SPEED_LOW) {
  234. port->ctrl |= OHCI_PORT_LSDA;
  235. } else {
  236. port->ctrl &= ~OHCI_PORT_LSDA;
  237. }
  238. /* notify of remote-wakeup */
  239. if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
  240. ohci_set_interrupt(s, OHCI_INTR_RD);
  241. }
  242. trace_usb_ohci_port_attach(port1->index);
  243. if (old_state != port->ctrl) {
  244. ohci_set_interrupt(s, OHCI_INTR_RHSC);
  245. }
  246. }
  247. static void ohci_detach(USBPort *port1)
  248. {
  249. OHCIState *s = port1->opaque;
  250. OHCIPort *port = &s->rhport[port1->index];
  251. uint32_t old_state = port->ctrl;
  252. ohci_async_cancel_device(s, port1->dev);
  253. /* set connect status */
  254. if (port->ctrl & OHCI_PORT_CCS) {
  255. port->ctrl &= ~OHCI_PORT_CCS;
  256. port->ctrl |= OHCI_PORT_CSC;
  257. }
  258. /* disable port */
  259. if (port->ctrl & OHCI_PORT_PES) {
  260. port->ctrl &= ~OHCI_PORT_PES;
  261. port->ctrl |= OHCI_PORT_PESC;
  262. }
  263. trace_usb_ohci_port_detach(port1->index);
  264. if (old_state != port->ctrl) {
  265. ohci_set_interrupt(s, OHCI_INTR_RHSC);
  266. }
  267. }
  268. static void ohci_wakeup(USBPort *port1)
  269. {
  270. OHCIState *s = port1->opaque;
  271. OHCIPort *port = &s->rhport[port1->index];
  272. uint32_t intr = 0;
  273. if (port->ctrl & OHCI_PORT_PSS) {
  274. trace_usb_ohci_port_wakeup(port1->index);
  275. port->ctrl |= OHCI_PORT_PSSC;
  276. port->ctrl &= ~OHCI_PORT_PSS;
  277. intr = OHCI_INTR_RHSC;
  278. }
  279. /* Note that the controller can be suspended even if this port is not */
  280. if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
  281. trace_usb_ohci_remote_wakeup(s->name);
  282. /* This is the one state transition the controller can do by itself */
  283. s->ctl &= ~OHCI_CTL_HCFS;
  284. s->ctl |= OHCI_USB_RESUME;
  285. /* In suspend mode only ResumeDetected is possible, not RHSC:
  286. * see the OHCI spec 5.1.2.3.
  287. */
  288. intr = OHCI_INTR_RD;
  289. }
  290. ohci_set_interrupt(s, intr);
  291. }
  292. static void ohci_child_detach(USBPort *port1, USBDevice *child)
  293. {
  294. OHCIState *s = port1->opaque;
  295. ohci_async_cancel_device(s, child);
  296. }
  297. static USBDevice *ohci_find_device(OHCIState *ohci, uint8_t addr)
  298. {
  299. USBDevice *dev;
  300. int i;
  301. for (i = 0; i < ohci->num_ports; i++) {
  302. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) {
  303. continue;
  304. }
  305. dev = usb_find_device(&ohci->rhport[i].port, addr);
  306. if (dev != NULL) {
  307. return dev;
  308. }
  309. }
  310. return NULL;
  311. }
  312. void ohci_stop_endpoints(OHCIState *ohci)
  313. {
  314. USBDevice *dev;
  315. int i, j;
  316. for (i = 0; i < ohci->num_ports; i++) {
  317. dev = ohci->rhport[i].port.dev;
  318. if (dev && dev->attached) {
  319. usb_device_ep_stopped(dev, &dev->ep_ctl);
  320. for (j = 0; j < USB_MAX_ENDPOINTS; j++) {
  321. usb_device_ep_stopped(dev, &dev->ep_in[j]);
  322. usb_device_ep_stopped(dev, &dev->ep_out[j]);
  323. }
  324. }
  325. }
  326. }
  327. static void ohci_roothub_reset(OHCIState *ohci)
  328. {
  329. OHCIPort *port;
  330. int i;
  331. ohci_bus_stop(ohci);
  332. ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
  333. ohci->rhdesc_b = 0x0; /* Impl. specific */
  334. ohci->rhstatus = 0;
  335. for (i = 0; i < ohci->num_ports; i++) {
  336. port = &ohci->rhport[i];
  337. port->ctrl = 0;
  338. if (port->port.dev && port->port.dev->attached) {
  339. usb_port_reset(&port->port);
  340. }
  341. }
  342. if (ohci->async_td) {
  343. usb_cancel_packet(&ohci->usb_packet);
  344. ohci->async_td = 0;
  345. }
  346. ohci_stop_endpoints(ohci);
  347. }
  348. /* Reset the controller */
  349. static void ohci_soft_reset(OHCIState *ohci)
  350. {
  351. trace_usb_ohci_reset(ohci->name);
  352. ohci_bus_stop(ohci);
  353. ohci->ctl = (ohci->ctl & OHCI_CTL_IR) | OHCI_USB_SUSPEND;
  354. ohci->old_ctl = 0;
  355. ohci->status = 0;
  356. ohci->intr_status = 0;
  357. ohci->intr = OHCI_INTR_MIE;
  358. ohci->hcca = 0;
  359. ohci->ctrl_head = ohci->ctrl_cur = 0;
  360. ohci->bulk_head = ohci->bulk_cur = 0;
  361. ohci->per_cur = 0;
  362. ohci->done = 0;
  363. ohci->done_count = 7;
  364. /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
  365. * I took the value linux sets ...
  366. */
  367. ohci->fsmps = 0x2778;
  368. ohci->fi = 0x2edf;
  369. ohci->fit = 0;
  370. ohci->frt = 0;
  371. ohci->frame_number = 0;
  372. ohci->pstart = 0;
  373. ohci->lst = OHCI_LS_THRESH;
  374. }
  375. void ohci_hard_reset(OHCIState *ohci)
  376. {
  377. ohci_soft_reset(ohci);
  378. ohci->ctl = 0;
  379. ohci_roothub_reset(ohci);
  380. }
  381. /* Get an array of dwords from main memory */
  382. static inline int get_dwords(OHCIState *ohci,
  383. dma_addr_t addr, uint32_t *buf, int num)
  384. {
  385. int i;
  386. addr += ohci->localmem_base;
  387. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  388. if (dma_memory_read(ohci->as, addr, buf, sizeof(*buf))) {
  389. return -1;
  390. }
  391. *buf = le32_to_cpu(*buf);
  392. }
  393. return 0;
  394. }
  395. /* Put an array of dwords in to main memory */
  396. static inline int put_dwords(OHCIState *ohci,
  397. dma_addr_t addr, uint32_t *buf, int num)
  398. {
  399. int i;
  400. addr += ohci->localmem_base;
  401. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  402. uint32_t tmp = cpu_to_le32(*buf);
  403. if (dma_memory_write(ohci->as, addr, &tmp, sizeof(tmp))) {
  404. return -1;
  405. }
  406. }
  407. return 0;
  408. }
  409. /* Get an array of words from main memory */
  410. static inline int get_words(OHCIState *ohci,
  411. dma_addr_t addr, uint16_t *buf, int num)
  412. {
  413. int i;
  414. addr += ohci->localmem_base;
  415. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  416. if (dma_memory_read(ohci->as, addr, buf, sizeof(*buf))) {
  417. return -1;
  418. }
  419. *buf = le16_to_cpu(*buf);
  420. }
  421. return 0;
  422. }
  423. /* Put an array of words in to main memory */
  424. static inline int put_words(OHCIState *ohci,
  425. dma_addr_t addr, uint16_t *buf, int num)
  426. {
  427. int i;
  428. addr += ohci->localmem_base;
  429. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  430. uint16_t tmp = cpu_to_le16(*buf);
  431. if (dma_memory_write(ohci->as, addr, &tmp, sizeof(tmp))) {
  432. return -1;
  433. }
  434. }
  435. return 0;
  436. }
  437. static inline int ohci_read_ed(OHCIState *ohci,
  438. dma_addr_t addr, struct ohci_ed *ed)
  439. {
  440. return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
  441. }
  442. static inline int ohci_read_td(OHCIState *ohci,
  443. dma_addr_t addr, struct ohci_td *td)
  444. {
  445. return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
  446. }
  447. static inline int ohci_read_iso_td(OHCIState *ohci,
  448. dma_addr_t addr, struct ohci_iso_td *td)
  449. {
  450. return get_dwords(ohci, addr, (uint32_t *)td, 4) ||
  451. get_words(ohci, addr + 16, td->offset, 8);
  452. }
  453. static inline int ohci_read_hcca(OHCIState *ohci,
  454. dma_addr_t addr, struct ohci_hcca *hcca)
  455. {
  456. return dma_memory_read(ohci->as, addr + ohci->localmem_base,
  457. hcca, sizeof(*hcca));
  458. }
  459. static inline int ohci_put_ed(OHCIState *ohci,
  460. dma_addr_t addr, struct ohci_ed *ed)
  461. {
  462. /* ed->tail is under control of the HCD.
  463. * Since just ed->head is changed by HC, just write back this
  464. */
  465. return put_dwords(ohci, addr + ED_WBACK_OFFSET,
  466. (uint32_t *)((char *)ed + ED_WBACK_OFFSET),
  467. ED_WBACK_SIZE >> 2);
  468. }
  469. static inline int ohci_put_td(OHCIState *ohci,
  470. dma_addr_t addr, struct ohci_td *td)
  471. {
  472. return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
  473. }
  474. static inline int ohci_put_iso_td(OHCIState *ohci,
  475. dma_addr_t addr, struct ohci_iso_td *td)
  476. {
  477. return put_dwords(ohci, addr, (uint32_t *)td, 4) ||
  478. put_words(ohci, addr + 16, td->offset, 8);
  479. }
  480. static inline int ohci_put_hcca(OHCIState *ohci,
  481. dma_addr_t addr, struct ohci_hcca *hcca)
  482. {
  483. return dma_memory_write(ohci->as,
  484. addr + ohci->localmem_base + HCCA_WRITEBACK_OFFSET,
  485. (char *)hcca + HCCA_WRITEBACK_OFFSET,
  486. HCCA_WRITEBACK_SIZE);
  487. }
  488. /* Read/Write the contents of a TD from/to main memory. */
  489. static int ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
  490. uint8_t *buf, int len, DMADirection dir)
  491. {
  492. dma_addr_t ptr, n;
  493. ptr = td->cbp;
  494. n = 0x1000 - (ptr & 0xfff);
  495. if (n > len)
  496. n = len;
  497. if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, n, dir)) {
  498. return -1;
  499. }
  500. if (n == len) {
  501. return 0;
  502. }
  503. ptr = td->be & ~0xfffu;
  504. buf += n;
  505. if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
  506. len - n, dir)) {
  507. return -1;
  508. }
  509. return 0;
  510. }
  511. /* Read/Write the contents of an ISO TD from/to main memory. */
  512. static int ohci_copy_iso_td(OHCIState *ohci,
  513. uint32_t start_addr, uint32_t end_addr,
  514. uint8_t *buf, int len, DMADirection dir)
  515. {
  516. dma_addr_t ptr, n;
  517. ptr = start_addr;
  518. n = 0x1000 - (ptr & 0xfff);
  519. if (n > len)
  520. n = len;
  521. if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf, n, dir)) {
  522. return -1;
  523. }
  524. if (n == len) {
  525. return 0;
  526. }
  527. ptr = end_addr & ~0xfffu;
  528. buf += n;
  529. if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
  530. len - n, dir)) {
  531. return -1;
  532. }
  533. return 0;
  534. }
  535. static void ohci_process_lists(OHCIState *ohci, int completion);
  536. static void ohci_async_complete_packet(USBPort *port, USBPacket *packet)
  537. {
  538. OHCIState *ohci = container_of(packet, OHCIState, usb_packet);
  539. trace_usb_ohci_async_complete();
  540. ohci->async_complete = true;
  541. ohci_process_lists(ohci, 1);
  542. }
  543. #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
  544. static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
  545. int completion)
  546. {
  547. int dir;
  548. size_t len = 0;
  549. const char *str = NULL;
  550. int pid;
  551. int ret;
  552. int i;
  553. USBDevice *dev;
  554. USBEndpoint *ep;
  555. struct ohci_iso_td iso_td;
  556. uint32_t addr;
  557. uint16_t starting_frame;
  558. int16_t relative_frame_number;
  559. int frame_count;
  560. uint32_t start_offset, next_offset, end_offset = 0;
  561. uint32_t start_addr, end_addr;
  562. addr = ed->head & OHCI_DPTR_MASK;
  563. if (ohci_read_iso_td(ohci, addr, &iso_td)) {
  564. trace_usb_ohci_iso_td_read_failed(addr);
  565. ohci_die(ohci);
  566. return 1;
  567. }
  568. starting_frame = OHCI_BM(iso_td.flags, TD_SF);
  569. frame_count = OHCI_BM(iso_td.flags, TD_FC);
  570. relative_frame_number = USUB(ohci->frame_number, starting_frame);
  571. trace_usb_ohci_iso_td_head(
  572. ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK,
  573. iso_td.flags, iso_td.bp, iso_td.next, iso_td.be,
  574. ohci->frame_number, starting_frame,
  575. frame_count, relative_frame_number);
  576. trace_usb_ohci_iso_td_head_offset(
  577. iso_td.offset[0], iso_td.offset[1],
  578. iso_td.offset[2], iso_td.offset[3],
  579. iso_td.offset[4], iso_td.offset[5],
  580. iso_td.offset[6], iso_td.offset[7]);
  581. if (relative_frame_number < 0) {
  582. trace_usb_ohci_iso_td_relative_frame_number_neg(relative_frame_number);
  583. return 1;
  584. } else if (relative_frame_number > frame_count) {
  585. /* ISO TD expired - retire the TD to the Done Queue and continue with
  586. the next ISO TD of the same ED */
  587. trace_usb_ohci_iso_td_relative_frame_number_big(relative_frame_number,
  588. frame_count);
  589. if (OHCI_CC_DATAOVERRUN == OHCI_BM(iso_td.flags, TD_CC)) {
  590. /* avoid infinite loop */
  591. return 1;
  592. }
  593. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  594. ed->head &= ~OHCI_DPTR_MASK;
  595. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  596. iso_td.next = ohci->done;
  597. ohci->done = addr;
  598. i = OHCI_BM(iso_td.flags, TD_DI);
  599. if (i < ohci->done_count)
  600. ohci->done_count = i;
  601. if (ohci_put_iso_td(ohci, addr, &iso_td)) {
  602. ohci_die(ohci);
  603. return 1;
  604. }
  605. return 0;
  606. }
  607. dir = OHCI_BM(ed->flags, ED_D);
  608. switch (dir) {
  609. case OHCI_TD_DIR_IN:
  610. str = "in";
  611. pid = USB_TOKEN_IN;
  612. break;
  613. case OHCI_TD_DIR_OUT:
  614. str = "out";
  615. pid = USB_TOKEN_OUT;
  616. break;
  617. case OHCI_TD_DIR_SETUP:
  618. str = "setup";
  619. pid = USB_TOKEN_SETUP;
  620. break;
  621. default:
  622. trace_usb_ohci_iso_td_bad_direction(dir);
  623. return 1;
  624. }
  625. if (!iso_td.bp || !iso_td.be) {
  626. trace_usb_ohci_iso_td_bad_bp_be(iso_td.bp, iso_td.be);
  627. return 1;
  628. }
  629. start_offset = iso_td.offset[relative_frame_number];
  630. if (relative_frame_number < frame_count) {
  631. next_offset = iso_td.offset[relative_frame_number + 1];
  632. } else {
  633. next_offset = iso_td.be;
  634. }
  635. if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
  636. ((relative_frame_number < frame_count) &&
  637. !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
  638. trace_usb_ohci_iso_td_bad_cc_not_accessed(start_offset, next_offset);
  639. return 1;
  640. }
  641. if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
  642. trace_usb_ohci_iso_td_bad_cc_overrun(start_offset, next_offset);
  643. return 1;
  644. }
  645. if ((start_offset & 0x1000) == 0) {
  646. start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  647. (start_offset & OHCI_OFFSET_MASK);
  648. } else {
  649. start_addr = (iso_td.be & OHCI_PAGE_MASK) |
  650. (start_offset & OHCI_OFFSET_MASK);
  651. }
  652. if (relative_frame_number < frame_count) {
  653. end_offset = next_offset - 1;
  654. if ((end_offset & 0x1000) == 0) {
  655. end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  656. (end_offset & OHCI_OFFSET_MASK);
  657. } else {
  658. end_addr = (iso_td.be & OHCI_PAGE_MASK) |
  659. (end_offset & OHCI_OFFSET_MASK);
  660. }
  661. } else {
  662. /* Last packet in the ISO TD */
  663. end_addr = next_offset;
  664. }
  665. if (start_addr > end_addr) {
  666. trace_usb_ohci_iso_td_bad_cc_overrun(start_addr, end_addr);
  667. return 1;
  668. }
  669. if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
  670. len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
  671. - (start_addr & OHCI_OFFSET_MASK);
  672. } else {
  673. len = end_addr - start_addr + 1;
  674. }
  675. if (len > sizeof(ohci->usb_buf)) {
  676. len = sizeof(ohci->usb_buf);
  677. }
  678. if (len && dir != OHCI_TD_DIR_IN) {
  679. if (ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len,
  680. DMA_DIRECTION_TO_DEVICE)) {
  681. ohci_die(ohci);
  682. return 1;
  683. }
  684. }
  685. if (!completion) {
  686. bool int_req = relative_frame_number == frame_count &&
  687. OHCI_BM(iso_td.flags, TD_DI) == 0;
  688. dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
  689. if (dev == NULL) {
  690. trace_usb_ohci_td_dev_error();
  691. return 1;
  692. }
  693. ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
  694. usb_packet_setup(&ohci->usb_packet, pid, ep, 0, addr, false, int_req);
  695. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, len);
  696. usb_handle_packet(dev, &ohci->usb_packet);
  697. if (ohci->usb_packet.status == USB_RET_ASYNC) {
  698. usb_device_flush_ep_queue(dev, ep);
  699. return 1;
  700. }
  701. }
  702. if (ohci->usb_packet.status == USB_RET_SUCCESS) {
  703. ret = ohci->usb_packet.actual_length;
  704. } else {
  705. ret = ohci->usb_packet.status;
  706. }
  707. trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr,
  708. str, len, ret);
  709. /* Writeback */
  710. if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
  711. /* IN transfer succeeded */
  712. if (ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret,
  713. DMA_DIRECTION_FROM_DEVICE)) {
  714. ohci_die(ohci);
  715. return 1;
  716. }
  717. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  718. OHCI_CC_NOERROR);
  719. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
  720. } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
  721. /* OUT transfer succeeded */
  722. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  723. OHCI_CC_NOERROR);
  724. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
  725. } else {
  726. if (ret > (ssize_t) len) {
  727. trace_usb_ohci_iso_td_data_overrun(ret, len);
  728. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  729. OHCI_CC_DATAOVERRUN);
  730. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  731. len);
  732. } else if (ret >= 0) {
  733. trace_usb_ohci_iso_td_data_underrun(ret);
  734. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  735. OHCI_CC_DATAUNDERRUN);
  736. } else {
  737. switch (ret) {
  738. case USB_RET_IOERROR:
  739. case USB_RET_NODEV:
  740. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  741. OHCI_CC_DEVICENOTRESPONDING);
  742. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  743. 0);
  744. break;
  745. case USB_RET_NAK:
  746. case USB_RET_STALL:
  747. trace_usb_ohci_iso_td_nak(ret);
  748. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  749. OHCI_CC_STALL);
  750. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  751. 0);
  752. break;
  753. default:
  754. trace_usb_ohci_iso_td_bad_response(ret);
  755. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  756. OHCI_CC_UNDEXPETEDPID);
  757. break;
  758. }
  759. }
  760. }
  761. if (relative_frame_number == frame_count) {
  762. /* Last data packet of ISO TD - retire the TD to the Done Queue */
  763. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
  764. ed->head &= ~OHCI_DPTR_MASK;
  765. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  766. iso_td.next = ohci->done;
  767. ohci->done = addr;
  768. i = OHCI_BM(iso_td.flags, TD_DI);
  769. if (i < ohci->done_count)
  770. ohci->done_count = i;
  771. }
  772. if (ohci_put_iso_td(ohci, addr, &iso_td)) {
  773. ohci_die(ohci);
  774. }
  775. return 1;
  776. }
  777. static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
  778. {
  779. bool print16;
  780. bool printall;
  781. const int width = 16;
  782. int i;
  783. char tmp[3 * width + 1];
  784. char *p = tmp;
  785. print16 = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_SHORT);
  786. printall = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_FULL);
  787. if (!printall && !print16) {
  788. return;
  789. }
  790. for (i = 0; ; i++) {
  791. if (i && (!(i % width) || (i == len))) {
  792. if (!printall) {
  793. trace_usb_ohci_td_pkt_short(msg, tmp);
  794. break;
  795. }
  796. trace_usb_ohci_td_pkt_full(msg, tmp);
  797. p = tmp;
  798. *p = 0;
  799. }
  800. if (i == len) {
  801. break;
  802. }
  803. p += sprintf(p, " %.2x", buf[i]);
  804. }
  805. }
  806. /* Service a transport descriptor.
  807. Returns nonzero to terminate processing of this endpoint. */
  808. static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
  809. {
  810. int dir;
  811. size_t len = 0, pktlen = 0;
  812. const char *str = NULL;
  813. int pid;
  814. int ret;
  815. int i;
  816. USBDevice *dev;
  817. USBEndpoint *ep;
  818. struct ohci_td td;
  819. uint32_t addr;
  820. int flag_r;
  821. int completion;
  822. addr = ed->head & OHCI_DPTR_MASK;
  823. /* See if this TD has already been submitted to the device. */
  824. completion = (addr == ohci->async_td);
  825. if (completion && !ohci->async_complete) {
  826. trace_usb_ohci_td_skip_async();
  827. return 1;
  828. }
  829. if (ohci_read_td(ohci, addr, &td)) {
  830. trace_usb_ohci_td_read_error(addr);
  831. ohci_die(ohci);
  832. return 1;
  833. }
  834. dir = OHCI_BM(ed->flags, ED_D);
  835. switch (dir) {
  836. case OHCI_TD_DIR_OUT:
  837. case OHCI_TD_DIR_IN:
  838. /* Same value. */
  839. break;
  840. default:
  841. dir = OHCI_BM(td.flags, TD_DP);
  842. break;
  843. }
  844. switch (dir) {
  845. case OHCI_TD_DIR_IN:
  846. str = "in";
  847. pid = USB_TOKEN_IN;
  848. break;
  849. case OHCI_TD_DIR_OUT:
  850. str = "out";
  851. pid = USB_TOKEN_OUT;
  852. break;
  853. case OHCI_TD_DIR_SETUP:
  854. str = "setup";
  855. pid = USB_TOKEN_SETUP;
  856. break;
  857. default:
  858. trace_usb_ohci_td_bad_direction(dir);
  859. return 1;
  860. }
  861. if (td.cbp && td.be) {
  862. if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
  863. len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
  864. } else {
  865. if (td.cbp > td.be) {
  866. trace_usb_ohci_iso_td_bad_cc_overrun(td.cbp, td.be);
  867. ohci_die(ohci);
  868. return 1;
  869. }
  870. len = (td.be - td.cbp) + 1;
  871. }
  872. if (len > sizeof(ohci->usb_buf)) {
  873. len = sizeof(ohci->usb_buf);
  874. }
  875. pktlen = len;
  876. if (len && dir != OHCI_TD_DIR_IN) {
  877. /* The endpoint may not allow us to transfer it all now */
  878. pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
  879. if (pktlen > len) {
  880. pktlen = len;
  881. }
  882. if (!completion) {
  883. if (ohci_copy_td(ohci, &td, ohci->usb_buf, pktlen,
  884. DMA_DIRECTION_TO_DEVICE)) {
  885. ohci_die(ohci);
  886. }
  887. }
  888. }
  889. }
  890. flag_r = (td.flags & OHCI_TD_R) != 0;
  891. trace_usb_ohci_td_pkt_hdr(addr, (int64_t)pktlen, (int64_t)len, str,
  892. flag_r, td.cbp, td.be);
  893. ohci_td_pkt("OUT", ohci->usb_buf, pktlen);
  894. if (completion) {
  895. ohci->async_td = 0;
  896. ohci->async_complete = false;
  897. } else {
  898. if (ohci->async_td) {
  899. /* ??? The hardware should allow one active packet per
  900. endpoint. We only allow one active packet per controller.
  901. This should be sufficient as long as devices respond in a
  902. timely manner.
  903. */
  904. trace_usb_ohci_td_too_many_pending();
  905. return 1;
  906. }
  907. dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
  908. if (dev == NULL) {
  909. trace_usb_ohci_td_dev_error();
  910. return 1;
  911. }
  912. ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
  913. usb_packet_setup(&ohci->usb_packet, pid, ep, 0, addr, !flag_r,
  914. OHCI_BM(td.flags, TD_DI) == 0);
  915. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, pktlen);
  916. usb_handle_packet(dev, &ohci->usb_packet);
  917. trace_usb_ohci_td_packet_status(ohci->usb_packet.status);
  918. if (ohci->usb_packet.status == USB_RET_ASYNC) {
  919. usb_device_flush_ep_queue(dev, ep);
  920. ohci->async_td = addr;
  921. return 1;
  922. }
  923. }
  924. if (ohci->usb_packet.status == USB_RET_SUCCESS) {
  925. ret = ohci->usb_packet.actual_length;
  926. } else {
  927. ret = ohci->usb_packet.status;
  928. }
  929. if (ret >= 0) {
  930. if (dir == OHCI_TD_DIR_IN) {
  931. if (ohci_copy_td(ohci, &td, ohci->usb_buf, ret,
  932. DMA_DIRECTION_FROM_DEVICE)) {
  933. ohci_die(ohci);
  934. }
  935. ohci_td_pkt("IN", ohci->usb_buf, pktlen);
  936. } else {
  937. ret = pktlen;
  938. }
  939. }
  940. /* Writeback */
  941. if (ret == pktlen || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
  942. /* Transmission succeeded. */
  943. if (ret == len) {
  944. td.cbp = 0;
  945. } else {
  946. if ((td.cbp & 0xfff) + ret > 0xfff) {
  947. td.cbp = (td.be & ~0xfff) + ((td.cbp + ret) & 0xfff);
  948. } else {
  949. td.cbp += ret;
  950. }
  951. }
  952. td.flags |= OHCI_TD_T1;
  953. td.flags ^= OHCI_TD_T0;
  954. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
  955. OHCI_SET_BM(td.flags, TD_EC, 0);
  956. if ((dir != OHCI_TD_DIR_IN) && (ret != len)) {
  957. /* Partial packet transfer: TD not ready to retire yet */
  958. goto exit_no_retire;
  959. }
  960. /* Setting ED_C is part of the TD retirement process */
  961. ed->head &= ~OHCI_ED_C;
  962. if (td.flags & OHCI_TD_T0)
  963. ed->head |= OHCI_ED_C;
  964. } else {
  965. if (ret >= 0) {
  966. trace_usb_ohci_td_underrun();
  967. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
  968. } else {
  969. switch (ret) {
  970. case USB_RET_IOERROR:
  971. case USB_RET_NODEV:
  972. trace_usb_ohci_td_dev_error();
  973. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
  974. break;
  975. case USB_RET_NAK:
  976. trace_usb_ohci_td_nak();
  977. return 1;
  978. case USB_RET_STALL:
  979. trace_usb_ohci_td_stall();
  980. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
  981. break;
  982. case USB_RET_BABBLE:
  983. trace_usb_ohci_td_babble();
  984. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  985. break;
  986. default:
  987. trace_usb_ohci_td_bad_device_response(ret);
  988. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
  989. OHCI_SET_BM(td.flags, TD_EC, 3);
  990. break;
  991. }
  992. /* An error occured so we have to clear the interrupt counter. See
  993. * spec at 6.4.4 on page 104 */
  994. ohci->done_count = 0;
  995. }
  996. ed->head |= OHCI_ED_H;
  997. }
  998. /* Retire this TD */
  999. ed->head &= ~OHCI_DPTR_MASK;
  1000. ed->head |= td.next & OHCI_DPTR_MASK;
  1001. td.next = ohci->done;
  1002. ohci->done = addr;
  1003. i = OHCI_BM(td.flags, TD_DI);
  1004. if (i < ohci->done_count)
  1005. ohci->done_count = i;
  1006. exit_no_retire:
  1007. if (ohci_put_td(ohci, addr, &td)) {
  1008. ohci_die(ohci);
  1009. return 1;
  1010. }
  1011. return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
  1012. }
  1013. /* Service an endpoint list. Returns nonzero if active TD were found. */
  1014. static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
  1015. {
  1016. struct ohci_ed ed;
  1017. uint32_t next_ed;
  1018. uint32_t cur;
  1019. int active;
  1020. uint32_t link_cnt = 0;
  1021. active = 0;
  1022. if (head == 0)
  1023. return 0;
  1024. for (cur = head; cur && link_cnt++ < ED_LINK_LIMIT; cur = next_ed) {
  1025. if (ohci_read_ed(ohci, cur, &ed)) {
  1026. trace_usb_ohci_ed_read_error(cur);
  1027. ohci_die(ohci);
  1028. return 0;
  1029. }
  1030. next_ed = ed.next & OHCI_DPTR_MASK;
  1031. if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
  1032. uint32_t addr;
  1033. /* Cancel pending packets for ED that have been paused. */
  1034. addr = ed.head & OHCI_DPTR_MASK;
  1035. if (ohci->async_td && addr == ohci->async_td) {
  1036. usb_cancel_packet(&ohci->usb_packet);
  1037. ohci->async_td = 0;
  1038. usb_device_ep_stopped(ohci->usb_packet.ep->dev,
  1039. ohci->usb_packet.ep);
  1040. }
  1041. continue;
  1042. }
  1043. while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
  1044. trace_usb_ohci_ed_pkt(cur, (ed.head & OHCI_ED_H) != 0,
  1045. (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
  1046. ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
  1047. trace_usb_ohci_ed_pkt_flags(
  1048. OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
  1049. OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
  1050. (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
  1051. OHCI_BM(ed.flags, ED_MPS));
  1052. active = 1;
  1053. if ((ed.flags & OHCI_ED_F) == 0) {
  1054. if (ohci_service_td(ohci, &ed))
  1055. break;
  1056. } else {
  1057. /* Handle isochronous endpoints */
  1058. if (ohci_service_iso_td(ohci, &ed, completion))
  1059. break;
  1060. }
  1061. }
  1062. if (ohci_put_ed(ohci, cur, &ed)) {
  1063. ohci_die(ohci);
  1064. return 0;
  1065. }
  1066. }
  1067. return active;
  1068. }
  1069. /* set a timer for EOF */
  1070. static void ohci_eof_timer(OHCIState *ohci)
  1071. {
  1072. timer_mod(ohci->eof_timer, ohci->sof_time + usb_frame_time);
  1073. }
  1074. /* Set a timer for EOF and generate a SOF event */
  1075. static void ohci_sof(OHCIState *ohci)
  1076. {
  1077. ohci->sof_time += usb_frame_time;
  1078. ohci_eof_timer(ohci);
  1079. ohci_set_interrupt(ohci, OHCI_INTR_SF);
  1080. }
  1081. /* Process Control and Bulk lists. */
  1082. static void ohci_process_lists(OHCIState *ohci, int completion)
  1083. {
  1084. if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
  1085. if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
  1086. trace_usb_ohci_process_lists(ohci->ctrl_head, ohci->ctrl_cur);
  1087. }
  1088. if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
  1089. ohci->ctrl_cur = 0;
  1090. ohci->status &= ~OHCI_STATUS_CLF;
  1091. }
  1092. }
  1093. if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
  1094. if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
  1095. ohci->bulk_cur = 0;
  1096. ohci->status &= ~OHCI_STATUS_BLF;
  1097. }
  1098. }
  1099. }
  1100. /* Do frame processing on frame boundary */
  1101. static void ohci_frame_boundary(void *opaque)
  1102. {
  1103. OHCIState *ohci = opaque;
  1104. struct ohci_hcca hcca;
  1105. if (ohci_read_hcca(ohci, ohci->hcca, &hcca)) {
  1106. trace_usb_ohci_hcca_read_error(ohci->hcca);
  1107. ohci_die(ohci);
  1108. return;
  1109. }
  1110. /* Process all the lists at the end of the frame */
  1111. if (ohci->ctl & OHCI_CTL_PLE) {
  1112. int n;
  1113. n = ohci->frame_number & 0x1f;
  1114. ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
  1115. }
  1116. /* Cancel all pending packets if either of the lists has been disabled. */
  1117. if (ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
  1118. if (ohci->async_td) {
  1119. usb_cancel_packet(&ohci->usb_packet);
  1120. ohci->async_td = 0;
  1121. }
  1122. ohci_stop_endpoints(ohci);
  1123. }
  1124. ohci->old_ctl = ohci->ctl;
  1125. ohci_process_lists(ohci, 0);
  1126. /* Stop if UnrecoverableError happened or ohci_sof will crash */
  1127. if (ohci->intr_status & OHCI_INTR_UE) {
  1128. return;
  1129. }
  1130. /* Frame boundary, so do EOF stuf here */
  1131. ohci->frt = ohci->fit;
  1132. /* Increment frame number and take care of endianness. */
  1133. ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
  1134. hcca.frame = cpu_to_le16(ohci->frame_number);
  1135. if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
  1136. if (!ohci->done)
  1137. abort();
  1138. if (ohci->intr & ohci->intr_status)
  1139. ohci->done |= 1;
  1140. hcca.done = cpu_to_le32(ohci->done);
  1141. ohci->done = 0;
  1142. ohci->done_count = 7;
  1143. ohci_set_interrupt(ohci, OHCI_INTR_WD);
  1144. }
  1145. if (ohci->done_count != 7 && ohci->done_count != 0)
  1146. ohci->done_count--;
  1147. /* Do SOF stuff here */
  1148. ohci_sof(ohci);
  1149. /* Writeback HCCA */
  1150. if (ohci_put_hcca(ohci, ohci->hcca, &hcca)) {
  1151. ohci_die(ohci);
  1152. }
  1153. }
  1154. /* Start sending SOF tokens across the USB bus, lists are processed in
  1155. * next frame
  1156. */
  1157. static int ohci_bus_start(OHCIState *ohci)
  1158. {
  1159. trace_usb_ohci_start(ohci->name);
  1160. /* Delay the first SOF event by one frame time as
  1161. * linux driver is not ready to receive it and
  1162. * can meet some race conditions
  1163. */
  1164. ohci->sof_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1165. ohci_eof_timer(ohci);
  1166. return 1;
  1167. }
  1168. /* Stop sending SOF tokens on the bus */
  1169. void ohci_bus_stop(OHCIState *ohci)
  1170. {
  1171. trace_usb_ohci_stop(ohci->name);
  1172. timer_del(ohci->eof_timer);
  1173. }
  1174. /* Sets a flag in a port status register but only set it if the port is
  1175. * connected, if not set ConnectStatusChange flag. If flag is enabled
  1176. * return 1.
  1177. */
  1178. static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
  1179. {
  1180. int ret = 1;
  1181. /* writing a 0 has no effect */
  1182. if (val == 0)
  1183. return 0;
  1184. /* If CurrentConnectStatus is cleared we set
  1185. * ConnectStatusChange
  1186. */
  1187. if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
  1188. ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
  1189. if (ohci->rhstatus & OHCI_RHS_DRWE) {
  1190. /* TODO: CSC is a wakeup event */
  1191. }
  1192. return 0;
  1193. }
  1194. if (ohci->rhport[i].ctrl & val)
  1195. ret = 0;
  1196. /* set the bit */
  1197. ohci->rhport[i].ctrl |= val;
  1198. return ret;
  1199. }
  1200. /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
  1201. static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
  1202. {
  1203. val &= OHCI_FMI_FI;
  1204. if (val != ohci->fi) {
  1205. trace_usb_ohci_set_frame_interval(ohci->name, ohci->fi, ohci->fi);
  1206. }
  1207. ohci->fi = val;
  1208. }
  1209. static void ohci_port_power(OHCIState *ohci, int i, int p)
  1210. {
  1211. if (p) {
  1212. ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
  1213. } else {
  1214. ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
  1215. OHCI_PORT_CCS|
  1216. OHCI_PORT_PSS|
  1217. OHCI_PORT_PRS);
  1218. }
  1219. }
  1220. /* Set HcControlRegister */
  1221. static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
  1222. {
  1223. uint32_t old_state;
  1224. uint32_t new_state;
  1225. old_state = ohci->ctl & OHCI_CTL_HCFS;
  1226. ohci->ctl = val;
  1227. new_state = ohci->ctl & OHCI_CTL_HCFS;
  1228. /* no state change */
  1229. if (old_state == new_state)
  1230. return;
  1231. trace_usb_ohci_set_ctl(ohci->name, new_state);
  1232. switch (new_state) {
  1233. case OHCI_USB_OPERATIONAL:
  1234. ohci_bus_start(ohci);
  1235. break;
  1236. case OHCI_USB_SUSPEND:
  1237. ohci_bus_stop(ohci);
  1238. /* clear pending SF otherwise linux driver loops in ohci_irq() */
  1239. ohci->intr_status &= ~OHCI_INTR_SF;
  1240. ohci_intr_update(ohci);
  1241. break;
  1242. case OHCI_USB_RESUME:
  1243. trace_usb_ohci_resume(ohci->name);
  1244. break;
  1245. case OHCI_USB_RESET:
  1246. ohci_roothub_reset(ohci);
  1247. break;
  1248. }
  1249. }
  1250. static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
  1251. {
  1252. uint16_t fr;
  1253. int64_t tks;
  1254. if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
  1255. return (ohci->frt << 31);
  1256. /* Being in USB operational state guarnatees sof_time was
  1257. * set already.
  1258. */
  1259. tks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - ohci->sof_time;
  1260. if (tks < 0) {
  1261. tks = 0;
  1262. }
  1263. /* avoid muldiv if possible */
  1264. if (tks >= usb_frame_time)
  1265. return (ohci->frt << 31);
  1266. tks = tks / usb_bit_time;
  1267. fr = (uint16_t)(ohci->fi - tks);
  1268. return (ohci->frt << 31) | fr;
  1269. }
  1270. /* Set root hub status */
  1271. static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
  1272. {
  1273. uint32_t old_state;
  1274. old_state = ohci->rhstatus;
  1275. /* write 1 to clear OCIC */
  1276. if (val & OHCI_RHS_OCIC)
  1277. ohci->rhstatus &= ~OHCI_RHS_OCIC;
  1278. if (val & OHCI_RHS_LPS) {
  1279. int i;
  1280. for (i = 0; i < ohci->num_ports; i++)
  1281. ohci_port_power(ohci, i, 0);
  1282. trace_usb_ohci_hub_power_down();
  1283. }
  1284. if (val & OHCI_RHS_LPSC) {
  1285. int i;
  1286. for (i = 0; i < ohci->num_ports; i++)
  1287. ohci_port_power(ohci, i, 1);
  1288. trace_usb_ohci_hub_power_up();
  1289. }
  1290. if (val & OHCI_RHS_DRWE)
  1291. ohci->rhstatus |= OHCI_RHS_DRWE;
  1292. if (val & OHCI_RHS_CRWE)
  1293. ohci->rhstatus &= ~OHCI_RHS_DRWE;
  1294. if (old_state != ohci->rhstatus)
  1295. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1296. }
  1297. /* Set root hub port status */
  1298. static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
  1299. {
  1300. uint32_t old_state;
  1301. OHCIPort *port;
  1302. port = &ohci->rhport[portnum];
  1303. old_state = port->ctrl;
  1304. /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
  1305. if (val & OHCI_PORT_WTC)
  1306. port->ctrl &= ~(val & OHCI_PORT_WTC);
  1307. if (val & OHCI_PORT_CCS)
  1308. port->ctrl &= ~OHCI_PORT_PES;
  1309. ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
  1310. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
  1311. trace_usb_ohci_port_suspend(portnum);
  1312. }
  1313. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
  1314. trace_usb_ohci_port_reset(portnum);
  1315. usb_device_reset(port->port.dev);
  1316. port->ctrl &= ~OHCI_PORT_PRS;
  1317. /* ??? Should this also set OHCI_PORT_PESC. */
  1318. port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
  1319. }
  1320. /* Invert order here to ensure in ambiguous case, device is
  1321. * powered up...
  1322. */
  1323. if (val & OHCI_PORT_LSDA)
  1324. ohci_port_power(ohci, portnum, 0);
  1325. if (val & OHCI_PORT_PPS)
  1326. ohci_port_power(ohci, portnum, 1);
  1327. if (old_state != port->ctrl)
  1328. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1329. }
  1330. static uint64_t ohci_mem_read(void *opaque,
  1331. hwaddr addr,
  1332. unsigned size)
  1333. {
  1334. OHCIState *ohci = opaque;
  1335. uint32_t retval;
  1336. /* Only aligned reads are allowed on OHCI */
  1337. if (addr & 3) {
  1338. trace_usb_ohci_mem_read_unaligned(addr);
  1339. return 0xffffffff;
  1340. } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1341. /* HcRhPortStatus */
  1342. retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
  1343. } else {
  1344. switch (addr >> 2) {
  1345. case 0: /* HcRevision */
  1346. retval = 0x10;
  1347. break;
  1348. case 1: /* HcControl */
  1349. retval = ohci->ctl;
  1350. break;
  1351. case 2: /* HcCommandStatus */
  1352. retval = ohci->status;
  1353. break;
  1354. case 3: /* HcInterruptStatus */
  1355. retval = ohci->intr_status;
  1356. break;
  1357. case 4: /* HcInterruptEnable */
  1358. case 5: /* HcInterruptDisable */
  1359. retval = ohci->intr;
  1360. break;
  1361. case 6: /* HcHCCA */
  1362. retval = ohci->hcca;
  1363. break;
  1364. case 7: /* HcPeriodCurrentED */
  1365. retval = ohci->per_cur;
  1366. break;
  1367. case 8: /* HcControlHeadED */
  1368. retval = ohci->ctrl_head;
  1369. break;
  1370. case 9: /* HcControlCurrentED */
  1371. retval = ohci->ctrl_cur;
  1372. break;
  1373. case 10: /* HcBulkHeadED */
  1374. retval = ohci->bulk_head;
  1375. break;
  1376. case 11: /* HcBulkCurrentED */
  1377. retval = ohci->bulk_cur;
  1378. break;
  1379. case 12: /* HcDoneHead */
  1380. retval = ohci->done;
  1381. break;
  1382. case 13: /* HcFmInterretval */
  1383. retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
  1384. break;
  1385. case 14: /* HcFmRemaining */
  1386. retval = ohci_get_frame_remaining(ohci);
  1387. break;
  1388. case 15: /* HcFmNumber */
  1389. retval = ohci->frame_number;
  1390. break;
  1391. case 16: /* HcPeriodicStart */
  1392. retval = ohci->pstart;
  1393. break;
  1394. case 17: /* HcLSThreshold */
  1395. retval = ohci->lst;
  1396. break;
  1397. case 18: /* HcRhDescriptorA */
  1398. retval = ohci->rhdesc_a;
  1399. break;
  1400. case 19: /* HcRhDescriptorB */
  1401. retval = ohci->rhdesc_b;
  1402. break;
  1403. case 20: /* HcRhStatus */
  1404. retval = ohci->rhstatus;
  1405. break;
  1406. /* PXA27x specific registers */
  1407. case 24: /* HcStatus */
  1408. retval = ohci->hstatus & ohci->hmask;
  1409. break;
  1410. case 25: /* HcHReset */
  1411. retval = ohci->hreset;
  1412. break;
  1413. case 26: /* HcHInterruptEnable */
  1414. retval = ohci->hmask;
  1415. break;
  1416. case 27: /* HcHInterruptTest */
  1417. retval = ohci->htest;
  1418. break;
  1419. default:
  1420. trace_usb_ohci_mem_read_bad_offset(addr);
  1421. retval = 0xffffffff;
  1422. }
  1423. }
  1424. return retval;
  1425. }
  1426. static void ohci_mem_write(void *opaque,
  1427. hwaddr addr,
  1428. uint64_t val,
  1429. unsigned size)
  1430. {
  1431. OHCIState *ohci = opaque;
  1432. /* Only aligned reads are allowed on OHCI */
  1433. if (addr & 3) {
  1434. trace_usb_ohci_mem_write_unaligned(addr);
  1435. return;
  1436. }
  1437. if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1438. /* HcRhPortStatus */
  1439. ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
  1440. return;
  1441. }
  1442. switch (addr >> 2) {
  1443. case 1: /* HcControl */
  1444. ohci_set_ctl(ohci, val);
  1445. break;
  1446. case 2: /* HcCommandStatus */
  1447. /* SOC is read-only */
  1448. val = (val & ~OHCI_STATUS_SOC);
  1449. /* Bits written as '0' remain unchanged in the register */
  1450. ohci->status |= val;
  1451. if (ohci->status & OHCI_STATUS_HCR)
  1452. ohci_soft_reset(ohci);
  1453. break;
  1454. case 3: /* HcInterruptStatus */
  1455. ohci->intr_status &= ~val;
  1456. ohci_intr_update(ohci);
  1457. break;
  1458. case 4: /* HcInterruptEnable */
  1459. ohci->intr |= val;
  1460. ohci_intr_update(ohci);
  1461. break;
  1462. case 5: /* HcInterruptDisable */
  1463. ohci->intr &= ~val;
  1464. ohci_intr_update(ohci);
  1465. break;
  1466. case 6: /* HcHCCA */
  1467. ohci->hcca = val & OHCI_HCCA_MASK;
  1468. break;
  1469. case 7: /* HcPeriodCurrentED */
  1470. /* Ignore writes to this read-only register, Linux does them */
  1471. break;
  1472. case 8: /* HcControlHeadED */
  1473. ohci->ctrl_head = val & OHCI_EDPTR_MASK;
  1474. break;
  1475. case 9: /* HcControlCurrentED */
  1476. ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
  1477. break;
  1478. case 10: /* HcBulkHeadED */
  1479. ohci->bulk_head = val & OHCI_EDPTR_MASK;
  1480. break;
  1481. case 11: /* HcBulkCurrentED */
  1482. ohci->bulk_cur = val & OHCI_EDPTR_MASK;
  1483. break;
  1484. case 13: /* HcFmInterval */
  1485. ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
  1486. ohci->fit = (val & OHCI_FMI_FIT) >> 31;
  1487. ohci_set_frame_interval(ohci, val);
  1488. break;
  1489. case 15: /* HcFmNumber */
  1490. break;
  1491. case 16: /* HcPeriodicStart */
  1492. ohci->pstart = val & 0xffff;
  1493. break;
  1494. case 17: /* HcLSThreshold */
  1495. ohci->lst = val & 0xffff;
  1496. break;
  1497. case 18: /* HcRhDescriptorA */
  1498. ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
  1499. ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
  1500. break;
  1501. case 19: /* HcRhDescriptorB */
  1502. break;
  1503. case 20: /* HcRhStatus */
  1504. ohci_set_hub_status(ohci, val);
  1505. break;
  1506. /* PXA27x specific registers */
  1507. case 24: /* HcStatus */
  1508. ohci->hstatus &= ~(val & ohci->hmask);
  1509. break;
  1510. case 25: /* HcHReset */
  1511. ohci->hreset = val & ~OHCI_HRESET_FSBIR;
  1512. if (val & OHCI_HRESET_FSBIR)
  1513. ohci_hard_reset(ohci);
  1514. break;
  1515. case 26: /* HcHInterruptEnable */
  1516. ohci->hmask = val;
  1517. break;
  1518. case 27: /* HcHInterruptTest */
  1519. ohci->htest = val;
  1520. break;
  1521. default:
  1522. trace_usb_ohci_mem_write_bad_offset(addr);
  1523. break;
  1524. }
  1525. }
  1526. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev)
  1527. {
  1528. if (ohci->async_td &&
  1529. usb_packet_is_inflight(&ohci->usb_packet) &&
  1530. ohci->usb_packet.ep->dev == dev) {
  1531. usb_cancel_packet(&ohci->usb_packet);
  1532. ohci->async_td = 0;
  1533. }
  1534. }
  1535. static const MemoryRegionOps ohci_mem_ops = {
  1536. .read = ohci_mem_read,
  1537. .write = ohci_mem_write,
  1538. .endianness = DEVICE_LITTLE_ENDIAN,
  1539. };
  1540. static USBPortOps ohci_port_ops = {
  1541. .attach = ohci_attach,
  1542. .detach = ohci_detach,
  1543. .child_detach = ohci_child_detach,
  1544. .wakeup = ohci_wakeup,
  1545. .complete = ohci_async_complete_packet,
  1546. };
  1547. static USBBusOps ohci_bus_ops = {
  1548. };
  1549. void usb_ohci_init(OHCIState *ohci, DeviceState *dev, uint32_t num_ports,
  1550. dma_addr_t localmem_base, char *masterbus,
  1551. uint32_t firstport, AddressSpace *as,
  1552. void (*ohci_die_fn)(struct OHCIState *), Error **errp)
  1553. {
  1554. Error *err = NULL;
  1555. int i;
  1556. ohci->as = as;
  1557. ohci->ohci_die = ohci_die_fn;
  1558. if (num_ports > OHCI_MAX_PORTS) {
  1559. error_setg(errp, "OHCI num-ports=%u is too big (limit is %u ports)",
  1560. num_ports, OHCI_MAX_PORTS);
  1561. return;
  1562. }
  1563. if (usb_frame_time == 0) {
  1564. #ifdef OHCI_TIME_WARP
  1565. usb_frame_time = NANOSECONDS_PER_SECOND;
  1566. usb_bit_time = NANOSECONDS_PER_SECOND / (USB_HZ / 1000);
  1567. #else
  1568. usb_frame_time = NANOSECONDS_PER_SECOND / 1000;
  1569. if (NANOSECONDS_PER_SECOND >= USB_HZ) {
  1570. usb_bit_time = NANOSECONDS_PER_SECOND / USB_HZ;
  1571. } else {
  1572. usb_bit_time = 1;
  1573. }
  1574. #endif
  1575. trace_usb_ohci_init_time(usb_frame_time, usb_bit_time);
  1576. }
  1577. ohci->num_ports = num_ports;
  1578. if (masterbus) {
  1579. USBPort *ports[OHCI_MAX_PORTS];
  1580. for(i = 0; i < num_ports; i++) {
  1581. ports[i] = &ohci->rhport[i].port;
  1582. }
  1583. usb_register_companion(masterbus, ports, num_ports,
  1584. firstport, ohci, &ohci_port_ops,
  1585. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL,
  1586. &err);
  1587. if (err) {
  1588. error_propagate(errp, err);
  1589. return;
  1590. }
  1591. } else {
  1592. usb_bus_new(&ohci->bus, sizeof(ohci->bus), &ohci_bus_ops, dev);
  1593. for (i = 0; i < num_ports; i++) {
  1594. usb_register_port(&ohci->bus, &ohci->rhport[i].port,
  1595. ohci, i, &ohci_port_ops,
  1596. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1597. }
  1598. }
  1599. memory_region_init_io(&ohci->mem, OBJECT(dev), &ohci_mem_ops,
  1600. ohci, "ohci", 256);
  1601. ohci->localmem_base = localmem_base;
  1602. ohci->name = object_get_typename(OBJECT(dev));
  1603. usb_packet_init(&ohci->usb_packet);
  1604. ohci->async_td = 0;
  1605. ohci->eof_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  1606. ohci_frame_boundary, ohci);
  1607. }
  1608. /**
  1609. * A typical OHCI will stop operating and set itself into error state
  1610. * (which can be queried by MMIO) to signal that it got an error.
  1611. */
  1612. void ohci_sysbus_die(struct OHCIState *ohci)
  1613. {
  1614. trace_usb_ohci_die();
  1615. ohci_set_interrupt(ohci, OHCI_INTR_UE);
  1616. ohci_bus_stop(ohci);
  1617. }
  1618. static void ohci_realize_pxa(DeviceState *dev, Error **errp)
  1619. {
  1620. OHCISysBusState *s = SYSBUS_OHCI(dev);
  1621. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1622. Error *err = NULL;
  1623. usb_ohci_init(&s->ohci, dev, s->num_ports, s->dma_offset,
  1624. s->masterbus, s->firstport,
  1625. &address_space_memory, ohci_sysbus_die, &err);
  1626. if (err) {
  1627. error_propagate(errp, err);
  1628. return;
  1629. }
  1630. sysbus_init_irq(sbd, &s->ohci.irq);
  1631. sysbus_init_mmio(sbd, &s->ohci.mem);
  1632. }
  1633. static void usb_ohci_reset_sysbus(DeviceState *dev)
  1634. {
  1635. OHCISysBusState *s = SYSBUS_OHCI(dev);
  1636. OHCIState *ohci = &s->ohci;
  1637. ohci_hard_reset(ohci);
  1638. }
  1639. static const VMStateDescription vmstate_ohci_state_port = {
  1640. .name = "ohci-core/port",
  1641. .version_id = 1,
  1642. .minimum_version_id = 1,
  1643. .fields = (VMStateField[]) {
  1644. VMSTATE_UINT32(ctrl, OHCIPort),
  1645. VMSTATE_END_OF_LIST()
  1646. },
  1647. };
  1648. static bool ohci_eof_timer_needed(void *opaque)
  1649. {
  1650. OHCIState *ohci = opaque;
  1651. return timer_pending(ohci->eof_timer);
  1652. }
  1653. static const VMStateDescription vmstate_ohci_eof_timer = {
  1654. .name = "ohci-core/eof-timer",
  1655. .version_id = 1,
  1656. .minimum_version_id = 1,
  1657. .needed = ohci_eof_timer_needed,
  1658. .fields = (VMStateField[]) {
  1659. VMSTATE_TIMER_PTR(eof_timer, OHCIState),
  1660. VMSTATE_END_OF_LIST()
  1661. },
  1662. };
  1663. const VMStateDescription vmstate_ohci_state = {
  1664. .name = "ohci-core",
  1665. .version_id = 1,
  1666. .minimum_version_id = 1,
  1667. .fields = (VMStateField[]) {
  1668. VMSTATE_INT64(sof_time, OHCIState),
  1669. VMSTATE_UINT32(ctl, OHCIState),
  1670. VMSTATE_UINT32(status, OHCIState),
  1671. VMSTATE_UINT32(intr_status, OHCIState),
  1672. VMSTATE_UINT32(intr, OHCIState),
  1673. VMSTATE_UINT32(hcca, OHCIState),
  1674. VMSTATE_UINT32(ctrl_head, OHCIState),
  1675. VMSTATE_UINT32(ctrl_cur, OHCIState),
  1676. VMSTATE_UINT32(bulk_head, OHCIState),
  1677. VMSTATE_UINT32(bulk_cur, OHCIState),
  1678. VMSTATE_UINT32(per_cur, OHCIState),
  1679. VMSTATE_UINT32(done, OHCIState),
  1680. VMSTATE_INT32(done_count, OHCIState),
  1681. VMSTATE_UINT16(fsmps, OHCIState),
  1682. VMSTATE_UINT8(fit, OHCIState),
  1683. VMSTATE_UINT16(fi, OHCIState),
  1684. VMSTATE_UINT8(frt, OHCIState),
  1685. VMSTATE_UINT16(frame_number, OHCIState),
  1686. VMSTATE_UINT16(padding, OHCIState),
  1687. VMSTATE_UINT32(pstart, OHCIState),
  1688. VMSTATE_UINT32(lst, OHCIState),
  1689. VMSTATE_UINT32(rhdesc_a, OHCIState),
  1690. VMSTATE_UINT32(rhdesc_b, OHCIState),
  1691. VMSTATE_UINT32(rhstatus, OHCIState),
  1692. VMSTATE_STRUCT_ARRAY(rhport, OHCIState, OHCI_MAX_PORTS, 0,
  1693. vmstate_ohci_state_port, OHCIPort),
  1694. VMSTATE_UINT32(hstatus, OHCIState),
  1695. VMSTATE_UINT32(hmask, OHCIState),
  1696. VMSTATE_UINT32(hreset, OHCIState),
  1697. VMSTATE_UINT32(htest, OHCIState),
  1698. VMSTATE_UINT32(old_ctl, OHCIState),
  1699. VMSTATE_UINT8_ARRAY(usb_buf, OHCIState, 8192),
  1700. VMSTATE_UINT32(async_td, OHCIState),
  1701. VMSTATE_BOOL(async_complete, OHCIState),
  1702. VMSTATE_END_OF_LIST()
  1703. },
  1704. .subsections = (const VMStateDescription*[]) {
  1705. &vmstate_ohci_eof_timer,
  1706. NULL
  1707. }
  1708. };
  1709. static Property ohci_sysbus_properties[] = {
  1710. DEFINE_PROP_STRING("masterbus", OHCISysBusState, masterbus),
  1711. DEFINE_PROP_UINT32("num-ports", OHCISysBusState, num_ports, 3),
  1712. DEFINE_PROP_UINT32("firstport", OHCISysBusState, firstport, 0),
  1713. DEFINE_PROP_DMAADDR("dma-offset", OHCISysBusState, dma_offset, 0),
  1714. DEFINE_PROP_END_OF_LIST(),
  1715. };
  1716. static void ohci_sysbus_class_init(ObjectClass *klass, void *data)
  1717. {
  1718. DeviceClass *dc = DEVICE_CLASS(klass);
  1719. dc->realize = ohci_realize_pxa;
  1720. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  1721. dc->desc = "OHCI USB Controller";
  1722. device_class_set_props(dc, ohci_sysbus_properties);
  1723. dc->reset = usb_ohci_reset_sysbus;
  1724. }
  1725. static const TypeInfo ohci_sysbus_info = {
  1726. .name = TYPE_SYSBUS_OHCI,
  1727. .parent = TYPE_SYS_BUS_DEVICE,
  1728. .instance_size = sizeof(OHCISysBusState),
  1729. .class_init = ohci_sysbus_class_init,
  1730. };
  1731. static void ohci_register_types(void)
  1732. {
  1733. type_register_static(&ohci_sysbus_info);
  1734. }
  1735. type_init(ohci_register_types)