28
#include "qemu/osdep.h"
30
#include "qapi/error.h"
31
#include "qemu/module.h"
32
#include "qemu/timer.h"
34
#include "migration/vmstate.h"
36
#include "hw/qdev-dma.h"
37
#include "hw/qdev-properties.h"
44
#define ED_LINK_LIMIT 32
46
static int64_t usb_frame_time;
47
static int64_t usb_bit_time;
55
#define HCCA_WRITEBACK_OFFSET offsetof(struct ohci_hcca, frame)
56
#define HCCA_WRITEBACK_SIZE 8
58
#define ED_WBACK_OFFSET offsetof(struct ohci_ed, head)
59
#define ED_WBACK_SIZE 4
62
#define OHCI_ED_FA_SHIFT 0
63
#define OHCI_ED_FA_MASK (0x7f << OHCI_ED_FA_SHIFT)
64
#define OHCI_ED_EN_SHIFT 7
65
#define OHCI_ED_EN_MASK (0xf << OHCI_ED_EN_SHIFT)
66
#define OHCI_ED_D_SHIFT 11
67
#define OHCI_ED_D_MASK (3 << OHCI_ED_D_SHIFT)
68
#define OHCI_ED_S (1 << 13)
69
#define OHCI_ED_K (1 << 14)
70
#define OHCI_ED_F (1 << 15)
71
#define OHCI_ED_MPS_SHIFT 16
72
#define OHCI_ED_MPS_MASK (0x7ff << OHCI_ED_MPS_SHIFT)
79
#define OHCI_TD_R (1 << 18)
80
#define OHCI_TD_DP_SHIFT 19
81
#define OHCI_TD_DP_MASK (3 << OHCI_TD_DP_SHIFT)
82
#define OHCI_TD_DI_SHIFT 21
83
#define OHCI_TD_DI_MASK (7 << OHCI_TD_DI_SHIFT)
84
#define OHCI_TD_T0 (1 << 24)
85
#define OHCI_TD_T1 (1 << 25)
86
#define OHCI_TD_EC_SHIFT 26
87
#define OHCI_TD_EC_MASK (3 << OHCI_TD_EC_SHIFT)
88
#define OHCI_TD_CC_SHIFT 28
89
#define OHCI_TD_CC_MASK (0xf << OHCI_TD_CC_SHIFT)
93
#define OHCI_TD_SF_SHIFT 0
94
#define OHCI_TD_SF_MASK (0xffff << OHCI_TD_SF_SHIFT)
95
#define OHCI_TD_FC_SHIFT 24
96
#define OHCI_TD_FC_MASK (7 << OHCI_TD_FC_SHIFT)
99
#define OHCI_TD_PSW_CC_SHIFT 12
100
#define OHCI_TD_PSW_CC_MASK (0xf << OHCI_TD_PSW_CC_SHIFT)
101
#define OHCI_TD_PSW_SIZE_SHIFT 0
102
#define OHCI_TD_PSW_SIZE_MASK (0xfff << OHCI_TD_PSW_SIZE_SHIFT)
104
#define OHCI_PAGE_MASK 0xfffff000
105
#define OHCI_OFFSET_MASK 0xfff
107
#define OHCI_DPTR_MASK 0xfffffff0
109
#define OHCI_BM(val, field) \
110
(((val) & OHCI_##field##_MASK) >> OHCI_##field##_SHIFT)
112
#define OHCI_SET_BM(val, field, newval) do { \
113
val &= ~OHCI_##field##_MASK; \
114
val |= ((newval) << OHCI_##field##_SHIFT) & OHCI_##field##_MASK; \
142
#define USB_HZ 12000000
145
#define OHCI_CTL_CBSR ((1 << 0) | (1 << 1))
146
#define OHCI_CTL_PLE (1 << 2)
147
#define OHCI_CTL_IE (1 << 3)
148
#define OHCI_CTL_CLE (1 << 4)
149
#define OHCI_CTL_BLE (1 << 5)
150
#define OHCI_CTL_HCFS ((1 << 6) | (1 << 7))
151
#define OHCI_USB_RESET 0x00
152
#define OHCI_USB_RESUME 0x40
153
#define OHCI_USB_OPERATIONAL 0x80
154
#define OHCI_USB_SUSPEND 0xc0
155
#define OHCI_CTL_IR (1 << 8)
156
#define OHCI_CTL_RWC (1 << 9)
157
#define OHCI_CTL_RWE (1 << 10)
159
#define OHCI_STATUS_HCR (1 << 0)
160
#define OHCI_STATUS_CLF (1 << 1)
161
#define OHCI_STATUS_BLF (1 << 2)
162
#define OHCI_STATUS_OCR (1 << 3)
163
#define OHCI_STATUS_SOC ((1 << 6) | (1 << 7))
165
#define OHCI_INTR_SO (1U << 0)
166
#define OHCI_INTR_WD (1U << 1)
167
#define OHCI_INTR_SF (1U << 2)
168
#define OHCI_INTR_RD (1U << 3)
169
#define OHCI_INTR_UE (1U << 4)
170
#define OHCI_INTR_FNO (1U << 5)
171
#define OHCI_INTR_RHSC (1U << 6)
172
#define OHCI_INTR_OC (1U << 30)
173
#define OHCI_INTR_MIE (1U << 31)
175
#define OHCI_HCCA_SIZE 0x100
176
#define OHCI_HCCA_MASK 0xffffff00
178
#define OHCI_EDPTR_MASK 0xfffffff0
180
#define OHCI_FMI_FI 0x00003fff
181
#define OHCI_FMI_FSMPS 0xffff0000
182
#define OHCI_FMI_FIT 0x80000000
184
#define OHCI_FR_RT (1U << 31)
186
#define OHCI_LS_THRESH 0x628
188
#define OHCI_RHA_RW_MASK 0x00000000
189
#define OHCI_RHA_PSM (1 << 8)
190
#define OHCI_RHA_NPS (1 << 9)
191
#define OHCI_RHA_DT (1 << 10)
192
#define OHCI_RHA_OCPM (1 << 11)
193
#define OHCI_RHA_NOCP (1 << 12)
194
#define OHCI_RHA_POTPGT_MASK 0xff000000
196
#define OHCI_RHS_LPS (1U << 0)
197
#define OHCI_RHS_OCI (1U << 1)
198
#define OHCI_RHS_DRWE (1U << 15)
199
#define OHCI_RHS_LPSC (1U << 16)
200
#define OHCI_RHS_OCIC (1U << 17)
201
#define OHCI_RHS_CRWE (1U << 31)
203
#define OHCI_PORT_CCS (1 << 0)
204
#define OHCI_PORT_PES (1 << 1)
205
#define OHCI_PORT_PSS (1 << 2)
206
#define OHCI_PORT_POCI (1 << 3)
207
#define OHCI_PORT_PRS (1 << 4)
208
#define OHCI_PORT_PPS (1 << 8)
209
#define OHCI_PORT_LSDA (1 << 9)
210
#define OHCI_PORT_CSC (1 << 16)
211
#define OHCI_PORT_PESC (1 << 17)
212
#define OHCI_PORT_PSSC (1 << 18)
213
#define OHCI_PORT_OCIC (1 << 19)
214
#define OHCI_PORT_PRSC (1 << 20)
215
#define OHCI_PORT_WTC (OHCI_PORT_CSC | OHCI_PORT_PESC | \
216
OHCI_PORT_PSSC | OHCI_PORT_OCIC | \
218
#define OHCI_TD_DIR_SETUP 0x0
219
#define OHCI_TD_DIR_OUT 0x1
220
#define OHCI_TD_DIR_IN 0x2
221
#define OHCI_TD_DIR_RESERVED 0x3
223
#define OHCI_CC_NOERROR 0x0
224
#define OHCI_CC_CRC 0x1
225
#define OHCI_CC_BITSTUFFING 0x2
226
#define OHCI_CC_DATATOGGLEMISMATCH 0x3
227
#define OHCI_CC_STALL 0x4
228
#define OHCI_CC_DEVICENOTRESPONDING 0x5
229
#define OHCI_CC_PIDCHECKFAILURE 0x6
230
#define OHCI_CC_UNDEXPETEDPID 0x7
231
#define OHCI_CC_DATAOVERRUN 0x8
232
#define OHCI_CC_DATAUNDERRUN 0x9
233
#define OHCI_CC_BUFFEROVERRUN 0xc
234
#define OHCI_CC_BUFFERUNDERRUN 0xd
236
#define OHCI_HRESET_FSBIR (1 << 0)
238
static const char *ohci_reg_names[] = {
239
"HcRevision", "HcControl", "HcCommandStatus", "HcInterruptStatus",
240
"HcInterruptEnable", "HcInterruptDisable", "HcHCCA", "HcPeriodCurrentED",
241
"HcControlHeadED", "HcControlCurrentED", "HcBulkHeadED", "HcBulkCurrentED",
242
"HcDoneHead", "HcFmInterval", "HcFmRemaining", "HcFmNumber",
243
"HcPeriodicStart", "HcLSThreshold", "HcRhDescriptorA", "HcRhDescriptorB",
247
static const char *ohci_reg_name(hwaddr addr)
249
if (addr >> 2 < ARRAY_SIZE(ohci_reg_names)) {
250
return ohci_reg_names[addr >> 2];
256
static void ohci_die(OHCIState *ohci)
258
ohci->ohci_die(ohci);
262
static inline void ohci_intr_update(OHCIState *ohci)
266
if ((ohci->intr & OHCI_INTR_MIE) &&
267
(ohci->intr_status & ohci->intr))
270
qemu_set_irq(ohci->irq, level);
274
static inline void ohci_set_interrupt(OHCIState *ohci, uint32_t intr)
276
ohci->intr_status |= intr;
277
ohci_intr_update(ohci);
280
static USBDevice *ohci_find_device(OHCIState *ohci, uint8_t addr)
285
for (i = 0; i < ohci->num_ports; i++) {
286
if ((ohci->rhport[i].ctrl & OHCI_PORT_PES) == 0) {
289
dev = usb_find_device(&ohci->rhport[i].port, addr);
297
void ohci_stop_endpoints(OHCIState *ohci)
302
if (ohci->async_td) {
303
usb_cancel_packet(&ohci->usb_packet);
306
for (i = 0; i < ohci->num_ports; i++) {
307
dev = ohci->rhport[i].port.dev;
308
if (dev && dev->attached) {
309
usb_device_ep_stopped(dev, &dev->ep_ctl);
310
for (j = 0; j < USB_MAX_ENDPOINTS; j++) {
311
usb_device_ep_stopped(dev, &dev->ep_in[j]);
312
usb_device_ep_stopped(dev, &dev->ep_out[j]);
318
static void ohci_roothub_reset(OHCIState *ohci)
324
ohci->rhdesc_a = OHCI_RHA_NPS | ohci->num_ports;
325
ohci->rhdesc_b = 0x0;
328
for (i = 0; i < ohci->num_ports; i++) {
329
port = &ohci->rhport[i];
331
if (port->port.dev && port->port.dev->attached) {
332
usb_port_reset(&port->port);
335
ohci_stop_endpoints(ohci);
339
static void ohci_soft_reset(OHCIState *ohci)
341
trace_usb_ohci_reset(ohci->name);
344
ohci->ctl = (ohci->ctl & OHCI_CTL_IR) | OHCI_USB_SUSPEND;
347
ohci->intr_status = 0;
348
ohci->intr = OHCI_INTR_MIE;
351
ohci->ctrl_head = ohci->ctrl_cur = 0;
352
ohci->bulk_head = ohci->bulk_cur = 0;
355
ohci->done_count = 7;
360
ohci->fsmps = 0x2778;
364
ohci->frame_number = 0;
366
ohci->lst = OHCI_LS_THRESH;
369
void ohci_hard_reset(OHCIState *ohci)
371
ohci_soft_reset(ohci);
373
ohci_roothub_reset(ohci);
377
static inline int get_dwords(OHCIState *ohci,
378
dma_addr_t addr, uint32_t *buf, int num)
382
addr += ohci->localmem_base;
384
for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
385
if (dma_memory_read(ohci->as, addr,
386
buf, sizeof(*buf), MEMTXATTRS_UNSPECIFIED)) {
389
*buf = le32_to_cpu(*buf);
396
static inline int put_dwords(OHCIState *ohci,
397
dma_addr_t addr, uint32_t *buf, int num)
401
addr += ohci->localmem_base;
403
for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
404
uint32_t tmp = cpu_to_le32(*buf);
405
if (dma_memory_write(ohci->as, addr,
406
&tmp, sizeof(tmp), MEMTXATTRS_UNSPECIFIED)) {
415
static inline int get_words(OHCIState *ohci,
416
dma_addr_t addr, uint16_t *buf, int num)
420
addr += ohci->localmem_base;
422
for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
423
if (dma_memory_read(ohci->as, addr,
424
buf, sizeof(*buf), MEMTXATTRS_UNSPECIFIED)) {
427
*buf = le16_to_cpu(*buf);
434
static inline int put_words(OHCIState *ohci,
435
dma_addr_t addr, uint16_t *buf, int num)
439
addr += ohci->localmem_base;
441
for (i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
442
uint16_t tmp = cpu_to_le16(*buf);
443
if (dma_memory_write(ohci->as, addr,
444
&tmp, sizeof(tmp), MEMTXATTRS_UNSPECIFIED)) {
452
static inline int ohci_read_ed(OHCIState *ohci,
453
dma_addr_t addr, struct ohci_ed *ed)
455
return get_dwords(ohci, addr, (uint32_t *)ed, sizeof(*ed) >> 2);
458
static inline int ohci_read_td(OHCIState *ohci,
459
dma_addr_t addr, struct ohci_td *td)
461
return get_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
464
static inline int ohci_read_iso_td(OHCIState *ohci,
465
dma_addr_t addr, struct ohci_iso_td *td)
467
return get_dwords(ohci, addr, (uint32_t *)td, 4) ||
468
get_words(ohci, addr + 16, td->offset, 8);
471
static inline int ohci_read_hcca(OHCIState *ohci,
472
dma_addr_t addr, struct ohci_hcca *hcca)
474
return dma_memory_read(ohci->as, addr + ohci->localmem_base, hcca,
475
sizeof(*hcca), MEMTXATTRS_UNSPECIFIED);
478
static inline int ohci_put_ed(OHCIState *ohci,
479
dma_addr_t addr, struct ohci_ed *ed)
485
return put_dwords(ohci, addr + ED_WBACK_OFFSET,
486
(uint32_t *)((char *)ed + ED_WBACK_OFFSET),
490
static inline int ohci_put_td(OHCIState *ohci,
491
dma_addr_t addr, struct ohci_td *td)
493
return put_dwords(ohci, addr, (uint32_t *)td, sizeof(*td) >> 2);
496
static inline int ohci_put_iso_td(OHCIState *ohci,
497
dma_addr_t addr, struct ohci_iso_td *td)
499
return put_dwords(ohci, addr, (uint32_t *)td, 4) ||
500
put_words(ohci, addr + 16, td->offset, 8);
503
static inline int ohci_put_hcca(OHCIState *ohci,
504
dma_addr_t addr, struct ohci_hcca *hcca)
506
return dma_memory_write(ohci->as,
507
addr + ohci->localmem_base + HCCA_WRITEBACK_OFFSET,
508
(char *)hcca + HCCA_WRITEBACK_OFFSET,
509
HCCA_WRITEBACK_SIZE, MEMTXATTRS_UNSPECIFIED);
513
static int ohci_copy_td(OHCIState *ohci, struct ohci_td *td,
514
uint8_t *buf, int len, DMADirection dir)
519
n = 0x1000 - (ptr & 0xfff);
523
if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
524
n, dir, MEMTXATTRS_UNSPECIFIED)) {
530
ptr = td->be & ~0xfffu;
532
if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
533
len - n, dir, MEMTXATTRS_UNSPECIFIED)) {
540
static int ohci_copy_iso_td(OHCIState *ohci,
541
uint32_t start_addr, uint32_t end_addr,
542
uint8_t *buf, int len, DMADirection dir)
547
n = 0x1000 - (ptr & 0xfff);
551
if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
552
n, dir, MEMTXATTRS_UNSPECIFIED)) {
558
ptr = end_addr & ~0xfffu;
560
if (dma_memory_rw(ohci->as, ptr + ohci->localmem_base, buf,
561
len - n, dir, MEMTXATTRS_UNSPECIFIED)) {
567
#define USUB(a, b) ((int16_t)((uint16_t)(a) - (uint16_t)(b)))
569
static int ohci_service_iso_td(OHCIState *ohci, struct ohci_ed *ed)
573
const char *str = NULL;
582
struct ohci_iso_td iso_td;
584
uint16_t starting_frame;
585
int16_t relative_frame_number;
587
uint32_t start_offset, next_offset, end_offset = 0;
588
uint32_t start_addr, end_addr;
590
addr = ed->head & OHCI_DPTR_MASK;
597
if (ohci_read_iso_td(ohci, addr, &iso_td)) {
598
trace_usb_ohci_iso_td_read_failed(addr);
603
starting_frame = OHCI_BM(iso_td.flags, TD_SF);
604
frame_count = OHCI_BM(iso_td.flags, TD_FC);
605
relative_frame_number = USUB(ohci->frame_number, starting_frame);
607
trace_usb_ohci_iso_td_head(
608
ed->head & OHCI_DPTR_MASK, ed->tail & OHCI_DPTR_MASK,
609
iso_td.flags, iso_td.bp, iso_td.next, iso_td.be,
610
ohci->frame_number, starting_frame,
611
frame_count, relative_frame_number);
612
trace_usb_ohci_iso_td_head_offset(
613
iso_td.offset[0], iso_td.offset[1],
614
iso_td.offset[2], iso_td.offset[3],
615
iso_td.offset[4], iso_td.offset[5],
616
iso_td.offset[6], iso_td.offset[7]);
618
if (relative_frame_number < 0) {
619
trace_usb_ohci_iso_td_relative_frame_number_neg(relative_frame_number);
621
} else if (relative_frame_number > frame_count) {
626
trace_usb_ohci_iso_td_relative_frame_number_big(relative_frame_number,
628
if (OHCI_CC_DATAOVERRUN == OHCI_BM(iso_td.flags, TD_CC)) {
632
OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
633
ed->head &= ~OHCI_DPTR_MASK;
634
ed->head |= (iso_td.next & OHCI_DPTR_MASK);
635
iso_td.next = ohci->done;
637
i = OHCI_BM(iso_td.flags, TD_DI);
638
if (i < ohci->done_count) {
639
ohci->done_count = i;
641
if (ohci_put_iso_td(ohci, addr, &iso_td)) {
648
dir = OHCI_BM(ed->flags, ED_D);
654
case OHCI_TD_DIR_OUT:
658
case OHCI_TD_DIR_SETUP:
660
pid = USB_TOKEN_SETUP;
663
trace_usb_ohci_iso_td_bad_direction(dir);
667
if (!iso_td.bp || !iso_td.be) {
668
trace_usb_ohci_iso_td_bad_bp_be(iso_td.bp, iso_td.be);
672
start_offset = iso_td.offset[relative_frame_number];
673
if (relative_frame_number < frame_count) {
674
next_offset = iso_td.offset[relative_frame_number + 1];
676
next_offset = iso_td.be;
679
if (!(OHCI_BM(start_offset, TD_PSW_CC) & 0xe) ||
680
((relative_frame_number < frame_count) &&
681
!(OHCI_BM(next_offset, TD_PSW_CC) & 0xe))) {
682
trace_usb_ohci_iso_td_bad_cc_not_accessed(start_offset, next_offset);
686
if ((relative_frame_number < frame_count) && (start_offset > next_offset)) {
687
trace_usb_ohci_iso_td_bad_cc_overrun(start_offset, next_offset);
691
if ((start_offset & 0x1000) == 0) {
692
start_addr = (iso_td.bp & OHCI_PAGE_MASK) |
693
(start_offset & OHCI_OFFSET_MASK);
695
start_addr = (iso_td.be & OHCI_PAGE_MASK) |
696
(start_offset & OHCI_OFFSET_MASK);
699
if (relative_frame_number < frame_count) {
700
end_offset = next_offset - 1;
701
if ((end_offset & 0x1000) == 0) {
702
end_addr = (iso_td.bp & OHCI_PAGE_MASK) |
703
(end_offset & OHCI_OFFSET_MASK);
705
end_addr = (iso_td.be & OHCI_PAGE_MASK) |
706
(end_offset & OHCI_OFFSET_MASK);
710
end_addr = next_offset;
713
if (start_addr > end_addr) {
714
trace_usb_ohci_iso_td_bad_cc_overrun(start_addr, end_addr);
718
if ((start_addr & OHCI_PAGE_MASK) != (end_addr & OHCI_PAGE_MASK)) {
719
len = (end_addr & OHCI_OFFSET_MASK) + 0x1001
720
- (start_addr & OHCI_OFFSET_MASK);
722
len = end_addr - start_addr + 1;
724
if (len > sizeof(buf)) {
728
if (len && dir != OHCI_TD_DIR_IN) {
729
if (ohci_copy_iso_td(ohci, start_addr, end_addr, buf, len,
730
DMA_DIRECTION_TO_DEVICE)) {
736
dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
738
trace_usb_ohci_td_dev_error();
741
ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
742
pkt = g_new0(USBPacket, 1);
743
usb_packet_init(pkt);
744
int_req = relative_frame_number == frame_count &&
745
OHCI_BM(iso_td.flags, TD_DI) == 0;
746
usb_packet_setup(pkt, pid, ep, 0, addr, false, int_req);
747
usb_packet_addbuf(pkt, buf, len);
748
usb_handle_packet(dev, pkt);
749
if (pkt->status == USB_RET_ASYNC) {
750
usb_device_flush_ep_queue(dev, ep);
754
if (pkt->status == USB_RET_SUCCESS) {
755
ret = pkt->actual_length;
761
trace_usb_ohci_iso_td_so(start_offset, end_offset, start_addr, end_addr,
765
if (dir == OHCI_TD_DIR_IN && ret >= 0 && ret <= len) {
767
if (ohci_copy_iso_td(ohci, start_addr, end_addr, buf, ret,
768
DMA_DIRECTION_FROM_DEVICE)) {
772
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
774
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, ret);
775
} else if (dir == OHCI_TD_DIR_OUT && ret == len) {
777
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
779
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE, 0);
781
if (ret > (ssize_t) len) {
782
trace_usb_ohci_iso_td_data_overrun(ret, len);
783
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
784
OHCI_CC_DATAOVERRUN);
785
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
787
} else if (ret >= 0) {
788
trace_usb_ohci_iso_td_data_underrun(ret);
789
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
790
OHCI_CC_DATAUNDERRUN);
793
case USB_RET_IOERROR:
795
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
796
OHCI_CC_DEVICENOTRESPONDING);
797
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
802
trace_usb_ohci_iso_td_nak(ret);
803
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
805
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_SIZE,
809
trace_usb_ohci_iso_td_bad_response(ret);
810
OHCI_SET_BM(iso_td.offset[relative_frame_number], TD_PSW_CC,
811
OHCI_CC_UNDEXPETEDPID);
817
if (relative_frame_number == frame_count) {
819
OHCI_SET_BM(iso_td.flags, TD_CC, OHCI_CC_NOERROR);
820
ed->head &= ~OHCI_DPTR_MASK;
821
ed->head |= (iso_td.next & OHCI_DPTR_MASK);
822
iso_td.next = ohci->done;
824
i = OHCI_BM(iso_td.flags, TD_DI);
825
if (i < ohci->done_count) {
826
ohci->done_count = i;
829
if (ohci_put_iso_td(ohci, addr, &iso_td)) {
835
#define HEX_CHAR_PER_LINE 16
837
static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
842
char tmp[3 * HEX_CHAR_PER_LINE + 1];
845
print16 = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_SHORT);
846
printall = !!trace_event_get_state_backends(TRACE_USB_OHCI_TD_PKT_FULL);
848
if (!printall && !print16) {
853
if (i && (!(i % HEX_CHAR_PER_LINE) || (i == len))) {
855
trace_usb_ohci_td_pkt_short(msg, tmp);
858
trace_usb_ohci_td_pkt_full(msg, tmp);
866
p += sprintf(p, " %.2x", buf[i]);
874
static int ohci_service_td(OHCIState *ohci, struct ohci_ed *ed)
877
size_t len = 0, pktlen = 0;
878
const char *str = NULL;
889
addr = ed->head & OHCI_DPTR_MASK;
896
completion = (addr == ohci->async_td);
897
if (completion && !ohci->async_complete) {
898
trace_usb_ohci_td_skip_async();
901
if (ohci_read_td(ohci, addr, &td)) {
902
trace_usb_ohci_td_read_error(addr);
907
dir = OHCI_BM(ed->flags, ED_D);
909
case OHCI_TD_DIR_OUT:
914
dir = OHCI_BM(td.flags, TD_DP);
923
case OHCI_TD_DIR_OUT:
927
case OHCI_TD_DIR_SETUP:
929
pid = USB_TOKEN_SETUP;
930
if (OHCI_BM(ed->flags, ED_EN) > 0) {
931
trace_usb_ohci_td_bad_pid(str, ed->flags, td.flags);
937
trace_usb_ohci_td_bad_direction(dir);
940
if (td.cbp && td.be) {
941
if ((td.cbp & 0xfffff000) != (td.be & 0xfffff000)) {
942
len = (td.be & 0xfff) + 0x1001 - (td.cbp & 0xfff);
944
if (td.cbp - 1 > td.be) {
945
trace_usb_ohci_td_bad_buf(td.cbp, td.be);
949
len = (td.be - td.cbp) + 1;
951
if (len > sizeof(ohci->usb_buf)) {
952
len = sizeof(ohci->usb_buf);
956
if (len && dir != OHCI_TD_DIR_IN) {
958
pktlen = (ed->flags & OHCI_ED_MPS_MASK) >> OHCI_ED_MPS_SHIFT;
963
if (ohci_copy_td(ohci, &td, ohci->usb_buf, pktlen,
964
DMA_DIRECTION_TO_DEVICE)) {
971
flag_r = (td.flags & OHCI_TD_R) != 0;
972
trace_usb_ohci_td_pkt_hdr(addr, (int64_t)pktlen, (int64_t)len, str,
973
flag_r, td.cbp, td.be);
974
ohci_td_pkt("OUT", ohci->usb_buf, pktlen);
978
ohci->async_complete = false;
980
dev = ohci_find_device(ohci, OHCI_BM(ed->flags, ED_FA));
982
trace_usb_ohci_td_dev_error();
985
ep = usb_ep_get(dev, pid, OHCI_BM(ed->flags, ED_EN));
986
if (ohci->async_td) {
993
trace_usb_ohci_td_too_many_pending(ep->nr);
996
usb_packet_setup(&ohci->usb_packet, pid, ep, 0, addr, !flag_r,
997
OHCI_BM(td.flags, TD_DI) == 0);
998
usb_packet_addbuf(&ohci->usb_packet, ohci->usb_buf, pktlen);
999
usb_handle_packet(dev, &ohci->usb_packet);
1000
trace_usb_ohci_td_packet_status(ohci->usb_packet.status);
1002
if (ohci->usb_packet.status == USB_RET_ASYNC) {
1003
usb_device_flush_ep_queue(dev, ep);
1004
ohci->async_td = addr;
1008
if (ohci->usb_packet.status == USB_RET_SUCCESS) {
1009
ret = ohci->usb_packet.actual_length;
1011
ret = ohci->usb_packet.status;
1015
if (dir == OHCI_TD_DIR_IN) {
1016
if (ohci_copy_td(ohci, &td, ohci->usb_buf, ret,
1017
DMA_DIRECTION_FROM_DEVICE)) {
1020
ohci_td_pkt("IN", ohci->usb_buf, pktlen);
1027
if (ret == pktlen || (dir == OHCI_TD_DIR_IN && ret >= 0 && flag_r)) {
1032
if ((td.cbp & 0xfff) + ret > 0xfff) {
1033
td.cbp = (td.be & ~0xfff) + ((td.cbp + ret) & 0xfff);
1038
td.flags |= OHCI_TD_T1;
1039
td.flags ^= OHCI_TD_T0;
1040
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_NOERROR);
1041
OHCI_SET_BM(td.flags, TD_EC, 0);
1043
if ((dir != OHCI_TD_DIR_IN) && (ret != len)) {
1045
goto exit_no_retire;
1049
ed->head &= ~OHCI_ED_C;
1050
if (td.flags & OHCI_TD_T0) {
1051
ed->head |= OHCI_ED_C;
1055
trace_usb_ohci_td_underrun();
1056
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAUNDERRUN);
1059
case USB_RET_IOERROR:
1061
trace_usb_ohci_td_dev_error();
1062
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DEVICENOTRESPONDING);
1065
trace_usb_ohci_td_nak();
1068
trace_usb_ohci_td_stall();
1069
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_STALL);
1071
case USB_RET_BABBLE:
1072
trace_usb_ohci_td_babble();
1073
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_DATAOVERRUN);
1076
trace_usb_ohci_td_bad_device_response(ret);
1077
OHCI_SET_BM(td.flags, TD_CC, OHCI_CC_UNDEXPETEDPID);
1078
OHCI_SET_BM(td.flags, TD_EC, 3);
1085
ohci->done_count = 0;
1087
ed->head |= OHCI_ED_H;
1091
ed->head &= ~OHCI_DPTR_MASK;
1092
ed->head |= td.next & OHCI_DPTR_MASK;
1093
td.next = ohci->done;
1095
i = OHCI_BM(td.flags, TD_DI);
1096
if (i < ohci->done_count) {
1097
ohci->done_count = i;
1100
if (ohci_put_td(ohci, addr, &td)) {
1104
return OHCI_BM(td.flags, TD_CC) != OHCI_CC_NOERROR;
1108
static int ohci_service_ed_list(OHCIState *ohci, uint32_t head)
1114
uint32_t link_cnt = 0;
1120
for (cur = head; cur && link_cnt++ < ED_LINK_LIMIT; cur = next_ed) {
1121
if (ohci_read_ed(ohci, cur, &ed)) {
1122
trace_usb_ohci_ed_read_error(cur);
1127
next_ed = ed.next & OHCI_DPTR_MASK;
1129
if ((ed.head & OHCI_ED_H) || (ed.flags & OHCI_ED_K)) {
1132
addr = ed.head & OHCI_DPTR_MASK;
1133
if (ohci->async_td && addr == ohci->async_td) {
1134
usb_cancel_packet(&ohci->usb_packet);
1136
usb_device_ep_stopped(ohci->usb_packet.ep->dev,
1137
ohci->usb_packet.ep);
1142
while ((ed.head & OHCI_DPTR_MASK) != ed.tail) {
1143
trace_usb_ohci_ed_pkt(cur, (ed.head & OHCI_ED_H) != 0,
1144
(ed.head & OHCI_ED_C) != 0, ed.head & OHCI_DPTR_MASK,
1145
ed.tail & OHCI_DPTR_MASK, ed.next & OHCI_DPTR_MASK);
1146
trace_usb_ohci_ed_pkt_flags(
1147
OHCI_BM(ed.flags, ED_FA), OHCI_BM(ed.flags, ED_EN),
1148
OHCI_BM(ed.flags, ED_D), (ed.flags & OHCI_ED_S) != 0,
1149
(ed.flags & OHCI_ED_K) != 0, (ed.flags & OHCI_ED_F) != 0,
1150
OHCI_BM(ed.flags, ED_MPS));
1154
if ((ed.flags & OHCI_ED_F) == 0) {
1155
if (ohci_service_td(ohci, &ed)) {
1160
if (ohci_service_iso_td(ohci, &ed)) {
1166
if (ohci_put_ed(ohci, cur, &ed)) {
1176
static void ohci_eof_timer(OHCIState *ohci)
1178
timer_mod(ohci->eof_timer, ohci->sof_time + usb_frame_time);
1181
static void ohci_sof(OHCIState *ohci)
1183
ohci->sof_time += usb_frame_time;
1184
ohci_eof_timer(ohci);
1185
ohci_set_interrupt(ohci, OHCI_INTR_SF);
1189
static void ohci_process_lists(OHCIState *ohci)
1191
if ((ohci->ctl & OHCI_CTL_CLE) && (ohci->status & OHCI_STATUS_CLF)) {
1192
if (ohci->ctrl_cur && ohci->ctrl_cur != ohci->ctrl_head) {
1193
trace_usb_ohci_process_lists(ohci->ctrl_head, ohci->ctrl_cur);
1195
if (!ohci_service_ed_list(ohci, ohci->ctrl_head)) {
1197
ohci->status &= ~OHCI_STATUS_CLF;
1201
if ((ohci->ctl & OHCI_CTL_BLE) && (ohci->status & OHCI_STATUS_BLF)) {
1202
if (!ohci_service_ed_list(ohci, ohci->bulk_head)) {
1204
ohci->status &= ~OHCI_STATUS_BLF;
1210
static void ohci_frame_boundary(void *opaque)
1212
OHCIState *ohci = opaque;
1213
struct ohci_hcca hcca;
1215
if (ohci_read_hcca(ohci, ohci->hcca, &hcca)) {
1216
trace_usb_ohci_hcca_read_error(ohci->hcca);
1222
if (ohci->ctl & OHCI_CTL_PLE) {
1225
n = ohci->frame_number & 0x1f;
1226
ohci_service_ed_list(ohci, le32_to_cpu(hcca.intr[n]));
1230
if (ohci->old_ctl & (~ohci->ctl) & (OHCI_CTL_BLE | OHCI_CTL_CLE)) {
1231
ohci_stop_endpoints(ohci);
1233
ohci->old_ctl = ohci->ctl;
1234
ohci_process_lists(ohci);
1237
if (ohci->intr_status & OHCI_INTR_UE) {
1242
ohci->frt = ohci->fit;
1245
ohci->frame_number = (ohci->frame_number + 1) & 0xffff;
1246
hcca.frame = cpu_to_le16(ohci->frame_number);
1250
if (ohci->done_count == 0 && !(ohci->intr_status & OHCI_INTR_WD)) {
1254
if (ohci->intr & ohci->intr_status) {
1257
hcca.done = cpu_to_le32(ohci->done);
1259
ohci->done_count = 7;
1260
ohci_set_interrupt(ohci, OHCI_INTR_WD);
1263
if (ohci->done_count != 7 && ohci->done_count != 0) {
1270
if (ohci_put_hcca(ohci, ohci->hcca, &hcca)) {
1279
static int ohci_bus_start(OHCIState *ohci)
1281
trace_usb_ohci_start(ohci->name);
1286
ohci->sof_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1287
ohci_eof_timer(ohci);
1293
void ohci_bus_stop(OHCIState *ohci)
1295
trace_usb_ohci_stop(ohci->name);
1296
timer_del(ohci->eof_timer);
1300
static void ohci_set_frame_interval(OHCIState *ohci, uint16_t val)
1304
if (val != ohci->fi) {
1305
trace_usb_ohci_set_frame_interval(ohci->name, ohci->fi, ohci->fi);
1311
static void ohci_port_power(OHCIState *ohci, int i, int p)
1314
ohci->rhport[i].ctrl |= OHCI_PORT_PPS;
1316
ohci->rhport[i].ctrl &= ~(OHCI_PORT_PPS | OHCI_PORT_CCS |
1317
OHCI_PORT_PSS | OHCI_PORT_PRS);
1322
static void ohci_set_ctl(OHCIState *ohci, uint32_t val)
1327
old_state = ohci->ctl & OHCI_CTL_HCFS;
1329
new_state = ohci->ctl & OHCI_CTL_HCFS;
1332
if (old_state == new_state) {
1335
trace_usb_ohci_set_ctl(ohci->name, new_state);
1336
switch (new_state) {
1337
case OHCI_USB_OPERATIONAL:
1338
ohci_bus_start(ohci);
1340
case OHCI_USB_SUSPEND:
1341
ohci_bus_stop(ohci);
1343
ohci->intr_status &= ~OHCI_INTR_SF;
1344
ohci_intr_update(ohci);
1346
case OHCI_USB_RESUME:
1347
trace_usb_ohci_resume(ohci->name);
1349
case OHCI_USB_RESET:
1350
ohci_roothub_reset(ohci);
1355
static uint32_t ohci_get_frame_remaining(OHCIState *ohci)
1360
if ((ohci->ctl & OHCI_CTL_HCFS) != OHCI_USB_OPERATIONAL) {
1361
return ohci->frt << 31;
1364
tks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - ohci->sof_time;
1370
if (tks >= usb_frame_time) {
1371
return ohci->frt << 31;
1373
tks = tks / usb_bit_time;
1374
fr = (uint16_t)(ohci->fi - tks);
1376
return (ohci->frt << 31) | fr;
1381
static void ohci_set_hub_status(OHCIState *ohci, uint32_t val)
1385
old_state = ohci->rhstatus;
1388
if (val & OHCI_RHS_OCIC) {
1389
ohci->rhstatus &= ~OHCI_RHS_OCIC;
1391
if (val & OHCI_RHS_LPS) {
1394
for (i = 0; i < ohci->num_ports; i++) {
1395
ohci_port_power(ohci, i, 0);
1397
trace_usb_ohci_hub_power_down();
1400
if (val & OHCI_RHS_LPSC) {
1403
for (i = 0; i < ohci->num_ports; i++) {
1404
ohci_port_power(ohci, i, 1);
1406
trace_usb_ohci_hub_power_up();
1409
if (val & OHCI_RHS_DRWE) {
1410
ohci->rhstatus |= OHCI_RHS_DRWE;
1412
if (val & OHCI_RHS_CRWE) {
1413
ohci->rhstatus &= ~OHCI_RHS_DRWE;
1415
if (old_state != ohci->rhstatus) {
1416
ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1421
static bool ohci_resume(OHCIState *s)
1423
if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
1424
trace_usb_ohci_remote_wakeup(s->name);
1425
s->ctl &= ~OHCI_CTL_HCFS;
1426
s->ctl |= OHCI_USB_RESUME;
1436
static int ohci_port_set_if_connected(OHCIState *ohci, int i, uint32_t val)
1445
if (!(ohci->rhport[i].ctrl & OHCI_PORT_CCS)) {
1446
ohci->rhport[i].ctrl |= OHCI_PORT_CSC;
1447
if (ohci->rhstatus & OHCI_RHS_DRWE) {
1449
if (ohci_resume(ohci)) {
1450
ohci_set_interrupt(ohci, OHCI_INTR_RD);
1456
if (ohci->rhport[i].ctrl & val) {
1460
ohci->rhport[i].ctrl |= val;
1466
static void ohci_port_set_status(OHCIState *ohci, int portnum, uint32_t val)
1471
port = &ohci->rhport[portnum];
1472
old_state = port->ctrl;
1475
if (val & OHCI_PORT_WTC) {
1476
port->ctrl &= ~(val & OHCI_PORT_WTC);
1478
if (val & OHCI_PORT_CCS) {
1479
port->ctrl &= ~OHCI_PORT_PES;
1481
ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PES);
1483
if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PSS)) {
1484
trace_usb_ohci_port_suspend(portnum);
1487
if (ohci_port_set_if_connected(ohci, portnum, val & OHCI_PORT_PRS)) {
1488
trace_usb_ohci_port_reset(portnum);
1489
usb_device_reset(port->port.dev);
1490
port->ctrl &= ~OHCI_PORT_PRS;
1492
port->ctrl |= OHCI_PORT_PES | OHCI_PORT_PRSC;
1496
if (val & OHCI_PORT_LSDA) {
1497
ohci_port_power(ohci, portnum, 0);
1499
if (val & OHCI_PORT_PPS) {
1500
ohci_port_power(ohci, portnum, 1);
1502
if (old_state != port->ctrl) {
1503
ohci_set_interrupt(ohci, OHCI_INTR_RHSC);
1507
static uint64_t ohci_mem_read(void *opaque,
1511
OHCIState *ohci = opaque;
1516
trace_usb_ohci_mem_read_unaligned(addr);
1518
} else if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
1520
retval = ohci->rhport[(addr - 0x54) >> 2].ctrl | OHCI_PORT_PPS;
1521
trace_usb_ohci_mem_port_read(size, "HcRhPortStatus", (addr - 0x50) >> 2,
1522
addr, addr >> 2, retval);
1524
switch (addr >> 2) {
1534
retval = ohci->status;
1538
retval = ohci->intr_status;
1543
retval = ohci->intr;
1547
retval = ohci->hcca;
1551
retval = ohci->per_cur;
1555
retval = ohci->ctrl_head;
1559
retval = ohci->ctrl_cur;
1563
retval = ohci->bulk_head;
1567
retval = ohci->bulk_cur;
1571
retval = ohci->done;
1575
retval = (ohci->fit << 31) | (ohci->fsmps << 16) | (ohci->fi);
1579
retval = ohci_get_frame_remaining(ohci);
1583
retval = ohci->frame_number;
1587
retval = ohci->pstart;
1595
retval = ohci->rhdesc_a;
1599
retval = ohci->rhdesc_b;
1603
retval = ohci->rhstatus;
1608
retval = ohci->hstatus & ohci->hmask;
1612
retval = ohci->hreset;
1616
retval = ohci->hmask;
1620
retval = ohci->htest;
1624
trace_usb_ohci_mem_read_bad_offset(addr);
1625
retval = 0xffffffff;
1627
if (addr != 0xc || retval) {
1628
trace_usb_ohci_mem_read(size, ohci_reg_name(addr), addr, addr >> 2,
1636
static void ohci_mem_write(void *opaque,
1641
OHCIState *ohci = opaque;
1645
trace_usb_ohci_mem_write_unaligned(addr);
1649
if (addr >= 0x54 && addr < 0x54 + ohci->num_ports * 4) {
1651
trace_usb_ohci_mem_port_write(size, "HcRhPortStatus",
1652
(addr - 0x50) >> 2, addr, addr >> 2, val);
1653
ohci_port_set_status(ohci, (addr - 0x54) >> 2, val);
1657
trace_usb_ohci_mem_write(size, ohci_reg_name(addr), addr, addr >> 2, val);
1658
switch (addr >> 2) {
1660
ohci_set_ctl(ohci, val);
1665
val = (val & ~OHCI_STATUS_SOC);
1668
ohci->status |= val;
1670
if (ohci->status & OHCI_STATUS_HCR) {
1671
ohci_soft_reset(ohci);
1676
ohci->intr_status &= ~val;
1677
ohci_intr_update(ohci);
1682
ohci_intr_update(ohci);
1687
ohci_intr_update(ohci);
1691
ohci->hcca = val & OHCI_HCCA_MASK;
1699
ohci->ctrl_head = val & OHCI_EDPTR_MASK;
1703
ohci->ctrl_cur = val & OHCI_EDPTR_MASK;
1707
ohci->bulk_head = val & OHCI_EDPTR_MASK;
1711
ohci->bulk_cur = val & OHCI_EDPTR_MASK;
1715
ohci->fsmps = (val & OHCI_FMI_FSMPS) >> 16;
1716
ohci->fit = (val & OHCI_FMI_FIT) >> 31;
1717
ohci_set_frame_interval(ohci, val);
1724
ohci->pstart = val & 0xffff;
1728
ohci->lst = val & 0xffff;
1732
ohci->rhdesc_a &= ~OHCI_RHA_RW_MASK;
1733
ohci->rhdesc_a |= val & OHCI_RHA_RW_MASK;
1740
ohci_set_hub_status(ohci, val);
1745
ohci->hstatus &= ~(val & ohci->hmask);
1749
ohci->hreset = val & ~OHCI_HRESET_FSBIR;
1750
if (val & OHCI_HRESET_FSBIR) {
1751
ohci_hard_reset(ohci);
1764
trace_usb_ohci_mem_write_bad_offset(addr);
1769
static const MemoryRegionOps ohci_mem_ops = {
1770
.read = ohci_mem_read,
1771
.write = ohci_mem_write,
1772
.endianness = DEVICE_LITTLE_ENDIAN,
1776
static void ohci_attach(USBPort *port1)
1778
OHCIState *s = port1->opaque;
1779
OHCIPort *port = &s->rhport[port1->index];
1780
uint32_t old_state = port->ctrl;
1783
port->ctrl |= OHCI_PORT_CCS | OHCI_PORT_CSC;
1786
if (port->port.dev->speed == USB_SPEED_LOW) {
1787
port->ctrl |= OHCI_PORT_LSDA;
1789
port->ctrl &= ~OHCI_PORT_LSDA;
1793
if ((s->ctl & OHCI_CTL_HCFS) == OHCI_USB_SUSPEND) {
1794
ohci_set_interrupt(s, OHCI_INTR_RD);
1797
trace_usb_ohci_port_attach(port1->index);
1799
if (old_state != port->ctrl) {
1800
ohci_set_interrupt(s, OHCI_INTR_RHSC);
1804
static void ohci_child_detach(USBPort *port1, USBDevice *dev)
1806
OHCIState *ohci = port1->opaque;
1808
if (ohci->async_td &&
1809
usb_packet_is_inflight(&ohci->usb_packet) &&
1810
ohci->usb_packet.ep->dev == dev) {
1811
usb_cancel_packet(&ohci->usb_packet);
1816
static void ohci_detach(USBPort *port1)
1818
OHCIState *s = port1->opaque;
1819
OHCIPort *port = &s->rhport[port1->index];
1820
uint32_t old_state = port->ctrl;
1822
ohci_child_detach(port1, port1->dev);
1825
if (port->ctrl & OHCI_PORT_CCS) {
1826
port->ctrl &= ~OHCI_PORT_CCS;
1827
port->ctrl |= OHCI_PORT_CSC;
1830
if (port->ctrl & OHCI_PORT_PES) {
1831
port->ctrl &= ~OHCI_PORT_PES;
1832
port->ctrl |= OHCI_PORT_PESC;
1834
trace_usb_ohci_port_detach(port1->index);
1836
if (old_state != port->ctrl) {
1837
ohci_set_interrupt(s, OHCI_INTR_RHSC);
1841
static void ohci_wakeup(USBPort *port1)
1843
OHCIState *s = port1->opaque;
1844
OHCIPort *port = &s->rhport[port1->index];
1846
if (port->ctrl & OHCI_PORT_PSS) {
1847
trace_usb_ohci_port_wakeup(port1->index);
1848
port->ctrl |= OHCI_PORT_PSSC;
1849
port->ctrl &= ~OHCI_PORT_PSS;
1850
intr = OHCI_INTR_RHSC;
1853
if (ohci_resume(s)) {
1858
intr = OHCI_INTR_RD;
1860
ohci_set_interrupt(s, intr);
1863
static void ohci_async_complete_packet(USBPort *port, USBPacket *packet)
1865
OHCIState *ohci = container_of(packet, OHCIState, usb_packet);
1867
trace_usb_ohci_async_complete();
1868
ohci->async_complete = true;
1869
ohci_process_lists(ohci);
1872
static USBPortOps ohci_port_ops = {
1873
.attach = ohci_attach,
1874
.detach = ohci_detach,
1875
.child_detach = ohci_child_detach,
1876
.wakeup = ohci_wakeup,
1877
.complete = ohci_async_complete_packet,
1880
static USBBusOps ohci_bus_ops = {
1883
void usb_ohci_init(OHCIState *ohci, DeviceState *dev, uint32_t num_ports,
1884
dma_addr_t localmem_base, char *masterbus,
1885
uint32_t firstport, AddressSpace *as,
1886
void (*ohci_die_fn)(OHCIState *), Error **errp)
1892
ohci->ohci_die = ohci_die_fn;
1894
if (num_ports > OHCI_MAX_PORTS) {
1895
error_setg(errp, "OHCI num-ports=%u is too big (limit is %u ports)",
1896
num_ports, OHCI_MAX_PORTS);
1900
if (usb_frame_time == 0) {
1901
#ifdef OHCI_TIME_WARP
1902
usb_frame_time = NANOSECONDS_PER_SECOND;
1903
usb_bit_time = NANOSECONDS_PER_SECOND / (USB_HZ / 1000);
1905
usb_frame_time = NANOSECONDS_PER_SECOND / 1000;
1906
if (NANOSECONDS_PER_SECOND >= USB_HZ) {
1907
usb_bit_time = NANOSECONDS_PER_SECOND / USB_HZ;
1912
trace_usb_ohci_init_time(usb_frame_time, usb_bit_time);
1915
ohci->num_ports = num_ports;
1917
USBPort *ports[OHCI_MAX_PORTS];
1918
for (i = 0; i < num_ports; i++) {
1919
ports[i] = &ohci->rhport[i].port;
1921
usb_register_companion(masterbus, ports, num_ports,
1922
firstport, ohci, &ohci_port_ops,
1923
USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL,
1926
error_propagate(errp, err);
1930
usb_bus_new(&ohci->bus, sizeof(ohci->bus), &ohci_bus_ops, dev);
1931
for (i = 0; i < num_ports; i++) {
1932
usb_register_port(&ohci->bus, &ohci->rhport[i].port,
1933
ohci, i, &ohci_port_ops,
1934
USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
1938
memory_region_init_io(&ohci->mem, OBJECT(dev), &ohci_mem_ops,
1940
ohci->localmem_base = localmem_base;
1942
ohci->name = object_get_typename(OBJECT(dev));
1943
usb_packet_init(&ohci->usb_packet);
1947
ohci->eof_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1948
ohci_frame_boundary, ohci);
1955
void ohci_sysbus_die(struct OHCIState *ohci)
1957
trace_usb_ohci_die();
1959
ohci_set_interrupt(ohci, OHCI_INTR_UE);
1960
ohci_bus_stop(ohci);
1963
static const VMStateDescription vmstate_ohci_state_port = {
1964
.name = "ohci-core/port",
1966
.minimum_version_id = 1,
1967
.fields = (const VMStateField[]) {
1968
VMSTATE_UINT32(ctrl, OHCIPort),
1969
VMSTATE_END_OF_LIST()
1973
static bool ohci_eof_timer_needed(void *opaque)
1975
OHCIState *ohci = opaque;
1977
return timer_pending(ohci->eof_timer);
1980
static const VMStateDescription vmstate_ohci_eof_timer = {
1981
.name = "ohci-core/eof-timer",
1983
.minimum_version_id = 1,
1984
.needed = ohci_eof_timer_needed,
1985
.fields = (const VMStateField[]) {
1986
VMSTATE_TIMER_PTR(eof_timer, OHCIState),
1987
VMSTATE_END_OF_LIST()
1991
const VMStateDescription vmstate_ohci_state = {
1992
.name = "ohci-core",
1994
.minimum_version_id = 1,
1995
.fields = (const VMStateField[]) {
1996
VMSTATE_INT64(sof_time, OHCIState),
1997
VMSTATE_UINT32(ctl, OHCIState),
1998
VMSTATE_UINT32(status, OHCIState),
1999
VMSTATE_UINT32(intr_status, OHCIState),
2000
VMSTATE_UINT32(intr, OHCIState),
2001
VMSTATE_UINT32(hcca, OHCIState),
2002
VMSTATE_UINT32(ctrl_head, OHCIState),
2003
VMSTATE_UINT32(ctrl_cur, OHCIState),
2004
VMSTATE_UINT32(bulk_head, OHCIState),
2005
VMSTATE_UINT32(bulk_cur, OHCIState),
2006
VMSTATE_UINT32(per_cur, OHCIState),
2007
VMSTATE_UINT32(done, OHCIState),
2008
VMSTATE_INT32(done_count, OHCIState),
2009
VMSTATE_UINT16(fsmps, OHCIState),
2010
VMSTATE_UINT8(fit, OHCIState),
2011
VMSTATE_UINT16(fi, OHCIState),
2012
VMSTATE_UINT8(frt, OHCIState),
2013
VMSTATE_UINT16(frame_number, OHCIState),
2014
VMSTATE_UINT16(padding, OHCIState),
2015
VMSTATE_UINT32(pstart, OHCIState),
2016
VMSTATE_UINT32(lst, OHCIState),
2017
VMSTATE_UINT32(rhdesc_a, OHCIState),
2018
VMSTATE_UINT32(rhdesc_b, OHCIState),
2019
VMSTATE_UINT32(rhstatus, OHCIState),
2020
VMSTATE_STRUCT_ARRAY(rhport, OHCIState, OHCI_MAX_PORTS, 0,
2021
vmstate_ohci_state_port, OHCIPort),
2022
VMSTATE_UINT32(hstatus, OHCIState),
2023
VMSTATE_UINT32(hmask, OHCIState),
2024
VMSTATE_UINT32(hreset, OHCIState),
2025
VMSTATE_UINT32(htest, OHCIState),
2026
VMSTATE_UINT32(old_ctl, OHCIState),
2027
VMSTATE_UINT8_ARRAY(usb_buf, OHCIState, 8192),
2028
VMSTATE_UINT32(async_td, OHCIState),
2029
VMSTATE_BOOL(async_complete, OHCIState),
2030
VMSTATE_END_OF_LIST()
2032
.subsections = (const VMStateDescription * const []) {
2033
&vmstate_ohci_eof_timer,