2 * QEMU USB OHCI Emulation
3 * Copyright (c) 2004 Gianni Tedesco
4 * Copyright (c) 2006 CodeSourcery
5 * Copyright (c) 2006 Openedhand Ltd.
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.
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.
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/>.
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
25 * o BIOS work to boot from USB storage
29 #include "qemu/timer.h"
31 #include "hw/pci/pci.h"
32 #include "hw/sysbus.h"
33 #include "hw/qdev-dma.h"
36 /* This causes frames to occur 1000x slower */
37 //#define OHCI_TIME_WARP 1
39 /* Number of Downstream Ports on the root hub. */
41 #define OHCI_MAX_PORTS 15
43 static int64_t usb_frame_time
;
44 static int64_t usb_bit_time
;
46 typedef struct OHCIPort
{
63 /* Control partition */
68 /* memory pointer partition */
70 uint32_t ctrl_head
, ctrl_cur
;
71 uint32_t bulk_head
, bulk_cur
;
76 /* Frame counter partition */
81 uint16_t frame_number
;
86 /* Root Hub partition */
87 uint32_t rhdesc_a
, rhdesc_b
;
89 OHCIPort rhport
[OHCI_MAX_PORTS
];
91 /* PXA27x Non-OHCI events */
97 /* SM501 local memory offset */
98 dma_addr_t localmem_base
;
100 /* Active packets. */
102 USBPacket usb_packet
;
103 uint8_t usb_buf
[8192];
109 /* Host Controller Communications Area */
115 #define HCCA_WRITEBACK_OFFSET offsetof(struct ohci_hcca, frame)
116 #define HCCA_WRITEBACK_SIZE 8 /* frame, pad, done */
118 #define ED_WBACK_OFFSET offsetof(struct ohci_ed, head)
119 #define ED_WBACK_SIZE 4
121 static void ohci_bus_stop(OHCIState
*ohci
);
122 static void ohci_async_cancel_device(OHCIState
*ohci
, USBDevice
*dev
);
124 /* Bitfields for the first word of an Endpoint Desciptor. */
125 #define OHCI_ED_FA_SHIFT 0
126 #define OHCI_ED_FA_MASK (0x7f<<OHCI_ED_FA_SHIFT)
127 #define OHCI_ED_EN_SHIFT 7
128 #define OHCI_ED_EN_MASK (0xf<<OHCI_ED_EN_SHIFT)
129 #define OHCI_ED_D_SHIFT 11
130 #define OHCI_ED_D_MASK (3<<OHCI_ED_D_SHIFT)
131 #define OHCI_ED_S (1<<13)
132 #define OHCI_ED_K (1<<14)
133 #define OHCI_ED_F (1<<15)
134 #define OHCI_ED_MPS_SHIFT 16
135 #define OHCI_ED_MPS_MASK (0x7ff<<OHCI_ED_MPS_SHIFT)
137 /* Flags in the head field of an Endpoint Desciptor. */
141 /* Bitfields for the first word of a Transfer Desciptor. */
142 #define OHCI_TD_R (1<<18)
143 #define OHCI_TD_DP_SHIFT 19
144 #define OHCI_TD_DP_MASK (3<<OHCI_TD_DP_SHIFT)
145 #define OHCI_TD_DI_SHIFT 21
146 #define OHCI_TD_DI_MASK (7<<OHCI_TD_DI_SHIFT)
147 #define OHCI_TD_T0 (1<<24)
148 #define OHCI_TD_T1 (1<<25)
149 #define OHCI_TD_EC_SHIFT 26
150 #define OHCI_TD_EC_MASK (3<<OHCI_TD_EC_SHIFT)
151 #define OHCI_TD_CC_SHIFT 28
152 #define OHCI_TD_CC_MASK (0xf<<OHCI_TD_CC_SHIFT)
154 /* Bitfields for the first word of an Isochronous Transfer Desciptor. */
155 /* CC & DI - same as in the General Transfer Desciptor */
156 #define OHCI_TD_SF_SHIFT 0
157 #define OHCI_TD_SF_MASK (0xffff<<OHCI_TD_SF_SHIFT)
158 #define OHCI_TD_FC_SHIFT 24
159 #define OHCI_TD_FC_MASK (7<<OHCI_TD_FC_SHIFT)
161 /* Isochronous Transfer Desciptor - Offset / PacketStatusWord */
162 #define OHCI_TD_PSW_CC_SHIFT 12
163 #define OHCI_TD_PSW_CC_MASK (0xf<<OHCI_TD_PSW_CC_SHIFT)
164 #define OHCI_TD_PSW_SIZE_SHIFT 0
165 #define OHCI_TD_PSW_SIZE_MASK (0xfff<<OHCI_TD_PSW_SIZE_SHIFT)
167 #define OHCI_PAGE_MASK 0xfffff000
168 #define OHCI_OFFSET_MASK 0xfff
170 #define OHCI_DPTR_MASK 0xfffffff0
172 #define OHCI_BM(val, field) \
173 (((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
175 #define OHCI_SET_BM(val, field, newval) do { \
176 val &= ~OHCI_##field##_MASK; \
177 val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
180 /* endpoint descriptor */
188 /* General transfer descriptor */
196 /* Isochronous transfer descriptor */
205 #define USB_HZ 12000000
207 /* OHCI Local stuff */
208 #define OHCI_CTL_CBSR ((1<<0)|(1<<1))
209 #define OHCI_CTL_PLE (1<<2)
210 #define OHCI_CTL_IE (1<<3)
211 #define OHCI_CTL_CLE (1<<4)
212 #define OHCI_CTL_BLE (1<<5)
213 #define OHCI_CTL_HCFS ((1<<6)|(1<<7))
214 #define OHCI_USB_RESET 0x00
215 #define OHCI_USB_RESUME 0x40
216 #define OHCI_USB_OPERATIONAL 0x80
217 #define OHCI_USB_SUSPEND 0xc0
218 #define OHCI_CTL_IR (1<<8)
219 #define OHCI_CTL_RWC (1<<9)
220 #define OHCI_CTL_RWE (1<<10)
222 #define OHCI_STATUS_HCR (1<<0)
223 #define OHCI_STATUS_CLF (1<<1)
224 #define OHCI_STATUS_BLF (1<<2)
225 #define OHCI_STATUS_OCR (1<<3)
226 #define OHCI_STATUS_SOC ((1<<6)|(1<<7))
228 #define OHCI_INTR_SO (1U<<0) /* Scheduling overrun */
229 #define OHCI_INTR_WD (1U<<1) /* HcDoneHead writeback */
230 #define OHCI_INTR_SF (1U<<2) /* Start of frame */
231 #define OHCI_INTR_RD (1U<<3) /* Resume detect */
232 #define OHCI_INTR_UE (1U<<4) /* Unrecoverable error */
233 #define OHCI_INTR_FNO (1U<<5) /* Frame number overflow */
234 #define OHCI_INTR_RHSC (1U<<6) /* Root hub status change */
235 #define OHCI_INTR_OC (1U<<30) /* Ownership change */
236 #define OHCI_INTR_MIE (1U<<31) /* Master Interrupt Enable */
238 #define OHCI_HCCA_SIZE 0x100
239 #define OHCI_HCCA_MASK 0xffffff00
241 #define OHCI_EDPTR_MASK 0xfffffff0
243 #define OHCI_FMI_FI 0x00003fff
244 #define OHCI_FMI_FSMPS 0xffff0000
245 #define OHCI_FMI_FIT 0x80000000
247 #define OHCI_FR_RT (1U<<31)
249 #define OHCI_LS_THRESH 0x628
251 #define OHCI_RHA_RW_MASK 0x00000000 /* Mask of supported features. */
252 #define OHCI_RHA_PSM (1<<8)
253 #define OHCI_RHA_NPS (1<<9)
254 #define OHCI_RHA_DT (1<<10)
255 #define OHCI_RHA_OCPM (1<<11)
256 #define OHCI_RHA_NOCP (1<<12)
257 #define OHCI_RHA_POTPGT_MASK 0xff000000
259 #define OHCI_RHS_LPS (1U<<0)
260 #define OHCI_RHS_OCI (1U<<1)
261 #define OHCI_RHS_DRWE (1U<<15)
262 #define OHCI_RHS_LPSC (1U<<16)
263 #define OHCI_RHS_OCIC (1U<<17)
264 #define OHCI_RHS_CRWE (1U<<31)
266 #define OHCI_PORT_CCS (1<<0)
267 #define OHCI_PORT_PES (1<<1)
268 #define OHCI_PORT_PSS (1<<2)
269 #define OHCI_PORT_POCI (1<<3)
270 #define OHCI_PORT_PRS (1<<4)
271 #define OHCI_PORT_PPS (1<<8)
272 #define OHCI_PORT_LSDA (1<<9)
273 #define OHCI_PORT_CSC (1<<16)
274 #define OHCI_PORT_PESC (1<<17)
275 #define OHCI_PORT_PSSC (1<<18)
276 #define OHCI_PORT_OCIC (1<<19)
277 #define OHCI_PORT_PRSC (1<<20)
278 #define OHCI_PORT_WTC (OHCI_PORT_CSC|OHCI_PORT_PESC|OHCI_PORT_PSSC \
279 |OHCI_PORT_OCIC|OHCI_PORT_PRSC)
281 #define OHCI_TD_DIR_SETUP 0x0
282 #define OHCI_TD_DIR_OUT 0x1
283 #define OHCI_TD_DIR_IN 0x2
284 #define OHCI_TD_DIR_RESERVED 0x3
286 #define OHCI_CC_NOERROR 0x0
287 #define OHCI_CC_CRC 0x1
288 #define OHCI_CC_BITSTUFFING 0x2
289 #define OHCI_CC_DATATOGGLEMISMATCH 0x3
290 #define OHCI_CC_STALL 0x4
291 #define OHCI_CC_DEVICENOTRESPONDING 0x5
292 #define OHCI_CC_PIDCHECKFAILURE 0x6
293 #define OHCI_CC_UNDEXPETEDPID 0x7
294 #define OHCI_CC_DATAOVERRUN 0x8
295 #define OHCI_CC_DATAUNDERRUN 0x9
296 #define OHCI_CC_BUFFEROVERRUN 0xc
297 #define OHCI_CC_BUFFERUNDERRUN 0xd
299 #define OHCI_HRESET_FSBIR (1 << 0)
301 static void ohci_die(OHCIState
*ohci
);
303 /* Update IRQ levels */
304 static inline void ohci_intr_update(OHCIState
*ohci
)
308 if ((ohci
->intr
& OHCI_INTR_MIE
) &&
309 (ohci
->intr_status
& ohci
->intr
))
312 qemu_set_irq(ohci
->irq
, level
);
315 /* Set an interrupt */
316 static inline void ohci_set_interrupt(OHCIState
*ohci
, uint32_t intr
)
318 ohci
->intr_status
|= intr
;
319 ohci_intr_update(ohci
);
322 /* Attach or detach a device on a root hub port. */
323 static void ohci_attach(USBPort
*port1
)
325 OHCIState
*s
= port1
->opaque
;
326 OHCIPort
*port
= &s
->rhport
[port1
->index
];
327 uint32_t old_state
= port
->ctrl
;
329 /* set connect status */
330 port
->ctrl
|= OHCI_PORT_CCS
| OHCI_PORT_CSC
;
333 if (port
->port
.dev
->speed
== USB_SPEED_LOW
) {
334 port
->ctrl
|= OHCI_PORT_LSDA
;
336 port
->ctrl
&= ~OHCI_PORT_LSDA
;
339 /* notify of remote-wakeup */
340 if ((s
->ctl
& OHCI_CTL_HCFS
) == OHCI_USB_SUSPEND
) {
341 ohci_set_interrupt(s
, OHCI_INTR_RD
);
344 trace_usb_ohci_port_attach(port1
->index
);
346 if (old_state
!= port
->ctrl
) {
347 ohci_set_interrupt(s
, OHCI_INTR_RHSC
);
351 static void ohci_detach(USBPort
*port1
)
353 OHCIState
*s
= port1
->opaque
;
354 OHCIPort
*port
= &s
->rhport
[port1
->index
];
355 uint32_t old_state
= port
->ctrl
;
357 ohci_async_cancel_device(s
, port1
->dev
);
359 /* set connect status */
360 if (port
->ctrl
& OHCI_PORT_CCS
) {
361 port
->ctrl
&= ~OHCI_PORT_CCS
;
362 port
->ctrl
|= OHCI_PORT_CSC
;
365 if (port
->ctrl
& OHCI_PORT_PES
) {
366 port
->ctrl
&= ~OHCI_PORT_PES
;
367 port
->ctrl
|= OHCI_PORT_PESC
;
369 trace_usb_ohci_port_detach(port1
->index
);
371 if (old_state
!= port
->ctrl
) {
372 ohci_set_interrupt(s
, OHCI_INTR_RHSC
);
376 static void ohci_wakeup(USBPort
*port1
)
378 OHCIState
*s
= port1
->opaque
;
379 OHCIPort
*port
= &s
->rhport
[port1
->index
];
381 if (port
->ctrl
& OHCI_PORT_PSS
) {
382 trace_usb_ohci_port_wakeup(port1
->index
);
383 port
->ctrl
|= OHCI_PORT_PSSC
;
384 port
->ctrl
&= ~OHCI_PORT_PSS
;
385 intr
= OHCI_INTR_RHSC
;
387 /* Note that the controller can be suspended even if this port is not */
388 if ((s
->ctl
& OHCI_CTL_HCFS
) == OHCI_USB_SUSPEND
) {
389 trace_usb_ohci_remote_wakeup(s
->name
);
390 /* This is the one state transition the controller can do by itself */
391 s
->ctl
&= ~OHCI_CTL_HCFS
;
392 s
->ctl
|= OHCI_USB_RESUME
;
393 /* In suspend mode only ResumeDetected is possible, not RHSC:
394 * see the OHCI spec 5.1.2.3.
398 ohci_set_interrupt(s
, intr
);
401 static void ohci_child_detach(USBPort
*port1
, USBDevice
*child
)
403 OHCIState
*s
= port1
->opaque
;
405 ohci_async_cancel_device(s
, child
);
408 static USBDevice
*ohci_find_device(OHCIState
*ohci
, uint8_t addr
)
413 for (i
= 0; i
< ohci
->num_ports
; i
++) {
414 if ((ohci
->rhport
[i
].ctrl
& OHCI_PORT_PES
) == 0) {
417 dev
= usb_find_device(&ohci
->rhport
[i
].port
, addr
);
425 static void ohci_stop_endpoints(OHCIState
*ohci
)
430 for (i
= 0; i
< ohci
->num_ports
; i
++) {
431 dev
= ohci
->rhport
[i
].port
.dev
;
432 if (dev
&& dev
->attached
) {
433 usb_device_ep_stopped(dev
, &dev
->ep_ctl
);
434 for (j
= 0; j
< USB_MAX_ENDPOINTS
; j
++) {
435 usb_device_ep_stopped(dev
, &dev
->ep_in
[j
]);
436 usb_device_ep_stopped(dev
, &dev
->ep_out
[j
]);
442 static void ohci_roothub_reset(OHCIState
*ohci
)
448 ohci
->rhdesc_a
= OHCI_RHA_NPS
| ohci
->num_ports
;
449 ohci
->rhdesc_b
= 0x0; /* Impl. specific */
452 for (i
= 0; i
< ohci
->num_ports
; i
++) {
453 port
= &ohci
->rhport
[i
];
455 if (port
->port
.dev
&& port
->port
.dev
->attached
) {
456 usb_port_reset(&port
->port
);
459 if (ohci
->async_td
) {
460 usb_cancel_packet(&ohci
->usb_packet
);
463 ohci_stop_endpoints(ohci
);
466 /* Reset the controller */
467 static void ohci_soft_reset(OHCIState
*ohci
)
469 trace_usb_ohci_reset(ohci
->name
);
472 ohci
->ctl
= (ohci
->ctl
& OHCI_CTL_IR
) | OHCI_USB_SUSPEND
;
475 ohci
->intr_status
= 0;
476 ohci
->intr
= OHCI_INTR_MIE
;
479 ohci
->ctrl_head
= ohci
->ctrl_cur
= 0;
480 ohci
->bulk_head
= ohci
->bulk_cur
= 0;
483 ohci
->done_count
= 7;
485 /* FSMPS is marked TBD in OCHI 1.0, what gives ffs?
486 * I took the value linux sets ...
488 ohci
->fsmps
= 0x2778;
492 ohci
->frame_number
= 0;
494 ohci
->lst
= OHCI_LS_THRESH
;
497 static void ohci_hard_reset(OHCIState
*ohci
)
499 ohci_soft_reset(ohci
);
501 ohci_roothub_reset(ohci
);
504 /* Get an array of dwords from main memory */
505 static inline int get_dwords(OHCIState
*ohci
,
506 dma_addr_t addr
, uint32_t *buf
, int num
)
510 addr
+= ohci
->localmem_base
;
512 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
513 if (dma_memory_read(ohci
->as
, addr
, buf
, sizeof(*buf
))) {
516 *buf
= le32_to_cpu(*buf
);
522 /* Put an array of dwords in to main memory */
523 static inline int put_dwords(OHCIState
*ohci
,
524 dma_addr_t addr
, uint32_t *buf
, int num
)
528 addr
+= ohci
->localmem_base
;
530 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
531 uint32_t tmp
= cpu_to_le32(*buf
);
532 if (dma_memory_write(ohci
->as
, addr
, &tmp
, sizeof(tmp
))) {
540 /* Get an array of words from main memory */
541 static inline int get_words(OHCIState
*ohci
,
542 dma_addr_t addr
, uint16_t *buf
, int num
)
546 addr
+= ohci
->localmem_base
;
548 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
549 if (dma_memory_read(ohci
->as
, addr
, buf
, sizeof(*buf
))) {
552 *buf
= le16_to_cpu(*buf
);
558 /* Put an array of words in to main memory */
559 static inline int put_words(OHCIState
*ohci
,
560 dma_addr_t addr
, uint16_t *buf
, int num
)
564 addr
+= ohci
->localmem_base
;
566 for (i
= 0; i
< num
; i
++, buf
++, addr
+= sizeof(*buf
)) {
567 uint16_t tmp
= cpu_to_le16(*buf
);
568 if (dma_memory_write(ohci
->as
, addr
, &tmp
, sizeof(tmp
))) {
576 static inline int ohci_read_ed(OHCIState
*ohci
,
577 dma_addr_t addr
, struct ohci_ed
*ed
)
579 return get_dwords(ohci
, addr
, (uint32_t *)ed
, sizeof(*ed
) >> 2);
582 static inline int ohci_read_td(OHCIState
*ohci
,
583 dma_addr_t addr
, struct ohci_td
*td
)
585 return get_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
588 static inline int ohci_read_iso_td(OHCIState
*ohci
,
589 dma_addr_t addr
, struct ohci_iso_td
*td
)
591 return get_dwords(ohci
, addr
, (uint32_t *)td
, 4) ||
592 get_words(ohci
, addr
+ 16, td
->offset
, 8);
595 static inline int ohci_read_hcca(OHCIState
*ohci
,
596 dma_addr_t addr
, struct ohci_hcca
*hcca
)
598 return dma_memory_read(ohci
->as
, addr
+ ohci
->localmem_base
,
599 hcca
, sizeof(*hcca
));
602 static inline int ohci_put_ed(OHCIState
*ohci
,
603 dma_addr_t addr
, struct ohci_ed
*ed
)
605 /* ed->tail is under control of the HCD.
606 * Since just ed->head is changed by HC, just write back this
609 return put_dwords(ohci
, addr
+ ED_WBACK_OFFSET
,
610 (uint32_t *)((char *)ed
+ ED_WBACK_OFFSET
),
614 static inline int ohci_put_td(OHCIState
*ohci
,
615 dma_addr_t addr
, struct ohci_td
*td
)
617 return put_dwords(ohci
, addr
, (uint32_t *)td
, sizeof(*td
) >> 2);
620 static inline int ohci_put_iso_td(OHCIState
*ohci
,
621 dma_addr_t addr
, struct ohci_iso_td
*td
)
623 return put_dwords(ohci
, addr
, (uint32_t *)td
, 4) ||
624 put_words(ohci
, addr
+ 16, td
->offset
, 8);
627 static inline int ohci_put_hcca(OHCIState
*ohci
,
628 dma_addr_t addr
, struct ohci_hcca
*hcca
)
630 return dma_memory_write(ohci
->as
,
631 addr
+ ohci
->localmem_base
+ HCCA_WRITEBACK_OFFSET
,
632 (char *)hcca
+ HCCA_WRITEBACK_OFFSET
,
633 HCCA_WRITEBACK_SIZE
);
636 /* Read/Write the contents of a TD from/to main memory. */
637 static int ohci_copy_td(OHCIState
*ohci
, struct ohci_td
*td
,
638 uint8_t *buf
, int len
, DMADirection dir
)
643 n
= 0x1000 - (ptr
& 0xfff);
647 if (dma_memory_rw(ohci
->as
, ptr
+ ohci
->localmem_base
, buf
, n
, dir
)) {
653 ptr
= td
->be
& ~0xfffu
;
655 if (dma_memory_rw(ohci
->as
, ptr
+ ohci
->localmem_base
, buf
,
662 /* Read/Write the contents of an ISO TD from/to main memory. */
663 static int ohci_copy_iso_td(OHCIState
*ohci
,
664 uint32_t start_addr
, uint32_t end_addr
,
665 uint8_t *buf
, int len
, DMADirection dir
)
670 n
= 0x1000 - (ptr
& 0xfff);
674 if (dma_memory_rw(ohci
->as
, ptr
+ ohci
->localmem_base
, buf
, n
, dir
)) {
680 ptr
= end_addr
& ~0xfffu
;
682 if (dma_memory_rw(ohci
->as
, ptr
+ ohci
->localmem_base
, buf
,
689 static void ohci_process_lists(OHCIState
*ohci
, int completion
);
691 static void ohci_async_complete_packet(USBPort
*port
, USBPacket
*packet
)
693 OHCIState
*ohci
= container_of(packet
, OHCIState
, usb_packet
);
695 trace_usb_ohci_async_complete();
696 ohci
->async_complete
= true;
697 ohci_process_lists(ohci
, 1);
700 #define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
702 static int ohci_service_iso_td(OHCIState
*ohci
, struct ohci_ed
*ed
,
707 const char *str
= NULL
;
713 struct ohci_iso_td iso_td
;
715 uint16_t starting_frame
;
716 int16_t relative_frame_number
;
718 uint32_t start_offset
, next_offset
, end_offset
= 0;
719 uint32_t start_addr
, end_addr
;
721 addr
= ed
->head
& OHCI_DPTR_MASK
;
723 if (ohci_read_iso_td(ohci
, addr
, &iso_td
)) {
724 trace_usb_ohci_iso_td_read_failed(addr
);
729 starting_frame
= OHCI_BM(iso_td
.flags
, TD_SF
);
730 frame_count
= OHCI_BM(iso_td
.flags
, TD_FC
);
731 relative_frame_number
= USUB(ohci
->frame_number
, starting_frame
);
733 trace_usb_ohci_iso_td_head(
734 ed
->head
& OHCI_DPTR_MASK
, ed
->tail
& OHCI_DPTR_MASK
,
735 iso_td
.flags
, iso_td
.bp
, iso_td
.next
, iso_td
.be
,
736 ohci
->frame_number
, starting_frame
,
737 frame_count
, relative_frame_number
);
738 trace_usb_ohci_iso_td_head_offset(
739 iso_td
.offset
[0], iso_td
.offset
[1],
740 iso_td
.offset
[2], iso_td
.offset
[3],
741 iso_td
.offset
[4], iso_td
.offset
[5],
742 iso_td
.offset
[6], iso_td
.offset
[7]);
744 if (relative_frame_number
< 0) {
745 trace_usb_ohci_iso_td_relative_frame_number_neg(relative_frame_number
);
747 } else if (relative_frame_number
> frame_count
) {
748 /* ISO TD expired - retire the TD to the Done Queue and continue with
749 the next ISO TD of the same ED */
750 trace_usb_ohci_iso_td_relative_frame_number_big(relative_frame_number
,
752 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
753 ed
->head
&= ~OHCI_DPTR_MASK
;
754 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
755 iso_td
.next
= ohci
->done
;
757 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
758 if (i
< ohci
->done_count
)
759 ohci
->done_count
= i
;
760 if (ohci_put_iso_td(ohci
, addr
, &iso_td
)) {
767 dir
= OHCI_BM(ed
->flags
, ED_D
);
773 case OHCI_TD_DIR_OUT
:
777 case OHCI_TD_DIR_SETUP
:
779 pid
= USB_TOKEN_SETUP
;
782 trace_usb_ohci_iso_td_bad_direction(dir
);
786 if (!iso_td
.bp
|| !iso_td
.be
) {
787 trace_usb_ohci_iso_td_bad_bp_be(iso_td
.bp
, iso_td
.be
);
791 start_offset
= iso_td
.offset
[relative_frame_number
];
792 next_offset
= iso_td
.offset
[relative_frame_number
+ 1];
794 if (!(OHCI_BM(start_offset
, TD_PSW_CC
) & 0xe) ||
795 ((relative_frame_number
< frame_count
) &&
796 !(OHCI_BM(next_offset
, TD_PSW_CC
) & 0xe))) {
797 trace_usb_ohci_iso_td_bad_cc_not_accessed(start_offset
, next_offset
);
801 if ((relative_frame_number
< frame_count
) && (start_offset
> next_offset
)) {
802 trace_usb_ohci_iso_td_bad_cc_overrun(start_offset
, next_offset
);
806 if ((start_offset
& 0x1000) == 0) {
807 start_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
808 (start_offset
& OHCI_OFFSET_MASK
);
810 start_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
811 (start_offset
& OHCI_OFFSET_MASK
);
814 if (relative_frame_number
< frame_count
) {
815 end_offset
= next_offset
- 1;
816 if ((end_offset
& 0x1000) == 0) {
817 end_addr
= (iso_td
.bp
& OHCI_PAGE_MASK
) |
818 (end_offset
& OHCI_OFFSET_MASK
);
820 end_addr
= (iso_td
.be
& OHCI_PAGE_MASK
) |
821 (end_offset
& OHCI_OFFSET_MASK
);
824 /* Last packet in the ISO TD */
825 end_addr
= iso_td
.be
;
828 if ((start_addr
& OHCI_PAGE_MASK
) != (end_addr
& OHCI_PAGE_MASK
)) {
829 len
= (end_addr
& OHCI_OFFSET_MASK
) + 0x1001
830 - (start_addr
& OHCI_OFFSET_MASK
);
832 len
= end_addr
- start_addr
+ 1;
835 if (len
&& dir
!= OHCI_TD_DIR_IN
) {
836 if (ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, len
,
837 DMA_DIRECTION_TO_DEVICE
)) {
844 bool int_req
= relative_frame_number
== frame_count
&&
845 OHCI_BM(iso_td
.flags
, TD_DI
) == 0;
846 dev
= ohci_find_device(ohci
, OHCI_BM(ed
->flags
, ED_FA
));
847 ep
= usb_ep_get(dev
, pid
, OHCI_BM(ed
->flags
, ED_EN
));
848 usb_packet_setup(&ohci
->usb_packet
, pid
, ep
, 0, addr
, false, int_req
);
849 usb_packet_addbuf(&ohci
->usb_packet
, ohci
->usb_buf
, len
);
850 usb_handle_packet(dev
, &ohci
->usb_packet
);
851 if (ohci
->usb_packet
.status
== USB_RET_ASYNC
) {
852 usb_device_flush_ep_queue(dev
, ep
);
856 if (ohci
->usb_packet
.status
== USB_RET_SUCCESS
) {
857 ret
= ohci
->usb_packet
.actual_length
;
859 ret
= ohci
->usb_packet
.status
;
862 trace_usb_ohci_iso_td_so(start_offset
, end_offset
, start_addr
, end_addr
,
866 if (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && ret
<= len
) {
867 /* IN transfer succeeded */
868 if (ohci_copy_iso_td(ohci
, start_addr
, end_addr
, ohci
->usb_buf
, ret
,
869 DMA_DIRECTION_FROM_DEVICE
)) {
873 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
875 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, ret
);
876 } else if (dir
== OHCI_TD_DIR_OUT
&& ret
== len
) {
877 /* OUT transfer succeeded */
878 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
880 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
, 0);
882 if (ret
> (ssize_t
) len
) {
883 trace_usb_ohci_iso_td_data_overrun(ret
, len
);
884 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
885 OHCI_CC_DATAOVERRUN
);
886 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
888 } else if (ret
>= 0) {
889 trace_usb_ohci_iso_td_data_underrun(ret
);
890 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
891 OHCI_CC_DATAUNDERRUN
);
894 case USB_RET_IOERROR
:
896 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
897 OHCI_CC_DEVICENOTRESPONDING
);
898 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
903 trace_usb_ohci_iso_td_nak(ret
);
904 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
906 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_SIZE
,
910 trace_usb_ohci_iso_td_bad_response(ret
);
911 OHCI_SET_BM(iso_td
.offset
[relative_frame_number
], TD_PSW_CC
,
912 OHCI_CC_UNDEXPETEDPID
);
918 if (relative_frame_number
== frame_count
) {
919 /* Last data packet of ISO TD - retire the TD to the Done Queue */
920 OHCI_SET_BM(iso_td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
921 ed
->head
&= ~OHCI_DPTR_MASK
;
922 ed
->head
|= (iso_td
.next
& OHCI_DPTR_MASK
);
923 iso_td
.next
= ohci
->done
;
925 i
= OHCI_BM(iso_td
.flags
, TD_DI
);
926 if (i
< ohci
->done_count
)
927 ohci
->done_count
= i
;
929 if (ohci_put_iso_td(ohci
, addr
, &iso_td
)) {
935 #ifdef trace_event_get_state
936 static void ohci_td_pkt(const char *msg
, const uint8_t *buf
, size_t len
)
938 bool print16
= !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_SHORT
);
939 bool printall
= !!trace_event_get_state(TRACE_USB_OHCI_TD_PKT_FULL
);
940 const int width
= 16;
942 char tmp
[3 * width
+ 1];
945 if (!printall
&& !print16
) {
950 if (i
&& (!(i
% width
) || (i
== len
))) {
952 trace_usb_ohci_td_pkt_short(msg
, tmp
);
955 trace_usb_ohci_td_pkt_full(msg
, tmp
);
963 p
+= sprintf(p
, " %.2x", buf
[i
]);
967 static void ohci_td_pkt(const char *msg
, const uint8_t *buf
, size_t len
)
972 /* Service a transport descriptor.
973 Returns nonzero to terminate processing of this endpoint. */
975 static int ohci_service_td(OHCIState
*ohci
, struct ohci_ed
*ed
)
978 size_t len
= 0, pktlen
= 0;
979 const char *str
= NULL
;
990 addr
= ed
->head
& OHCI_DPTR_MASK
;
991 /* See if this TD has already been submitted to the device. */
992 completion
= (addr
== ohci
->async_td
);
993 if (completion
&& !ohci
->async_complete
) {
994 trace_usb_ohci_td_skip_async();
997 if (ohci_read_td(ohci
, addr
, &td
)) {
998 trace_usb_ohci_td_read_error(addr
);
1003 dir
= OHCI_BM(ed
->flags
, ED_D
);
1005 case OHCI_TD_DIR_OUT
:
1006 case OHCI_TD_DIR_IN
:
1010 dir
= OHCI_BM(td
.flags
, TD_DP
);
1015 case OHCI_TD_DIR_IN
:
1019 case OHCI_TD_DIR_OUT
:
1021 pid
= USB_TOKEN_OUT
;
1023 case OHCI_TD_DIR_SETUP
:
1025 pid
= USB_TOKEN_SETUP
;
1028 trace_usb_ohci_td_bad_direction(dir
);
1031 if (td
.cbp
&& td
.be
) {
1032 if ((td
.cbp
& 0xfffff000) != (td
.be
& 0xfffff000)) {
1033 len
= (td
.be
& 0xfff) + 0x1001 - (td
.cbp
& 0xfff);
1035 len
= (td
.be
- td
.cbp
) + 1;
1039 if (len
&& dir
!= OHCI_TD_DIR_IN
) {
1040 /* The endpoint may not allow us to transfer it all now */
1041 pktlen
= (ed
->flags
& OHCI_ED_MPS_MASK
) >> OHCI_ED_MPS_SHIFT
;
1046 if (ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, pktlen
,
1047 DMA_DIRECTION_TO_DEVICE
)) {
1054 flag_r
= (td
.flags
& OHCI_TD_R
) != 0;
1055 trace_usb_ohci_td_pkt_hdr(addr
, (int64_t)pktlen
, (int64_t)len
, str
,
1056 flag_r
, td
.cbp
, td
.be
);
1057 ohci_td_pkt("OUT", ohci
->usb_buf
, pktlen
);
1061 ohci
->async_complete
= false;
1063 if (ohci
->async_td
) {
1064 /* ??? The hardware should allow one active packet per
1065 endpoint. We only allow one active packet per controller.
1066 This should be sufficient as long as devices respond in a
1069 trace_usb_ohci_td_too_many_pending();
1072 dev
= ohci_find_device(ohci
, OHCI_BM(ed
->flags
, ED_FA
));
1073 ep
= usb_ep_get(dev
, pid
, OHCI_BM(ed
->flags
, ED_EN
));
1074 usb_packet_setup(&ohci
->usb_packet
, pid
, ep
, 0, addr
, !flag_r
,
1075 OHCI_BM(td
.flags
, TD_DI
) == 0);
1076 usb_packet_addbuf(&ohci
->usb_packet
, ohci
->usb_buf
, pktlen
);
1077 usb_handle_packet(dev
, &ohci
->usb_packet
);
1078 trace_usb_ohci_td_packet_status(ohci
->usb_packet
.status
);
1080 if (ohci
->usb_packet
.status
== USB_RET_ASYNC
) {
1081 usb_device_flush_ep_queue(dev
, ep
);
1082 ohci
->async_td
= addr
;
1086 if (ohci
->usb_packet
.status
== USB_RET_SUCCESS
) {
1087 ret
= ohci
->usb_packet
.actual_length
;
1089 ret
= ohci
->usb_packet
.status
;
1093 if (dir
== OHCI_TD_DIR_IN
) {
1094 if (ohci_copy_td(ohci
, &td
, ohci
->usb_buf
, ret
,
1095 DMA_DIRECTION_FROM_DEVICE
)) {
1098 ohci_td_pkt("IN", ohci
->usb_buf
, pktlen
);
1105 if (ret
== pktlen
|| (dir
== OHCI_TD_DIR_IN
&& ret
>= 0 && flag_r
)) {
1106 /* Transmission succeeded. */
1110 if ((td
.cbp
& 0xfff) + ret
> 0xfff) {
1111 td
.cbp
= (td
.be
& ~0xfff) + ((td
.cbp
+ ret
) & 0xfff);
1116 td
.flags
|= OHCI_TD_T1
;
1117 td
.flags
^= OHCI_TD_T0
;
1118 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_NOERROR
);
1119 OHCI_SET_BM(td
.flags
, TD_EC
, 0);
1121 if ((dir
!= OHCI_TD_DIR_IN
) && (ret
!= len
)) {
1122 /* Partial packet transfer: TD not ready to retire yet */
1123 goto exit_no_retire
;
1126 /* Setting ED_C is part of the TD retirement process */
1127 ed
->head
&= ~OHCI_ED_C
;
1128 if (td
.flags
& OHCI_TD_T0
)
1129 ed
->head
|= OHCI_ED_C
;
1132 trace_usb_ohci_td_underrun();
1133 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAUNDERRUN
);
1136 case USB_RET_IOERROR
:
1138 trace_usb_ohci_td_dev_error();
1139 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DEVICENOTRESPONDING
);
1142 trace_usb_ohci_td_nak();
1145 trace_usb_ohci_td_stall();
1146 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_STALL
);
1148 case USB_RET_BABBLE
:
1149 trace_usb_ohci_td_babble();
1150 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_DATAOVERRUN
);
1153 trace_usb_ohci_td_bad_device_response(ret
);
1154 OHCI_SET_BM(td
.flags
, TD_CC
, OHCI_CC_UNDEXPETEDPID
);
1155 OHCI_SET_BM(td
.flags
, TD_EC
, 3);
1159 ed
->head
|= OHCI_ED_H
;
1162 /* Retire this TD */
1163 ed
->head
&= ~OHCI_DPTR_MASK
;
1164 ed
->head
|= td
.next
& OHCI_DPTR_MASK
;
1165 td
.next
= ohci
->done
;
1167 i
= OHCI_BM(td
.flags
, TD_DI
);
1168 if (i
< ohci
->done_count
)
1169 ohci
->done_count
= i
;
1171 if (ohci_put_td(ohci
, addr
, &td
)) {
1175 return OHCI_BM(td
.flags
, TD_CC
) != OHCI_CC_NOERROR
;
1178 /* Service an endpoint list. Returns nonzero if active TD were found. */
1179 static int ohci_service_ed_list(OHCIState
*ohci
, uint32_t head
, int completion
)
1191 for (cur
= head
; cur
; cur
= next_ed
) {
1192 if (ohci_read_ed(ohci
, cur
, &ed
)) {
1193 trace_usb_ohci_ed_read_error(cur
);
1198 next_ed
= ed
.next
& OHCI_DPTR_MASK
;
1200 if ((ed
.head
& OHCI_ED_H
) || (ed
.flags
& OHCI_ED_K
)) {
1202 /* Cancel pending packets for ED that have been paused. */
1203 addr
= ed
.head
& OHCI_DPTR_MASK
;
1204 if (ohci
->async_td
&& addr
== ohci
->async_td
) {
1205 usb_cancel_packet(&ohci
->usb_packet
);
1207 usb_device_ep_stopped(ohci
->usb_packet
.ep
->dev
,
1208 ohci
->usb_packet
.ep
);
1213 while ((ed
.head
& OHCI_DPTR_MASK
) != ed
.tail
) {
1214 trace_usb_ohci_ed_pkt(cur
, (ed
.head
& OHCI_ED_H
) != 0,
1215 (ed
.head
& OHCI_ED_C
) != 0, ed
.head
& OHCI_DPTR_MASK
,
1216 ed
.tail
& OHCI_DPTR_MASK
, ed
.next
& OHCI_DPTR_MASK
);
1217 trace_usb_ohci_ed_pkt_flags(
1218 OHCI_BM(ed
.flags
, ED_FA
), OHCI_BM(ed
.flags
, ED_EN
),
1219 OHCI_BM(ed
.flags
, ED_D
), (ed
.flags
& OHCI_ED_S
)!= 0,
1220 (ed
.flags
& OHCI_ED_K
) != 0, (ed
.flags
& OHCI_ED_F
) != 0,
1221 OHCI_BM(ed
.flags
, ED_MPS
));
1225 if ((ed
.flags
& OHCI_ED_F
) == 0) {
1226 if (ohci_service_td(ohci
, &ed
))
1229 /* Handle isochronous endpoints */
1230 if (ohci_service_iso_td(ohci
, &ed
, completion
))
1235 if (ohci_put_ed(ohci
, cur
, &ed
)) {
1244 /* set a timer for EOF */
1245 static void ohci_eof_timer(OHCIState
*ohci
)
1247 ohci
->sof_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1248 timer_mod(ohci
->eof_timer
, ohci
->sof_time
+ usb_frame_time
);
1250 /* Set a timer for EOF and generate a SOF event */
1251 static void ohci_sof(OHCIState
*ohci
)
1253 ohci_eof_timer(ohci
);
1254 ohci_set_interrupt(ohci
, OHCI_INTR_SF
);
1257 /* Process Control and Bulk lists. */
1258 static void ohci_process_lists(OHCIState
*ohci
, int completion
)
1260 if ((ohci
->ctl
& OHCI_CTL_CLE
) && (ohci
->status
& OHCI_STATUS_CLF
)) {
1261 if (ohci
->ctrl_cur
&& ohci
->ctrl_cur
!= ohci
->ctrl_head
) {
1262 trace_usb_ohci_process_lists(ohci
->ctrl_head
, ohci
->ctrl_cur
);
1264 if (!ohci_service_ed_list(ohci
, ohci
->ctrl_head
, completion
)) {
1266 ohci
->status
&= ~OHCI_STATUS_CLF
;
1270 if ((ohci
->ctl
& OHCI_CTL_BLE
) && (ohci
->status
& OHCI_STATUS_BLF
)) {
1271 if (!ohci_service_ed_list(ohci
, ohci
->bulk_head
, completion
)) {
1273 ohci
->status
&= ~OHCI_STATUS_BLF
;
1278 /* Do frame processing on frame boundary */
1279 static void ohci_frame_boundary(void *opaque
)
1281 OHCIState
*ohci
= opaque
;
1282 struct ohci_hcca hcca
;
1284 if (ohci_read_hcca(ohci
, ohci
->hcca
, &hcca
)) {
1285 trace_usb_ohci_hcca_read_error(ohci
->hcca
);
1290 /* Process all the lists at the end of the frame */
1291 if (ohci
->ctl
& OHCI_CTL_PLE
) {
1294 n
= ohci
->frame_number
& 0x1f;
1295 ohci_service_ed_list(ohci
, le32_to_cpu(hcca
.intr
[n
]), 0);
1298 /* Cancel all pending packets if either of the lists has been disabled. */
1299 if (ohci
->old_ctl
& (~ohci
->ctl
) & (OHCI_CTL_BLE
| OHCI_CTL_CLE
)) {
1300 if (ohci
->async_td
) {
1301 usb_cancel_packet(&ohci
->usb_packet
);
1304 ohci_stop_endpoints(ohci
);
1306 ohci
->old_ctl
= ohci
->ctl
;
1307 ohci_process_lists(ohci
, 0);
1309 /* Stop if UnrecoverableError happened or ohci_sof will crash */
1310 if (ohci
->intr_status
& OHCI_INTR_UE
) {
1314 /* Frame boundary, so do EOF stuf here */
1315 ohci
->frt
= ohci
->fit
;
1317 /* Increment frame number and take care of endianness. */
1318 ohci
->frame_number
= (ohci
->frame_number
+ 1) & 0xffff;
1319 hcca
.frame
= cpu_to_le16(ohci
->frame_number
);
1321 if (ohci
->done_count
== 0 && !(ohci
->intr_status
& OHCI_INTR_WD
)) {
1324 if (ohci
->intr
& ohci
->intr_status
)
1326 hcca
.done
= cpu_to_le32(ohci
->done
);
1328 ohci
->done_count
= 7;
1329 ohci_set_interrupt(ohci
, OHCI_INTR_WD
);
1332 if (ohci
->done_count
!= 7 && ohci
->done_count
!= 0)
1335 /* Do SOF stuff here */
1338 /* Writeback HCCA */
1339 if (ohci_put_hcca(ohci
, ohci
->hcca
, &hcca
)) {
1344 /* Start sending SOF tokens across the USB bus, lists are processed in
1347 static int ohci_bus_start(OHCIState
*ohci
)
1349 ohci
->eof_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
1350 ohci_frame_boundary
,
1353 if (ohci
->eof_timer
== NULL
) {
1354 trace_usb_ohci_bus_eof_timer_failed(ohci
->name
);
1359 trace_usb_ohci_start(ohci
->name
);
1361 /* Delay the first SOF event by one frame time as
1362 * linux driver is not ready to receive it and
1363 * can meet some race conditions
1366 ohci_eof_timer(ohci
);
1371 /* Stop sending SOF tokens on the bus */
1372 static void ohci_bus_stop(OHCIState
*ohci
)
1374 trace_usb_ohci_stop(ohci
->name
);
1375 if (ohci
->eof_timer
) {
1376 timer_del(ohci
->eof_timer
);
1377 timer_free(ohci
->eof_timer
);
1379 ohci
->eof_timer
= NULL
;
1382 /* Sets a flag in a port status register but only set it if the port is
1383 * connected, if not set ConnectStatusChange flag. If flag is enabled
1386 static int ohci_port_set_if_connected(OHCIState
*ohci
, int i
, uint32_t val
)
1390 /* writing a 0 has no effect */
1394 /* If CurrentConnectStatus is cleared we set
1395 * ConnectStatusChange
1397 if (!(ohci
->rhport
[i
].ctrl
& OHCI_PORT_CCS
)) {
1398 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_CSC
;
1399 if (ohci
->rhstatus
& OHCI_RHS_DRWE
) {
1400 /* TODO: CSC is a wakeup event */
1405 if (ohci
->rhport
[i
].ctrl
& val
)
1409 ohci
->rhport
[i
].ctrl
|= val
;
1414 /* Set the frame interval - frame interval toggle is manipulated by the hcd only */
1415 static void ohci_set_frame_interval(OHCIState
*ohci
, uint16_t val
)
1419 if (val
!= ohci
->fi
) {
1420 trace_usb_ohci_set_frame_interval(ohci
->name
, ohci
->fi
, ohci
->fi
);
1426 static void ohci_port_power(OHCIState
*ohci
, int i
, int p
)
1429 ohci
->rhport
[i
].ctrl
|= OHCI_PORT_PPS
;
1431 ohci
->rhport
[i
].ctrl
&= ~(OHCI_PORT_PPS
|
1438 /* Set HcControlRegister */
1439 static void ohci_set_ctl(OHCIState
*ohci
, uint32_t val
)
1444 old_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1446 new_state
= ohci
->ctl
& OHCI_CTL_HCFS
;
1448 /* no state change */
1449 if (old_state
== new_state
)
1452 trace_usb_ohci_set_ctl(ohci
->name
, new_state
);
1453 switch (new_state
) {
1454 case OHCI_USB_OPERATIONAL
:
1455 ohci_bus_start(ohci
);
1457 case OHCI_USB_SUSPEND
:
1458 ohci_bus_stop(ohci
);
1459 /* clear pending SF otherwise linux driver loops in ohci_irq() */
1460 ohci
->intr_status
&= ~OHCI_INTR_SF
;
1461 ohci_intr_update(ohci
);
1463 case OHCI_USB_RESUME
:
1464 trace_usb_ohci_resume(ohci
->name
);
1466 case OHCI_USB_RESET
:
1467 ohci_roothub_reset(ohci
);
1472 static uint32_t ohci_get_frame_remaining(OHCIState
*ohci
)
1477 if ((ohci
->ctl
& OHCI_CTL_HCFS
) != OHCI_USB_OPERATIONAL
)
1478 return (ohci
->frt
<< 31);
1480 /* Being in USB operational state guarnatees sof_time was
1483 tks
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) - ohci
->sof_time
;
1485 /* avoid muldiv if possible */
1486 if (tks
>= usb_frame_time
)
1487 return (ohci
->frt
<< 31);
1489 tks
= muldiv64(1, tks
, usb_bit_time
);
1490 fr
= (uint16_t)(ohci
->fi
- tks
);
1492 return (ohci
->frt
<< 31) | fr
;
1496 /* Set root hub status */
1497 static void ohci_set_hub_status(OHCIState
*ohci
, uint32_t val
)
1501 old_state
= ohci
->rhstatus
;
1503 /* write 1 to clear OCIC */
1504 if (val
& OHCI_RHS_OCIC
)
1505 ohci
->rhstatus
&= ~OHCI_RHS_OCIC
;
1507 if (val
& OHCI_RHS_LPS
) {
1510 for (i
= 0; i
< ohci
->num_ports
; i
++)
1511 ohci_port_power(ohci
, i
, 0);
1512 trace_usb_ohci_hub_power_down();
1515 if (val
& OHCI_RHS_LPSC
) {
1518 for (i
= 0; i
< ohci
->num_ports
; i
++)
1519 ohci_port_power(ohci
, i
, 1);
1520 trace_usb_ohci_hub_power_up();
1523 if (val
& OHCI_RHS_DRWE
)
1524 ohci
->rhstatus
|= OHCI_RHS_DRWE
;
1526 if (val
& OHCI_RHS_CRWE
)
1527 ohci
->rhstatus
&= ~OHCI_RHS_DRWE
;
1529 if (old_state
!= ohci
->rhstatus
)
1530 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1533 /* Set root hub port status */
1534 static void ohci_port_set_status(OHCIState
*ohci
, int portnum
, uint32_t val
)
1539 port
= &ohci
->rhport
[portnum
];
1540 old_state
= port
->ctrl
;
1542 /* Write to clear CSC, PESC, PSSC, OCIC, PRSC */
1543 if (val
& OHCI_PORT_WTC
)
1544 port
->ctrl
&= ~(val
& OHCI_PORT_WTC
);
1546 if (val
& OHCI_PORT_CCS
)
1547 port
->ctrl
&= ~OHCI_PORT_PES
;
1549 ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PES
);
1551 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PSS
)) {
1552 trace_usb_ohci_port_suspend(portnum
);
1555 if (ohci_port_set_if_connected(ohci
, portnum
, val
& OHCI_PORT_PRS
)) {
1556 trace_usb_ohci_port_reset(portnum
);
1557 usb_device_reset(port
->port
.dev
);
1558 port
->ctrl
&= ~OHCI_PORT_PRS
;
1559 /* ??? Should this also set OHCI_PORT_PESC. */
1560 port
->ctrl
|= OHCI_PORT_PES
| OHCI_PORT_PRSC
;
1563 /* Invert order here to ensure in ambiguous case, device is
1566 if (val
& OHCI_PORT_LSDA
)
1567 ohci_port_power(ohci
, portnum
, 0);
1568 if (val
& OHCI_PORT_PPS
)
1569 ohci_port_power(ohci
, portnum
, 1);
1571 if (old_state
!= port
->ctrl
)
1572 ohci_set_interrupt(ohci
, OHCI_INTR_RHSC
);
1575 static uint64_t ohci_mem_read(void *opaque
,
1579 OHCIState
*ohci
= opaque
;
1582 /* Only aligned reads are allowed on OHCI */
1584 trace_usb_ohci_mem_read_unaligned(addr
);
1586 } else if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1587 /* HcRhPortStatus */
1588 retval
= ohci
->rhport
[(addr
- 0x54) >> 2].ctrl
| OHCI_PORT_PPS
;
1590 switch (addr
>> 2) {
1591 case 0: /* HcRevision */
1595 case 1: /* HcControl */
1599 case 2: /* HcCommandStatus */
1600 retval
= ohci
->status
;
1603 case 3: /* HcInterruptStatus */
1604 retval
= ohci
->intr_status
;
1607 case 4: /* HcInterruptEnable */
1608 case 5: /* HcInterruptDisable */
1609 retval
= ohci
->intr
;
1612 case 6: /* HcHCCA */
1613 retval
= ohci
->hcca
;
1616 case 7: /* HcPeriodCurrentED */
1617 retval
= ohci
->per_cur
;
1620 case 8: /* HcControlHeadED */
1621 retval
= ohci
->ctrl_head
;
1624 case 9: /* HcControlCurrentED */
1625 retval
= ohci
->ctrl_cur
;
1628 case 10: /* HcBulkHeadED */
1629 retval
= ohci
->bulk_head
;
1632 case 11: /* HcBulkCurrentED */
1633 retval
= ohci
->bulk_cur
;
1636 case 12: /* HcDoneHead */
1637 retval
= ohci
->done
;
1640 case 13: /* HcFmInterretval */
1641 retval
= (ohci
->fit
<< 31) | (ohci
->fsmps
<< 16) | (ohci
->fi
);
1644 case 14: /* HcFmRemaining */
1645 retval
= ohci_get_frame_remaining(ohci
);
1648 case 15: /* HcFmNumber */
1649 retval
= ohci
->frame_number
;
1652 case 16: /* HcPeriodicStart */
1653 retval
= ohci
->pstart
;
1656 case 17: /* HcLSThreshold */
1660 case 18: /* HcRhDescriptorA */
1661 retval
= ohci
->rhdesc_a
;
1664 case 19: /* HcRhDescriptorB */
1665 retval
= ohci
->rhdesc_b
;
1668 case 20: /* HcRhStatus */
1669 retval
= ohci
->rhstatus
;
1672 /* PXA27x specific registers */
1673 case 24: /* HcStatus */
1674 retval
= ohci
->hstatus
& ohci
->hmask
;
1677 case 25: /* HcHReset */
1678 retval
= ohci
->hreset
;
1681 case 26: /* HcHInterruptEnable */
1682 retval
= ohci
->hmask
;
1685 case 27: /* HcHInterruptTest */
1686 retval
= ohci
->htest
;
1690 trace_usb_ohci_mem_read_bad_offset(addr
);
1691 retval
= 0xffffffff;
1698 static void ohci_mem_write(void *opaque
,
1703 OHCIState
*ohci
= opaque
;
1705 /* Only aligned reads are allowed on OHCI */
1707 trace_usb_ohci_mem_write_unaligned(addr
);
1711 if (addr
>= 0x54 && addr
< 0x54 + ohci
->num_ports
* 4) {
1712 /* HcRhPortStatus */
1713 ohci_port_set_status(ohci
, (addr
- 0x54) >> 2, val
);
1717 switch (addr
>> 2) {
1718 case 1: /* HcControl */
1719 ohci_set_ctl(ohci
, val
);
1722 case 2: /* HcCommandStatus */
1723 /* SOC is read-only */
1724 val
= (val
& ~OHCI_STATUS_SOC
);
1726 /* Bits written as '0' remain unchanged in the register */
1727 ohci
->status
|= val
;
1729 if (ohci
->status
& OHCI_STATUS_HCR
)
1730 ohci_soft_reset(ohci
);
1733 case 3: /* HcInterruptStatus */
1734 ohci
->intr_status
&= ~val
;
1735 ohci_intr_update(ohci
);
1738 case 4: /* HcInterruptEnable */
1740 ohci_intr_update(ohci
);
1743 case 5: /* HcInterruptDisable */
1745 ohci_intr_update(ohci
);
1748 case 6: /* HcHCCA */
1749 ohci
->hcca
= val
& OHCI_HCCA_MASK
;
1752 case 7: /* HcPeriodCurrentED */
1753 /* Ignore writes to this read-only register, Linux does them */
1756 case 8: /* HcControlHeadED */
1757 ohci
->ctrl_head
= val
& OHCI_EDPTR_MASK
;
1760 case 9: /* HcControlCurrentED */
1761 ohci
->ctrl_cur
= val
& OHCI_EDPTR_MASK
;
1764 case 10: /* HcBulkHeadED */
1765 ohci
->bulk_head
= val
& OHCI_EDPTR_MASK
;
1768 case 11: /* HcBulkCurrentED */
1769 ohci
->bulk_cur
= val
& OHCI_EDPTR_MASK
;
1772 case 13: /* HcFmInterval */
1773 ohci
->fsmps
= (val
& OHCI_FMI_FSMPS
) >> 16;
1774 ohci
->fit
= (val
& OHCI_FMI_FIT
) >> 31;
1775 ohci_set_frame_interval(ohci
, val
);
1778 case 15: /* HcFmNumber */
1781 case 16: /* HcPeriodicStart */
1782 ohci
->pstart
= val
& 0xffff;
1785 case 17: /* HcLSThreshold */
1786 ohci
->lst
= val
& 0xffff;
1789 case 18: /* HcRhDescriptorA */
1790 ohci
->rhdesc_a
&= ~OHCI_RHA_RW_MASK
;
1791 ohci
->rhdesc_a
|= val
& OHCI_RHA_RW_MASK
;
1794 case 19: /* HcRhDescriptorB */
1797 case 20: /* HcRhStatus */
1798 ohci_set_hub_status(ohci
, val
);
1801 /* PXA27x specific registers */
1802 case 24: /* HcStatus */
1803 ohci
->hstatus
&= ~(val
& ohci
->hmask
);
1806 case 25: /* HcHReset */
1807 ohci
->hreset
= val
& ~OHCI_HRESET_FSBIR
;
1808 if (val
& OHCI_HRESET_FSBIR
)
1809 ohci_hard_reset(ohci
);
1812 case 26: /* HcHInterruptEnable */
1816 case 27: /* HcHInterruptTest */
1821 trace_usb_ohci_mem_write_bad_offset(addr
);
1826 static void ohci_async_cancel_device(OHCIState
*ohci
, USBDevice
*dev
)
1828 if (ohci
->async_td
&&
1829 usb_packet_is_inflight(&ohci
->usb_packet
) &&
1830 ohci
->usb_packet
.ep
->dev
== dev
) {
1831 usb_cancel_packet(&ohci
->usb_packet
);
1836 static const MemoryRegionOps ohci_mem_ops
= {
1837 .read
= ohci_mem_read
,
1838 .write
= ohci_mem_write
,
1839 .endianness
= DEVICE_LITTLE_ENDIAN
,
1842 static USBPortOps ohci_port_ops
= {
1843 .attach
= ohci_attach
,
1844 .detach
= ohci_detach
,
1845 .child_detach
= ohci_child_detach
,
1846 .wakeup
= ohci_wakeup
,
1847 .complete
= ohci_async_complete_packet
,
1850 static USBBusOps ohci_bus_ops
= {
1853 static void usb_ohci_init(OHCIState
*ohci
, DeviceState
*dev
,
1854 int num_ports
, dma_addr_t localmem_base
,
1855 char *masterbus
, uint32_t firstport
,
1856 AddressSpace
*as
, Error
**errp
)
1863 if (usb_frame_time
== 0) {
1864 #ifdef OHCI_TIME_WARP
1865 usb_frame_time
= get_ticks_per_sec();
1866 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
/1000);
1868 usb_frame_time
= muldiv64(1, get_ticks_per_sec(), 1000);
1869 if (get_ticks_per_sec() >= USB_HZ
) {
1870 usb_bit_time
= muldiv64(1, get_ticks_per_sec(), USB_HZ
);
1875 trace_usb_ohci_init_time(usb_frame_time
, usb_bit_time
);
1878 ohci
->num_ports
= num_ports
;
1880 USBPort
*ports
[OHCI_MAX_PORTS
];
1881 for(i
= 0; i
< num_ports
; i
++) {
1882 ports
[i
] = &ohci
->rhport
[i
].port
;
1884 usb_register_companion(masterbus
, ports
, num_ports
,
1885 firstport
, ohci
, &ohci_port_ops
,
1886 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
,
1889 error_propagate(errp
, err
);
1893 usb_bus_new(&ohci
->bus
, sizeof(ohci
->bus
), &ohci_bus_ops
, dev
);
1894 for (i
= 0; i
< num_ports
; i
++) {
1895 usb_register_port(&ohci
->bus
, &ohci
->rhport
[i
].port
,
1896 ohci
, i
, &ohci_port_ops
,
1897 USB_SPEED_MASK_LOW
| USB_SPEED_MASK_FULL
);
1901 memory_region_init_io(&ohci
->mem
, OBJECT(dev
), &ohci_mem_ops
,
1903 ohci
->localmem_base
= localmem_base
;
1905 ohci
->name
= object_get_typename(OBJECT(dev
));
1906 usb_packet_init(&ohci
->usb_packet
);
1911 #define TYPE_PCI_OHCI "pci-ohci"
1912 #define PCI_OHCI(obj) OBJECT_CHECK(OHCIPCIState, (obj), TYPE_PCI_OHCI)
1916 PCIDevice parent_obj
;
1925 /** A typical O/EHCI will stop operating, set itself into error state
1926 * (which can be queried by MMIO) and will set PERR in its config
1927 * space to signal that it got an error
1929 static void ohci_die(OHCIState
*ohci
)
1931 OHCIPCIState
*dev
= container_of(ohci
, OHCIPCIState
, state
);
1933 trace_usb_ohci_die();
1935 ohci_set_interrupt(ohci
, OHCI_INTR_UE
);
1936 ohci_bus_stop(ohci
);
1937 pci_set_word(dev
->parent_obj
.config
+ PCI_STATUS
,
1938 PCI_STATUS_DETECTED_PARITY
);
1941 static void usb_ohci_realize_pci(PCIDevice
*dev
, Error
**errp
)
1944 OHCIPCIState
*ohci
= PCI_OHCI(dev
);
1946 dev
->config
[PCI_CLASS_PROG
] = 0x10; /* OHCI */
1947 dev
->config
[PCI_INTERRUPT_PIN
] = 0x01; /* interrupt pin A */
1949 usb_ohci_init(&ohci
->state
, DEVICE(dev
), ohci
->num_ports
, 0,
1950 ohci
->masterbus
, ohci
->firstport
,
1951 pci_get_address_space(dev
), &err
);
1953 error_propagate(errp
, err
);
1957 ohci
->state
.irq
= pci_allocate_irq(dev
);
1958 pci_register_bar(dev
, 0, 0, &ohci
->state
.mem
);
1961 static void usb_ohci_exit(PCIDevice
*dev
)
1963 OHCIPCIState
*ohci
= PCI_OHCI(dev
);
1964 OHCIState
*s
= &ohci
->state
;
1966 trace_usb_ohci_exit(s
->name
);
1970 usb_cancel_packet(&s
->usb_packet
);
1973 ohci_stop_endpoints(s
);
1975 if (!ohci
->masterbus
) {
1976 usb_bus_release(&s
->bus
);
1980 static void usb_ohci_reset_pci(DeviceState
*d
)
1982 PCIDevice
*dev
= PCI_DEVICE(d
);
1983 OHCIPCIState
*ohci
= PCI_OHCI(dev
);
1984 OHCIState
*s
= &ohci
->state
;
1989 #define TYPE_SYSBUS_OHCI "sysbus-ohci"
1990 #define SYSBUS_OHCI(obj) OBJECT_CHECK(OHCISysBusState, (obj), TYPE_SYSBUS_OHCI)
1994 SysBusDevice parent_obj
;
1999 dma_addr_t dma_offset
;
2002 static void ohci_realize_pxa(DeviceState
*dev
, Error
**errp
)
2004 OHCISysBusState
*s
= SYSBUS_OHCI(dev
);
2005 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
2007 /* Cannot fail as we pass NULL for masterbus */
2008 usb_ohci_init(&s
->ohci
, dev
, s
->num_ports
, s
->dma_offset
, NULL
, 0,
2009 &address_space_memory
, &error_abort
);
2010 sysbus_init_irq(sbd
, &s
->ohci
.irq
);
2011 sysbus_init_mmio(sbd
, &s
->ohci
.mem
);
2014 static void usb_ohci_reset_sysbus(DeviceState
*dev
)
2016 OHCISysBusState
*s
= SYSBUS_OHCI(dev
);
2017 OHCIState
*ohci
= &s
->ohci
;
2019 ohci_hard_reset(ohci
);
2022 static Property ohci_pci_properties
[] = {
2023 DEFINE_PROP_STRING("masterbus", OHCIPCIState
, masterbus
),
2024 DEFINE_PROP_UINT32("num-ports", OHCIPCIState
, num_ports
, 3),
2025 DEFINE_PROP_UINT32("firstport", OHCIPCIState
, firstport
, 0),
2026 DEFINE_PROP_END_OF_LIST(),
2029 static const VMStateDescription vmstate_ohci_state_port
= {
2030 .name
= "ohci-core/port",
2032 .minimum_version_id
= 1,
2033 .fields
= (VMStateField
[]) {
2034 VMSTATE_UINT32(ctrl
, OHCIPort
),
2035 VMSTATE_END_OF_LIST()
2039 static bool ohci_eof_timer_needed(void *opaque
)
2041 OHCIState
*ohci
= opaque
;
2043 return ohci
->eof_timer
!= NULL
;
2046 static int ohci_eof_timer_pre_load(void *opaque
)
2048 OHCIState
*ohci
= opaque
;
2050 ohci_bus_start(ohci
);
2055 static const VMStateDescription vmstate_ohci_eof_timer
= {
2056 .name
= "ohci-core/eof-timer",
2058 .minimum_version_id
= 1,
2059 .pre_load
= ohci_eof_timer_pre_load
,
2060 .needed
= ohci_eof_timer_needed
,
2061 .fields
= (VMStateField
[]) {
2062 VMSTATE_TIMER_PTR(eof_timer
, OHCIState
),
2063 VMSTATE_END_OF_LIST()
2067 static const VMStateDescription vmstate_ohci_state
= {
2068 .name
= "ohci-core",
2070 .minimum_version_id
= 1,
2071 .fields
= (VMStateField
[]) {
2072 VMSTATE_INT64(sof_time
, OHCIState
),
2073 VMSTATE_UINT32(ctl
, OHCIState
),
2074 VMSTATE_UINT32(status
, OHCIState
),
2075 VMSTATE_UINT32(intr_status
, OHCIState
),
2076 VMSTATE_UINT32(intr
, OHCIState
),
2077 VMSTATE_UINT32(hcca
, OHCIState
),
2078 VMSTATE_UINT32(ctrl_head
, OHCIState
),
2079 VMSTATE_UINT32(ctrl_cur
, OHCIState
),
2080 VMSTATE_UINT32(bulk_head
, OHCIState
),
2081 VMSTATE_UINT32(bulk_cur
, OHCIState
),
2082 VMSTATE_UINT32(per_cur
, OHCIState
),
2083 VMSTATE_UINT32(done
, OHCIState
),
2084 VMSTATE_INT32(done_count
, OHCIState
),
2085 VMSTATE_UINT16(fsmps
, OHCIState
),
2086 VMSTATE_UINT8(fit
, OHCIState
),
2087 VMSTATE_UINT16(fi
, OHCIState
),
2088 VMSTATE_UINT8(frt
, OHCIState
),
2089 VMSTATE_UINT16(frame_number
, OHCIState
),
2090 VMSTATE_UINT16(padding
, OHCIState
),
2091 VMSTATE_UINT32(pstart
, OHCIState
),
2092 VMSTATE_UINT32(lst
, OHCIState
),
2093 VMSTATE_UINT32(rhdesc_a
, OHCIState
),
2094 VMSTATE_UINT32(rhdesc_b
, OHCIState
),
2095 VMSTATE_UINT32(rhstatus
, OHCIState
),
2096 VMSTATE_STRUCT_ARRAY(rhport
, OHCIState
, OHCI_MAX_PORTS
, 0,
2097 vmstate_ohci_state_port
, OHCIPort
),
2098 VMSTATE_UINT32(hstatus
, OHCIState
),
2099 VMSTATE_UINT32(hmask
, OHCIState
),
2100 VMSTATE_UINT32(hreset
, OHCIState
),
2101 VMSTATE_UINT32(htest
, OHCIState
),
2102 VMSTATE_UINT32(old_ctl
, OHCIState
),
2103 VMSTATE_UINT8_ARRAY(usb_buf
, OHCIState
, 8192),
2104 VMSTATE_UINT32(async_td
, OHCIState
),
2105 VMSTATE_BOOL(async_complete
, OHCIState
),
2106 VMSTATE_END_OF_LIST()
2108 .subsections
= (const VMStateDescription
*[]) {
2109 &vmstate_ohci_eof_timer
,
2114 static const VMStateDescription vmstate_ohci
= {
2117 .minimum_version_id
= 1,
2118 .fields
= (VMStateField
[]) {
2119 VMSTATE_PCI_DEVICE(parent_obj
, OHCIPCIState
),
2120 VMSTATE_STRUCT(state
, OHCIPCIState
, 1, vmstate_ohci_state
, OHCIState
),
2121 VMSTATE_END_OF_LIST()
2125 static void ohci_pci_class_init(ObjectClass
*klass
, void *data
)
2127 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2128 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
2130 k
->realize
= usb_ohci_realize_pci
;
2131 k
->exit
= usb_ohci_exit
;
2132 k
->vendor_id
= PCI_VENDOR_ID_APPLE
;
2133 k
->device_id
= PCI_DEVICE_ID_APPLE_IPID_USB
;
2134 k
->class_id
= PCI_CLASS_SERIAL_USB
;
2135 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
2136 dc
->desc
= "Apple USB Controller";
2137 dc
->props
= ohci_pci_properties
;
2138 dc
->hotpluggable
= false;
2139 dc
->vmsd
= &vmstate_ohci
;
2140 dc
->reset
= usb_ohci_reset_pci
;
2143 static const TypeInfo ohci_pci_info
= {
2144 .name
= TYPE_PCI_OHCI
,
2145 .parent
= TYPE_PCI_DEVICE
,
2146 .instance_size
= sizeof(OHCIPCIState
),
2147 .class_init
= ohci_pci_class_init
,
2150 static Property ohci_sysbus_properties
[] = {
2151 DEFINE_PROP_UINT32("num-ports", OHCISysBusState
, num_ports
, 3),
2152 DEFINE_PROP_DMAADDR("dma-offset", OHCISysBusState
, dma_offset
, 3),
2153 DEFINE_PROP_END_OF_LIST(),
2156 static void ohci_sysbus_class_init(ObjectClass
*klass
, void *data
)
2158 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2160 dc
->realize
= ohci_realize_pxa
;
2161 set_bit(DEVICE_CATEGORY_USB
, dc
->categories
);
2162 dc
->desc
= "OHCI USB Controller";
2163 dc
->props
= ohci_sysbus_properties
;
2164 dc
->reset
= usb_ohci_reset_sysbus
;
2167 static const TypeInfo ohci_sysbus_info
= {
2168 .name
= TYPE_SYSBUS_OHCI
,
2169 .parent
= TYPE_SYS_BUS_DEVICE
,
2170 .instance_size
= sizeof(OHCISysBusState
),
2171 .class_init
= ohci_sysbus_class_init
,
2174 static void ohci_register_types(void)
2176 type_register_static(&ohci_pci_info
);
2177 type_register_static(&ohci_sysbus_info
);
2180 type_init(ohci_register_types
)