hcd-ohci.c 55 KB

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