usb-ohci.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  20. *
  21. * TODO:
  22. * o Isochronous transfers
  23. * o Allocate bandwidth in frames properly
  24. * o Disable timers when nothing needs to be done, or remove timer usage
  25. * all together.
  26. * o Handle unrecoverable errors properly
  27. * o BIOS work to boot from USB storage
  28. */
  29. #include "hw.h"
  30. #include "qemu-timer.h"
  31. #include "usb.h"
  32. #include "pci.h"
  33. #include "pxa.h"
  34. //#define DEBUG_OHCI
  35. /* Dump packet contents. */
  36. //#define DEBUG_PACKET
  37. //#define DEBUG_ISOCH
  38. /* This causes frames to occur 1000x slower */
  39. //#define OHCI_TIME_WARP 1
  40. #ifdef DEBUG_OHCI
  41. #define dprintf printf
  42. #else
  43. #define dprintf(...)
  44. #endif
  45. /* Number of Downstream Ports on the root hub. */
  46. #define OHCI_MAX_PORTS 15
  47. static int64_t usb_frame_time;
  48. static int64_t usb_bit_time;
  49. typedef struct OHCIPort {
  50. USBPort port;
  51. uint32_t ctrl;
  52. } OHCIPort;
  53. enum ohci_type {
  54. OHCI_TYPE_PCI,
  55. OHCI_TYPE_PXA
  56. };
  57. typedef struct {
  58. qemu_irq irq;
  59. enum ohci_type type;
  60. int mem;
  61. int num_ports;
  62. const char *name;
  63. QEMUTimer *eof_timer;
  64. int64_t sof_time;
  65. /* OHCI state */
  66. /* Control partition */
  67. uint32_t ctl, status;
  68. uint32_t intr_status;
  69. uint32_t intr;
  70. /* memory pointer partition */
  71. uint32_t hcca;
  72. uint32_t ctrl_head, ctrl_cur;
  73. uint32_t bulk_head, bulk_cur;
  74. uint32_t per_cur;
  75. uint32_t done;
  76. int done_count;
  77. /* Frame counter partition */
  78. uint32_t fsmps:15;
  79. uint32_t fit:1;
  80. uint32_t fi:14;
  81. uint32_t frt:1;
  82. uint16_t frame_number;
  83. uint16_t padding;
  84. uint32_t pstart;
  85. uint32_t lst;
  86. /* Root Hub partition */
  87. uint32_t rhdesc_a, rhdesc_b;
  88. uint32_t rhstatus;
  89. OHCIPort rhport[OHCI_MAX_PORTS];
  90. /* PXA27x Non-OHCI events */
  91. uint32_t hstatus;
  92. uint32_t hmask;
  93. uint32_t hreset;
  94. uint32_t htest;
  95. /* Active packets. */
  96. uint32_t old_ctl;
  97. USBPacket usb_packet;
  98. uint8_t usb_buf[8192];
  99. uint32_t async_td;
  100. int async_complete;
  101. } OHCIState;
  102. /* Host Controller Communications Area */
  103. struct ohci_hcca {
  104. uint32_t intr[32];
  105. uint16_t frame, pad;
  106. uint32_t done;
  107. };
  108. static void ohci_bus_stop(OHCIState *ohci);
  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, USBDevice *dev)
  276. {
  277. OHCIState *s = port1->opaque;
  278. OHCIPort *port = &s->rhport[port1->index];
  279. uint32_t old_state = port->ctrl;
  280. if (dev) {
  281. if (port->port.dev) {
  282. usb_attach(port1, NULL);
  283. }
  284. /* set connect status */
  285. port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC;
  286. /* update speed */
  287. if (dev->speed == USB_SPEED_LOW)
  288. port->ctrl |= OHCI_PORT_LSDA;
  289. else
  290. port->ctrl &= ~OHCI_PORT_LSDA;
  291. port->port.dev = dev;
  292. /* notify of remote-wakeup */
  293. if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND)
  294. ohci_set_interrupt(s, OHCI_INTR_RD);
  295. /* send the attach message */
  296. usb_send_msg(dev, USB_MSG_ATTACH);
  297. dprintf("usb-ohci: Attached port %d\n", port1->index);
  298. } else {
  299. /* set connect status */
  300. if (port->ctrl & OHCI_PORT_CCS) {
  301. port->ctrl &= ~OHCI_PORT_CCS;
  302. port->ctrl |= OHCI_PORT_CSC;
  303. }
  304. /* disable port */
  305. if (port->ctrl & OHCI_PORT_PES) {
  306. port->ctrl &= ~OHCI_PORT_PES;
  307. port->ctrl |= OHCI_PORT_PESC;
  308. }
  309. dev = port->port.dev;
  310. if (dev) {
  311. /* send the detach message */
  312. usb_send_msg(dev, USB_MSG_DETACH);
  313. }
  314. port->port.dev = NULL;
  315. dprintf("usb-ohci: Detached port %d\n", port1->index);
  316. }
  317. if (old_state != port->ctrl)
  318. ohci_set_interrupt(s, OHCI_INTR_RHSC);
  319. }
  320. /* Reset the controller */
  321. static void ohci_reset(void *opaque)
  322. {
  323. OHCIState *ohci = opaque;
  324. OHCIPort *port;
  325. int i;
  326. ohci_bus_stop(ohci);
  327. ohci->ctl = 0;
  328. ohci->old_ctl = 0;
  329. ohci->status = 0;
  330. ohci->intr_status = 0;
  331. ohci->intr = OHCI_INTR_MIE;
  332. ohci->hcca = 0;
  333. ohci->ctrl_head = ohci->ctrl_cur = 0;
  334. ohci->bulk_head = ohci->bulk_cur = 0;
  335. ohci->per_cur = 0;
  336. ohci->done = 0;
  337. ohci->done_count = 7;
  338. /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
  339. * I took the value linux sets ...
  340. */
  341. ohci->fsmps = 0x2778;
  342. ohci->fi = 0x2edf;
  343. ohci->fit = 0;
  344. ohci->frt = 0;
  345. ohci->frame_number = 0;
  346. ohci->pstart = 0;
  347. ohci->lst = OHCI_LS_THRESH;
  348. ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
  349. ohci->rhdesc_b = 0x0; /* Impl. specific */
  350. ohci->rhstatus = 0;
  351. for (i = 0; i < ohci->num_ports; i++)
  352. {
  353. port = &ohci->rhport[i];
  354. port->ctrl = 0;
  355. if (port->port.dev)
  356. ohci_attach(&port->port, port->port.dev);
  357. }
  358. if (ohci->async_td) {
  359. usb_cancel_packet(&ohci->usb_packet);
  360. ohci->async_td = 0;
  361. }
  362. dprintf("usb-ohci: Reset %s\n", ohci->name);
  363. }
  364. /* Get an array of dwords from main memory */
  365. static inline int get_dwords(uint32_t addr, uint32_t *buf, int num)
  366. {
  367. int i;
  368. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  369. cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0);
  370. *buf = le32_to_cpu(*buf);
  371. }
  372. return 1;
  373. }
  374. /* Put an array of dwords in to main memory */
  375. static inline int put_dwords(uint32_t addr, uint32_t *buf, int num)
  376. {
  377. int i;
  378. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  379. uint32_t tmp = cpu_to_le32(*buf);
  380. cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1);
  381. }
  382. return 1;
  383. }
  384. /* Get an array of words from main memory */
  385. static inline int get_words(uint32_t addr, uint16_t *buf, int num)
  386. {
  387. int i;
  388. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  389. cpu_physical_memory_rw(addr, (uint8_t *)buf, sizeof(*buf), 0);
  390. *buf = le16_to_cpu(*buf);
  391. }
  392. return 1;
  393. }
  394. /* Put an array of words in to main memory */
  395. static inline int put_words(uint32_t addr, uint16_t *buf, int num)
  396. {
  397. int i;
  398. for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
  399. uint16_t tmp = cpu_to_le16(*buf);
  400. cpu_physical_memory_rw(addr, (uint8_t *)&tmp, sizeof(tmp), 1);
  401. }
  402. return 1;
  403. }
  404. static inline int ohci_read_ed(uint32_t addr, struct ohci_ed *ed)
  405. {
  406. return get_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2);
  407. }
  408. static inline int ohci_read_td(uint32_t addr, struct ohci_td *td)
  409. {
  410. return get_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2);
  411. }
  412. static inline int ohci_read_iso_td(uint32_t addr, struct ohci_iso_td *td)
  413. {
  414. return (get_dwords(addr, (uint32_t *)td, 4) &&
  415. get_words(addr + 16, td->offset, 8));
  416. }
  417. static inline int ohci_put_ed(uint32_t addr, struct ohci_ed *ed)
  418. {
  419. return put_dwords(addr, (uint32_t *)ed, sizeof(*ed) >> 2);
  420. }
  421. static inline int ohci_put_td(uint32_t addr, struct ohci_td *td)
  422. {
  423. return put_dwords(addr, (uint32_t *)td, sizeof(*td) >> 2);
  424. }
  425. static inline int ohci_put_iso_td(uint32_t addr, struct ohci_iso_td *td)
  426. {
  427. return (put_dwords(addr, (uint32_t *)td, 4) &&
  428. put_words(addr + 16, td->offset, 8));
  429. }
  430. /* Read/Write the contents of a TD from/to main memory. */
  431. static void ohci_copy_td(struct ohci_td *td, uint8_t *buf, int len, int write)
  432. {
  433. uint32_t ptr;
  434. uint32_t n;
  435. ptr = td->cbp;
  436. n = 0x1000 - (ptr & 0xfff);
  437. if (n > len)
  438. n = len;
  439. cpu_physical_memory_rw(ptr, buf, n, write);
  440. if (n == len)
  441. return;
  442. ptr = td->be & ~0xfffu;
  443. buf += n;
  444. cpu_physical_memory_rw(ptr, buf, len - n, write);
  445. }
  446. /* Read/Write the contents of an ISO TD from/to main memory. */
  447. static void ohci_copy_iso_td(uint32_t start_addr, uint32_t end_addr,
  448. uint8_t *buf, int len, int write)
  449. {
  450. uint32_t ptr;
  451. uint32_t n;
  452. ptr = start_addr;
  453. n = 0x1000 - (ptr & 0xfff);
  454. if (n > len)
  455. n = len;
  456. cpu_physical_memory_rw(ptr, buf, n, write);
  457. if (n == len)
  458. return;
  459. ptr = end_addr & ~0xfffu;
  460. buf += n;
  461. cpu_physical_memory_rw(ptr, buf, len - n, write);
  462. }
  463. static void ohci_process_lists(OHCIState *ohci, int completion);
  464. static void ohci_async_complete_packet(USBPacket *packet, void *opaque)
  465. {
  466. OHCIState *ohci = opaque;
  467. #ifdef DEBUG_PACKET
  468. dprintf("Async packet complete\n");
  469. #endif
  470. ohci->async_complete = 1;
  471. ohci_process_lists(ohci, 1);
  472. }
  473. #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
  474. static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed,
  475. int completion)
  476. {
  477. int dir;
  478. size_t len = 0;
  479. const char *str = NULL;
  480. int pid;
  481. int ret;
  482. int i;
  483. USBDevice *dev;
  484. struct ohci_iso_td iso_td;
  485. uint32_t addr;
  486. uint16_t starting_frame;
  487. int16_t relative_frame_number;
  488. int frame_count;
  489. uint32_t start_offset, next_offset, end_offset = 0;
  490. uint32_t start_addr, end_addr;
  491. addr = ed->head & OHCI_DPTR_MASK;
  492. if (!ohci_read_iso_td(addr, &iso_td)) {
  493. printf("usb-ohci: ISO_TD read error at %x\n", addr);
  494. return 0;
  495. }
  496. starting_frame = OHCI_BM(iso_td.flags, TD_SF);
  497. frame_count = OHCI_BM(iso_td.flags, TD_FC);
  498. relative_frame_number = USUB(ohci->frame_number, starting_frame);
  499. #ifdef DEBUG_ISOCH
  500. printf("--- ISO_TD ED head 0x%.8x tailp 0x%.8x\n"
  501. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  502. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  503. "0x%.8x 0x%.8x 0x%.8x 0x%.8x\n"
  504. "frame_number 0x%.8x starting_frame 0x%.8x\n"
  505. "frame_count 0x%.8x relative %d\n"
  506. "di 0x%.8x cc 0x%.8x\n",
  507. ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK,
  508. iso_td.flags, iso_td.bp, iso_td.next, iso_td.be,
  509. iso_td.offset[0], iso_td.offset[1], iso_td.offset[2], iso_td.offset[3],
  510. iso_td.offset[4], iso_td.offset[5], iso_td.offset[6], iso_td.offset[7],
  511. ohci->frame_number, starting_frame,
  512. frame_count, relative_frame_number,
  513. OHCI_BM(iso_td.flags, TD_DI), OHCI_BM(iso_td.flags, TD_CC));
  514. #endif
  515. if (relative_frame_number < 0) {
  516. dprintf("usb-ohci: ISO_TD R=%d < 0\n", relative_frame_number);
  517. return 1;
  518. } else if (relative_frame_number > frame_count) {
  519. /* ISO TD expired - retire the TD to the Done Queue and continue with
  520. the next ISO TD of the same ED */
  521. dprintf("usb-ohci: ISO_TD R=%d > FC=%d\n", relative_frame_number,
  522. frame_count);
  523. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  524. ed->head &= ~OHCI_DPTR_MASK;
  525. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  526. iso_td.next = ohci->done;
  527. ohci->done = addr;
  528. i = OHCI_BM(iso_td.flags, TD_DI);
  529. if (i < ohci->done_count)
  530. ohci->done_count = i;
  531. ohci_put_iso_td(addr, &iso_td);
  532. return 0;
  533. }
  534. dir = OHCI_BM(ed->flags, ED_D);
  535. switch (dir) {
  536. case OHCI_TD_DIR_IN:
  537. str = "in";
  538. pid = USB_TOKEN_IN;
  539. break;
  540. case OHCI_TD_DIR_OUT:
  541. str = "out";
  542. pid = USB_TOKEN_OUT;
  543. break;
  544. case OHCI_TD_DIR_SETUP:
  545. str = "setup";
  546. pid = USB_TOKEN_SETUP;
  547. break;
  548. default:
  549. printf("usb-ohci: Bad direction %d\n", dir);
  550. return 1;
  551. }
  552. if (!iso_td.bp || !iso_td.be) {
  553. printf("usb-ohci: ISO_TD bp 0x%.8x be 0x%.8x\n", iso_td.bp, iso_td.be);
  554. return 1;
  555. }
  556. start_offset = iso_td.offset[relative_frame_number];
  557. next_offset = iso_td.offset[relative_frame_number + 1];
  558. if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
  559. ((relative_frame_number < frame_count) &&
  560. !(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
  561. printf("usb-ohci: ISO_TD cc != not accessed 0x%.8x 0x%.8x\n",
  562. start_offset, next_offset);
  563. return 1;
  564. }
  565. if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
  566. printf("usb-ohci: ISO_TD start_offset=0x%.8x > next_offset=0x%.8x\n",
  567. start_offset, next_offset);
  568. return 1;
  569. }
  570. if ((start_offset & 0x1000) == 0) {
  571. start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  572. (start_offset & OHCI_OFFSET_MASK);
  573. } else {
  574. start_addr = (iso_td.be & OHCI_PAGE_MASK) |
  575. (start_offset & OHCI_OFFSET_MASK);
  576. }
  577. if (relative_frame_number < frame_count) {
  578. end_offset = next_offset - 1;
  579. if ((end_offset & 0x1000) == 0) {
  580. end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
  581. (end_offset & OHCI_OFFSET_MASK);
  582. } else {
  583. end_addr = (iso_td.be & OHCI_PAGE_MASK) |
  584. (end_offset & OHCI_OFFSET_MASK);
  585. }
  586. } else {
  587. /* Last packet in the ISO TD */
  588. end_addr = iso_td.be;
  589. }
  590. if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
  591. len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
  592. - (start_addr & OHCI_OFFSET_MASK);
  593. } else {
  594. len = end_addr - start_addr + 1;
  595. }
  596. if (len && dir != OHCI_TD_DIR_IN) {
  597. ohci_copy_iso_td(start_addr, end_addr, ohci->usb_buf, len, 0);
  598. }
  599. if (completion) {
  600. ret = ohci->usb_packet.len;
  601. } else {
  602. ret = USB_RET_NODEV;
  603. for (i = 0; i < ohci->num_ports; i++) {
  604. dev = ohci->rhport[i].port.dev;
  605. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
  606. continue;
  607. ohci->usb_packet.pid = pid;
  608. ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
  609. ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
  610. ohci->usb_packet.data = ohci->usb_buf;
  611. ohci->usb_packet.len = len;
  612. ohci->usb_packet.complete_cb = ohci_async_complete_packet;
  613. ohci->usb_packet.complete_opaque = ohci;
  614. ret = dev->handle_packet(dev, &ohci->usb_packet);
  615. if (ret != USB_RET_NODEV)
  616. break;
  617. }
  618. if (ret == USB_RET_ASYNC) {
  619. return 1;
  620. }
  621. }
  622. #ifdef DEBUG_ISOCH
  623. printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
  624. start_offset, end_offset, start_addr, end_addr, str, len, ret);
  625. #endif
  626. /* Writeback */
  627. if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
  628. /* IN transfer succeeded */
  629. ohci_copy_iso_td(start_addr, end_addr, ohci->usb_buf, ret, 1);
  630. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  631. OHCI_CC_NOERROR);
  632. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
  633. } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
  634. /* OUT transfer succeeded */
  635. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  636. OHCI_CC_NOERROR);
  637. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
  638. } else {
  639. if (ret > (ssize_t) len) {
  640. printf("usb-ohci: DataOverrun %d > %zu\n", ret, len);
  641. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  642. OHCI_CC_DATAOVERRUN);
  643. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  644. len);
  645. } else if (ret >= 0) {
  646. printf("usb-ohci: DataUnderrun %d\n", ret);
  647. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  648. OHCI_CC_DATAUNDERRUN);
  649. } else {
  650. switch (ret) {
  651. case USB_RET_NODEV:
  652. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  653. OHCI_CC_DEVICENOTRESPONDING);
  654. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  655. 0);
  656. break;
  657. case USB_RET_NAK:
  658. case USB_RET_STALL:
  659. printf("usb-ohci: got NAK/STALL %d\n", ret);
  660. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  661. OHCI_CC_STALL);
  662. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  663. 0);
  664. break;
  665. default:
  666. printf("usb-ohci: Bad device response %d\n", ret);
  667. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  668. OHCI_CC_UNDEXPETEDPID);
  669. break;
  670. }
  671. }
  672. }
  673. if (relative_frame_number == frame_count) {
  674. /* Last data packet of ISO TD - retire the TD to the Done Queue */
  675. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
  676. ed->head &= ~OHCI_DPTR_MASK;
  677. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  678. iso_td.next = ohci->done;
  679. ohci->done = addr;
  680. i = OHCI_BM(iso_td.flags, TD_DI);
  681. if (i < ohci->done_count)
  682. ohci->done_count = i;
  683. }
  684. ohci_put_iso_td(addr, &iso_td);
  685. return 1;
  686. }
  687. /* Service a transport descriptor.
  688. Returns nonzero to terminate processing of this endpoint. */
  689. static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
  690. {
  691. int dir;
  692. size_t len = 0;
  693. const char *str = NULL;
  694. int pid;
  695. int ret;
  696. int i;
  697. USBDevice *dev;
  698. struct ohci_td td;
  699. uint32_t addr;
  700. int flag_r;
  701. int completion;
  702. addr = ed->head & OHCI_DPTR_MASK;
  703. /* See if this TD has already been submitted to the device. */
  704. completion = (addr == ohci->async_td);
  705. if (completion && !ohci->async_complete) {
  706. #ifdef DEBUG_PACKET
  707. dprintf("Skipping async TD\n");
  708. #endif
  709. return 1;
  710. }
  711. if (!ohci_read_td(addr, &td)) {
  712. fprintf(stderr, "usb-ohci: TD read error at %x\n", addr);
  713. return 0;
  714. }
  715. dir = OHCI_BM(ed->flags, ED_D);
  716. switch (dir) {
  717. case OHCI_TD_DIR_OUT:
  718. case OHCI_TD_DIR_IN:
  719. /* Same value. */
  720. break;
  721. default:
  722. dir = OHCI_BM(td.flags, TD_DP);
  723. break;
  724. }
  725. switch (dir) {
  726. case OHCI_TD_DIR_IN:
  727. str = "in";
  728. pid = USB_TOKEN_IN;
  729. break;
  730. case OHCI_TD_DIR_OUT:
  731. str = "out";
  732. pid = USB_TOKEN_OUT;
  733. break;
  734. case OHCI_TD_DIR_SETUP:
  735. str = "setup";
  736. pid = USB_TOKEN_SETUP;
  737. break;
  738. default:
  739. fprintf(stderr, "usb-ohci: Bad direction\n");
  740. return 1;
  741. }
  742. if (td.cbp && td.be) {
  743. if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
  744. len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
  745. } else {
  746. len = (td.be - td.cbp) + 1;
  747. }
  748. if (len && dir != OHCI_TD_DIR_IN && !completion) {
  749. ohci_copy_td(&td, ohci->usb_buf, len, 0);
  750. }
  751. }
  752. flag_r = (td.flags & OHCI_TD_R) != 0;
  753. #ifdef DEBUG_PACKET
  754. dprintf(" TD @ 0x%.8x %u bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
  755. addr, len, str, flag_r, td.cbp, td.be);
  756. if (len > 0 && dir != OHCI_TD_DIR_IN) {
  757. dprintf(" data:");
  758. for (i = 0; i < len; i++)
  759. printf(" %.2x", ohci->usb_buf[i]);
  760. dprintf("\n");
  761. }
  762. #endif
  763. if (completion) {
  764. ret = ohci->usb_packet.len;
  765. ohci->async_td = 0;
  766. ohci->async_complete = 0;
  767. } else {
  768. ret = USB_RET_NODEV;
  769. for (i = 0; i < ohci->num_ports; i++) {
  770. dev = ohci->rhport[i].port.dev;
  771. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
  772. continue;
  773. if (ohci->async_td) {
  774. /* ??? The hardware should allow one active packet per
  775. endpoint. We only allow one active packet per controller.
  776. This should be sufficient as long as devices respond in a
  777. timely manner.
  778. */
  779. #ifdef DEBUG_PACKET
  780. dprintf("Too many pending packets\n");
  781. #endif
  782. return 1;
  783. }
  784. ohci->usb_packet.pid = pid;
  785. ohci->usb_packet.devaddr = OHCI_BM(ed->flags, ED_FA);
  786. ohci->usb_packet.devep = OHCI_BM(ed->flags, ED_EN);
  787. ohci->usb_packet.data = ohci->usb_buf;
  788. ohci->usb_packet.len = len;
  789. ohci->usb_packet.complete_cb = ohci_async_complete_packet;
  790. ohci->usb_packet.complete_opaque = ohci;
  791. ret = dev->handle_packet(dev, &ohci->usb_packet);
  792. if (ret != USB_RET_NODEV)
  793. break;
  794. }
  795. #ifdef DEBUG_PACKET
  796. dprintf("ret=%d\n", ret);
  797. #endif
  798. if (ret == USB_RET_ASYNC) {
  799. ohci->async_td = addr;
  800. return 1;
  801. }
  802. }
  803. if (ret >= 0) {
  804. if (dir == OHCI_TD_DIR_IN) {
  805. ohci_copy_td(&td, ohci->usb_buf, ret, 1);
  806. #ifdef DEBUG_PACKET
  807. dprintf(" data:");
  808. for (i = 0; i < ret; i++)
  809. printf(" %.2x", ohci->usb_buf[i]);
  810. dprintf("\n");
  811. #endif
  812. } else {
  813. ret = len;
  814. }
  815. }
  816. /* Writeback */
  817. if (ret == len || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
  818. /* Transmission succeeded. */
  819. if (ret == len) {
  820. td.cbp = 0;
  821. } else {
  822. td.cbp += ret;
  823. if ((td.cbp & 0xfff) + ret > 0xfff) {
  824. td.cbp &= 0xfff;
  825. td.cbp |= td.be & ~0xfff;
  826. }
  827. }
  828. td.flags |= OHCI_TD_T1;
  829. td.flags ^= OHCI_TD_T0;
  830. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
  831. OHCI_SET_BM(td.flags, TD_EC, 0);
  832. ed->head &= ~OHCI_ED_C;
  833. if (td.flags & OHCI_TD_T0)
  834. ed->head |= OHCI_ED_C;
  835. } else {
  836. if (ret >= 0) {
  837. dprintf("usb-ohci: Underrun\n");
  838. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
  839. } else {
  840. switch (ret) {
  841. case USB_RET_NODEV:
  842. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
  843. case USB_RET_NAK:
  844. dprintf("usb-ohci: got NAK\n");
  845. return 1;
  846. case USB_RET_STALL:
  847. dprintf("usb-ohci: got STALL\n");
  848. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
  849. break;
  850. case USB_RET_BABBLE:
  851. dprintf("usb-ohci: got BABBLE\n");
  852. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  853. break;
  854. default:
  855. fprintf(stderr, "usb-ohci: Bad device response %d\n", ret);
  856. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
  857. OHCI_SET_BM(td.flags, TD_EC, 3);
  858. break;
  859. }
  860. }
  861. ed->head |= OHCI_ED_H;
  862. }
  863. /* Retire this TD */
  864. ed->head &= ~OHCI_DPTR_MASK;
  865. ed->head |= td.next & OHCI_DPTR_MASK;
  866. td.next = ohci->done;
  867. ohci->done = addr;
  868. i = OHCI_BM(td.flags, TD_DI);
  869. if (i < ohci->done_count)
  870. ohci->done_count = i;
  871. ohci_put_td(addr, &td);
  872. return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
  873. }
  874. /* Service an endpoint list. Returns nonzero if active TD were found. */
  875. static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
  876. {
  877. struct ohci_ed ed;
  878. uint32_t next_ed;
  879. uint32_t cur;
  880. int active;
  881. active = 0;
  882. if (head == 0)
  883. return 0;
  884. for (cur = head; cur; cur = next_ed) {
  885. if (!ohci_read_ed(cur, &ed)) {
  886. fprintf(stderr, "usb-ohci: ED read error at %x\n", cur);
  887. return 0;
  888. }
  889. next_ed = ed.next & OHCI_DPTR_MASK;
  890. if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
  891. uint32_t addr;
  892. /* Cancel pending packets for ED that have been paused. */
  893. addr = ed.head & OHCI_DPTR_MASK;
  894. if (ohci->async_td && addr == ohci->async_td) {
  895. usb_cancel_packet(&ohci->usb_packet);
  896. ohci->async_td = 0;
  897. }
  898. continue;
  899. }
  900. while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
  901. #ifdef DEBUG_PACKET
  902. dprintf("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
  903. "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur,
  904. OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
  905. OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
  906. (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
  907. OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0,
  908. (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
  909. ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
  910. #endif
  911. active = 1;
  912. if ((ed.flags & OHCI_ED_F) == 0) {
  913. if (ohci_service_td(ohci, &ed))
  914. break;
  915. } else {
  916. /* Handle isochronous endpoints */
  917. if (ohci_service_iso_td(ohci, &ed, completion))
  918. break;
  919. }
  920. }
  921. ohci_put_ed(cur, &ed);
  922. }
  923. return active;
  924. }
  925. /* Generate a SOF event, and set a timer for EOF */
  926. static void ohci_sof(OHCIState *ohci)
  927. {
  928. ohci->sof_time = qemu_get_clock(vm_clock);
  929. qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time);
  930. ohci_set_interrupt(ohci, OHCI_INTR_SF);
  931. }
  932. /* Process Control and Bulk lists. */
  933. static void ohci_process_lists(OHCIState *ohci, int completion)
  934. {
  935. if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
  936. if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head)
  937. dprintf("usb-ohci: head %x, cur %x\n",
  938. ohci->ctrl_head, ohci->ctrl_cur);
  939. if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
  940. ohci->ctrl_cur = 0;
  941. ohci->status &= ~OHCI_STATUS_CLF;
  942. }
  943. }
  944. if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
  945. if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
  946. ohci->bulk_cur = 0;
  947. ohci->status &= ~OHCI_STATUS_BLF;
  948. }
  949. }
  950. }
  951. /* Do frame processing on frame boundary */
  952. static void ohci_frame_boundary(void *opaque)
  953. {
  954. OHCIState *ohci = opaque;
  955. struct ohci_hcca hcca;
  956. cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 0);
  957. /* Process all the lists at the end of the frame */
  958. if (ohci->ctl & OHCI_CTL_PLE) {
  959. int n;
  960. n = ohci->frame_number & 0x1f;
  961. ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
  962. }
  963. /* Cancel all pending packets if either of the lists has been disabled. */
  964. if (ohci->async_td &&
  965. ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
  966. usb_cancel_packet(&ohci->usb_packet);
  967. ohci->async_td = 0;
  968. }
  969. ohci->old_ctl = ohci->ctl;
  970. ohci_process_lists(ohci, 0);
  971. /* Frame boundary, so do EOF stuf here */
  972. ohci->frt = ohci->fit;
  973. /* XXX: endianness */
  974. ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
  975. hcca.frame = cpu_to_le32(ohci->frame_number);
  976. if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
  977. if (!ohci->done)
  978. abort();
  979. if (ohci->intr & ohci->intr_status)
  980. ohci->done |= 1;
  981. hcca.done = cpu_to_le32(ohci->done);
  982. ohci->done = 0;
  983. ohci->done_count = 7;
  984. ohci_set_interrupt(ohci, OHCI_INTR_WD);
  985. }
  986. if (ohci->done_count != 7 && ohci->done_count != 0)
  987. ohci->done_count--;
  988. /* Do SOF stuff here */
  989. ohci_sof(ohci);
  990. /* Writeback HCCA */
  991. cpu_physical_memory_rw(ohci->hcca, (uint8_t *)&hcca, sizeof(hcca), 1);
  992. }
  993. /* Start sending SOF tokens across the USB bus, lists are processed in
  994. * next frame
  995. */
  996. static int ohci_bus_start(OHCIState *ohci)
  997. {
  998. ohci->eof_timer = qemu_new_timer(vm_clock,
  999. ohci_frame_boundary,
  1000. ohci);
  1001. if (ohci->eof_timer == NULL) {
  1002. fprintf(stderr, "usb-ohci: %s: qemu_new_timer failed\n", ohci->name);
  1003. /* TODO: Signal unrecoverable error */
  1004. return 0;
  1005. }
  1006. dprintf("usb-ohci: %s: USB Operational\n", ohci->name);
  1007. ohci_sof(ohci);
  1008. return 1;
  1009. }
  1010. /* Stop sending SOF tokens on the bus */
  1011. static void ohci_bus_stop(OHCIState *ohci)
  1012. {
  1013. if (ohci->eof_timer)
  1014. qemu_del_timer(ohci->eof_timer);
  1015. ohci->eof_timer = NULL;
  1016. }
  1017. /* Sets a flag in a port status register but only set it if the port is
  1018. * connected, if not set ConnectStatusChange flag. If flag is enabled
  1019. * return 1.
  1020. */
  1021. static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
  1022. {
  1023. int ret = 1;
  1024. /* writing a 0 has no effect */
  1025. if (val == 0)
  1026. return 0;
  1027. /* If CurrentConnectStatus is cleared we set
  1028. * ConnectStatusChange
  1029. */
  1030. if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
  1031. ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
  1032. if (ohci->rhstatus & OHCI_RHS_DRWE) {
  1033. /* TODO: CSC is a wakeup event */
  1034. }
  1035. return 0;
  1036. }
  1037. if (ohci->rhport[i].ctrl & val)
  1038. ret = 0;
  1039. /* set the bit */
  1040. ohci->rhport[i].ctrl |= val;
  1041. return ret;
  1042. }
  1043. /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
  1044. static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
  1045. {
  1046. val &= OHCI_FMI_FI;
  1047. if (val != ohci->fi) {
  1048. dprintf("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
  1049. ohci->name, ohci->fi, ohci->fi);
  1050. }
  1051. ohci->fi = val;
  1052. }
  1053. static void ohci_port_power(OHCIState *ohci, int i, int p)
  1054. {
  1055. if (p) {
  1056. ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
  1057. } else {
  1058. ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
  1059. OHCI_PORT_CCS|
  1060. OHCI_PORT_PSS|
  1061. OHCI_PORT_PRS);
  1062. }
  1063. }
  1064. /* Set HcControlRegister */
  1065. static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
  1066. {
  1067. uint32_t old_state;
  1068. uint32_t new_state;
  1069. old_state = ohci->ctl & OHCI_CTL_HCFS;
  1070. ohci->ctl = val;
  1071. new_state = ohci->ctl & OHCI_CTL_HCFS;
  1072. /* no state change */
  1073. if (old_state == new_state)
  1074. return;
  1075. switch (new_state) {
  1076. case OHCI_USB_OPERATIONAL:
  1077. ohci_bus_start(ohci);
  1078. break;
  1079. case OHCI_USB_SUSPEND:
  1080. ohci_bus_stop(ohci);
  1081. dprintf("usb-ohci: %s: USB Suspended\n", ohci->name);
  1082. break;
  1083. case OHCI_USB_RESUME:
  1084. dprintf("usb-ohci: %s: USB Resume\n", ohci->name);
  1085. break;
  1086. case OHCI_USB_RESET:
  1087. ohci_reset(ohci);
  1088. dprintf("usb-ohci: %s: USB Reset\n", ohci->name);
  1089. break;
  1090. }
  1091. }
  1092. static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
  1093. {
  1094. uint16_t fr;
  1095. int64_t tks;
  1096. if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
  1097. return (ohci->frt << 31);
  1098. /* Being in USB operational state guarnatees sof_time was
  1099. * set already.
  1100. */
  1101. tks = qemu_get_clock(vm_clock) - ohci->sof_time;
  1102. /* avoid muldiv if possible */
  1103. if (tks >= usb_frame_time)
  1104. return (ohci->frt << 31);
  1105. tks = muldiv64(1, tks, usb_bit_time);
  1106. fr = (uint16_t)(ohci->fi - tks);
  1107. return (ohci->frt << 31) | fr;
  1108. }
  1109. /* Set root hub status */
  1110. static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
  1111. {
  1112. uint32_t old_state;
  1113. old_state = ohci->rhstatus;
  1114. /* write 1 to clear OCIC */
  1115. if (val & OHCI_RHS_OCIC)
  1116. ohci->rhstatus &= ~OHCI_RHS_OCIC;
  1117. if (val & OHCI_RHS_LPS) {
  1118. int i;
  1119. for (i = 0; i < ohci->num_ports; i++)
  1120. ohci_port_power(ohci, i, 0);
  1121. dprintf("usb-ohci: powered down all ports\n");
  1122. }
  1123. if (val & OHCI_RHS_LPSC) {
  1124. int i;
  1125. for (i = 0; i < ohci->num_ports; i++)
  1126. ohci_port_power(ohci, i, 1);
  1127. dprintf("usb-ohci: powered up all ports\n");
  1128. }
  1129. if (val & OHCI_RHS_DRWE)
  1130. ohci->rhstatus |= OHCI_RHS_DRWE;
  1131. if (val & OHCI_RHS_CRWE)
  1132. ohci->rhstatus &= ~OHCI_RHS_DRWE;
  1133. if (old_state != ohci->rhstatus)
  1134. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1135. }
  1136. /* Set root hub port status */
  1137. static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
  1138. {
  1139. uint32_t old_state;
  1140. OHCIPort *port;
  1141. port = &ohci->rhport[portnum];
  1142. old_state = port->ctrl;
  1143. /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
  1144. if (val & OHCI_PORT_WTC)
  1145. port->ctrl &= ~(val & OHCI_PORT_WTC);
  1146. if (val & OHCI_PORT_CCS)
  1147. port->ctrl &= ~OHCI_PORT_PES;
  1148. ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
  1149. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS))
  1150. dprintf("usb-ohci: port %d: SUSPEND\n", portnum);
  1151. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
  1152. dprintf("usb-ohci: port %d: RESET\n", portnum);
  1153. usb_send_msg(port->port.dev, USB_MSG_RESET);
  1154. port->ctrl &= ~OHCI_PORT_PRS;
  1155. /* ??? Should this also set OHCI_PORT_PESC. */
  1156. port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
  1157. }
  1158. /* Invert order here to ensure in ambiguous case, device is
  1159. * powered up...
  1160. */
  1161. if (val & OHCI_PORT_LSDA)
  1162. ohci_port_power(ohci, portnum, 0);
  1163. if (val & OHCI_PORT_PPS)
  1164. ohci_port_power(ohci, portnum, 1);
  1165. if (old_state != port->ctrl)
  1166. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1167. return;
  1168. }
  1169. static uint32_t ohci_mem_read(void *ptr, target_phys_addr_t addr)
  1170. {
  1171. OHCIState *ohci = ptr;
  1172. uint32_t retval;
  1173. /* Only aligned reads are allowed on OHCI */
  1174. if (addr & 3) {
  1175. fprintf(stderr, "usb-ohci: Mis-aligned read\n");
  1176. return 0xffffffff;
  1177. } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1178. /* HcRhPortStatus */
  1179. retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
  1180. } else {
  1181. switch (addr >> 2) {
  1182. case 0: /* HcRevision */
  1183. retval = 0x10;
  1184. break;
  1185. case 1: /* HcControl */
  1186. retval = ohci->ctl;
  1187. break;
  1188. case 2: /* HcCommandStatus */
  1189. retval = ohci->status;
  1190. break;
  1191. case 3: /* HcInterruptStatus */
  1192. retval = ohci->intr_status;
  1193. break;
  1194. case 4: /* HcInterruptEnable */
  1195. case 5: /* HcInterruptDisable */
  1196. retval = ohci->intr;
  1197. break;
  1198. case 6: /* HcHCCA */
  1199. retval = ohci->hcca;
  1200. break;
  1201. case 7: /* HcPeriodCurrentED */
  1202. retval = ohci->per_cur;
  1203. break;
  1204. case 8: /* HcControlHeadED */
  1205. retval = ohci->ctrl_head;
  1206. break;
  1207. case 9: /* HcControlCurrentED */
  1208. retval = ohci->ctrl_cur;
  1209. break;
  1210. case 10: /* HcBulkHeadED */
  1211. retval = ohci->bulk_head;
  1212. break;
  1213. case 11: /* HcBulkCurrentED */
  1214. retval = ohci->bulk_cur;
  1215. break;
  1216. case 12: /* HcDoneHead */
  1217. retval = ohci->done;
  1218. break;
  1219. case 13: /* HcFmInterretval */
  1220. retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
  1221. break;
  1222. case 14: /* HcFmRemaining */
  1223. retval = ohci_get_frame_remaining(ohci);
  1224. break;
  1225. case 15: /* HcFmNumber */
  1226. retval = ohci->frame_number;
  1227. break;
  1228. case 16: /* HcPeriodicStart */
  1229. retval = ohci->pstart;
  1230. break;
  1231. case 17: /* HcLSThreshold */
  1232. retval = ohci->lst;
  1233. break;
  1234. case 18: /* HcRhDescriptorA */
  1235. retval = ohci->rhdesc_a;
  1236. break;
  1237. case 19: /* HcRhDescriptorB */
  1238. retval = ohci->rhdesc_b;
  1239. break;
  1240. case 20: /* HcRhStatus */
  1241. retval = ohci->rhstatus;
  1242. break;
  1243. /* PXA27x specific registers */
  1244. case 24: /* HcStatus */
  1245. retval = ohci->hstatus & ohci->hmask;
  1246. break;
  1247. case 25: /* HcHReset */
  1248. retval = ohci->hreset;
  1249. break;
  1250. case 26: /* HcHInterruptEnable */
  1251. retval = ohci->hmask;
  1252. break;
  1253. case 27: /* HcHInterruptTest */
  1254. retval = ohci->htest;
  1255. break;
  1256. default:
  1257. fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr);
  1258. retval = 0xffffffff;
  1259. }
  1260. }
  1261. #ifdef TARGET_WORDS_BIGENDIAN
  1262. retval = bswap32(retval);
  1263. #endif
  1264. return retval;
  1265. }
  1266. static void ohci_mem_write(void *ptr, target_phys_addr_t addr, uint32_t val)
  1267. {
  1268. OHCIState *ohci = ptr;
  1269. #ifdef TARGET_WORDS_BIGENDIAN
  1270. val = bswap32(val);
  1271. #endif
  1272. /* Only aligned reads are allowed on OHCI */
  1273. if (addr & 3) {
  1274. fprintf(stderr, "usb-ohci: Mis-aligned write\n");
  1275. return;
  1276. }
  1277. if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1278. /* HcRhPortStatus */
  1279. ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
  1280. return;
  1281. }
  1282. switch (addr >> 2) {
  1283. case 1: /* HcControl */
  1284. ohci_set_ctl(ohci, val);
  1285. break;
  1286. case 2: /* HcCommandStatus */
  1287. /* SOC is read-only */
  1288. val = (val & ~OHCI_STATUS_SOC);
  1289. /* Bits written as '0' remain unchanged in the register */
  1290. ohci->status |= val;
  1291. if (ohci->status & OHCI_STATUS_HCR)
  1292. ohci_reset(ohci);
  1293. break;
  1294. case 3: /* HcInterruptStatus */
  1295. ohci->intr_status &= ~val;
  1296. ohci_intr_update(ohci);
  1297. break;
  1298. case 4: /* HcInterruptEnable */
  1299. ohci->intr |= val;
  1300. ohci_intr_update(ohci);
  1301. break;
  1302. case 5: /* HcInterruptDisable */
  1303. ohci->intr &= ~val;
  1304. ohci_intr_update(ohci);
  1305. break;
  1306. case 6: /* HcHCCA */
  1307. ohci->hcca = val & OHCI_HCCA_MASK;
  1308. break;
  1309. case 8: /* HcControlHeadED */
  1310. ohci->ctrl_head = val & OHCI_EDPTR_MASK;
  1311. break;
  1312. case 9: /* HcControlCurrentED */
  1313. ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
  1314. break;
  1315. case 10: /* HcBulkHeadED */
  1316. ohci->bulk_head = val & OHCI_EDPTR_MASK;
  1317. break;
  1318. case 11: /* HcBulkCurrentED */
  1319. ohci->bulk_cur = val & OHCI_EDPTR_MASK;
  1320. break;
  1321. case 13: /* HcFmInterval */
  1322. ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
  1323. ohci->fit = (val & OHCI_FMI_FIT) >> 31;
  1324. ohci_set_frame_interval(ohci, val);
  1325. break;
  1326. case 15: /* HcFmNumber */
  1327. break;
  1328. case 16: /* HcPeriodicStart */
  1329. ohci->pstart = val & 0xffff;
  1330. break;
  1331. case 17: /* HcLSThreshold */
  1332. ohci->lst = val & 0xffff;
  1333. break;
  1334. case 18: /* HcRhDescriptorA */
  1335. ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
  1336. ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
  1337. break;
  1338. case 19: /* HcRhDescriptorB */
  1339. break;
  1340. case 20: /* HcRhStatus */
  1341. ohci_set_hub_status(ohci, val);
  1342. break;
  1343. /* PXA27x specific registers */
  1344. case 24: /* HcStatus */
  1345. ohci->hstatus &= ~(val & ohci->hmask);
  1346. case 25: /* HcHReset */
  1347. ohci->hreset = val & ~OHCI_HRESET_FSBIR;
  1348. if (val & OHCI_HRESET_FSBIR)
  1349. ohci_reset(ohci);
  1350. break;
  1351. case 26: /* HcHInterruptEnable */
  1352. ohci->hmask = val;
  1353. break;
  1354. case 27: /* HcHInterruptTest */
  1355. ohci->htest = val;
  1356. break;
  1357. default:
  1358. fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr);
  1359. break;
  1360. }
  1361. }
  1362. /* Only dword reads are defined on OHCI register space */
  1363. static CPUReadMemoryFunc *ohci_readfn[3]={
  1364. ohci_mem_read,
  1365. ohci_mem_read,
  1366. ohci_mem_read
  1367. };
  1368. /* Only dword writes are defined on OHCI register space */
  1369. static CPUWriteMemoryFunc *ohci_writefn[3]={
  1370. ohci_mem_write,
  1371. ohci_mem_write,
  1372. ohci_mem_write
  1373. };
  1374. static void usb_ohci_init(OHCIState *ohci, int num_ports, int devfn,
  1375. qemu_irq irq, enum ohci_type type, const char *name)
  1376. {
  1377. int i;
  1378. if (usb_frame_time == 0) {
  1379. #ifdef OHCI_TIME_WARP
  1380. usb_frame_time = ticks_per_sec;
  1381. usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ/1000);
  1382. #else
  1383. usb_frame_time = muldiv64(1, ticks_per_sec, 1000);
  1384. if (ticks_per_sec >= USB_HZ) {
  1385. usb_bit_time = muldiv64(1, ticks_per_sec, USB_HZ);
  1386. } else {
  1387. usb_bit_time = 1;
  1388. }
  1389. #endif
  1390. dprintf("usb-ohci: usb_bit_time=%lli usb_frame_time=%lli\n",
  1391. usb_frame_time, usb_bit_time);
  1392. }
  1393. ohci->mem = cpu_register_io_memory(0, ohci_readfn, ohci_writefn, ohci);
  1394. ohci->name = name;
  1395. ohci->irq = irq;
  1396. ohci->type = type;
  1397. ohci->num_ports = num_ports;
  1398. for (i = 0; i < num_ports; i++) {
  1399. qemu_register_usb_port(&ohci->rhport[i].port, ohci, i, ohci_attach);
  1400. }
  1401. ohci->async_td = 0;
  1402. qemu_register_reset(ohci_reset, ohci);
  1403. ohci_reset(ohci);
  1404. }
  1405. typedef struct {
  1406. PCIDevice pci_dev;
  1407. OHCIState state;
  1408. } OHCIPCIState;
  1409. static void ohci_mapfunc(PCIDevice *pci_dev, int i,
  1410. uint32_t addr, uint32_t size, int type)
  1411. {
  1412. OHCIPCIState *ohci = (OHCIPCIState *)pci_dev;
  1413. cpu_register_physical_memory(addr, size, ohci->state.mem);
  1414. }
  1415. void usb_ohci_init_pci(struct PCIBus *bus, int num_ports, int devfn)
  1416. {
  1417. OHCIPCIState *ohci;
  1418. ohci = (OHCIPCIState *)pci_register_device(bus, "OHCI USB", sizeof(*ohci),
  1419. devfn, NULL, NULL);
  1420. if (ohci == NULL) {
  1421. fprintf(stderr, "usb-ohci: Failed to register PCI device\n");
  1422. return;
  1423. }
  1424. pci_config_set_vendor_id(ohci->pci_dev.config, PCI_VENDOR_ID_APPLE);
  1425. pci_config_set_device_id(ohci->pci_dev.config, 0x003f); // device_id
  1426. ohci->pci_dev.config[0x09] = 0x10; /* OHCI */
  1427. pci_config_set_class(ohci->pci_dev.config, PCI_CLASS_SERIAL_USB);
  1428. ohci->pci_dev.config[0x3d] = 0x01; /* interrupt pin 1 */
  1429. usb_ohci_init(&ohci->state, num_ports, devfn, ohci->pci_dev.irq[0],
  1430. OHCI_TYPE_PCI, ohci->pci_dev.name);
  1431. pci_register_io_region((struct PCIDevice *)ohci, 0, 256,
  1432. PCI_ADDRESS_SPACE_MEM, ohci_mapfunc);
  1433. }
  1434. void usb_ohci_init_pxa(target_phys_addr_t base, int num_ports, int devfn,
  1435. qemu_irq irq)
  1436. {
  1437. OHCIState *ohci = (OHCIState *)qemu_mallocz(sizeof(OHCIState));
  1438. usb_ohci_init(ohci, num_ports, devfn, irq,
  1439. OHCI_TYPE_PXA, "OHCI USB");
  1440. cpu_register_physical_memory(base, 0x1000, ohci->mem);
  1441. }