2
0

hcd-ohci.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  590. ed->head &= ~OHCI_DPTR_MASK;
  591. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  592. iso_td.next = ohci->done;
  593. ohci->done = addr;
  594. i = OHCI_BM(iso_td.flags, TD_DI);
  595. if (i < ohci->done_count)
  596. ohci->done_count = i;
  597. if (ohci_put_iso_td(ohci, addr, &iso_td)) {
  598. ohci_die(ohci);
  599. return 1;
  600. }
  601. return 0;
  602. }
  603. dir = OHCI_BM(ed->flags, ED_D);
  604. switch (dir) {
  605. case OHCI_TD_DIR_IN:
  606. str = "in";
  607. pid = USB_TOKEN_IN;
  608. break;
  609. case OHCI_TD_DIR_OUT:
  610. str = "out";
  611. pid = USB_TOKEN_OUT;
  612. break;
  613. case OHCI_TD_DIR_SETUP:
  614. str = "setup";
  615. pid = USB_TOKEN_SETUP;
  616. break;
  617. default:
  618. trace_usb_ohci_iso_td_bad_direction(dir);
  619. return 1;
  620. }
  621. if (!iso_td.bp || !iso_td.be) {
  622. trace_usb_ohci_iso_td_bad_bp_be(iso_td.bp, iso_td.be);
  623. return 1;
  624. }
  625. start_offset = iso_td.offset[relative_frame_number];
  626. next_offset = iso_td.offset[relative_frame_number + 1];
  627. if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
  628. ((relative_frame_number < frame_count) &&
  629. !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
  630. trace_usb_ohci_iso_td_bad_cc_not_accessed(start_offset, next_offset);
  631. return 1;
  632. }
  633. if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
  634. trace_usb_ohci_iso_td_bad_cc_overrun(start_offset, next_offset);
  635. return 1;
  636. }
  637. if ((start_offset & 0x1000) == 0) {
  638. start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  639. (start_offset & OHCI_OFFSET_MASK);
  640. } else {
  641. start_addr = (iso_td.be & OHCI_PAGE_MASK) |
  642. (start_offset & OHCI_OFFSET_MASK);
  643. }
  644. if (relative_frame_number < frame_count) {
  645. end_offset = next_offset - 1;
  646. if ((end_offset & 0x1000) == 0) {
  647. end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  648. (end_offset & OHCI_OFFSET_MASK);
  649. } else {
  650. end_addr = (iso_td.be & OHCI_PAGE_MASK) |
  651. (end_offset & OHCI_OFFSET_MASK);
  652. }
  653. } else {
  654. /* Last packet in the ISO TD */
  655. end_addr = iso_td.be;
  656. }
  657. if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
  658. len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
  659. - (start_addr & OHCI_OFFSET_MASK);
  660. } else {
  661. len = end_addr - start_addr + 1;
  662. }
  663. if (len && dir != OHCI_TD_DIR_IN) {
  664. if (ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len,
  665. DMA_DIRECTION_TO_DEVICE)) {
  666. ohci_die(ohci);
  667. return 1;
  668. }
  669. }
  670. if (!completion) {
  671. bool int_req = relative_frame_number == frame_count &&
  672. OHCI_BM(iso_td.flags, TD_DI) == 0;
  673. dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
  674. if (dev == NULL) {
  675. trace_usb_ohci_td_dev_error();
  676. return 1;
  677. }
  678. ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
  679. usb_packet_setup(&ohci->usb_packet, pid, ep, 0, addr, false, int_req);
  680. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, len);
  681. usb_handle_packet(dev, &ohci->usb_packet);
  682. if (ohci->usb_packet.status == USB_RET_ASYNC) {
  683. usb_device_flush_ep_queue(dev, ep);
  684. return 1;
  685. }
  686. }
  687. if (ohci->usb_packet.status == USB_RET_SUCCESS) {
  688. ret = ohci->usb_packet.actual_length;
  689. } else {
  690. ret = ohci->usb_packet.status;
  691. }
  692. trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr,
  693. str, len, ret);
  694. /* Writeback */
  695. if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
  696. /* IN transfer succeeded */
  697. if (ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret,
  698. DMA_DIRECTION_FROM_DEVICE)) {
  699. ohci_die(ohci);
  700. return 1;
  701. }
  702. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  703. OHCI_CC_NOERROR);
  704. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
  705. } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
  706. /* OUT transfer succeeded */
  707. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  708. OHCI_CC_NOERROR);
  709. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
  710. } else {
  711. if (ret > (ssize_t) len) {
  712. trace_usb_ohci_iso_td_data_overrun(ret, len);
  713. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  714. OHCI_CC_DATAOVERRUN);
  715. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  716. len);
  717. } else if (ret >= 0) {
  718. trace_usb_ohci_iso_td_data_underrun(ret);
  719. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  720. OHCI_CC_DATAUNDERRUN);
  721. } else {
  722. switch (ret) {
  723. case USB_RET_IOERROR:
  724. case USB_RET_NODEV:
  725. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  726. OHCI_CC_DEVICENOTRESPONDING);
  727. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  728. 0);
  729. break;
  730. case USB_RET_NAK:
  731. case USB_RET_STALL:
  732. trace_usb_ohci_iso_td_nak(ret);
  733. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  734. OHCI_CC_STALL);
  735. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  736. 0);
  737. break;
  738. default:
  739. trace_usb_ohci_iso_td_bad_response(ret);
  740. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  741. OHCI_CC_UNDEXPETEDPID);
  742. break;
  743. }
  744. }
  745. }
  746. if (relative_frame_number == frame_count) {
  747. /* Last data packet of ISO TD - retire the TD to the Done Queue */
  748. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
  749. ed->head &= ~OHCI_DPTR_MASK;
  750. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  751. iso_td.next = ohci->done;
  752. ohci->done = addr;
  753. i = OHCI_BM(iso_td.flags, TD_DI);
  754. if (i < ohci->done_count)
  755. ohci->done_count = i;
  756. }
  757. if (ohci_put_iso_td(ohci, addr, &iso_td)) {
  758. ohci_die(ohci);
  759. }
  760. return 1;
  761. }
  762. static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
  763. {
  764. bool print16;
  765. bool printall;
  766. const int width = 16;
  767. int i;
  768. char tmp[3 * width + 1];
  769. char *p = tmp;
  770. print16 = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_SHORT);
  771. printall = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_FULL);
  772. if (!printall && !print16) {
  773. return;
  774. }
  775. for (i = 0; ; i++) {
  776. if (i && (!(i % width) || (i == len))) {
  777. if (!printall) {
  778. trace_usb_ohci_td_pkt_short(msg, tmp);
  779. break;
  780. }
  781. trace_usb_ohci_td_pkt_full(msg, tmp);
  782. p = tmp;
  783. *p = 0;
  784. }
  785. if (i == len) {
  786. break;
  787. }
  788. p += sprintf(p, " %.2x", buf[i]);
  789. }
  790. }
  791. /* Service a transport descriptor.
  792. Returns nonzero to terminate processing of this endpoint. */
  793. static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
  794. {
  795. int dir;
  796. size_t len = 0, pktlen = 0;
  797. const char *str = NULL;
  798. int pid;
  799. int ret;
  800. int i;
  801. USBDevice *dev;
  802. USBEndpoint *ep;
  803. struct ohci_td td;
  804. uint32_t addr;
  805. int flag_r;
  806. int completion;
  807. addr = ed->head & OHCI_DPTR_MASK;
  808. /* See if this TD has already been submitted to the device. */
  809. completion = (addr == ohci->async_td);
  810. if (completion && !ohci->async_complete) {
  811. trace_usb_ohci_td_skip_async();
  812. return 1;
  813. }
  814. if (ohci_read_td(ohci, addr, &td)) {
  815. trace_usb_ohci_td_read_error(addr);
  816. ohci_die(ohci);
  817. return 1;
  818. }
  819. dir = OHCI_BM(ed->flags, ED_D);
  820. switch (dir) {
  821. case OHCI_TD_DIR_OUT:
  822. case OHCI_TD_DIR_IN:
  823. /* Same value. */
  824. break;
  825. default:
  826. dir = OHCI_BM(td.flags, TD_DP);
  827. break;
  828. }
  829. switch (dir) {
  830. case OHCI_TD_DIR_IN:
  831. str = "in";
  832. pid = USB_TOKEN_IN;
  833. break;
  834. case OHCI_TD_DIR_OUT:
  835. str = "out";
  836. pid = USB_TOKEN_OUT;
  837. break;
  838. case OHCI_TD_DIR_SETUP:
  839. str = "setup";
  840. pid = USB_TOKEN_SETUP;
  841. break;
  842. default:
  843. trace_usb_ohci_td_bad_direction(dir);
  844. return 1;
  845. }
  846. if (td.cbp && td.be) {
  847. if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
  848. len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
  849. } else {
  850. len = (td.be - td.cbp) + 1;
  851. }
  852. pktlen = len;
  853. if (len && dir != OHCI_TD_DIR_IN) {
  854. /* The endpoint may not allow us to transfer it all now */
  855. pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
  856. if (pktlen > len) {
  857. pktlen = len;
  858. }
  859. if (!completion) {
  860. if (ohci_copy_td(ohci, &td, ohci->usb_buf, pktlen,
  861. DMA_DIRECTION_TO_DEVICE)) {
  862. ohci_die(ohci);
  863. }
  864. }
  865. }
  866. }
  867. flag_r = (td.flags & OHCI_TD_R) != 0;
  868. trace_usb_ohci_td_pkt_hdr(addr, (int64_t)pktlen, (int64_t)len, str,
  869. flag_r, td.cbp, td.be);
  870. ohci_td_pkt("OUT", ohci->usb_buf, pktlen);
  871. if (completion) {
  872. ohci->async_td = 0;
  873. ohci->async_complete = false;
  874. } else {
  875. if (ohci->async_td) {
  876. /* ??? The hardware should allow one active packet per
  877. endpoint. We only allow one active packet per controller.
  878. This should be sufficient as long as devices respond in a
  879. timely manner.
  880. */
  881. trace_usb_ohci_td_too_many_pending();
  882. return 1;
  883. }
  884. dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
  885. if (dev == NULL) {
  886. trace_usb_ohci_td_dev_error();
  887. return 1;
  888. }
  889. ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
  890. usb_packet_setup(&ohci->usb_packet, pid, ep, 0, addr, !flag_r,
  891. OHCI_BM(td.flags, TD_DI) == 0);
  892. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, pktlen);
  893. usb_handle_packet(dev, &ohci->usb_packet);
  894. trace_usb_ohci_td_packet_status(ohci->usb_packet.status);
  895. if (ohci->usb_packet.status == USB_RET_ASYNC) {
  896. usb_device_flush_ep_queue(dev, ep);
  897. ohci->async_td = addr;
  898. return 1;
  899. }
  900. }
  901. if (ohci->usb_packet.status == USB_RET_SUCCESS) {
  902. ret = ohci->usb_packet.actual_length;
  903. } else {
  904. ret = ohci->usb_packet.status;
  905. }
  906. if (ret >= 0) {
  907. if (dir == OHCI_TD_DIR_IN) {
  908. if (ohci_copy_td(ohci, &td, ohci->usb_buf, ret,
  909. DMA_DIRECTION_FROM_DEVICE)) {
  910. ohci_die(ohci);
  911. }
  912. ohci_td_pkt("IN", ohci->usb_buf, pktlen);
  913. } else {
  914. ret = pktlen;
  915. }
  916. }
  917. /* Writeback */
  918. if (ret == pktlen || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
  919. /* Transmission succeeded. */
  920. if (ret == len) {
  921. td.cbp = 0;
  922. } else {
  923. if ((td.cbp & 0xfff) + ret > 0xfff) {
  924. td.cbp = (td.be & ~0xfff) + ((td.cbp + ret) & 0xfff);
  925. } else {
  926. td.cbp += ret;
  927. }
  928. }
  929. td.flags |= OHCI_TD_T1;
  930. td.flags ^= OHCI_TD_T0;
  931. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
  932. OHCI_SET_BM(td.flags, TD_EC, 0);
  933. if ((dir != OHCI_TD_DIR_IN) && (ret != len)) {
  934. /* Partial packet transfer: TD not ready to retire yet */
  935. goto exit_no_retire;
  936. }
  937. /* Setting ED_C is part of the TD retirement process */
  938. ed->head &= ~OHCI_ED_C;
  939. if (td.flags & OHCI_TD_T0)
  940. ed->head |= OHCI_ED_C;
  941. } else {
  942. if (ret >= 0) {
  943. trace_usb_ohci_td_underrun();
  944. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
  945. } else {
  946. switch (ret) {
  947. case USB_RET_IOERROR:
  948. case USB_RET_NODEV:
  949. trace_usb_ohci_td_dev_error();
  950. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
  951. break;
  952. case USB_RET_NAK:
  953. trace_usb_ohci_td_nak();
  954. return 1;
  955. case USB_RET_STALL:
  956. trace_usb_ohci_td_stall();
  957. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
  958. break;
  959. case USB_RET_BABBLE:
  960. trace_usb_ohci_td_babble();
  961. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  962. break;
  963. default:
  964. trace_usb_ohci_td_bad_device_response(ret);
  965. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
  966. OHCI_SET_BM(td.flags, TD_EC, 3);
  967. break;
  968. }
  969. /* An error occured so we have to clear the interrupt counter. See
  970. * spec at 6.4.4 on page 104 */
  971. ohci->done_count = 0;
  972. }
  973. ed->head |= OHCI_ED_H;
  974. }
  975. /* Retire this TD */
  976. ed->head &= ~OHCI_DPTR_MASK;
  977. ed->head |= td.next & OHCI_DPTR_MASK;
  978. td.next = ohci->done;
  979. ohci->done = addr;
  980. i = OHCI_BM(td.flags, TD_DI);
  981. if (i < ohci->done_count)
  982. ohci->done_count = i;
  983. exit_no_retire:
  984. if (ohci_put_td(ohci, addr, &td)) {
  985. ohci_die(ohci);
  986. return 1;
  987. }
  988. return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
  989. }
  990. /* Service an endpoint list. Returns nonzero if active TD were found. */
  991. static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
  992. {
  993. struct ohci_ed ed;
  994. uint32_t next_ed;
  995. uint32_t cur;
  996. int active;
  997. uint32_t link_cnt = 0;
  998. active = 0;
  999. if (head == 0)
  1000. return 0;
  1001. for (cur = head; cur && link_cnt++ < ED_LINK_LIMIT; cur = next_ed) {
  1002. if (ohci_read_ed(ohci, cur, &ed)) {
  1003. trace_usb_ohci_ed_read_error(cur);
  1004. ohci_die(ohci);
  1005. return 0;
  1006. }
  1007. next_ed = ed.next & OHCI_DPTR_MASK;
  1008. if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
  1009. uint32_t addr;
  1010. /* Cancel pending packets for ED that have been paused. */
  1011. addr = ed.head & OHCI_DPTR_MASK;
  1012. if (ohci->async_td && addr == ohci->async_td) {
  1013. usb_cancel_packet(&ohci->usb_packet);
  1014. ohci->async_td = 0;
  1015. usb_device_ep_stopped(ohci->usb_packet.ep->dev,
  1016. ohci->usb_packet.ep);
  1017. }
  1018. continue;
  1019. }
  1020. while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
  1021. trace_usb_ohci_ed_pkt(cur, (ed.head & OHCI_ED_H) != 0,
  1022. (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
  1023. ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
  1024. trace_usb_ohci_ed_pkt_flags(
  1025. OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
  1026. OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
  1027. (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
  1028. OHCI_BM(ed.flags, ED_MPS));
  1029. active = 1;
  1030. if ((ed.flags & OHCI_ED_F) == 0) {
  1031. if (ohci_service_td(ohci, &ed))
  1032. break;
  1033. } else {
  1034. /* Handle isochronous endpoints */
  1035. if (ohci_service_iso_td(ohci, &ed, completion))
  1036. break;
  1037. }
  1038. }
  1039. if (ohci_put_ed(ohci, cur, &ed)) {
  1040. ohci_die(ohci);
  1041. return 0;
  1042. }
  1043. }
  1044. return active;
  1045. }
  1046. /* set a timer for EOF */
  1047. static void ohci_eof_timer(OHCIState *ohci)
  1048. {
  1049. timer_mod(ohci->eof_timer, ohci->sof_time + usb_frame_time);
  1050. }
  1051. /* Set a timer for EOF and generate a SOF event */
  1052. static void ohci_sof(OHCIState *ohci)
  1053. {
  1054. ohci->sof_time += usb_frame_time;
  1055. ohci_eof_timer(ohci);
  1056. ohci_set_interrupt(ohci, OHCI_INTR_SF);
  1057. }
  1058. /* Process Control and Bulk lists. */
  1059. static void ohci_process_lists(OHCIState *ohci, int completion)
  1060. {
  1061. if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
  1062. if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
  1063. trace_usb_ohci_process_lists(ohci->ctrl_head, ohci->ctrl_cur);
  1064. }
  1065. if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
  1066. ohci->ctrl_cur = 0;
  1067. ohci->status &= ~OHCI_STATUS_CLF;
  1068. }
  1069. }
  1070. if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
  1071. if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
  1072. ohci->bulk_cur = 0;
  1073. ohci->status &= ~OHCI_STATUS_BLF;
  1074. }
  1075. }
  1076. }
  1077. /* Do frame processing on frame boundary */
  1078. static void ohci_frame_boundary(void *opaque)
  1079. {
  1080. OHCIState *ohci = opaque;
  1081. struct ohci_hcca hcca;
  1082. if (ohci_read_hcca(ohci, ohci->hcca, &hcca)) {
  1083. trace_usb_ohci_hcca_read_error(ohci->hcca);
  1084. ohci_die(ohci);
  1085. return;
  1086. }
  1087. /* Process all the lists at the end of the frame */
  1088. if (ohci->ctl & OHCI_CTL_PLE) {
  1089. int n;
  1090. n = ohci->frame_number & 0x1f;
  1091. ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
  1092. }
  1093. /* Cancel all pending packets if either of the lists has been disabled. */
  1094. if (ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
  1095. if (ohci->async_td) {
  1096. usb_cancel_packet(&ohci->usb_packet);
  1097. ohci->async_td = 0;
  1098. }
  1099. ohci_stop_endpoints(ohci);
  1100. }
  1101. ohci->old_ctl = ohci->ctl;
  1102. ohci_process_lists(ohci, 0);
  1103. /* Stop if UnrecoverableError happened or ohci_sof will crash */
  1104. if (ohci->intr_status & OHCI_INTR_UE) {
  1105. return;
  1106. }
  1107. /* Frame boundary, so do EOF stuf here */
  1108. ohci->frt = ohci->fit;
  1109. /* Increment frame number and take care of endianness. */
  1110. ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
  1111. hcca.frame = cpu_to_le16(ohci->frame_number);
  1112. if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
  1113. if (!ohci->done)
  1114. abort();
  1115. if (ohci->intr & ohci->intr_status)
  1116. ohci->done |= 1;
  1117. hcca.done = cpu_to_le32(ohci->done);
  1118. ohci->done = 0;
  1119. ohci->done_count = 7;
  1120. ohci_set_interrupt(ohci, OHCI_INTR_WD);
  1121. }
  1122. if (ohci->done_count != 7 && ohci->done_count != 0)
  1123. ohci->done_count--;
  1124. /* Do SOF stuff here */
  1125. ohci_sof(ohci);
  1126. /* Writeback HCCA */
  1127. if (ohci_put_hcca(ohci, ohci->hcca, &hcca)) {
  1128. ohci_die(ohci);
  1129. }
  1130. }
  1131. /* Start sending SOF tokens across the USB bus, lists are processed in
  1132. * next frame
  1133. */
  1134. static int ohci_bus_start(OHCIState *ohci)
  1135. {
  1136. trace_usb_ohci_start(ohci->name);
  1137. /* Delay the first SOF event by one frame time as
  1138. * linux driver is not ready to receive it and
  1139. * can meet some race conditions
  1140. */
  1141. ohci->sof_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  1142. ohci_eof_timer(ohci);
  1143. return 1;
  1144. }
  1145. /* Stop sending SOF tokens on the bus */
  1146. void ohci_bus_stop(OHCIState *ohci)
  1147. {
  1148. trace_usb_ohci_stop(ohci->name);
  1149. timer_del(ohci->eof_timer);
  1150. }
  1151. /* Sets a flag in a port status register but only set it if the port is
  1152. * connected, if not set ConnectStatusChange flag. If flag is enabled
  1153. * return 1.
  1154. */
  1155. static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
  1156. {
  1157. int ret = 1;
  1158. /* writing a 0 has no effect */
  1159. if (val == 0)
  1160. return 0;
  1161. /* If CurrentConnectStatus is cleared we set
  1162. * ConnectStatusChange
  1163. */
  1164. if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
  1165. ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
  1166. if (ohci->rhstatus & OHCI_RHS_DRWE) {
  1167. /* TODO: CSC is a wakeup event */
  1168. }
  1169. return 0;
  1170. }
  1171. if (ohci->rhport[i].ctrl & val)
  1172. ret = 0;
  1173. /* set the bit */
  1174. ohci->rhport[i].ctrl |= val;
  1175. return ret;
  1176. }
  1177. /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
  1178. static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
  1179. {
  1180. val &= OHCI_FMI_FI;
  1181. if (val != ohci->fi) {
  1182. trace_usb_ohci_set_frame_interval(ohci->name, ohci->fi, ohci->fi);
  1183. }
  1184. ohci->fi = val;
  1185. }
  1186. static void ohci_port_power(OHCIState *ohci, int i, int p)
  1187. {
  1188. if (p) {
  1189. ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
  1190. } else {
  1191. ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
  1192. OHCI_PORT_CCS|
  1193. OHCI_PORT_PSS|
  1194. OHCI_PORT_PRS);
  1195. }
  1196. }
  1197. /* Set HcControlRegister */
  1198. static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
  1199. {
  1200. uint32_t old_state;
  1201. uint32_t new_state;
  1202. old_state = ohci->ctl & OHCI_CTL_HCFS;
  1203. ohci->ctl = val;
  1204. new_state = ohci->ctl & OHCI_CTL_HCFS;
  1205. /* no state change */
  1206. if (old_state == new_state)
  1207. return;
  1208. trace_usb_ohci_set_ctl(ohci->name, new_state);
  1209. switch (new_state) {
  1210. case OHCI_USB_OPERATIONAL:
  1211. ohci_bus_start(ohci);
  1212. break;
  1213. case OHCI_USB_SUSPEND:
  1214. ohci_bus_stop(ohci);
  1215. /* clear pending SF otherwise linux driver loops in ohci_irq() */
  1216. ohci->intr_status &= ~OHCI_INTR_SF;
  1217. ohci_intr_update(ohci);
  1218. break;
  1219. case OHCI_USB_RESUME:
  1220. trace_usb_ohci_resume(ohci->name);
  1221. break;
  1222. case OHCI_USB_RESET:
  1223. ohci_roothub_reset(ohci);
  1224. break;
  1225. }
  1226. }
  1227. static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
  1228. {
  1229. uint16_t fr;
  1230. int64_t tks;
  1231. if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
  1232. return (ohci->frt << 31);
  1233. /* Being in USB operational state guarnatees sof_time was
  1234. * set already.
  1235. */
  1236. tks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - ohci->sof_time;
  1237. if (tks < 0) {
  1238. tks = 0;
  1239. }
  1240. /* avoid muldiv if possible */
  1241. if (tks >= usb_frame_time)
  1242. return (ohci->frt << 31);
  1243. tks = tks / usb_bit_time;
  1244. fr = (uint16_t)(ohci->fi - tks);
  1245. return (ohci->frt << 31) | fr;
  1246. }
  1247. /* Set root hub status */
  1248. static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
  1249. {
  1250. uint32_t old_state;
  1251. old_state = ohci->rhstatus;
  1252. /* write 1 to clear OCIC */
  1253. if (val & OHCI_RHS_OCIC)
  1254. ohci->rhstatus &= ~OHCI_RHS_OCIC;
  1255. if (val & OHCI_RHS_LPS) {
  1256. int i;
  1257. for (i = 0; i < ohci->num_ports; i++)
  1258. ohci_port_power(ohci, i, 0);
  1259. trace_usb_ohci_hub_power_down();
  1260. }
  1261. if (val & OHCI_RHS_LPSC) {
  1262. int i;
  1263. for (i = 0; i < ohci->num_ports; i++)
  1264. ohci_port_power(ohci, i, 1);
  1265. trace_usb_ohci_hub_power_up();
  1266. }
  1267. if (val & OHCI_RHS_DRWE)
  1268. ohci->rhstatus |= OHCI_RHS_DRWE;
  1269. if (val & OHCI_RHS_CRWE)
  1270. ohci->rhstatus &= ~OHCI_RHS_DRWE;
  1271. if (old_state != ohci->rhstatus)
  1272. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1273. }
  1274. /* Set root hub port status */
  1275. static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
  1276. {
  1277. uint32_t old_state;
  1278. OHCIPort *port;
  1279. port = &ohci->rhport[portnum];
  1280. old_state = port->ctrl;
  1281. /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
  1282. if (val & OHCI_PORT_WTC)
  1283. port->ctrl &= ~(val & OHCI_PORT_WTC);
  1284. if (val & OHCI_PORT_CCS)
  1285. port->ctrl &= ~OHCI_PORT_PES;
  1286. ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
  1287. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
  1288. trace_usb_ohci_port_suspend(portnum);
  1289. }
  1290. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
  1291. trace_usb_ohci_port_reset(portnum);
  1292. usb_device_reset(port->port.dev);
  1293. port->ctrl &= ~OHCI_PORT_PRS;
  1294. /* ??? Should this also set OHCI_PORT_PESC. */
  1295. port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
  1296. }
  1297. /* Invert order here to ensure in ambiguous case, device is
  1298. * powered up...
  1299. */
  1300. if (val & OHCI_PORT_LSDA)
  1301. ohci_port_power(ohci, portnum, 0);
  1302. if (val & OHCI_PORT_PPS)
  1303. ohci_port_power(ohci, portnum, 1);
  1304. if (old_state != port->ctrl)
  1305. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1306. }
  1307. static uint64_t ohci_mem_read(void *opaque,
  1308. hwaddr addr,
  1309. unsigned size)
  1310. {
  1311. OHCIState *ohci = opaque;
  1312. uint32_t retval;
  1313. /* Only aligned reads are allowed on OHCI */
  1314. if (addr & 3) {
  1315. trace_usb_ohci_mem_read_unaligned(addr);
  1316. return 0xffffffff;
  1317. } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1318. /* HcRhPortStatus */
  1319. retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
  1320. } else {
  1321. switch (addr >> 2) {
  1322. case 0: /* HcRevision */
  1323. retval = 0x10;
  1324. break;
  1325. case 1: /* HcControl */
  1326. retval = ohci->ctl;
  1327. break;
  1328. case 2: /* HcCommandStatus */
  1329. retval = ohci->status;
  1330. break;
  1331. case 3: /* HcInterruptStatus */
  1332. retval = ohci->intr_status;
  1333. break;
  1334. case 4: /* HcInterruptEnable */
  1335. case 5: /* HcInterruptDisable */
  1336. retval = ohci->intr;
  1337. break;
  1338. case 6: /* HcHCCA */
  1339. retval = ohci->hcca;
  1340. break;
  1341. case 7: /* HcPeriodCurrentED */
  1342. retval = ohci->per_cur;
  1343. break;
  1344. case 8: /* HcControlHeadED */
  1345. retval = ohci->ctrl_head;
  1346. break;
  1347. case 9: /* HcControlCurrentED */
  1348. retval = ohci->ctrl_cur;
  1349. break;
  1350. case 10: /* HcBulkHeadED */
  1351. retval = ohci->bulk_head;
  1352. break;
  1353. case 11: /* HcBulkCurrentED */
  1354. retval = ohci->bulk_cur;
  1355. break;
  1356. case 12: /* HcDoneHead */
  1357. retval = ohci->done;
  1358. break;
  1359. case 13: /* HcFmInterretval */
  1360. retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
  1361. break;
  1362. case 14: /* HcFmRemaining */
  1363. retval = ohci_get_frame_remaining(ohci);
  1364. break;
  1365. case 15: /* HcFmNumber */
  1366. retval = ohci->frame_number;
  1367. break;
  1368. case 16: /* HcPeriodicStart */
  1369. retval = ohci->pstart;
  1370. break;
  1371. case 17: /* HcLSThreshold */
  1372. retval = ohci->lst;
  1373. break;
  1374. case 18: /* HcRhDescriptorA */
  1375. retval = ohci->rhdesc_a;
  1376. break;
  1377. case 19: /* HcRhDescriptorB */
  1378. retval = ohci->rhdesc_b;
  1379. break;
  1380. case 20: /* HcRhStatus */
  1381. retval = ohci->rhstatus;
  1382. break;
  1383. /* PXA27x specific registers */
  1384. case 24: /* HcStatus */
  1385. retval = ohci->hstatus & ohci->hmask;
  1386. break;
  1387. case 25: /* HcHReset */
  1388. retval = ohci->hreset;
  1389. break;
  1390. case 26: /* HcHInterruptEnable */
  1391. retval = ohci->hmask;
  1392. break;
  1393. case 27: /* HcHInterruptTest */
  1394. retval = ohci->htest;
  1395. break;
  1396. default:
  1397. trace_usb_ohci_mem_read_bad_offset(addr);
  1398. retval = 0xffffffff;
  1399. }
  1400. }
  1401. return retval;
  1402. }
  1403. static void ohci_mem_write(void *opaque,
  1404. hwaddr addr,
  1405. uint64_t val,
  1406. unsigned size)
  1407. {
  1408. OHCIState *ohci = opaque;
  1409. /* Only aligned reads are allowed on OHCI */
  1410. if (addr & 3) {
  1411. trace_usb_ohci_mem_write_unaligned(addr);
  1412. return;
  1413. }
  1414. if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1415. /* HcRhPortStatus */
  1416. ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
  1417. return;
  1418. }
  1419. switch (addr >> 2) {
  1420. case 1: /* HcControl */
  1421. ohci_set_ctl(ohci, val);
  1422. break;
  1423. case 2: /* HcCommandStatus */
  1424. /* SOC is read-only */
  1425. val = (val & ~OHCI_STATUS_SOC);
  1426. /* Bits written as '0' remain unchanged in the register */
  1427. ohci->status |= val;
  1428. if (ohci->status & OHCI_STATUS_HCR)
  1429. ohci_soft_reset(ohci);
  1430. break;
  1431. case 3: /* HcInterruptStatus */
  1432. ohci->intr_status &= ~val;
  1433. ohci_intr_update(ohci);
  1434. break;
  1435. case 4: /* HcInterruptEnable */
  1436. ohci->intr |= val;
  1437. ohci_intr_update(ohci);
  1438. break;
  1439. case 5: /* HcInterruptDisable */
  1440. ohci->intr &= ~val;
  1441. ohci_intr_update(ohci);
  1442. break;
  1443. case 6: /* HcHCCA */
  1444. ohci->hcca = val & OHCI_HCCA_MASK;
  1445. break;
  1446. case 7: /* HcPeriodCurrentED */
  1447. /* Ignore writes to this read-only register, Linux does them */
  1448. break;
  1449. case 8: /* HcControlHeadED */
  1450. ohci->ctrl_head = val & OHCI_EDPTR_MASK;
  1451. break;
  1452. case 9: /* HcControlCurrentED */
  1453. ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
  1454. break;
  1455. case 10: /* HcBulkHeadED */
  1456. ohci->bulk_head = val & OHCI_EDPTR_MASK;
  1457. break;
  1458. case 11: /* HcBulkCurrentED */
  1459. ohci->bulk_cur = val & OHCI_EDPTR_MASK;
  1460. break;
  1461. case 13: /* HcFmInterval */
  1462. ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
  1463. ohci->fit = (val & OHCI_FMI_FIT) >> 31;
  1464. ohci_set_frame_interval(ohci, val);
  1465. break;
  1466. case 15: /* HcFmNumber */
  1467. break;
  1468. case 16: /* HcPeriodicStart */
  1469. ohci->pstart = val & 0xffff;
  1470. break;
  1471. case 17: /* HcLSThreshold */
  1472. ohci->lst = val & 0xffff;
  1473. break;
  1474. case 18: /* HcRhDescriptorA */
  1475. ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
  1476. ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
  1477. break;
  1478. case 19: /* HcRhDescriptorB */
  1479. break;
  1480. case 20: /* HcRhStatus */
  1481. ohci_set_hub_status(ohci, val);
  1482. break;
  1483. /* PXA27x specific registers */
  1484. case 24: /* HcStatus */
  1485. ohci->hstatus &= ~(val & ohci->hmask);
  1486. break;
  1487. case 25: /* HcHReset */
  1488. ohci->hreset = val & ~OHCI_HRESET_FSBIR;
  1489. if (val & OHCI_HRESET_FSBIR)
  1490. ohci_hard_reset(ohci);
  1491. break;
  1492. case 26: /* HcHInterruptEnable */
  1493. ohci->hmask = val;
  1494. break;
  1495. case 27: /* HcHInterruptTest */
  1496. ohci->htest = val;
  1497. break;
  1498. default:
  1499. trace_usb_ohci_mem_write_bad_offset(addr);
  1500. break;
  1501. }
  1502. }
  1503. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev)
  1504. {
  1505. if (ohci->async_td &&
  1506. usb_packet_is_inflight(&ohci->usb_packet) &&
  1507. ohci->usb_packet.ep->dev == dev) {
  1508. usb_cancel_packet(&ohci->usb_packet);
  1509. ohci->async_td = 0;
  1510. }
  1511. }
  1512. static const MemoryRegionOps ohci_mem_ops = {
  1513. .read = ohci_mem_read,
  1514. .write = ohci_mem_write,
  1515. .endianness = DEVICE_LITTLE_ENDIAN,
  1516. };
  1517. static USBPortOps ohci_port_ops = {
  1518. .attach = ohci_attach,
  1519. .detach = ohci_detach,
  1520. .child_detach = ohci_child_detach,
  1521. .wakeup = ohci_wakeup,
  1522. .complete = ohci_async_complete_packet,
  1523. };
  1524. static USBBusOps ohci_bus_ops = {
  1525. };
  1526. void usb_ohci_init(OHCIState *ohci, DeviceState *dev, uint32_t num_ports,
  1527. dma_addr_t localmem_base, char *masterbus,
  1528. uint32_t firstport, AddressSpace *as,
  1529. void (*ohci_die_fn)(struct OHCIState *), Error **errp)
  1530. {
  1531. Error *err = NULL;
  1532. int i;
  1533. ohci->as = as;
  1534. ohci->ohci_die = ohci_die_fn;
  1535. if (num_ports > OHCI_MAX_PORTS) {
  1536. error_setg(errp, "OHCI num-ports=%u is too big (limit is %u ports)",
  1537. num_ports, OHCI_MAX_PORTS);
  1538. return;
  1539. }
  1540. if (usb_frame_time == 0) {
  1541. #ifdef OHCI_TIME_WARP
  1542. usb_frame_time = NANOSECONDS_PER_SECOND;
  1543. usb_bit_time = NANOSECONDS_PER_SECOND / (USB_HZ / 1000);
  1544. #else
  1545. usb_frame_time = NANOSECONDS_PER_SECOND / 1000;
  1546. if (NANOSECONDS_PER_SECOND >= USB_HZ) {
  1547. usb_bit_time = NANOSECONDS_PER_SECOND / USB_HZ;
  1548. } else {
  1549. usb_bit_time = 1;
  1550. }
  1551. #endif
  1552. trace_usb_ohci_init_time(usb_frame_time, usb_bit_time);
  1553. }
  1554. ohci->num_ports = num_ports;
  1555. if (masterbus) {
  1556. USBPort *ports[OHCI_MAX_PORTS];
  1557. for(i = 0; i < num_ports; i++) {
  1558. ports[i] = &ohci->rhport[i].port;
  1559. }
  1560. usb_register_companion(masterbus, ports, num_ports,
  1561. firstport, ohci, &ohci_port_ops,
  1562. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL,
  1563. &err);
  1564. if (err) {
  1565. error_propagate(errp, err);
  1566. return;
  1567. }
  1568. } else {
  1569. usb_bus_new(&ohci->bus, sizeof(ohci->bus), &ohci_bus_ops, dev);
  1570. for (i = 0; i < num_ports; i++) {
  1571. usb_register_port(&ohci->bus, &ohci->rhport[i].port,
  1572. ohci, i, &ohci_port_ops,
  1573. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1574. }
  1575. }
  1576. memory_region_init_io(&ohci->mem, OBJECT(dev), &ohci_mem_ops,
  1577. ohci, "ohci", 256);
  1578. ohci->localmem_base = localmem_base;
  1579. ohci->name = object_get_typename(OBJECT(dev));
  1580. usb_packet_init(&ohci->usb_packet);
  1581. ohci->async_td = 0;
  1582. ohci->eof_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  1583. ohci_frame_boundary, ohci);
  1584. }
  1585. /**
  1586. * A typical OHCI will stop operating and set itself into error state
  1587. * (which can be queried by MMIO) to signal that it got an error.
  1588. */
  1589. void ohci_sysbus_die(struct OHCIState *ohci)
  1590. {
  1591. trace_usb_ohci_die();
  1592. ohci_set_interrupt(ohci, OHCI_INTR_UE);
  1593. ohci_bus_stop(ohci);
  1594. }
  1595. #define TYPE_SYSBUS_OHCI "sysbus-ohci"
  1596. #define SYSBUS_OHCI(obj) OBJECT_CHECK(OHCISysBusState, (obj), TYPE_SYSBUS_OHCI)
  1597. typedef struct {
  1598. /*< private >*/
  1599. SysBusDevice parent_obj;
  1600. /*< public >*/
  1601. OHCIState ohci;
  1602. char *masterbus;
  1603. uint32_t num_ports;
  1604. uint32_t firstport;
  1605. dma_addr_t dma_offset;
  1606. } OHCISysBusState;
  1607. static void ohci_realize_pxa(DeviceState *dev, Error **errp)
  1608. {
  1609. OHCISysBusState *s = SYSBUS_OHCI(dev);
  1610. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1611. Error *err = NULL;
  1612. usb_ohci_init(&s->ohci, dev, s->num_ports, s->dma_offset,
  1613. s->masterbus, s->firstport,
  1614. &address_space_memory, ohci_sysbus_die, &err);
  1615. if (err) {
  1616. error_propagate(errp, err);
  1617. return;
  1618. }
  1619. sysbus_init_irq(sbd, &s->ohci.irq);
  1620. sysbus_init_mmio(sbd, &s->ohci.mem);
  1621. }
  1622. static void usb_ohci_reset_sysbus(DeviceState *dev)
  1623. {
  1624. OHCISysBusState *s = SYSBUS_OHCI(dev);
  1625. OHCIState *ohci = &s->ohci;
  1626. ohci_hard_reset(ohci);
  1627. }
  1628. static const VMStateDescription vmstate_ohci_state_port = {
  1629. .name = "ohci-core/port",
  1630. .version_id = 1,
  1631. .minimum_version_id = 1,
  1632. .fields = (VMStateField[]) {
  1633. VMSTATE_UINT32(ctrl, OHCIPort),
  1634. VMSTATE_END_OF_LIST()
  1635. },
  1636. };
  1637. static bool ohci_eof_timer_needed(void *opaque)
  1638. {
  1639. OHCIState *ohci = opaque;
  1640. return timer_pending(ohci->eof_timer);
  1641. }
  1642. static const VMStateDescription vmstate_ohci_eof_timer = {
  1643. .name = "ohci-core/eof-timer",
  1644. .version_id = 1,
  1645. .minimum_version_id = 1,
  1646. .needed = ohci_eof_timer_needed,
  1647. .fields = (VMStateField[]) {
  1648. VMSTATE_TIMER_PTR(eof_timer, OHCIState),
  1649. VMSTATE_END_OF_LIST()
  1650. },
  1651. };
  1652. const VMStateDescription vmstate_ohci_state = {
  1653. .name = "ohci-core",
  1654. .version_id = 1,
  1655. .minimum_version_id = 1,
  1656. .fields = (VMStateField[]) {
  1657. VMSTATE_INT64(sof_time, OHCIState),
  1658. VMSTATE_UINT32(ctl, OHCIState),
  1659. VMSTATE_UINT32(status, OHCIState),
  1660. VMSTATE_UINT32(intr_status, OHCIState),
  1661. VMSTATE_UINT32(intr, OHCIState),
  1662. VMSTATE_UINT32(hcca, OHCIState),
  1663. VMSTATE_UINT32(ctrl_head, OHCIState),
  1664. VMSTATE_UINT32(ctrl_cur, OHCIState),
  1665. VMSTATE_UINT32(bulk_head, OHCIState),
  1666. VMSTATE_UINT32(bulk_cur, OHCIState),
  1667. VMSTATE_UINT32(per_cur, OHCIState),
  1668. VMSTATE_UINT32(done, OHCIState),
  1669. VMSTATE_INT32(done_count, OHCIState),
  1670. VMSTATE_UINT16(fsmps, OHCIState),
  1671. VMSTATE_UINT8(fit, OHCIState),
  1672. VMSTATE_UINT16(fi, OHCIState),
  1673. VMSTATE_UINT8(frt, OHCIState),
  1674. VMSTATE_UINT16(frame_number, OHCIState),
  1675. VMSTATE_UINT16(padding, OHCIState),
  1676. VMSTATE_UINT32(pstart, OHCIState),
  1677. VMSTATE_UINT32(lst, OHCIState),
  1678. VMSTATE_UINT32(rhdesc_a, OHCIState),
  1679. VMSTATE_UINT32(rhdesc_b, OHCIState),
  1680. VMSTATE_UINT32(rhstatus, OHCIState),
  1681. VMSTATE_STRUCT_ARRAY(rhport, OHCIState, OHCI_MAX_PORTS, 0,
  1682. vmstate_ohci_state_port, OHCIPort),
  1683. VMSTATE_UINT32(hstatus, OHCIState),
  1684. VMSTATE_UINT32(hmask, OHCIState),
  1685. VMSTATE_UINT32(hreset, OHCIState),
  1686. VMSTATE_UINT32(htest, OHCIState),
  1687. VMSTATE_UINT32(old_ctl, OHCIState),
  1688. VMSTATE_UINT8_ARRAY(usb_buf, OHCIState, 8192),
  1689. VMSTATE_UINT32(async_td, OHCIState),
  1690. VMSTATE_BOOL(async_complete, OHCIState),
  1691. VMSTATE_END_OF_LIST()
  1692. },
  1693. .subsections = (const VMStateDescription*[]) {
  1694. &vmstate_ohci_eof_timer,
  1695. NULL
  1696. }
  1697. };
  1698. static Property ohci_sysbus_properties[] = {
  1699. DEFINE_PROP_STRING("masterbus", OHCISysBusState, masterbus),
  1700. DEFINE_PROP_UINT32("num-ports", OHCISysBusState, num_ports, 3),
  1701. DEFINE_PROP_UINT32("firstport", OHCISysBusState, firstport, 0),
  1702. DEFINE_PROP_DMAADDR("dma-offset", OHCISysBusState, dma_offset, 0),
  1703. DEFINE_PROP_END_OF_LIST(),
  1704. };
  1705. static void ohci_sysbus_class_init(ObjectClass *klass, void *data)
  1706. {
  1707. DeviceClass *dc = DEVICE_CLASS(klass);
  1708. dc->realize = ohci_realize_pxa;
  1709. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  1710. dc->desc = "OHCI USB Controller";
  1711. dc->props = ohci_sysbus_properties;
  1712. dc->reset = usb_ohci_reset_sysbus;
  1713. }
  1714. static const TypeInfo ohci_sysbus_info = {
  1715. .name = TYPE_SYSBUS_OHCI,
  1716. .parent = TYPE_SYS_BUS_DEVICE,
  1717. .instance_size = sizeof(OHCISysBusState),
  1718. .class_init = ohci_sysbus_class_init,
  1719. };
  1720. static void ohci_register_types(void)
  1721. {
  1722. type_register_static(&ohci_sysbus_info);
  1723. }
  1724. type_init(ohci_register_types)