usb-ohci.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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 Handle unrecoverable errors properly
  26. * o BIOS work to boot from USB storage
  27. */
  28. #include "hw.h"
  29. #include "qemu-timer.h"
  30. #include "usb.h"
  31. #include "pci.h"
  32. #include "usb-ohci.h"
  33. #include "sysbus.h"
  34. #include "qdev-addr.h"
  35. //#define DEBUG_OHCI
  36. /* Dump packet contents. */
  37. //#define DEBUG_PACKET
  38. //#define DEBUG_ISOCH
  39. /* This causes frames to occur 1000x slower */
  40. //#define OHCI_TIME_WARP 1
  41. #ifdef DEBUG_OHCI
  42. #define DPRINTF printf
  43. #else
  44. #define DPRINTF(...)
  45. #endif
  46. /* Number of Downstream Ports on the root hub. */
  47. #define OHCI_MAX_PORTS 15
  48. static int64_t usb_frame_time;
  49. static int64_t usb_bit_time;
  50. typedef struct OHCIPort {
  51. USBPort port;
  52. uint32_t ctrl;
  53. } OHCIPort;
  54. typedef struct {
  55. USBBus bus;
  56. qemu_irq irq;
  57. int mem;
  58. int num_ports;
  59. const char *name;
  60. QEMUTimer *eof_timer;
  61. int64_t sof_time;
  62. /* OHCI state */
  63. /* Control partition */
  64. uint32_t ctl, status;
  65. uint32_t intr_status;
  66. uint32_t intr;
  67. /* memory pointer partition */
  68. uint32_t hcca;
  69. uint32_t ctrl_head, ctrl_cur;
  70. uint32_t bulk_head, bulk_cur;
  71. uint32_t per_cur;
  72. uint32_t done;
  73. int done_count;
  74. /* Frame counter partition */
  75. uint32_t fsmps:15;
  76. uint32_t fit:1;
  77. uint32_t fi:14;
  78. uint32_t frt:1;
  79. uint16_t frame_number;
  80. uint16_t padding;
  81. uint32_t pstart;
  82. uint32_t lst;
  83. /* Root Hub partition */
  84. uint32_t rhdesc_a, rhdesc_b;
  85. uint32_t rhstatus;
  86. OHCIPort rhport[OHCI_MAX_PORTS];
  87. /* PXA27x Non-OHCI events */
  88. uint32_t hstatus;
  89. uint32_t hmask;
  90. uint32_t hreset;
  91. uint32_t htest;
  92. /* SM501 local memory offset */
  93. target_phys_addr_t localmem_base;
  94. /* Active packets. */
  95. uint32_t old_ctl;
  96. USBPacket usb_packet;
  97. uint8_t usb_buf[8192];
  98. uint32_t async_td;
  99. int async_complete;
  100. } OHCIState;
  101. /* Host Controller Communications Area */
  102. struct ohci_hcca {
  103. uint32_t intr[32];
  104. uint16_t frame, pad;
  105. uint32_t done;
  106. };
  107. static void ohci_bus_stop(OHCIState *ohci);
  108. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev);
  109. /* Bitfields for the first word of an Endpoint Desciptor. */
  110. #define OHCI_ED_FA_SHIFT 0
  111. #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
  112. #define OHCI_ED_EN_SHIFT 7
  113. #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
  114. #define OHCI_ED_D_SHIFT 11
  115. #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
  116. #define OHCI_ED_S (1<<13)
  117. #define OHCI_ED_K (1<<14)
  118. #define OHCI_ED_F (1<<15)
  119. #define OHCI_ED_MPS_SHIFT 16
  120. #define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
  121. /* Flags in the head field of an Endpoint Desciptor. */
  122. #define OHCI_ED_H 1
  123. #define OHCI_ED_C 2
  124. /* Bitfields for the first word of a Transfer Desciptor. */
  125. #define OHCI_TD_R (1<<18)
  126. #define OHCI_TD_DP_SHIFT 19
  127. #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
  128. #define OHCI_TD_DI_SHIFT 21
  129. #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
  130. #define OHCI_TD_T0 (1<<24)
  131. #define OHCI_TD_T1 (1<<24)
  132. #define OHCI_TD_EC_SHIFT 26
  133. #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
  134. #define OHCI_TD_CC_SHIFT 28
  135. #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
  136. /* Bitfields for the first word of an Isochronous Transfer Desciptor. */
  137. /* CC & DI - same as in the General Transfer Desciptor */
  138. #define OHCI_TD_SF_SHIFT 0
  139. #define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
  140. #define OHCI_TD_FC_SHIFT 24
  141. #define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
  142. /* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
  143. #define OHCI_TD_PSW_CC_SHIFT 12
  144. #define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
  145. #define OHCI_TD_PSW_SIZE_SHIFT 0
  146. #define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
  147. #define OHCI_PAGE_MASK 0xfffff000
  148. #define OHCI_OFFSET_MASK 0xfff
  149. #define OHCI_DPTR_MASK 0xfffffff0
  150. #define OHCI_BM(val, field) \
  151. (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
  152. #define OHCI_SET_BM(val, field, newval) do { \
  153. val &= ~OHCI_##field##_MASK; \
  154. val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
  155. } while(0)
  156. /* endpoint descriptor */
  157. struct ohci_ed {
  158. uint32_t flags;
  159. uint32_t tail;
  160. uint32_t head;
  161. uint32_t next;
  162. };
  163. /* General transfer descriptor */
  164. struct ohci_td {
  165. uint32_t flags;
  166. uint32_t cbp;
  167. uint32_t next;
  168. uint32_t be;
  169. };
  170. /* Isochronous transfer descriptor */
  171. struct ohci_iso_td {
  172. uint32_t flags;
  173. uint32_t bp;
  174. uint32_t next;
  175. uint32_t be;
  176. uint16_t offset[8];
  177. };
  178. #define USB_HZ 12000000
  179. /* OHCI Local stuff */
  180. #define OHCI_CTL_CBSR ((1<<0)|(1<<1))
  181. #define OHCI_CTL_PLE (1<<2)
  182. #define OHCI_CTL_IE (1<<3)
  183. #define OHCI_CTL_CLE (1<<4)
  184. #define OHCI_CTL_BLE (1<<5)
  185. #define OHCI_CTL_HCFS ((1<<6)|(1<<7))
  186. #define OHCI_USB_RESET 0x00
  187. #define OHCI_USB_RESUME 0x40
  188. #define OHCI_USB_OPERATIONAL 0x80
  189. #define OHCI_USB_SUSPEND 0xc0
  190. #define OHCI_CTL_IR (1<<8)
  191. #define OHCI_CTL_RWC (1<<9)
  192. #define OHCI_CTL_RWE (1<<10)
  193. #define OHCI_STATUS_HCR (1<<0)
  194. #define OHCI_STATUS_CLF (1<<1)
  195. #define OHCI_STATUS_BLF (1<<2)
  196. #define OHCI_STATUS_OCR (1<<3)
  197. #define OHCI_STATUS_SOC ((1<<6)|(1<<7))
  198. #define OHCI_INTR_SO (1<<0) /* Scheduling overrun */
  199. #define OHCI_INTR_WD (1<<1) /* HcDoneHead writeback */
  200. #define OHCI_INTR_SF (1<<2) /* Start of frame */
  201. #define OHCI_INTR_RD (1<<3) /* Resume detect */
  202. #define OHCI_INTR_UE (1<<4) /* Unrecoverable error */
  203. #define OHCI_INTR_FNO (1<<5) /* Frame number overflow */
  204. #define OHCI_INTR_RHSC (1<<6) /* Root hub status change */
  205. #define OHCI_INTR_OC (1<<30) /* Ownership change */
  206. #define OHCI_INTR_MIE (1<<31) /* Master Interrupt Enable */
  207. #define OHCI_HCCA_SIZE 0x100
  208. #define OHCI_HCCA_MASK 0xffffff00
  209. #define OHCI_EDPTR_MASK 0xfffffff0
  210. #define OHCI_FMI_FI 0x00003fff
  211. #define OHCI_FMI_FSMPS 0xffff0000
  212. #define OHCI_FMI_FIT 0x80000000
  213. #define OHCI_FR_RT (1<<31)
  214. #define OHCI_LS_THRESH 0x628
  215. #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
  216. #define OHCI_RHA_PSM (1<<8)
  217. #define OHCI_RHA_NPS (1<<9)
  218. #define OHCI_RHA_DT (1<<10)
  219. #define OHCI_RHA_OCPM (1<<11)
  220. #define OHCI_RHA_NOCP (1<<12)
  221. #define OHCI_RHA_POTPGT_MASK 0xff000000
  222. #define OHCI_RHS_LPS (1<<0)
  223. #define OHCI_RHS_OCI (1<<1)
  224. #define OHCI_RHS_DRWE (1<<15)
  225. #define OHCI_RHS_LPSC (1<<16)
  226. #define OHCI_RHS_OCIC (1<<17)
  227. #define OHCI_RHS_CRWE (1<<31)
  228. #define OHCI_PORT_CCS (1<<0)
  229. #define OHCI_PORT_PES (1<<1)
  230. #define OHCI_PORT_PSS (1<<2)
  231. #define OHCI_PORT_POCI (1<<3)
  232. #define OHCI_PORT_PRS (1<<4)
  233. #define OHCI_PORT_PPS (1<<8)
  234. #define OHCI_PORT_LSDA (1<<9)
  235. #define OHCI_PORT_CSC (1<<16)
  236. #define OHCI_PORT_PESC (1<<17)
  237. #define OHCI_PORT_PSSC (1<<18)
  238. #define OHCI_PORT_OCIC (1<<19)
  239. #define OHCI_PORT_PRSC (1<<20)
  240. #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
  241. |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
  242. #define OHCI_TD_DIR_SETUP 0x0
  243. #define OHCI_TD_DIR_OUT 0x1
  244. #define OHCI_TD_DIR_IN 0x2
  245. #define OHCI_TD_DIR_RESERVED 0x3
  246. #define OHCI_CC_NOERROR 0x0
  247. #define OHCI_CC_CRC 0x1
  248. #define OHCI_CC_BITSTUFFING 0x2
  249. #define OHCI_CC_DATATOGGLEMISMATCH 0x3
  250. #define OHCI_CC_STALL 0x4
  251. #define OHCI_CC_DEVICENOTRESPONDING 0x5
  252. #define OHCI_CC_PIDCHECKFAILURE 0x6
  253. #define OHCI_CC_UNDEXPETEDPID 0x7
  254. #define OHCI_CC_DATAOVERRUN 0x8
  255. #define OHCI_CC_DATAUNDERRUN 0x9
  256. #define OHCI_CC_BUFFEROVERRUN 0xc
  257. #define OHCI_CC_BUFFERUNDERRUN 0xd
  258. #define OHCI_HRESET_FSBIR (1 << 0)
  259. /* Update IRQ levels */
  260. static inline void ohci_intr_update(OHCIState *ohci)
  261. {
  262. int level = 0;
  263. if ((ohci->intr & OHCI_INTR_MIE) &&
  264. (ohci->intr_status & ohci->intr))
  265. level = 1;
  266. qemu_set_irq(ohci->irq, level);
  267. }
  268. /* Set an interrupt */
  269. static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr)
  270. {
  271. ohci->intr_status |= intr;
  272. ohci_intr_update(ohci);
  273. }
  274. /* Attach or detach a device on a root hub port. */
  275. static void ohci_attach(USBPort *port1)
  276. {
  277. OHCIState *s = port1->opaque;
  278. OHCIPort *port = &s->rhport[port1->index];
  279. uint32_t old_state = port->ctrl;
  280. /* set connect status */
  281. port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC;
  282. /* update speed */
  283. if (port->port.dev->speed == USB_SPEED_LOW) {
  284. port->ctrl |= OHCI_PORT_LSDA;
  285. } else {
  286. port->ctrl &= ~OHCI_PORT_LSDA;
  287. }
  288. /* notify of remote-wakeup */
  289. if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
  290. ohci_set_interrupt(s, OHCI_INTR_RD);
  291. }
  292. DPRINTF("usb-ohci: Attached port %d\n", port1->index);
  293. if (old_state != port->ctrl) {
  294. ohci_set_interrupt(s, OHCI_INTR_RHSC);
  295. }
  296. }
  297. static void ohci_detach(USBPort *port1)
  298. {
  299. OHCIState *s = port1->opaque;
  300. OHCIPort *port = &s->rhport[port1->index];
  301. uint32_t old_state = port->ctrl;
  302. ohci_async_cancel_device(s, port1->dev);
  303. /* set connect status */
  304. if (port->ctrl & OHCI_PORT_CCS) {
  305. port->ctrl &= ~OHCI_PORT_CCS;
  306. port->ctrl |= OHCI_PORT_CSC;
  307. }
  308. /* disable port */
  309. if (port->ctrl & OHCI_PORT_PES) {
  310. port->ctrl &= ~OHCI_PORT_PES;
  311. port->ctrl |= OHCI_PORT_PESC;
  312. }
  313. DPRINTF("usb-ohci: Detached port %d\n", port1->index);
  314. if (old_state != port->ctrl) {
  315. ohci_set_interrupt(s, OHCI_INTR_RHSC);
  316. }
  317. }
  318. static void ohci_wakeup(USBPort *port1)
  319. {
  320. OHCIState *s = port1->opaque;
  321. OHCIPort *port = &s->rhport[port1->index];
  322. uint32_t intr = 0;
  323. if (port->ctrl & OHCI_PORT_PSS) {
  324. DPRINTF("usb-ohci: port %d: wakeup\n", port1->index);
  325. port->ctrl |= OHCI_PORT_PSSC;
  326. port->ctrl &= ~OHCI_PORT_PSS;
  327. intr = OHCI_INTR_RHSC;
  328. }
  329. /* Note that the controller can be suspended even if this port is not */
  330. if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
  331. DPRINTF("usb-ohci: remote-wakeup: SUSPEND->RESUME\n");
  332. /* This is the one state transition the controller can do by itself */
  333. s->ctl &= ~OHCI_CTL_HCFS;
  334. s->ctl |= OHCI_USB_RESUME;
  335. /* In suspend mode only ResumeDetected is possible, not RHSC:
  336. * see the OHCI spec 5.1.2.3.
  337. */
  338. intr = OHCI_INTR_RD;
  339. }
  340. ohci_set_interrupt(s, intr);
  341. }
  342. static void ohci_child_detach(USBPort *port1, USBDevice *child)
  343. {
  344. OHCIState *s = port1->opaque;
  345. ohci_async_cancel_device(s, child);
  346. }
  347. /* Reset the controller */
  348. static void ohci_reset(void *opaque)
  349. {
  350. OHCIState *ohci = opaque;
  351. OHCIPort *port;
  352. int i;
  353. ohci_bus_stop(ohci);
  354. ohci->ctl = 0;
  355. ohci->old_ctl = 0;
  356. ohci->status = 0;
  357. ohci->intr_status = 0;
  358. ohci->intr = OHCI_INTR_MIE;
  359. ohci->hcca = 0;
  360. ohci->ctrl_head = ohci->ctrl_cur = 0;
  361. ohci->bulk_head = ohci->bulk_cur = 0;
  362. ohci->per_cur = 0;
  363. ohci->done = 0;
  364. ohci->done_count = 7;
  365. /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
  366. * I took the value linux sets ...
  367. */
  368. ohci->fsmps = 0x2778;
  369. ohci->fi = 0x2edf;
  370. ohci->fit = 0;
  371. ohci->frt = 0;
  372. ohci->frame_number = 0;
  373. ohci->pstart = 0;
  374. ohci->lst = OHCI_LS_THRESH;
  375. ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
  376. ohci->rhdesc_b = 0x0; /* Impl. specific */
  377. ohci->rhstatus = 0;
  378. for (i = 0; i < ohci->num_ports; i++)
  379. {
  380. port = &ohci->rhport[i];
  381. port->ctrl = 0;
  382. if (port->port.dev) {
  383. usb_attach(&port->port, port->port.dev);
  384. }
  385. }
  386. if (ohci->async_td) {
  387. usb_cancel_packet(&ohci->usb_packet);
  388. ohci->async_td = 0;
  389. }
  390. DPRINTF("usb-ohci: Reset %s\n", ohci->name);
  391. }
  392. /* Get an array of dwords from main memory */
  393. static inline int get_dwords(OHCIState *ohci,
  394. uint32_t addr, uint32_t *buf, int num)
  395. {
  396. int i;
  397. addr += ohci->localmem_base;
  398. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  399. cpu_physical_memory_read(addr, buf, sizeof(*buf));
  400. *buf = le32_to_cpu(*buf);
  401. }
  402. return 1;
  403. }
  404. /* Put an array of dwords in to main memory */
  405. static inline int put_dwords(OHCIState *ohci,
  406. uint32_t addr, uint32_t *buf, int num)
  407. {
  408. int i;
  409. addr += ohci->localmem_base;
  410. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  411. uint32_t tmp = cpu_to_le32(*buf);
  412. cpu_physical_memory_write(addr, &tmp, sizeof(tmp));
  413. }
  414. return 1;
  415. }
  416. /* Get an array of words from main memory */
  417. static inline int get_words(OHCIState *ohci,
  418. uint32_t addr, uint16_t *buf, int num)
  419. {
  420. int i;
  421. addr += ohci->localmem_base;
  422. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  423. cpu_physical_memory_read(addr, buf, sizeof(*buf));
  424. *buf = le16_to_cpu(*buf);
  425. }
  426. return 1;
  427. }
  428. /* Put an array of words in to main memory */
  429. static inline int put_words(OHCIState *ohci,
  430. uint32_t addr, uint16_t *buf, int num)
  431. {
  432. int i;
  433. addr += ohci->localmem_base;
  434. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  435. uint16_t tmp = cpu_to_le16(*buf);
  436. cpu_physical_memory_write(addr, &tmp, sizeof(tmp));
  437. }
  438. return 1;
  439. }
  440. static inline int ohci_read_ed(OHCIState *ohci,
  441. uint32_t addr, struct ohci_ed *ed)
  442. {
  443. return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
  444. }
  445. static inline int ohci_read_td(OHCIState *ohci,
  446. uint32_t addr, struct ohci_td *td)
  447. {
  448. return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
  449. }
  450. static inline int ohci_read_iso_td(OHCIState *ohci,
  451. uint32_t addr, struct ohci_iso_td *td)
  452. {
  453. return (get_dwords(ohci, addr, (uint32_t *)td, 4) &&
  454. get_words(ohci, addr + 16, td->offset, 8));
  455. }
  456. static inline int ohci_read_hcca(OHCIState *ohci,
  457. uint32_t addr, struct ohci_hcca *hcca)
  458. {
  459. cpu_physical_memory_read(addr + ohci->localmem_base, hcca, sizeof(*hcca));
  460. return 1;
  461. }
  462. static inline int ohci_put_ed(OHCIState *ohci,
  463. uint32_t addr, struct ohci_ed *ed)
  464. {
  465. return put_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
  466. }
  467. static inline int ohci_put_td(OHCIState *ohci,
  468. uint32_t addr, struct ohci_td *td)
  469. {
  470. return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
  471. }
  472. static inline int ohci_put_iso_td(OHCIState *ohci,
  473. uint32_t addr, struct ohci_iso_td *td)
  474. {
  475. return (put_dwords(ohci, addr, (uint32_t *)td, 4) &&
  476. put_words(ohci, addr + 16, td->offset, 8));
  477. }
  478. static inline int ohci_put_hcca(OHCIState *ohci,
  479. uint32_t addr, struct ohci_hcca *hcca)
  480. {
  481. cpu_physical_memory_write(addr + ohci->localmem_base, hcca, sizeof(*hcca));
  482. return 1;
  483. }
  484. /* Read/Write the contents of a TD from/to main memory. */
  485. static void ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
  486. uint8_t *buf, int len, int write)
  487. {
  488. uint32_t ptr;
  489. uint32_t n;
  490. ptr = td->cbp;
  491. n = 0x1000 - (ptr & 0xfff);
  492. if (n > len)
  493. n = len;
  494. cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
  495. if (n == len)
  496. return;
  497. ptr = td->be & ~0xfffu;
  498. buf += n;
  499. cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
  500. }
  501. /* Read/Write the contents of an ISO TD from/to main memory. */
  502. static void ohci_copy_iso_td(OHCIState *ohci,
  503. uint32_t start_addr, uint32_t end_addr,
  504. uint8_t *buf, int len, int write)
  505. {
  506. uint32_t ptr;
  507. uint32_t n;
  508. ptr = start_addr;
  509. n = 0x1000 - (ptr & 0xfff);
  510. if (n > len)
  511. n = len;
  512. cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, n, write);
  513. if (n == len)
  514. return;
  515. ptr = end_addr & ~0xfffu;
  516. buf += n;
  517. cpu_physical_memory_rw(ptr + ohci->localmem_base, buf, len - n, write);
  518. }
  519. static void ohci_process_lists(OHCIState *ohci, int completion);
  520. static void ohci_async_complete_packet(USBPort *port, USBPacket *packet)
  521. {
  522. OHCIState *ohci = container_of(packet, OHCIState, usb_packet);
  523. #ifdef DEBUG_PACKET
  524. DPRINTF("Async packet complete\n");
  525. #endif
  526. ohci->async_complete = 1;
  527. ohci_process_lists(ohci, 1);
  528. }
  529. #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
  530. static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
  531. int completion)
  532. {
  533. int dir;
  534. size_t len = 0;
  535. #ifdef DEBUG_ISOCH
  536. const char *str = NULL;
  537. #endif
  538. int pid;
  539. int ret;
  540. int i;
  541. USBDevice *dev;
  542. struct ohci_iso_td iso_td;
  543. uint32_t addr;
  544. uint16_t starting_frame;
  545. int16_t relative_frame_number;
  546. int frame_count;
  547. uint32_t start_offset, next_offset, end_offset = 0;
  548. uint32_t start_addr, end_addr;
  549. addr = ed->head & OHCI_DPTR_MASK;
  550. if (!ohci_read_iso_td(ohci, addr, &iso_td)) {
  551. printf("usb-ohci: ISO_TD read error at %x\n", addr);
  552. return 0;
  553. }
  554. starting_frame = OHCI_BM(iso_td.flags, TD_SF);
  555. frame_count = OHCI_BM(iso_td.flags, TD_FC);
  556. relative_frame_number = USUB(ohci->frame_number, starting_frame);
  557. #ifdef DEBUG_ISOCH
  558. printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n"
  559. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  560. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  561. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  562. "frame_number 0x%.8x starting_frame 0x%.8x\n"
  563. "frame_count 0x%.8x relative %d\n"
  564. "di 0x%.8x cc 0x%.8x\n",
  565. ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK,
  566. iso_td.flags, iso_td.bp, iso_td.next, iso_td.be,
  567. iso_td.offset[0], iso_td.offset[1], iso_td.offset[2], iso_td.offset[3],
  568. iso_td.offset[4], iso_td.offset[5], iso_td.offset[6], iso_td.offset[7],
  569. ohci->frame_number, starting_frame,
  570. frame_count, relative_frame_number,
  571. OHCI_BM(iso_td.flags, TD_DI), OHCI_BM(iso_td.flags, TD_CC));
  572. #endif
  573. if (relative_frame_number < 0) {
  574. DPRINTF("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number);
  575. return 1;
  576. } else if (relative_frame_number > frame_count) {
  577. /* ISO TD expired - retire the TD to the Done Queue and continue with
  578. the next ISO TD of the same ED */
  579. DPRINTF("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number,
  580. frame_count);
  581. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  582. ed->head &= ~OHCI_DPTR_MASK;
  583. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  584. iso_td.next = ohci->done;
  585. ohci->done = addr;
  586. i = OHCI_BM(iso_td.flags, TD_DI);
  587. if (i < ohci->done_count)
  588. ohci->done_count = i;
  589. ohci_put_iso_td(ohci, addr, &iso_td);
  590. return 0;
  591. }
  592. dir = OHCI_BM(ed->flags, ED_D);
  593. switch (dir) {
  594. case OHCI_TD_DIR_IN:
  595. #ifdef DEBUG_ISOCH
  596. str = "in";
  597. #endif
  598. pid = USB_TOKEN_IN;
  599. break;
  600. case OHCI_TD_DIR_OUT:
  601. #ifdef DEBUG_ISOCH
  602. str = "out";
  603. #endif
  604. pid = USB_TOKEN_OUT;
  605. break;
  606. case OHCI_TD_DIR_SETUP:
  607. #ifdef DEBUG_ISOCH
  608. str = "setup";
  609. #endif
  610. pid = USB_TOKEN_SETUP;
  611. break;
  612. default:
  613. printf("usb-ohci: Bad direction %d\n", dir);
  614. return 1;
  615. }
  616. if (!iso_td.bp || !iso_td.be) {
  617. printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td.bp, iso_td.be);
  618. return 1;
  619. }
  620. start_offset = iso_td.offset[relative_frame_number];
  621. next_offset = iso_td.offset[relative_frame_number + 1];
  622. if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
  623. ((relative_frame_number < frame_count) &&
  624. !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
  625. printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
  626. start_offset, next_offset);
  627. return 1;
  628. }
  629. if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
  630. printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
  631. start_offset, next_offset);
  632. return 1;
  633. }
  634. if ((start_offset & 0x1000) == 0) {
  635. start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  636. (start_offset & OHCI_OFFSET_MASK);
  637. } else {
  638. start_addr = (iso_td.be & OHCI_PAGE_MASK) |
  639. (start_offset & OHCI_OFFSET_MASK);
  640. }
  641. if (relative_frame_number < frame_count) {
  642. end_offset = next_offset - 1;
  643. if ((end_offset & 0x1000) == 0) {
  644. end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  645. (end_offset & OHCI_OFFSET_MASK);
  646. } else {
  647. end_addr = (iso_td.be & OHCI_PAGE_MASK) |
  648. (end_offset & OHCI_OFFSET_MASK);
  649. }
  650. } else {
  651. /* Last packet in the ISO TD */
  652. end_addr = iso_td.be;
  653. }
  654. if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
  655. len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
  656. - (start_addr & OHCI_OFFSET_MASK);
  657. } else {
  658. len = end_addr - start_addr + 1;
  659. }
  660. if (len && dir != OHCI_TD_DIR_IN) {
  661. ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, len, 0);
  662. }
  663. if (completion) {
  664. ret = ohci->usb_packet.len;
  665. } else {
  666. ret = USB_RET_NODEV;
  667. for (i = 0; i < ohci->num_ports; i++) {
  668. dev = ohci->rhport[i].port.dev;
  669. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
  670. continue;
  671. ohci->usb_packet.pid = pid;
  672. ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
  673. ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
  674. ohci->usb_packet.data = ohci->usb_buf;
  675. ohci->usb_packet.len = len;
  676. ret = usb_handle_packet(dev, &ohci->usb_packet);
  677. if (ret != USB_RET_NODEV)
  678. break;
  679. }
  680. if (ret == USB_RET_ASYNC) {
  681. return 1;
  682. }
  683. }
  684. #ifdef DEBUG_ISOCH
  685. printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
  686. start_offset, end_offset, start_addr, end_addr, str, len, ret);
  687. #endif
  688. /* Writeback */
  689. if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
  690. /* IN transfer succeeded */
  691. ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret, 1);
  692. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  693. OHCI_CC_NOERROR);
  694. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
  695. } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
  696. /* OUT transfer succeeded */
  697. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  698. OHCI_CC_NOERROR);
  699. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
  700. } else {
  701. if (ret > (ssize_t) len) {
  702. printf("usb-ohci: DataOverrun %d > %zu\n", ret, len);
  703. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  704. OHCI_CC_DATAOVERRUN);
  705. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  706. len);
  707. } else if (ret >= 0) {
  708. printf("usb-ohci: DataUnderrun %d\n", ret);
  709. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  710. OHCI_CC_DATAUNDERRUN);
  711. } else {
  712. switch (ret) {
  713. case USB_RET_NODEV:
  714. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  715. OHCI_CC_DEVICENOTRESPONDING);
  716. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  717. 0);
  718. break;
  719. case USB_RET_NAK:
  720. case USB_RET_STALL:
  721. printf("usb-ohci: got NAK/STALL %d\n", ret);
  722. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  723. OHCI_CC_STALL);
  724. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  725. 0);
  726. break;
  727. default:
  728. printf("usb-ohci: Bad device response %d\n", ret);
  729. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  730. OHCI_CC_UNDEXPETEDPID);
  731. break;
  732. }
  733. }
  734. }
  735. if (relative_frame_number == frame_count) {
  736. /* Last data packet of ISO TD - retire the TD to the Done Queue */
  737. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
  738. ed->head &= ~OHCI_DPTR_MASK;
  739. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  740. iso_td.next = ohci->done;
  741. ohci->done = addr;
  742. i = OHCI_BM(iso_td.flags, TD_DI);
  743. if (i < ohci->done_count)
  744. ohci->done_count = i;
  745. }
  746. ohci_put_iso_td(ohci, addr, &iso_td);
  747. return 1;
  748. }
  749. /* Service a transport descriptor.
  750. Returns nonzero to terminate processing of this endpoint. */
  751. static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
  752. {
  753. int dir;
  754. size_t len = 0;
  755. #ifdef DEBUG_PACKET
  756. const char *str = NULL;
  757. #endif
  758. int pid;
  759. int ret;
  760. int i;
  761. USBDevice *dev;
  762. struct ohci_td td;
  763. uint32_t addr;
  764. int flag_r;
  765. int completion;
  766. addr = ed->head & OHCI_DPTR_MASK;
  767. /* See if this TD has already been submitted to the device. */
  768. completion = (addr == ohci->async_td);
  769. if (completion && !ohci->async_complete) {
  770. #ifdef DEBUG_PACKET
  771. DPRINTF("Skipping async TD\n");
  772. #endif
  773. return 1;
  774. }
  775. if (!ohci_read_td(ohci, addr, &td)) {
  776. fprintf(stderr, "usb-ohci: TD read error at %x\n", addr);
  777. return 0;
  778. }
  779. dir = OHCI_BM(ed->flags, ED_D);
  780. switch (dir) {
  781. case OHCI_TD_DIR_OUT:
  782. case OHCI_TD_DIR_IN:
  783. /* Same value. */
  784. break;
  785. default:
  786. dir = OHCI_BM(td.flags, TD_DP);
  787. break;
  788. }
  789. switch (dir) {
  790. case OHCI_TD_DIR_IN:
  791. #ifdef DEBUG_PACKET
  792. str = "in";
  793. #endif
  794. pid = USB_TOKEN_IN;
  795. break;
  796. case OHCI_TD_DIR_OUT:
  797. #ifdef DEBUG_PACKET
  798. str = "out";
  799. #endif
  800. pid = USB_TOKEN_OUT;
  801. break;
  802. case OHCI_TD_DIR_SETUP:
  803. #ifdef DEBUG_PACKET
  804. str = "setup";
  805. #endif
  806. pid = USB_TOKEN_SETUP;
  807. break;
  808. default:
  809. fprintf(stderr, "usb-ohci: Bad direction\n");
  810. return 1;
  811. }
  812. if (td.cbp && td.be) {
  813. if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
  814. len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
  815. } else {
  816. len = (td.be - td.cbp) + 1;
  817. }
  818. if (len && dir != OHCI_TD_DIR_IN && !completion) {
  819. ohci_copy_td(ohci, &td, ohci->usb_buf, len, 0);
  820. }
  821. }
  822. flag_r = (td.flags & OHCI_TD_R) != 0;
  823. #ifdef DEBUG_PACKET
  824. DPRINTF(" TD @ 0x%.8x %" PRId64 " bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
  825. addr, (int64_t)len, str, flag_r, td.cbp, td.be);
  826. if (len > 0 && dir != OHCI_TD_DIR_IN) {
  827. DPRINTF(" data:");
  828. for (i = 0; i < len; i++)
  829. printf(" %.2x", ohci->usb_buf[i]);
  830. DPRINTF("\n");
  831. }
  832. #endif
  833. if (completion) {
  834. ret = ohci->usb_packet.len;
  835. ohci->async_td = 0;
  836. ohci->async_complete = 0;
  837. } else {
  838. ret = USB_RET_NODEV;
  839. for (i = 0; i < ohci->num_ports; i++) {
  840. dev = ohci->rhport[i].port.dev;
  841. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
  842. continue;
  843. if (ohci->async_td) {
  844. /* ??? The hardware should allow one active packet per
  845. endpoint. We only allow one active packet per controller.
  846. This should be sufficient as long as devices respond in a
  847. timely manner.
  848. */
  849. #ifdef DEBUG_PACKET
  850. DPRINTF("Too many pending packets\n");
  851. #endif
  852. return 1;
  853. }
  854. ohci->usb_packet.pid = pid;
  855. ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
  856. ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
  857. ohci->usb_packet.data = ohci->usb_buf;
  858. ohci->usb_packet.len = len;
  859. ret = usb_handle_packet(dev, &ohci->usb_packet);
  860. if (ret != USB_RET_NODEV)
  861. break;
  862. }
  863. #ifdef DEBUG_PACKET
  864. DPRINTF("ret=%d\n", ret);
  865. #endif
  866. if (ret == USB_RET_ASYNC) {
  867. ohci->async_td = addr;
  868. return 1;
  869. }
  870. }
  871. if (ret >= 0) {
  872. if (dir == OHCI_TD_DIR_IN) {
  873. ohci_copy_td(ohci, &td, ohci->usb_buf, ret, 1);
  874. #ifdef DEBUG_PACKET
  875. DPRINTF(" data:");
  876. for (i = 0; i < ret; i++)
  877. printf(" %.2x", ohci->usb_buf[i]);
  878. DPRINTF("\n");
  879. #endif
  880. } else {
  881. ret = len;
  882. }
  883. }
  884. /* Writeback */
  885. if (ret == len || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
  886. /* Transmission succeeded. */
  887. if (ret == len) {
  888. td.cbp = 0;
  889. } else {
  890. td.cbp += ret;
  891. if ((td.cbp & 0xfff) + ret > 0xfff) {
  892. td.cbp &= 0xfff;
  893. td.cbp |= td.be & ~0xfff;
  894. }
  895. }
  896. td.flags |= OHCI_TD_T1;
  897. td.flags ^= OHCI_TD_T0;
  898. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
  899. OHCI_SET_BM(td.flags, TD_EC, 0);
  900. ed->head &= ~OHCI_ED_C;
  901. if (td.flags & OHCI_TD_T0)
  902. ed->head |= OHCI_ED_C;
  903. } else {
  904. if (ret >= 0) {
  905. DPRINTF("usb-ohci: Underrun\n");
  906. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
  907. } else {
  908. switch (ret) {
  909. case USB_RET_NODEV:
  910. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
  911. case USB_RET_NAK:
  912. DPRINTF("usb-ohci: got NAK\n");
  913. return 1;
  914. case USB_RET_STALL:
  915. DPRINTF("usb-ohci: got STALL\n");
  916. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
  917. break;
  918. case USB_RET_BABBLE:
  919. DPRINTF("usb-ohci: got BABBLE\n");
  920. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  921. break;
  922. default:
  923. fprintf(stderr, "usb-ohci: Bad device response %d\n", ret);
  924. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
  925. OHCI_SET_BM(td.flags, TD_EC, 3);
  926. break;
  927. }
  928. }
  929. ed->head |= OHCI_ED_H;
  930. }
  931. /* Retire this TD */
  932. ed->head &= ~OHCI_DPTR_MASK;
  933. ed->head |= td.next & OHCI_DPTR_MASK;
  934. td.next = ohci->done;
  935. ohci->done = addr;
  936. i = OHCI_BM(td.flags, TD_DI);
  937. if (i < ohci->done_count)
  938. ohci->done_count = i;
  939. ohci_put_td(ohci, addr, &td);
  940. return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
  941. }
  942. /* Service an endpoint list. Returns nonzero if active TD were found. */
  943. static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
  944. {
  945. struct ohci_ed ed;
  946. uint32_t next_ed;
  947. uint32_t cur;
  948. int active;
  949. active = 0;
  950. if (head == 0)
  951. return 0;
  952. for (cur = head; cur; cur = next_ed) {
  953. if (!ohci_read_ed(ohci, cur, &ed)) {
  954. fprintf(stderr, "usb-ohci: ED read error at %x\n", cur);
  955. return 0;
  956. }
  957. next_ed = ed.next & OHCI_DPTR_MASK;
  958. if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
  959. uint32_t addr;
  960. /* Cancel pending packets for ED that have been paused. */
  961. addr = ed.head & OHCI_DPTR_MASK;
  962. if (ohci->async_td && addr == ohci->async_td) {
  963. usb_cancel_packet(&ohci->usb_packet);
  964. ohci->async_td = 0;
  965. }
  966. continue;
  967. }
  968. while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
  969. #ifdef DEBUG_PACKET
  970. DPRINTF("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
  971. "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur,
  972. OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
  973. OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
  974. (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
  975. OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0,
  976. (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
  977. ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
  978. #endif
  979. active = 1;
  980. if ((ed.flags & OHCI_ED_F) == 0) {
  981. if (ohci_service_td(ohci, &ed))
  982. break;
  983. } else {
  984. /* Handle isochronous endpoints */
  985. if (ohci_service_iso_td(ohci, &ed, completion))
  986. break;
  987. }
  988. }
  989. ohci_put_ed(ohci, cur, &ed);
  990. }
  991. return active;
  992. }
  993. /* Generate a SOF event, and set a timer for EOF */
  994. static void ohci_sof(OHCIState *ohci)
  995. {
  996. ohci->sof_time = qemu_get_clock_ns(vm_clock);
  997. qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time);
  998. ohci_set_interrupt(ohci, OHCI_INTR_SF);
  999. }
  1000. /* Process Control and Bulk lists. */
  1001. static void ohci_process_lists(OHCIState *ohci, int completion)
  1002. {
  1003. if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
  1004. if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
  1005. DPRINTF("usb-ohci: head %x, cur %x\n",
  1006. ohci->ctrl_head, ohci->ctrl_cur);
  1007. }
  1008. if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
  1009. ohci->ctrl_cur = 0;
  1010. ohci->status &= ~OHCI_STATUS_CLF;
  1011. }
  1012. }
  1013. if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
  1014. if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
  1015. ohci->bulk_cur = 0;
  1016. ohci->status &= ~OHCI_STATUS_BLF;
  1017. }
  1018. }
  1019. }
  1020. /* Do frame processing on frame boundary */
  1021. static void ohci_frame_boundary(void *opaque)
  1022. {
  1023. OHCIState *ohci = opaque;
  1024. struct ohci_hcca hcca;
  1025. ohci_read_hcca(ohci, ohci->hcca, &hcca);
  1026. /* Process all the lists at the end of the frame */
  1027. if (ohci->ctl & OHCI_CTL_PLE) {
  1028. int n;
  1029. n = ohci->frame_number & 0x1f;
  1030. ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
  1031. }
  1032. /* Cancel all pending packets if either of the lists has been disabled. */
  1033. if (ohci->async_td &&
  1034. ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
  1035. usb_cancel_packet(&ohci->usb_packet);
  1036. ohci->async_td = 0;
  1037. }
  1038. ohci->old_ctl = ohci->ctl;
  1039. ohci_process_lists(ohci, 0);
  1040. /* Frame boundary, so do EOF stuf here */
  1041. ohci->frt = ohci->fit;
  1042. /* Increment frame number and take care of endianness. */
  1043. ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
  1044. hcca.frame = cpu_to_le16(ohci->frame_number);
  1045. if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
  1046. if (!ohci->done)
  1047. abort();
  1048. if (ohci->intr & ohci->intr_status)
  1049. ohci->done |= 1;
  1050. hcca.done = cpu_to_le32(ohci->done);
  1051. ohci->done = 0;
  1052. ohci->done_count = 7;
  1053. ohci_set_interrupt(ohci, OHCI_INTR_WD);
  1054. }
  1055. if (ohci->done_count != 7 && ohci->done_count != 0)
  1056. ohci->done_count--;
  1057. /* Do SOF stuff here */
  1058. ohci_sof(ohci);
  1059. /* Writeback HCCA */
  1060. ohci_put_hcca(ohci, ohci->hcca, &hcca);
  1061. }
  1062. /* Start sending SOF tokens across the USB bus, lists are processed in
  1063. * next frame
  1064. */
  1065. static int ohci_bus_start(OHCIState *ohci)
  1066. {
  1067. ohci->eof_timer = qemu_new_timer_ns(vm_clock,
  1068. ohci_frame_boundary,
  1069. ohci);
  1070. if (ohci->eof_timer == NULL) {
  1071. fprintf(stderr, "usb-ohci: %s: qemu_new_timer_ns failed\n", ohci->name);
  1072. /* TODO: Signal unrecoverable error */
  1073. return 0;
  1074. }
  1075. DPRINTF("usb-ohci: %s: USB Operational\n", ohci->name);
  1076. ohci_sof(ohci);
  1077. return 1;
  1078. }
  1079. /* Stop sending SOF tokens on the bus */
  1080. static void ohci_bus_stop(OHCIState *ohci)
  1081. {
  1082. if (ohci->eof_timer)
  1083. qemu_del_timer(ohci->eof_timer);
  1084. ohci->eof_timer = NULL;
  1085. }
  1086. /* Sets a flag in a port status register but only set it if the port is
  1087. * connected, if not set ConnectStatusChange flag. If flag is enabled
  1088. * return 1.
  1089. */
  1090. static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
  1091. {
  1092. int ret = 1;
  1093. /* writing a 0 has no effect */
  1094. if (val == 0)
  1095. return 0;
  1096. /* If CurrentConnectStatus is cleared we set
  1097. * ConnectStatusChange
  1098. */
  1099. if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
  1100. ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
  1101. if (ohci->rhstatus & OHCI_RHS_DRWE) {
  1102. /* TODO: CSC is a wakeup event */
  1103. }
  1104. return 0;
  1105. }
  1106. if (ohci->rhport[i].ctrl & val)
  1107. ret = 0;
  1108. /* set the bit */
  1109. ohci->rhport[i].ctrl |= val;
  1110. return ret;
  1111. }
  1112. /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
  1113. static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
  1114. {
  1115. val &= OHCI_FMI_FI;
  1116. if (val != ohci->fi) {
  1117. DPRINTF("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
  1118. ohci->name, ohci->fi, ohci->fi);
  1119. }
  1120. ohci->fi = val;
  1121. }
  1122. static void ohci_port_power(OHCIState *ohci, int i, int p)
  1123. {
  1124. if (p) {
  1125. ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
  1126. } else {
  1127. ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
  1128. OHCI_PORT_CCS|
  1129. OHCI_PORT_PSS|
  1130. OHCI_PORT_PRS);
  1131. }
  1132. }
  1133. /* Set HcControlRegister */
  1134. static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
  1135. {
  1136. uint32_t old_state;
  1137. uint32_t new_state;
  1138. old_state = ohci->ctl & OHCI_CTL_HCFS;
  1139. ohci->ctl = val;
  1140. new_state = ohci->ctl & OHCI_CTL_HCFS;
  1141. /* no state change */
  1142. if (old_state == new_state)
  1143. return;
  1144. switch (new_state) {
  1145. case OHCI_USB_OPERATIONAL:
  1146. ohci_bus_start(ohci);
  1147. break;
  1148. case OHCI_USB_SUSPEND:
  1149. ohci_bus_stop(ohci);
  1150. DPRINTF("usb-ohci: %s: USB Suspended\n", ohci->name);
  1151. break;
  1152. case OHCI_USB_RESUME:
  1153. DPRINTF("usb-ohci: %s: USB Resume\n", ohci->name);
  1154. break;
  1155. case OHCI_USB_RESET:
  1156. ohci_reset(ohci);
  1157. DPRINTF("usb-ohci: %s: USB Reset\n", ohci->name);
  1158. break;
  1159. }
  1160. }
  1161. static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
  1162. {
  1163. uint16_t fr;
  1164. int64_t tks;
  1165. if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
  1166. return (ohci->frt << 31);
  1167. /* Being in USB operational state guarnatees sof_time was
  1168. * set already.
  1169. */
  1170. tks = qemu_get_clock_ns(vm_clock) - ohci->sof_time;
  1171. /* avoid muldiv if possible */
  1172. if (tks >= usb_frame_time)
  1173. return (ohci->frt << 31);
  1174. tks = muldiv64(1, tks, usb_bit_time);
  1175. fr = (uint16_t)(ohci->fi - tks);
  1176. return (ohci->frt << 31) | fr;
  1177. }
  1178. /* Set root hub status */
  1179. static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
  1180. {
  1181. uint32_t old_state;
  1182. old_state = ohci->rhstatus;
  1183. /* write 1 to clear OCIC */
  1184. if (val & OHCI_RHS_OCIC)
  1185. ohci->rhstatus &= ~OHCI_RHS_OCIC;
  1186. if (val & OHCI_RHS_LPS) {
  1187. int i;
  1188. for (i = 0; i < ohci->num_ports; i++)
  1189. ohci_port_power(ohci, i, 0);
  1190. DPRINTF("usb-ohci: powered down all ports\n");
  1191. }
  1192. if (val & OHCI_RHS_LPSC) {
  1193. int i;
  1194. for (i = 0; i < ohci->num_ports; i++)
  1195. ohci_port_power(ohci, i, 1);
  1196. DPRINTF("usb-ohci: powered up all ports\n");
  1197. }
  1198. if (val & OHCI_RHS_DRWE)
  1199. ohci->rhstatus |= OHCI_RHS_DRWE;
  1200. if (val & OHCI_RHS_CRWE)
  1201. ohci->rhstatus &= ~OHCI_RHS_DRWE;
  1202. if (old_state != ohci->rhstatus)
  1203. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1204. }
  1205. /* Set root hub port status */
  1206. static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
  1207. {
  1208. uint32_t old_state;
  1209. OHCIPort *port;
  1210. port = &ohci->rhport[portnum];
  1211. old_state = port->ctrl;
  1212. /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
  1213. if (val & OHCI_PORT_WTC)
  1214. port->ctrl &= ~(val & OHCI_PORT_WTC);
  1215. if (val & OHCI_PORT_CCS)
  1216. port->ctrl &= ~OHCI_PORT_PES;
  1217. ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
  1218. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
  1219. DPRINTF("usb-ohci: port %d: SUSPEND\n", portnum);
  1220. }
  1221. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
  1222. DPRINTF("usb-ohci: port %d: RESET\n", portnum);
  1223. usb_send_msg(port->port.dev, USB_MSG_RESET);
  1224. port->ctrl &= ~OHCI_PORT_PRS;
  1225. /* ??? Should this also set OHCI_PORT_PESC. */
  1226. port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
  1227. }
  1228. /* Invert order here to ensure in ambiguous case, device is
  1229. * powered up...
  1230. */
  1231. if (val & OHCI_PORT_LSDA)
  1232. ohci_port_power(ohci, portnum, 0);
  1233. if (val & OHCI_PORT_PPS)
  1234. ohci_port_power(ohci, portnum, 1);
  1235. if (old_state != port->ctrl)
  1236. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1237. return;
  1238. }
  1239. static uint32_t ohci_mem_read(void *ptr, target_phys_addr_t addr)
  1240. {
  1241. OHCIState *ohci = ptr;
  1242. uint32_t retval;
  1243. addr &= 0xff;
  1244. /* Only aligned reads are allowed on OHCI */
  1245. if (addr & 3) {
  1246. fprintf(stderr, "usb-ohci: Mis-aligned read\n");
  1247. return 0xffffffff;
  1248. } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1249. /* HcRhPortStatus */
  1250. retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
  1251. } else {
  1252. switch (addr >> 2) {
  1253. case 0: /* HcRevision */
  1254. retval = 0x10;
  1255. break;
  1256. case 1: /* HcControl */
  1257. retval = ohci->ctl;
  1258. break;
  1259. case 2: /* HcCommandStatus */
  1260. retval = ohci->status;
  1261. break;
  1262. case 3: /* HcInterruptStatus */
  1263. retval = ohci->intr_status;
  1264. break;
  1265. case 4: /* HcInterruptEnable */
  1266. case 5: /* HcInterruptDisable */
  1267. retval = ohci->intr;
  1268. break;
  1269. case 6: /* HcHCCA */
  1270. retval = ohci->hcca;
  1271. break;
  1272. case 7: /* HcPeriodCurrentED */
  1273. retval = ohci->per_cur;
  1274. break;
  1275. case 8: /* HcControlHeadED */
  1276. retval = ohci->ctrl_head;
  1277. break;
  1278. case 9: /* HcControlCurrentED */
  1279. retval = ohci->ctrl_cur;
  1280. break;
  1281. case 10: /* HcBulkHeadED */
  1282. retval = ohci->bulk_head;
  1283. break;
  1284. case 11: /* HcBulkCurrentED */
  1285. retval = ohci->bulk_cur;
  1286. break;
  1287. case 12: /* HcDoneHead */
  1288. retval = ohci->done;
  1289. break;
  1290. case 13: /* HcFmInterretval */
  1291. retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
  1292. break;
  1293. case 14: /* HcFmRemaining */
  1294. retval = ohci_get_frame_remaining(ohci);
  1295. break;
  1296. case 15: /* HcFmNumber */
  1297. retval = ohci->frame_number;
  1298. break;
  1299. case 16: /* HcPeriodicStart */
  1300. retval = ohci->pstart;
  1301. break;
  1302. case 17: /* HcLSThreshold */
  1303. retval = ohci->lst;
  1304. break;
  1305. case 18: /* HcRhDescriptorA */
  1306. retval = ohci->rhdesc_a;
  1307. break;
  1308. case 19: /* HcRhDescriptorB */
  1309. retval = ohci->rhdesc_b;
  1310. break;
  1311. case 20: /* HcRhStatus */
  1312. retval = ohci->rhstatus;
  1313. break;
  1314. /* PXA27x specific registers */
  1315. case 24: /* HcStatus */
  1316. retval = ohci->hstatus & ohci->hmask;
  1317. break;
  1318. case 25: /* HcHReset */
  1319. retval = ohci->hreset;
  1320. break;
  1321. case 26: /* HcHInterruptEnable */
  1322. retval = ohci->hmask;
  1323. break;
  1324. case 27: /* HcHInterruptTest */
  1325. retval = ohci->htest;
  1326. break;
  1327. default:
  1328. fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr);
  1329. retval = 0xffffffff;
  1330. }
  1331. }
  1332. return retval;
  1333. }
  1334. static void ohci_mem_write(void *ptr, target_phys_addr_t addr, uint32_t val)
  1335. {
  1336. OHCIState *ohci = ptr;
  1337. addr &= 0xff;
  1338. /* Only aligned reads are allowed on OHCI */
  1339. if (addr & 3) {
  1340. fprintf(stderr, "usb-ohci: Mis-aligned write\n");
  1341. return;
  1342. }
  1343. if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1344. /* HcRhPortStatus */
  1345. ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
  1346. return;
  1347. }
  1348. switch (addr >> 2) {
  1349. case 1: /* HcControl */
  1350. ohci_set_ctl(ohci, val);
  1351. break;
  1352. case 2: /* HcCommandStatus */
  1353. /* SOC is read-only */
  1354. val = (val & ~OHCI_STATUS_SOC);
  1355. /* Bits written as '0' remain unchanged in the register */
  1356. ohci->status |= val;
  1357. if (ohci->status & OHCI_STATUS_HCR)
  1358. ohci_reset(ohci);
  1359. break;
  1360. case 3: /* HcInterruptStatus */
  1361. ohci->intr_status &= ~val;
  1362. ohci_intr_update(ohci);
  1363. break;
  1364. case 4: /* HcInterruptEnable */
  1365. ohci->intr |= val;
  1366. ohci_intr_update(ohci);
  1367. break;
  1368. case 5: /* HcInterruptDisable */
  1369. ohci->intr &= ~val;
  1370. ohci_intr_update(ohci);
  1371. break;
  1372. case 6: /* HcHCCA */
  1373. ohci->hcca = val & OHCI_HCCA_MASK;
  1374. break;
  1375. case 7: /* HcPeriodCurrentED */
  1376. /* Ignore writes to this read-only register, Linux does them */
  1377. break;
  1378. case 8: /* HcControlHeadED */
  1379. ohci->ctrl_head = val & OHCI_EDPTR_MASK;
  1380. break;
  1381. case 9: /* HcControlCurrentED */
  1382. ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
  1383. break;
  1384. case 10: /* HcBulkHeadED */
  1385. ohci->bulk_head = val & OHCI_EDPTR_MASK;
  1386. break;
  1387. case 11: /* HcBulkCurrentED */
  1388. ohci->bulk_cur = val & OHCI_EDPTR_MASK;
  1389. break;
  1390. case 13: /* HcFmInterval */
  1391. ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
  1392. ohci->fit = (val & OHCI_FMI_FIT) >> 31;
  1393. ohci_set_frame_interval(ohci, val);
  1394. break;
  1395. case 15: /* HcFmNumber */
  1396. break;
  1397. case 16: /* HcPeriodicStart */
  1398. ohci->pstart = val & 0xffff;
  1399. break;
  1400. case 17: /* HcLSThreshold */
  1401. ohci->lst = val & 0xffff;
  1402. break;
  1403. case 18: /* HcRhDescriptorA */
  1404. ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
  1405. ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
  1406. break;
  1407. case 19: /* HcRhDescriptorB */
  1408. break;
  1409. case 20: /* HcRhStatus */
  1410. ohci_set_hub_status(ohci, val);
  1411. break;
  1412. /* PXA27x specific registers */
  1413. case 24: /* HcStatus */
  1414. ohci->hstatus &= ~(val & ohci->hmask);
  1415. case 25: /* HcHReset */
  1416. ohci->hreset = val & ~OHCI_HRESET_FSBIR;
  1417. if (val & OHCI_HRESET_FSBIR)
  1418. ohci_reset(ohci);
  1419. break;
  1420. case 26: /* HcHInterruptEnable */
  1421. ohci->hmask = val;
  1422. break;
  1423. case 27: /* HcHInterruptTest */
  1424. ohci->htest = val;
  1425. break;
  1426. default:
  1427. fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr);
  1428. break;
  1429. }
  1430. }
  1431. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev)
  1432. {
  1433. if (ohci->async_td && ohci->usb_packet.owner == dev) {
  1434. usb_cancel_packet(&ohci->usb_packet);
  1435. ohci->async_td = 0;
  1436. }
  1437. }
  1438. /* Only dword reads are defined on OHCI register space */
  1439. static CPUReadMemoryFunc * const ohci_readfn[3]={
  1440. ohci_mem_read,
  1441. ohci_mem_read,
  1442. ohci_mem_read
  1443. };
  1444. /* Only dword writes are defined on OHCI register space */
  1445. static CPUWriteMemoryFunc * const ohci_writefn[3]={
  1446. ohci_mem_write,
  1447. ohci_mem_write,
  1448. ohci_mem_write
  1449. };
  1450. static USBPortOps ohci_port_ops = {
  1451. .attach = ohci_attach,
  1452. .detach = ohci_detach,
  1453. .child_detach = ohci_child_detach,
  1454. .wakeup = ohci_wakeup,
  1455. .complete = ohci_async_complete_packet,
  1456. };
  1457. static USBBusOps ohci_bus_ops = {
  1458. };
  1459. static int usb_ohci_init(OHCIState *ohci, DeviceState *dev,
  1460. int num_ports, uint32_t localmem_base,
  1461. char *masterbus, uint32_t firstport)
  1462. {
  1463. int i;
  1464. if (usb_frame_time == 0) {
  1465. #ifdef OHCI_TIME_WARP
  1466. usb_frame_time = get_ticks_per_sec();
  1467. usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ/1000);
  1468. #else
  1469. usb_frame_time = muldiv64(1, get_ticks_per_sec(), 1000);
  1470. if (get_ticks_per_sec() >= USB_HZ) {
  1471. usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ);
  1472. } else {
  1473. usb_bit_time = 1;
  1474. }
  1475. #endif
  1476. DPRINTF("usb-ohci: usb_bit_time=%" PRId64 " usb_frame_time=%" PRId64 "\n",
  1477. usb_frame_time, usb_bit_time);
  1478. }
  1479. ohci->num_ports = num_ports;
  1480. if (masterbus) {
  1481. USBPort *ports[OHCI_MAX_PORTS];
  1482. for(i = 0; i < num_ports; i++) {
  1483. ports[i] = &ohci->rhport[i].port;
  1484. }
  1485. if (usb_register_companion(masterbus, ports, num_ports,
  1486. firstport, ohci, &ohci_port_ops,
  1487. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
  1488. return -1;
  1489. }
  1490. } else {
  1491. usb_bus_new(&ohci->bus, &ohci_bus_ops, dev);
  1492. for (i = 0; i < num_ports; i++) {
  1493. usb_register_port(&ohci->bus, &ohci->rhport[i].port,
  1494. ohci, i, &ohci_port_ops,
  1495. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1496. }
  1497. }
  1498. ohci->mem = cpu_register_io_memory(ohci_readfn, ohci_writefn, ohci,
  1499. DEVICE_LITTLE_ENDIAN);
  1500. ohci->localmem_base = localmem_base;
  1501. ohci->name = dev->info->name;
  1502. ohci->async_td = 0;
  1503. qemu_register_reset(ohci_reset, ohci);
  1504. return 0;
  1505. }
  1506. typedef struct {
  1507. PCIDevice pci_dev;
  1508. OHCIState state;
  1509. char *masterbus;
  1510. uint32_t num_ports;
  1511. uint32_t firstport;
  1512. } OHCIPCIState;
  1513. static int usb_ohci_initfn_pci(struct PCIDevice *dev)
  1514. {
  1515. OHCIPCIState *ohci = DO_UPCAST(OHCIPCIState, pci_dev, dev);
  1516. ohci->pci_dev.config[PCI_CLASS_PROG] = 0x10; /* OHCI */
  1517. /* TODO: RST# value should be 0. */
  1518. ohci->pci_dev.config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin 1 */
  1519. if (usb_ohci_init(&ohci->state, &dev->qdev, ohci->num_ports, 0,
  1520. ohci->masterbus, ohci->firstport) != 0) {
  1521. return -1;
  1522. }
  1523. ohci->state.irq = ohci->pci_dev.irq[0];
  1524. /* TODO: avoid cast below by using dev */
  1525. pci_register_bar_simple(&ohci->pci_dev, 0, 256, 0, ohci->state.mem);
  1526. return 0;
  1527. }
  1528. void usb_ohci_init_pci(struct PCIBus *bus, int devfn)
  1529. {
  1530. pci_create_simple(bus, devfn, "pci-ohci");
  1531. }
  1532. typedef struct {
  1533. SysBusDevice busdev;
  1534. OHCIState ohci;
  1535. uint32_t num_ports;
  1536. target_phys_addr_t dma_offset;
  1537. } OHCISysBusState;
  1538. static int ohci_init_pxa(SysBusDevice *dev)
  1539. {
  1540. OHCISysBusState *s = FROM_SYSBUS(OHCISysBusState, dev);
  1541. /* Cannot fail as we pass NULL for masterbus */
  1542. usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset, NULL, 0);
  1543. sysbus_init_irq(dev, &s->ohci.irq);
  1544. sysbus_init_mmio(dev, 0x1000, s->ohci.mem);
  1545. return 0;
  1546. }
  1547. static PCIDeviceInfo ohci_pci_info = {
  1548. .qdev.name = "pci-ohci",
  1549. .qdev.desc = "Apple USB Controller",
  1550. .qdev.size = sizeof(OHCIPCIState),
  1551. .init = usb_ohci_initfn_pci,
  1552. .vendor_id = PCI_VENDOR_ID_APPLE,
  1553. .device_id = PCI_DEVICE_ID_APPLE_IPID_USB,
  1554. .class_id = PCI_CLASS_SERIAL_USB,
  1555. .qdev.props = (Property[]) {
  1556. DEFINE_PROP_STRING("masterbus", OHCIPCIState, masterbus),
  1557. DEFINE_PROP_UINT32("num-ports", OHCIPCIState, num_ports, 3),
  1558. DEFINE_PROP_UINT32("firstport", OHCIPCIState, firstport, 0),
  1559. DEFINE_PROP_END_OF_LIST(),
  1560. },
  1561. };
  1562. static SysBusDeviceInfo ohci_sysbus_info = {
  1563. .init = ohci_init_pxa,
  1564. .qdev.name = "sysbus-ohci",
  1565. .qdev.desc = "OHCI USB Controller",
  1566. .qdev.size = sizeof(OHCISysBusState),
  1567. .qdev.props = (Property[]) {
  1568. DEFINE_PROP_UINT32("num-ports", OHCISysBusState, num_ports, 3),
  1569. DEFINE_PROP_TADDR("dma-offset", OHCISysBusState, dma_offset, 3),
  1570. DEFINE_PROP_END_OF_LIST(),
  1571. }
  1572. };
  1573. static void ohci_register(void)
  1574. {
  1575. pci_qdev_register(&ohci_pci_info);
  1576. sysbus_register_withprop(&ohci_sysbus_info);
  1577. }
  1578. device_init(ohci_register);