usb-ohci.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  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. MemoryRegion 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<<25)
  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 && port->port.dev->attached) {
  383. usb_reset(&port->port);
  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.result;
  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. usb_packet_setup(&ohci->usb_packet, pid,
  672. OHCI_BM(ed->flags, ED_FA),
  673. OHCI_BM(ed->flags, ED_EN));
  674. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, len);
  675. ret = usb_handle_packet(dev, &ohci->usb_packet);
  676. if (ret != USB_RET_NODEV)
  677. break;
  678. }
  679. if (ret == USB_RET_ASYNC) {
  680. return 1;
  681. }
  682. }
  683. #ifdef DEBUG_ISOCH
  684. printf("so 0x%.8x eo 0x%.8x\nsa 0x%.8x ea 0x%.8x\ndir %s len %zu ret %d\n",
  685. start_offset, end_offset, start_addr, end_addr, str, len, ret);
  686. #endif
  687. /* Writeback */
  688. if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
  689. /* IN transfer succeeded */
  690. ohci_copy_iso_td(ohci, start_addr, end_addr, ohci->usb_buf, ret, 1);
  691. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  692. OHCI_CC_NOERROR);
  693. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
  694. } else if (dir == OHCI_TD_DIR_OUT && ret == len) {
  695. /* OUT transfer succeeded */
  696. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  697. OHCI_CC_NOERROR);
  698. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
  699. } else {
  700. if (ret > (ssize_t) len) {
  701. printf("usb-ohci: DataOverrun %d > %zu\n", ret, len);
  702. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  703. OHCI_CC_DATAOVERRUN);
  704. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  705. len);
  706. } else if (ret >= 0) {
  707. printf("usb-ohci: DataUnderrun %d\n", ret);
  708. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  709. OHCI_CC_DATAUNDERRUN);
  710. } else {
  711. switch (ret) {
  712. case USB_RET_NODEV:
  713. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  714. OHCI_CC_DEVICENOTRESPONDING);
  715. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  716. 0);
  717. break;
  718. case USB_RET_NAK:
  719. case USB_RET_STALL:
  720. printf("usb-ohci: got NAK/STALL %d\n", ret);
  721. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  722. OHCI_CC_STALL);
  723. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
  724. 0);
  725. break;
  726. default:
  727. printf("usb-ohci: Bad device response %d\n", ret);
  728. OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
  729. OHCI_CC_UNDEXPETEDPID);
  730. break;
  731. }
  732. }
  733. }
  734. if (relative_frame_number == frame_count) {
  735. /* Last data packet of ISO TD - retire the TD to the Done Queue */
  736. OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
  737. ed->head &= ~OHCI_DPTR_MASK;
  738. ed->head |= (iso_td.next & OHCI_DPTR_MASK);
  739. iso_td.next = ohci->done;
  740. ohci->done = addr;
  741. i = OHCI_BM(iso_td.flags, TD_DI);
  742. if (i < ohci->done_count)
  743. ohci->done_count = i;
  744. }
  745. ohci_put_iso_td(ohci, addr, &iso_td);
  746. return 1;
  747. }
  748. /* Service a transport descriptor.
  749. Returns nonzero to terminate processing of this endpoint. */
  750. static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
  751. {
  752. int dir;
  753. size_t len = 0, pktlen = 0;
  754. #ifdef DEBUG_PACKET
  755. const char *str = NULL;
  756. #endif
  757. int pid;
  758. int ret;
  759. int i;
  760. USBDevice *dev;
  761. struct ohci_td td;
  762. uint32_t addr;
  763. int flag_r;
  764. int completion;
  765. addr = ed->head & OHCI_DPTR_MASK;
  766. /* See if this TD has already been submitted to the device. */
  767. completion = (addr == ohci->async_td);
  768. if (completion && !ohci->async_complete) {
  769. #ifdef DEBUG_PACKET
  770. DPRINTF("Skipping async TD\n");
  771. #endif
  772. return 1;
  773. }
  774. if (!ohci_read_td(ohci, addr, &td)) {
  775. fprintf(stderr, "usb-ohci: TD read error at %x\n", addr);
  776. return 0;
  777. }
  778. dir = OHCI_BM(ed->flags, ED_D);
  779. switch (dir) {
  780. case OHCI_TD_DIR_OUT:
  781. case OHCI_TD_DIR_IN:
  782. /* Same value. */
  783. break;
  784. default:
  785. dir = OHCI_BM(td.flags, TD_DP);
  786. break;
  787. }
  788. switch (dir) {
  789. case OHCI_TD_DIR_IN:
  790. #ifdef DEBUG_PACKET
  791. str = "in";
  792. #endif
  793. pid = USB_TOKEN_IN;
  794. break;
  795. case OHCI_TD_DIR_OUT:
  796. #ifdef DEBUG_PACKET
  797. str = "out";
  798. #endif
  799. pid = USB_TOKEN_OUT;
  800. break;
  801. case OHCI_TD_DIR_SETUP:
  802. #ifdef DEBUG_PACKET
  803. str = "setup";
  804. #endif
  805. pid = USB_TOKEN_SETUP;
  806. break;
  807. default:
  808. fprintf(stderr, "usb-ohci: Bad direction\n");
  809. return 1;
  810. }
  811. if (td.cbp && td.be) {
  812. if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
  813. len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
  814. } else {
  815. len = (td.be - td.cbp) + 1;
  816. }
  817. pktlen = len;
  818. if (len && dir != OHCI_TD_DIR_IN) {
  819. /* The endpoint may not allow us to transfer it all now */
  820. pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
  821. if (pktlen > len) {
  822. pktlen = len;
  823. }
  824. if (!completion) {
  825. ohci_copy_td(ohci, &td, ohci->usb_buf, pktlen, 0);
  826. }
  827. }
  828. }
  829. flag_r = (td.flags & OHCI_TD_R) != 0;
  830. #ifdef DEBUG_PACKET
  831. DPRINTF(" TD @ 0x%.8x %" PRId64 " of %" PRId64
  832. " bytes %s r=%d cbp=0x%.8x be=0x%.8x\n",
  833. addr, (int64_t)pktlen, (int64_t)len, str, flag_r, td.cbp, td.be);
  834. if (pktlen > 0 && dir != OHCI_TD_DIR_IN) {
  835. DPRINTF(" data:");
  836. for (i = 0; i < pktlen; i++) {
  837. printf(" %.2x", ohci->usb_buf[i]);
  838. }
  839. DPRINTF("\n");
  840. }
  841. #endif
  842. if (completion) {
  843. ret = ohci->usb_packet.result;
  844. ohci->async_td = 0;
  845. ohci->async_complete = 0;
  846. } else {
  847. ret = USB_RET_NODEV;
  848. for (i = 0; i < ohci->num_ports; i++) {
  849. dev = ohci->rhport[i].port.dev;
  850. if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0)
  851. continue;
  852. if (ohci->async_td) {
  853. /* ??? The hardware should allow one active packet per
  854. endpoint. We only allow one active packet per controller.
  855. This should be sufficient as long as devices respond in a
  856. timely manner.
  857. */
  858. #ifdef DEBUG_PACKET
  859. DPRINTF("Too many pending packets\n");
  860. #endif
  861. return 1;
  862. }
  863. usb_packet_setup(&ohci->usb_packet, pid,
  864. OHCI_BM(ed->flags, ED_FA),
  865. OHCI_BM(ed->flags, ED_EN));
  866. usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, pktlen);
  867. ret = usb_handle_packet(dev, &ohci->usb_packet);
  868. if (ret != USB_RET_NODEV)
  869. break;
  870. }
  871. #ifdef DEBUG_PACKET
  872. DPRINTF("ret=%d\n", ret);
  873. #endif
  874. if (ret == USB_RET_ASYNC) {
  875. ohci->async_td = addr;
  876. return 1;
  877. }
  878. }
  879. if (ret >= 0) {
  880. if (dir == OHCI_TD_DIR_IN) {
  881. ohci_copy_td(ohci, &td, ohci->usb_buf, ret, 1);
  882. #ifdef DEBUG_PACKET
  883. DPRINTF(" data:");
  884. for (i = 0; i < ret; i++)
  885. printf(" %.2x", ohci->usb_buf[i]);
  886. DPRINTF("\n");
  887. #endif
  888. } else {
  889. ret = pktlen;
  890. }
  891. }
  892. /* Writeback */
  893. if (ret == pktlen || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
  894. /* Transmission succeeded. */
  895. if (ret == len) {
  896. td.cbp = 0;
  897. } else {
  898. if ((td.cbp & 0xfff) + ret > 0xfff) {
  899. td.cbp = (td.be & ~0xfff) + ((td.cbp + ret) & 0xfff);
  900. } else {
  901. td.cbp += ret;
  902. }
  903. }
  904. td.flags |= OHCI_TD_T1;
  905. td.flags ^= OHCI_TD_T0;
  906. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
  907. OHCI_SET_BM(td.flags, TD_EC, 0);
  908. if ((dir != OHCI_TD_DIR_IN) && (ret != len)) {
  909. /* Partial packet transfer: TD not ready to retire yet */
  910. goto exit_no_retire;
  911. }
  912. /* Setting ED_C is part of the TD retirement process */
  913. ed->head &= ~OHCI_ED_C;
  914. if (td.flags & OHCI_TD_T0)
  915. ed->head |= OHCI_ED_C;
  916. } else {
  917. if (ret >= 0) {
  918. DPRINTF("usb-ohci: Underrun\n");
  919. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
  920. } else {
  921. switch (ret) {
  922. case USB_RET_NODEV:
  923. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
  924. case USB_RET_NAK:
  925. DPRINTF("usb-ohci: got NAK\n");
  926. return 1;
  927. case USB_RET_STALL:
  928. DPRINTF("usb-ohci: got STALL\n");
  929. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
  930. break;
  931. case USB_RET_BABBLE:
  932. DPRINTF("usb-ohci: got BABBLE\n");
  933. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
  934. break;
  935. default:
  936. fprintf(stderr, "usb-ohci: Bad device response %d\n", ret);
  937. OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
  938. OHCI_SET_BM(td.flags, TD_EC, 3);
  939. break;
  940. }
  941. }
  942. ed->head |= OHCI_ED_H;
  943. }
  944. /* Retire this TD */
  945. ed->head &= ~OHCI_DPTR_MASK;
  946. ed->head |= td.next & OHCI_DPTR_MASK;
  947. td.next = ohci->done;
  948. ohci->done = addr;
  949. i = OHCI_BM(td.flags, TD_DI);
  950. if (i < ohci->done_count)
  951. ohci->done_count = i;
  952. exit_no_retire:
  953. ohci_put_td(ohci, addr, &td);
  954. return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
  955. }
  956. /* Service an endpoint list. Returns nonzero if active TD were found. */
  957. static int ohci_service_ed_list(OHCIState *ohci, uint32_t head, int completion)
  958. {
  959. struct ohci_ed ed;
  960. uint32_t next_ed;
  961. uint32_t cur;
  962. int active;
  963. active = 0;
  964. if (head == 0)
  965. return 0;
  966. for (cur = head; cur; cur = next_ed) {
  967. if (!ohci_read_ed(ohci, cur, &ed)) {
  968. fprintf(stderr, "usb-ohci: ED read error at %x\n", cur);
  969. return 0;
  970. }
  971. next_ed = ed.next & OHCI_DPTR_MASK;
  972. if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
  973. uint32_t addr;
  974. /* Cancel pending packets for ED that have been paused. */
  975. addr = ed.head & OHCI_DPTR_MASK;
  976. if (ohci->async_td && addr == ohci->async_td) {
  977. usb_cancel_packet(&ohci->usb_packet);
  978. ohci->async_td = 0;
  979. }
  980. continue;
  981. }
  982. while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
  983. #ifdef DEBUG_PACKET
  984. DPRINTF("ED @ 0x%.8x fa=%u en=%u d=%u s=%u k=%u f=%u mps=%u "
  985. "h=%u c=%u\n head=0x%.8x tailp=0x%.8x next=0x%.8x\n", cur,
  986. OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
  987. OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S)!= 0,
  988. (ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
  989. OHCI_BM(ed.flags, ED_MPS), (ed.head & OHCI_ED_H) != 0,
  990. (ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
  991. ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
  992. #endif
  993. active = 1;
  994. if ((ed.flags & OHCI_ED_F) == 0) {
  995. if (ohci_service_td(ohci, &ed))
  996. break;
  997. } else {
  998. /* Handle isochronous endpoints */
  999. if (ohci_service_iso_td(ohci, &ed, completion))
  1000. break;
  1001. }
  1002. }
  1003. ohci_put_ed(ohci, cur, &ed);
  1004. }
  1005. return active;
  1006. }
  1007. /* Generate a SOF event, and set a timer for EOF */
  1008. static void ohci_sof(OHCIState *ohci)
  1009. {
  1010. ohci->sof_time = qemu_get_clock_ns(vm_clock);
  1011. qemu_mod_timer(ohci->eof_timer, ohci->sof_time + usb_frame_time);
  1012. ohci_set_interrupt(ohci, OHCI_INTR_SF);
  1013. }
  1014. /* Process Control and Bulk lists. */
  1015. static void ohci_process_lists(OHCIState *ohci, int completion)
  1016. {
  1017. if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
  1018. if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
  1019. DPRINTF("usb-ohci: head %x, cur %x\n",
  1020. ohci->ctrl_head, ohci->ctrl_cur);
  1021. }
  1022. if (!ohci_service_ed_list(ohci, ohci->ctrl_head, completion)) {
  1023. ohci->ctrl_cur = 0;
  1024. ohci->status &= ~OHCI_STATUS_CLF;
  1025. }
  1026. }
  1027. if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
  1028. if (!ohci_service_ed_list(ohci, ohci->bulk_head, completion)) {
  1029. ohci->bulk_cur = 0;
  1030. ohci->status &= ~OHCI_STATUS_BLF;
  1031. }
  1032. }
  1033. }
  1034. /* Do frame processing on frame boundary */
  1035. static void ohci_frame_boundary(void *opaque)
  1036. {
  1037. OHCIState *ohci = opaque;
  1038. struct ohci_hcca hcca;
  1039. ohci_read_hcca(ohci, ohci->hcca, &hcca);
  1040. /* Process all the lists at the end of the frame */
  1041. if (ohci->ctl & OHCI_CTL_PLE) {
  1042. int n;
  1043. n = ohci->frame_number & 0x1f;
  1044. ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]), 0);
  1045. }
  1046. /* Cancel all pending packets if either of the lists has been disabled. */
  1047. if (ohci->async_td &&
  1048. ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
  1049. usb_cancel_packet(&ohci->usb_packet);
  1050. ohci->async_td = 0;
  1051. }
  1052. ohci->old_ctl = ohci->ctl;
  1053. ohci_process_lists(ohci, 0);
  1054. /* Frame boundary, so do EOF stuf here */
  1055. ohci->frt = ohci->fit;
  1056. /* Increment frame number and take care of endianness. */
  1057. ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
  1058. hcca.frame = cpu_to_le16(ohci->frame_number);
  1059. if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
  1060. if (!ohci->done)
  1061. abort();
  1062. if (ohci->intr & ohci->intr_status)
  1063. ohci->done |= 1;
  1064. hcca.done = cpu_to_le32(ohci->done);
  1065. ohci->done = 0;
  1066. ohci->done_count = 7;
  1067. ohci_set_interrupt(ohci, OHCI_INTR_WD);
  1068. }
  1069. if (ohci->done_count != 7 && ohci->done_count != 0)
  1070. ohci->done_count--;
  1071. /* Do SOF stuff here */
  1072. ohci_sof(ohci);
  1073. /* Writeback HCCA */
  1074. ohci_put_hcca(ohci, ohci->hcca, &hcca);
  1075. }
  1076. /* Start sending SOF tokens across the USB bus, lists are processed in
  1077. * next frame
  1078. */
  1079. static int ohci_bus_start(OHCIState *ohci)
  1080. {
  1081. ohci->eof_timer = qemu_new_timer_ns(vm_clock,
  1082. ohci_frame_boundary,
  1083. ohci);
  1084. if (ohci->eof_timer == NULL) {
  1085. fprintf(stderr, "usb-ohci: %s: qemu_new_timer_ns failed\n", ohci->name);
  1086. /* TODO: Signal unrecoverable error */
  1087. return 0;
  1088. }
  1089. DPRINTF("usb-ohci: %s: USB Operational\n", ohci->name);
  1090. ohci_sof(ohci);
  1091. return 1;
  1092. }
  1093. /* Stop sending SOF tokens on the bus */
  1094. static void ohci_bus_stop(OHCIState *ohci)
  1095. {
  1096. if (ohci->eof_timer)
  1097. qemu_del_timer(ohci->eof_timer);
  1098. ohci->eof_timer = NULL;
  1099. }
  1100. /* Sets a flag in a port status register but only set it if the port is
  1101. * connected, if not set ConnectStatusChange flag. If flag is enabled
  1102. * return 1.
  1103. */
  1104. static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
  1105. {
  1106. int ret = 1;
  1107. /* writing a 0 has no effect */
  1108. if (val == 0)
  1109. return 0;
  1110. /* If CurrentConnectStatus is cleared we set
  1111. * ConnectStatusChange
  1112. */
  1113. if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
  1114. ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
  1115. if (ohci->rhstatus & OHCI_RHS_DRWE) {
  1116. /* TODO: CSC is a wakeup event */
  1117. }
  1118. return 0;
  1119. }
  1120. if (ohci->rhport[i].ctrl & val)
  1121. ret = 0;
  1122. /* set the bit */
  1123. ohci->rhport[i].ctrl |= val;
  1124. return ret;
  1125. }
  1126. /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
  1127. static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
  1128. {
  1129. val &= OHCI_FMI_FI;
  1130. if (val != ohci->fi) {
  1131. DPRINTF("usb-ohci: %s: FrameInterval = 0x%x (%u)\n",
  1132. ohci->name, ohci->fi, ohci->fi);
  1133. }
  1134. ohci->fi = val;
  1135. }
  1136. static void ohci_port_power(OHCIState *ohci, int i, int p)
  1137. {
  1138. if (p) {
  1139. ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
  1140. } else {
  1141. ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS|
  1142. OHCI_PORT_CCS|
  1143. OHCI_PORT_PSS|
  1144. OHCI_PORT_PRS);
  1145. }
  1146. }
  1147. /* Set HcControlRegister */
  1148. static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
  1149. {
  1150. uint32_t old_state;
  1151. uint32_t new_state;
  1152. old_state = ohci->ctl & OHCI_CTL_HCFS;
  1153. ohci->ctl = val;
  1154. new_state = ohci->ctl & OHCI_CTL_HCFS;
  1155. /* no state change */
  1156. if (old_state == new_state)
  1157. return;
  1158. switch (new_state) {
  1159. case OHCI_USB_OPERATIONAL:
  1160. ohci_bus_start(ohci);
  1161. break;
  1162. case OHCI_USB_SUSPEND:
  1163. ohci_bus_stop(ohci);
  1164. DPRINTF("usb-ohci: %s: USB Suspended\n", ohci->name);
  1165. break;
  1166. case OHCI_USB_RESUME:
  1167. DPRINTF("usb-ohci: %s: USB Resume\n", ohci->name);
  1168. break;
  1169. case OHCI_USB_RESET:
  1170. ohci_reset(ohci);
  1171. DPRINTF("usb-ohci: %s: USB Reset\n", ohci->name);
  1172. break;
  1173. }
  1174. }
  1175. static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
  1176. {
  1177. uint16_t fr;
  1178. int64_t tks;
  1179. if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL)
  1180. return (ohci->frt << 31);
  1181. /* Being in USB operational state guarnatees sof_time was
  1182. * set already.
  1183. */
  1184. tks = qemu_get_clock_ns(vm_clock) - ohci->sof_time;
  1185. /* avoid muldiv if possible */
  1186. if (tks >= usb_frame_time)
  1187. return (ohci->frt << 31);
  1188. tks = muldiv64(1, tks, usb_bit_time);
  1189. fr = (uint16_t)(ohci->fi - tks);
  1190. return (ohci->frt << 31) | fr;
  1191. }
  1192. /* Set root hub status */
  1193. static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
  1194. {
  1195. uint32_t old_state;
  1196. old_state = ohci->rhstatus;
  1197. /* write 1 to clear OCIC */
  1198. if (val & OHCI_RHS_OCIC)
  1199. ohci->rhstatus &= ~OHCI_RHS_OCIC;
  1200. if (val & OHCI_RHS_LPS) {
  1201. int i;
  1202. for (i = 0; i < ohci->num_ports; i++)
  1203. ohci_port_power(ohci, i, 0);
  1204. DPRINTF("usb-ohci: powered down all ports\n");
  1205. }
  1206. if (val & OHCI_RHS_LPSC) {
  1207. int i;
  1208. for (i = 0; i < ohci->num_ports; i++)
  1209. ohci_port_power(ohci, i, 1);
  1210. DPRINTF("usb-ohci: powered up all ports\n");
  1211. }
  1212. if (val & OHCI_RHS_DRWE)
  1213. ohci->rhstatus |= OHCI_RHS_DRWE;
  1214. if (val & OHCI_RHS_CRWE)
  1215. ohci->rhstatus &= ~OHCI_RHS_DRWE;
  1216. if (old_state != ohci->rhstatus)
  1217. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1218. }
  1219. /* Set root hub port status */
  1220. static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
  1221. {
  1222. uint32_t old_state;
  1223. OHCIPort *port;
  1224. port = &ohci->rhport[portnum];
  1225. old_state = port->ctrl;
  1226. /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
  1227. if (val & OHCI_PORT_WTC)
  1228. port->ctrl &= ~(val & OHCI_PORT_WTC);
  1229. if (val & OHCI_PORT_CCS)
  1230. port->ctrl &= ~OHCI_PORT_PES;
  1231. ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
  1232. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
  1233. DPRINTF("usb-ohci: port %d: SUSPEND\n", portnum);
  1234. }
  1235. if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
  1236. DPRINTF("usb-ohci: port %d: RESET\n", portnum);
  1237. usb_send_msg(port->port.dev, USB_MSG_RESET);
  1238. port->ctrl &= ~OHCI_PORT_PRS;
  1239. /* ??? Should this also set OHCI_PORT_PESC. */
  1240. port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
  1241. }
  1242. /* Invert order here to ensure in ambiguous case, device is
  1243. * powered up...
  1244. */
  1245. if (val & OHCI_PORT_LSDA)
  1246. ohci_port_power(ohci, portnum, 0);
  1247. if (val & OHCI_PORT_PPS)
  1248. ohci_port_power(ohci, portnum, 1);
  1249. if (old_state != port->ctrl)
  1250. ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
  1251. return;
  1252. }
  1253. static uint64_t ohci_mem_read(void *opaque,
  1254. target_phys_addr_t addr,
  1255. unsigned size)
  1256. {
  1257. OHCIState *ohci = opaque;
  1258. uint32_t retval;
  1259. /* Only aligned reads are allowed on OHCI */
  1260. if (addr & 3) {
  1261. fprintf(stderr, "usb-ohci: Mis-aligned read\n");
  1262. return 0xffffffff;
  1263. } else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1264. /* HcRhPortStatus */
  1265. retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
  1266. } else {
  1267. switch (addr >> 2) {
  1268. case 0: /* HcRevision */
  1269. retval = 0x10;
  1270. break;
  1271. case 1: /* HcControl */
  1272. retval = ohci->ctl;
  1273. break;
  1274. case 2: /* HcCommandStatus */
  1275. retval = ohci->status;
  1276. break;
  1277. case 3: /* HcInterruptStatus */
  1278. retval = ohci->intr_status;
  1279. break;
  1280. case 4: /* HcInterruptEnable */
  1281. case 5: /* HcInterruptDisable */
  1282. retval = ohci->intr;
  1283. break;
  1284. case 6: /* HcHCCA */
  1285. retval = ohci->hcca;
  1286. break;
  1287. case 7: /* HcPeriodCurrentED */
  1288. retval = ohci->per_cur;
  1289. break;
  1290. case 8: /* HcControlHeadED */
  1291. retval = ohci->ctrl_head;
  1292. break;
  1293. case 9: /* HcControlCurrentED */
  1294. retval = ohci->ctrl_cur;
  1295. break;
  1296. case 10: /* HcBulkHeadED */
  1297. retval = ohci->bulk_head;
  1298. break;
  1299. case 11: /* HcBulkCurrentED */
  1300. retval = ohci->bulk_cur;
  1301. break;
  1302. case 12: /* HcDoneHead */
  1303. retval = ohci->done;
  1304. break;
  1305. case 13: /* HcFmInterretval */
  1306. retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
  1307. break;
  1308. case 14: /* HcFmRemaining */
  1309. retval = ohci_get_frame_remaining(ohci);
  1310. break;
  1311. case 15: /* HcFmNumber */
  1312. retval = ohci->frame_number;
  1313. break;
  1314. case 16: /* HcPeriodicStart */
  1315. retval = ohci->pstart;
  1316. break;
  1317. case 17: /* HcLSThreshold */
  1318. retval = ohci->lst;
  1319. break;
  1320. case 18: /* HcRhDescriptorA */
  1321. retval = ohci->rhdesc_a;
  1322. break;
  1323. case 19: /* HcRhDescriptorB */
  1324. retval = ohci->rhdesc_b;
  1325. break;
  1326. case 20: /* HcRhStatus */
  1327. retval = ohci->rhstatus;
  1328. break;
  1329. /* PXA27x specific registers */
  1330. case 24: /* HcStatus */
  1331. retval = ohci->hstatus & ohci->hmask;
  1332. break;
  1333. case 25: /* HcHReset */
  1334. retval = ohci->hreset;
  1335. break;
  1336. case 26: /* HcHInterruptEnable */
  1337. retval = ohci->hmask;
  1338. break;
  1339. case 27: /* HcHInterruptTest */
  1340. retval = ohci->htest;
  1341. break;
  1342. default:
  1343. fprintf(stderr, "ohci_read: Bad offset %x\n", (int)addr);
  1344. retval = 0xffffffff;
  1345. }
  1346. }
  1347. return retval;
  1348. }
  1349. static void ohci_mem_write(void *opaque,
  1350. target_phys_addr_t addr,
  1351. uint64_t val,
  1352. unsigned size)
  1353. {
  1354. OHCIState *ohci = opaque;
  1355. /* Only aligned reads are allowed on OHCI */
  1356. if (addr & 3) {
  1357. fprintf(stderr, "usb-ohci: Mis-aligned write\n");
  1358. return;
  1359. }
  1360. if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
  1361. /* HcRhPortStatus */
  1362. ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
  1363. return;
  1364. }
  1365. switch (addr >> 2) {
  1366. case 1: /* HcControl */
  1367. ohci_set_ctl(ohci, val);
  1368. break;
  1369. case 2: /* HcCommandStatus */
  1370. /* SOC is read-only */
  1371. val = (val & ~OHCI_STATUS_SOC);
  1372. /* Bits written as '0' remain unchanged in the register */
  1373. ohci->status |= val;
  1374. if (ohci->status & OHCI_STATUS_HCR)
  1375. ohci_reset(ohci);
  1376. break;
  1377. case 3: /* HcInterruptStatus */
  1378. ohci->intr_status &= ~val;
  1379. ohci_intr_update(ohci);
  1380. break;
  1381. case 4: /* HcInterruptEnable */
  1382. ohci->intr |= val;
  1383. ohci_intr_update(ohci);
  1384. break;
  1385. case 5: /* HcInterruptDisable */
  1386. ohci->intr &= ~val;
  1387. ohci_intr_update(ohci);
  1388. break;
  1389. case 6: /* HcHCCA */
  1390. ohci->hcca = val & OHCI_HCCA_MASK;
  1391. break;
  1392. case 7: /* HcPeriodCurrentED */
  1393. /* Ignore writes to this read-only register, Linux does them */
  1394. break;
  1395. case 8: /* HcControlHeadED */
  1396. ohci->ctrl_head = val & OHCI_EDPTR_MASK;
  1397. break;
  1398. case 9: /* HcControlCurrentED */
  1399. ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
  1400. break;
  1401. case 10: /* HcBulkHeadED */
  1402. ohci->bulk_head = val & OHCI_EDPTR_MASK;
  1403. break;
  1404. case 11: /* HcBulkCurrentED */
  1405. ohci->bulk_cur = val & OHCI_EDPTR_MASK;
  1406. break;
  1407. case 13: /* HcFmInterval */
  1408. ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
  1409. ohci->fit = (val & OHCI_FMI_FIT) >> 31;
  1410. ohci_set_frame_interval(ohci, val);
  1411. break;
  1412. case 15: /* HcFmNumber */
  1413. break;
  1414. case 16: /* HcPeriodicStart */
  1415. ohci->pstart = val & 0xffff;
  1416. break;
  1417. case 17: /* HcLSThreshold */
  1418. ohci->lst = val & 0xffff;
  1419. break;
  1420. case 18: /* HcRhDescriptorA */
  1421. ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
  1422. ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
  1423. break;
  1424. case 19: /* HcRhDescriptorB */
  1425. break;
  1426. case 20: /* HcRhStatus */
  1427. ohci_set_hub_status(ohci, val);
  1428. break;
  1429. /* PXA27x specific registers */
  1430. case 24: /* HcStatus */
  1431. ohci->hstatus &= ~(val & ohci->hmask);
  1432. case 25: /* HcHReset */
  1433. ohci->hreset = val & ~OHCI_HRESET_FSBIR;
  1434. if (val & OHCI_HRESET_FSBIR)
  1435. ohci_reset(ohci);
  1436. break;
  1437. case 26: /* HcHInterruptEnable */
  1438. ohci->hmask = val;
  1439. break;
  1440. case 27: /* HcHInterruptTest */
  1441. ohci->htest = val;
  1442. break;
  1443. default:
  1444. fprintf(stderr, "ohci_write: Bad offset %x\n", (int)addr);
  1445. break;
  1446. }
  1447. }
  1448. static void ohci_async_cancel_device(OHCIState *ohci, USBDevice *dev)
  1449. {
  1450. if (ohci->async_td && ohci->usb_packet.owner == dev) {
  1451. usb_cancel_packet(&ohci->usb_packet);
  1452. ohci->async_td = 0;
  1453. }
  1454. }
  1455. static const MemoryRegionOps ohci_mem_ops = {
  1456. .read = ohci_mem_read,
  1457. .write = ohci_mem_write,
  1458. .endianness = DEVICE_LITTLE_ENDIAN,
  1459. };
  1460. static USBPortOps ohci_port_ops = {
  1461. .attach = ohci_attach,
  1462. .detach = ohci_detach,
  1463. .child_detach = ohci_child_detach,
  1464. .wakeup = ohci_wakeup,
  1465. .complete = ohci_async_complete_packet,
  1466. };
  1467. static USBBusOps ohci_bus_ops = {
  1468. };
  1469. static int usb_ohci_init(OHCIState *ohci, DeviceState *dev,
  1470. int num_ports, uint32_t localmem_base,
  1471. char *masterbus, uint32_t firstport)
  1472. {
  1473. int i;
  1474. if (usb_frame_time == 0) {
  1475. #ifdef OHCI_TIME_WARP
  1476. usb_frame_time = get_ticks_per_sec();
  1477. usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ/1000);
  1478. #else
  1479. usb_frame_time = muldiv64(1, get_ticks_per_sec(), 1000);
  1480. if (get_ticks_per_sec() >= USB_HZ) {
  1481. usb_bit_time = muldiv64(1, get_ticks_per_sec(), USB_HZ);
  1482. } else {
  1483. usb_bit_time = 1;
  1484. }
  1485. #endif
  1486. DPRINTF("usb-ohci: usb_bit_time=%" PRId64 " usb_frame_time=%" PRId64 "\n",
  1487. usb_frame_time, usb_bit_time);
  1488. }
  1489. ohci->num_ports = num_ports;
  1490. if (masterbus) {
  1491. USBPort *ports[OHCI_MAX_PORTS];
  1492. for(i = 0; i < num_ports; i++) {
  1493. ports[i] = &ohci->rhport[i].port;
  1494. }
  1495. if (usb_register_companion(masterbus, ports, num_ports,
  1496. firstport, ohci, &ohci_port_ops,
  1497. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL) != 0) {
  1498. return -1;
  1499. }
  1500. } else {
  1501. usb_bus_new(&ohci->bus, &ohci_bus_ops, dev);
  1502. for (i = 0; i < num_ports; i++) {
  1503. usb_register_port(&ohci->bus, &ohci->rhport[i].port,
  1504. ohci, i, &ohci_port_ops,
  1505. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1506. }
  1507. }
  1508. memory_region_init_io(&ohci->mem, &ohci_mem_ops, ohci, "ohci", 256);
  1509. ohci->localmem_base = localmem_base;
  1510. ohci->name = dev->info->name;
  1511. usb_packet_init(&ohci->usb_packet);
  1512. ohci->async_td = 0;
  1513. qemu_register_reset(ohci_reset, ohci);
  1514. return 0;
  1515. }
  1516. typedef struct {
  1517. PCIDevice pci_dev;
  1518. OHCIState state;
  1519. char *masterbus;
  1520. uint32_t num_ports;
  1521. uint32_t firstport;
  1522. } OHCIPCIState;
  1523. static int usb_ohci_initfn_pci(struct PCIDevice *dev)
  1524. {
  1525. OHCIPCIState *ohci = DO_UPCAST(OHCIPCIState, pci_dev, dev);
  1526. ohci->pci_dev.config[PCI_CLASS_PROG] = 0x10; /* OHCI */
  1527. ohci->pci_dev.config[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
  1528. if (usb_ohci_init(&ohci->state, &dev->qdev, ohci->num_ports, 0,
  1529. ohci->masterbus, ohci->firstport) != 0) {
  1530. return -1;
  1531. }
  1532. ohci->state.irq = ohci->pci_dev.irq[0];
  1533. /* TODO: avoid cast below by using dev */
  1534. pci_register_bar(&ohci->pci_dev, 0, 0, &ohci->state.mem);
  1535. return 0;
  1536. }
  1537. void usb_ohci_init_pci(struct PCIBus *bus, int devfn)
  1538. {
  1539. pci_create_simple(bus, devfn, "pci-ohci");
  1540. }
  1541. typedef struct {
  1542. SysBusDevice busdev;
  1543. OHCIState ohci;
  1544. uint32_t num_ports;
  1545. target_phys_addr_t dma_offset;
  1546. } OHCISysBusState;
  1547. static int ohci_init_pxa(SysBusDevice *dev)
  1548. {
  1549. OHCISysBusState *s = FROM_SYSBUS(OHCISysBusState, dev);
  1550. /* Cannot fail as we pass NULL for masterbus */
  1551. usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset, NULL, 0);
  1552. sysbus_init_irq(dev, &s->ohci.irq);
  1553. sysbus_init_mmio_region(dev, &s->ohci.mem);
  1554. return 0;
  1555. }
  1556. static PCIDeviceInfo ohci_pci_info = {
  1557. .qdev.name = "pci-ohci",
  1558. .qdev.desc = "Apple USB Controller",
  1559. .qdev.size = sizeof(OHCIPCIState),
  1560. .init = usb_ohci_initfn_pci,
  1561. .vendor_id = PCI_VENDOR_ID_APPLE,
  1562. .device_id = PCI_DEVICE_ID_APPLE_IPID_USB,
  1563. .class_id = PCI_CLASS_SERIAL_USB,
  1564. .qdev.props = (Property[]) {
  1565. DEFINE_PROP_STRING("masterbus", OHCIPCIState, masterbus),
  1566. DEFINE_PROP_UINT32("num-ports", OHCIPCIState, num_ports, 3),
  1567. DEFINE_PROP_UINT32("firstport", OHCIPCIState, firstport, 0),
  1568. DEFINE_PROP_END_OF_LIST(),
  1569. },
  1570. };
  1571. static SysBusDeviceInfo ohci_sysbus_info = {
  1572. .init = ohci_init_pxa,
  1573. .qdev.name = "sysbus-ohci",
  1574. .qdev.desc = "OHCI USB Controller",
  1575. .qdev.size = sizeof(OHCISysBusState),
  1576. .qdev.props = (Property[]) {
  1577. DEFINE_PROP_UINT32("num-ports", OHCISysBusState, num_ports, 3),
  1578. DEFINE_PROP_TADDR("dma-offset", OHCISysBusState, dma_offset, 3),
  1579. DEFINE_PROP_END_OF_LIST(),
  1580. }
  1581. };
  1582. static void ohci_register(void)
  1583. {
  1584. pci_qdev_register(&ohci_pci_info);
  1585. sysbus_register_withprop(&ohci_sysbus_info);
  1586. }
  1587. device_init(ohci_register);