Revision 94527ead

b/Makefile.objs
193 193
hw-obj-$(CONFIG_PCKBD) += pckbd.o
194 194
hw-obj-$(CONFIG_USB_UHCI) += usb-uhci.o
195 195
hw-obj-$(CONFIG_USB_OHCI) += usb-ohci.o
196
hw-obj-$(CONFIG_USB_EHCI) += usb-ehci.o
196 197
hw-obj-$(CONFIG_FDC) += fdc.o
197 198
hw-obj-$(CONFIG_ACPI) += acpi.o acpi_piix4.o
198 199
hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o
b/default-configs/pci.mak
3 3
CONFIG_VIRTIO=y
4 4
CONFIG_USB_UHCI=y
5 5
CONFIG_USB_OHCI=y
6
CONFIG_USB_EHCI=y
6 7
CONFIG_NE2000_PCI=y
7 8
CONFIG_EEPRO100_PCI=y
8 9
CONFIG_PCNET_PCI=y
b/docs/usb2.txt
1

  
2
USB 2.0 Quick Start
3
===================
4

  
5
The QEMU EHCI Adapter does *not* support companion controllers.  That
6
implies there are two completely separate USB busses: One USB 1.1 bus
7
driven by the UHCI controller and one USB 2.0 bus driven by the EHCI
8
controller.  Devices must be attached to the correct controller
9
manually.
10

  
11
The '-usb' switch will make qemu create the UHCI controller as part of
12
the PIIX3 chipset.  The USB 1.1 bus will carry the name "usb.0".
13

  
14
You can use the standard -device switch to add a EHCI controller to
15
your virtual machine.  It is strongly recommended to specify an ID for
16
the controller so the USB 2.0 bus gets a individual name, for example
17
'-device usb-ehci,id=ehci".  This will give you a USB 2.0 bus named
18
"ehci.0".
19

  
20
I strongly recomment to also use -device to attach usb devices because
21
you can specify the bus they should be attached to this way.  Here is
22
a complete example:
23

  
24
    qemu -M pc ${otheroptions}                           \
25
        -drive if=none,id=usbstick,file=/path/to/image   \
26
        -usb                                             \
27
        -device usb-ehci,id=ehci                         \
28
        -device usb-tablet,bus=usb.0                     \
29
        -device usb-storage,bus=ehci.0,drive=usbstick
30

  
31
This attaches a usb tablet to the UHCI adapter and a usb mass storage
32
device to the EHCI adapter.
33

  
34
enjoy,
35
  Gerd
36

  
37
--
38
Gerd Hoffmann <kraxel@redhat.com>
b/hw/pci_ids.h
100 100
#define PCI_VENDOR_ID_INTEL              0x8086
101 101
#define PCI_DEVICE_ID_INTEL_82441        0x1237
102 102
#define PCI_DEVICE_ID_INTEL_82801AA_5    0x2415
103
#define PCI_DEVICE_ID_INTEL_82801D       0x24CD
103 104
#define PCI_DEVICE_ID_INTEL_ESB_9        0x25ab
104 105
#define PCI_DEVICE_ID_INTEL_82371SB_0    0x7000
105 106
#define PCI_DEVICE_ID_INTEL_82371SB_1    0x7010
b/hw/usb-ehci.c
1
/*
2
 * QEMU USB EHCI Emulation
3
 *
4
 * Copyright(c) 2008  Emutex Ltd. (address@hidden)
5
 *
6
 * EHCI project was started by Mark Burkley, with contributions by
7
 * Niels de Vos.  David S. Ahern continued working on it.  Kevin Wolf,
8
 * Jan Kiszka and Vincent Palatin contributed bugfixes.
9
 *
10
 *
11
 * This library is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2 of the License, or(at your option) any later version.
15
 *
16
 * This library is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * TODO:
25
 *  o Downstream port handoff
26
 */
27

  
28
#include "hw.h"
29
#include "qemu-timer.h"
30
#include "usb.h"
31
#include "pci.h"
32
#include "monitor.h"
33

  
34
#define EHCI_DEBUG   0
35
#define STATE_DEBUG  0       /* state transitions  */
36

  
37
#if EHCI_DEBUG || STATE_DEBUG
38
#define DPRINTF printf
39
#else
40
#define DPRINTF(...)
41
#endif
42

  
43
#if STATE_DEBUG
44
#define DPRINTF_ST DPRINTF
45
#else
46
#define DPRINTF_ST(...)
47
#endif
48

  
49
/* internal processing - reset HC to try and recover */
50
#define USB_RET_PROCERR   (-99)
51

  
52
#define MMIO_SIZE        0x1000
53

  
54
/* Capability Registers Base Address - section 2.2 */
55
#define CAPREGBASE       0x0000
56
#define CAPLENGTH        CAPREGBASE + 0x0000  // 1-byte, 0x0001 reserved
57
#define HCIVERSION       CAPREGBASE + 0x0002  // 2-bytes, i/f version #
58
#define HCSPARAMS        CAPREGBASE + 0x0004  // 4-bytes, structural params
59
#define HCCPARAMS        CAPREGBASE + 0x0008  // 4-bytes, capability params
60
#define EECP             HCCPARAMS + 1
61
#define HCSPPORTROUTE1   CAPREGBASE + 0x000c
62
#define HCSPPORTROUTE2   CAPREGBASE + 0x0010
63

  
64
#define OPREGBASE        0x0020        // Operational Registers Base Address
65

  
66
#define USBCMD           OPREGBASE + 0x0000
67
#define USBCMD_RUNSTOP   (1 << 0)      // run / Stop
68
#define USBCMD_HCRESET   (1 << 1)      // HC Reset
69
#define USBCMD_FLS       (3 << 2)      // Frame List Size
70
#define USBCMD_FLS_SH    2             // Frame List Size Shift
71
#define USBCMD_PSE       (1 << 4)      // Periodic Schedule Enable
72
#define USBCMD_ASE       (1 << 5)      // Asynch Schedule Enable
73
#define USBCMD_IAAD      (1 << 6)      // Int Asynch Advance Doorbell
74
#define USBCMD_LHCR      (1 << 7)      // Light Host Controller Reset
75
#define USBCMD_ASPMC     (3 << 8)      // Async Sched Park Mode Count
76
#define USBCMD_ASPME     (1 << 11)     // Async Sched Park Mode Enable
77
#define USBCMD_ITC       (0x7f << 16)  // Int Threshold Control
78
#define USBCMD_ITC_SH    16            // Int Threshold Control Shift
79

  
80
#define USBSTS           OPREGBASE + 0x0004
81
#define USBSTS_RO_MASK   0x0000003f
82
#define USBSTS_INT       (1 << 0)      // USB Interrupt
83
#define USBSTS_ERRINT    (1 << 1)      // Error Interrupt
84
#define USBSTS_PCD       (1 << 2)      // Port Change Detect
85
#define USBSTS_FLR       (1 << 3)      // Frame List Rollover
86
#define USBSTS_HSE       (1 << 4)      // Host System Error
87
#define USBSTS_IAA       (1 << 5)      // Interrupt on Async Advance
88
#define USBSTS_HALT      (1 << 12)     // HC Halted
89
#define USBSTS_REC       (1 << 13)     // Reclamation
90
#define USBSTS_PSS       (1 << 14)     // Periodic Schedule Status
91
#define USBSTS_ASS       (1 << 15)     // Asynchronous Schedule Status
92

  
93
/*
94
 *  Interrupt enable bits correspond to the interrupt active bits in USBSTS
95
 *  so no need to redefine here.
96
 */
97
#define USBINTR              OPREGBASE + 0x0008
98
#define USBINTR_MASK         0x0000003f
99

  
100
#define FRINDEX              OPREGBASE + 0x000c
101
#define CTRLDSSEGMENT        OPREGBASE + 0x0010
102
#define PERIODICLISTBASE     OPREGBASE + 0x0014
103
#define ASYNCLISTADDR        OPREGBASE + 0x0018
104
#define ASYNCLISTADDR_MASK   0xffffffe0
105

  
106
#define CONFIGFLAG           OPREGBASE + 0x0040
107

  
108
#define PORTSC               (OPREGBASE + 0x0044)
109
#define PORTSC_BEGIN         PORTSC
110
#define PORTSC_END           (PORTSC + 4 * NB_PORTS)
111
/*
112
 * Bits that are reserverd or are read-only are masked out of values
113
 * written to us by software
114
 */
115
#define PORTSC_RO_MASK       0x007021c5
116
#define PORTSC_RWC_MASK      0x0000002a
117
#define PORTSC_WKOC_E        (1 << 22)    // Wake on Over Current Enable
118
#define PORTSC_WKDS_E        (1 << 21)    // Wake on Disconnect Enable
119
#define PORTSC_WKCN_E        (1 << 20)    // Wake on Connect Enable
120
#define PORTSC_PTC           (15 << 16)   // Port Test Control
121
#define PORTSC_PTC_SH        16           // Port Test Control shift
122
#define PORTSC_PIC           (3 << 14)    // Port Indicator Control
123
#define PORTSC_PIC_SH        14           // Port Indicator Control Shift
124
#define PORTSC_POWNER        (1 << 13)    // Port Owner
125
#define PORTSC_PPOWER        (1 << 12)    // Port Power
126
#define PORTSC_LINESTAT      (3 << 10)    // Port Line Status
127
#define PORTSC_LINESTAT_SH   10           // Port Line Status Shift
128
#define PORTSC_PRESET        (1 << 8)     // Port Reset
129
#define PORTSC_SUSPEND       (1 << 7)     // Port Suspend
130
#define PORTSC_FPRES         (1 << 6)     // Force Port Resume
131
#define PORTSC_OCC           (1 << 5)     // Over Current Change
132
#define PORTSC_OCA           (1 << 4)     // Over Current Active
133
#define PORTSC_PEDC          (1 << 3)     // Port Enable/Disable Change
134
#define PORTSC_PED           (1 << 2)     // Port Enable/Disable
135
#define PORTSC_CSC           (1 << 1)     // Connect Status Change
136
#define PORTSC_CONNECT       (1 << 0)     // Current Connect Status
137

  
138
#define FRAME_TIMER_FREQ 1000
139
#define FRAME_TIMER_USEC (1000000 / FRAME_TIMER_FREQ)
140

  
141
#define NB_MAXINTRATE    8        // Max rate at which controller issues ints
142
#define NB_PORTS         4        // Number of downstream ports
143
#define BUFF_SIZE        5*4096   // Max bytes to transfer per transaction
144
#define MAX_ITERATIONS   20       // Max number of QH before we break the loop
145
#define MAX_QH           100      // Max allowable queue heads in a chain
146

  
147
/*  Internal periodic / asynchronous schedule state machine states
148
 */
149
typedef enum {
150
    EST_INACTIVE = 1000,
151
    EST_ACTIVE,
152
    EST_EXECUTING,
153
    EST_SLEEPING,
154
    /*  The following states are internal to the state machine function
155
    */
156
    EST_WAITLISTHEAD,
157
    EST_FETCHENTRY,
158
    EST_FETCHQH,
159
    EST_FETCHITD,
160
    EST_ADVANCEQUEUE,
161
    EST_FETCHQTD,
162
    EST_EXECUTE,
163
    EST_WRITEBACK,
164
    EST_HORIZONTALQH
165
} EHCI_STATES;
166

  
167
/* macros for accessing fields within next link pointer entry */
168
#define NLPTR_GET(x)             ((x) & 0xffffffe0)
169
#define NLPTR_TYPE_GET(x)        (((x) >> 1) & 3)
170
#define NLPTR_TBIT(x)            ((x) & 1)  // 1=invalid, 0=valid
171

  
172
/* link pointer types */
173
#define NLPTR_TYPE_ITD           0     // isoc xfer descriptor
174
#define NLPTR_TYPE_QH            1     // queue head
175
#define NLPTR_TYPE_STITD         2     // split xaction, isoc xfer descriptor
176
#define NLPTR_TYPE_FSTN          3     // frame span traversal node
177

  
178

  
179
/*  EHCI spec version 1.0 Section 3.3
180
 */
181
typedef struct EHCIitd {
182
    uint32_t next;
183

  
184
    uint32_t transact[8];
185
#define ITD_XACT_ACTIVE          (1 << 31)
186
#define ITD_XACT_DBERROR         (1 << 30)
187
#define ITD_XACT_BABBLE          (1 << 29)
188
#define ITD_XACT_XACTERR         (1 << 28)
189
#define ITD_XACT_LENGTH_MASK     0x0fff0000
190
#define ITD_XACT_LENGTH_SH       16
191
#define ITD_XACT_IOC             (1 << 15)
192
#define ITD_XACT_PGSEL_MASK      0x00007000
193
#define ITD_XACT_PGSEL_SH        12
194
#define ITD_XACT_OFFSET_MASK     0x00000fff
195

  
196
    uint32_t bufptr[7];
197
#define ITD_BUFPTR_MASK          0xfffff000
198
#define ITD_BUFPTR_SH            12
199
#define ITD_BUFPTR_EP_MASK       0x00000f00
200
#define ITD_BUFPTR_EP_SH         8
201
#define ITD_BUFPTR_DEVADDR_MASK  0x0000007f
202
#define ITD_BUFPTR_DEVADDR_SH    0
203
#define ITD_BUFPTR_DIRECTION     (1 << 11)
204
#define ITD_BUFPTR_MAXPKT_MASK   0x000007ff
205
#define ITD_BUFPTR_MAXPKT_SH     0
206
#define ITD_BUFPTR_MULT_MASK     0x00000003
207
} EHCIitd;
208

  
209
/*  EHCI spec version 1.0 Section 3.4
210
 */
211
typedef struct EHCIsitd {
212
    uint32_t next;                  // Standard next link pointer
213
    uint32_t epchar;
214
#define SITD_EPCHAR_IO              (1 << 31)
215
#define SITD_EPCHAR_PORTNUM_MASK    0x7f000000
216
#define SITD_EPCHAR_PORTNUM_SH      24
217
#define SITD_EPCHAR_HUBADD_MASK     0x007f0000
218
#define SITD_EPCHAR_HUBADDR_SH      16
219
#define SITD_EPCHAR_EPNUM_MASK      0x00000f00
220
#define SITD_EPCHAR_EPNUM_SH        8
221
#define SITD_EPCHAR_DEVADDR_MASK    0x0000007f
222

  
223
    uint32_t uframe;
224
#define SITD_UFRAME_CMASK_MASK      0x0000ff00
225
#define SITD_UFRAME_CMASK_SH        8
226
#define SITD_UFRAME_SMASK_MASK      0x000000ff
227

  
228
    uint32_t results;
229
#define SITD_RESULTS_IOC              (1 << 31)
230
#define SITD_RESULTS_PGSEL            (1 << 30)
231
#define SITD_RESULTS_TBYTES_MASK      0x03ff0000
232
#define SITD_RESULTS_TYBYTES_SH       16
233
#define SITD_RESULTS_CPROGMASK_MASK   0x0000ff00
234
#define SITD_RESULTS_CPROGMASK_SH     8
235
#define SITD_RESULTS_ACTIVE           (1 << 7)
236
#define SITD_RESULTS_ERR              (1 << 6)
237
#define SITD_RESULTS_DBERR            (1 << 5)
238
#define SITD_RESULTS_BABBLE           (1 << 4)
239
#define SITD_RESULTS_XACTERR          (1 << 3)
240
#define SITD_RESULTS_MISSEDUF         (1 << 2)
241
#define SITD_RESULTS_SPLITXSTATE      (1 << 1)
242

  
243
    uint32_t bufptr[2];
244
#define SITD_BUFPTR_MASK              0xfffff000
245
#define SITD_BUFPTR_CURROFF_MASK      0x00000fff
246
#define SITD_BUFPTR_TPOS_MASK         0x00000018
247
#define SITD_BUFPTR_TPOS_SH           3
248
#define SITD_BUFPTR_TCNT_MASK         0x00000007
249

  
250
    uint32_t backptr;                 // Standard next link pointer
251
} EHCIsitd;
252

  
253
/*  EHCI spec version 1.0 Section 3.5
254
 */
255
typedef struct EHCIqtd {
256
    uint32_t next;                    // Standard next link pointer
257
    uint32_t altnext;                 // Standard next link pointer
258
    uint32_t token;
259
#define QTD_TOKEN_DTOGGLE             (1 << 31)
260
#define QTD_TOKEN_TBYTES_MASK         0x7fff0000
261
#define QTD_TOKEN_TBYTES_SH           16
262
#define QTD_TOKEN_IOC                 (1 << 15)
263
#define QTD_TOKEN_CPAGE_MASK          0x00007000
264
#define QTD_TOKEN_CPAGE_SH            12
265
#define QTD_TOKEN_CERR_MASK           0x00000c00
266
#define QTD_TOKEN_CERR_SH             10
267
#define QTD_TOKEN_PID_MASK            0x00000300
268
#define QTD_TOKEN_PID_SH              8
269
#define QTD_TOKEN_ACTIVE              (1 << 7)
270
#define QTD_TOKEN_HALT                (1 << 6)
271
#define QTD_TOKEN_DBERR               (1 << 5)
272
#define QTD_TOKEN_BABBLE              (1 << 4)
273
#define QTD_TOKEN_XACTERR             (1 << 3)
274
#define QTD_TOKEN_MISSEDUF            (1 << 2)
275
#define QTD_TOKEN_SPLITXSTATE         (1 << 1)
276
#define QTD_TOKEN_PING                (1 << 0)
277

  
278
    uint32_t bufptr[5];               // Standard buffer pointer
279
#define QTD_BUFPTR_MASK               0xfffff000
280
} EHCIqtd;
281

  
282
/*  EHCI spec version 1.0 Section 3.6
283
 */
284
typedef struct EHCIqh {
285
    uint32_t next;                    // Standard next link pointer
286

  
287
    /* endpoint characteristics */
288
    uint32_t epchar;
289
#define QH_EPCHAR_RL_MASK             0xf0000000
290
#define QH_EPCHAR_RL_SH               28
291
#define QH_EPCHAR_C                   (1 << 27)
292
#define QH_EPCHAR_MPLEN_MASK          0x07FF0000
293
#define QH_EPCHAR_MPLEN_SH            16
294
#define QH_EPCHAR_H                   (1 << 15)
295
#define QH_EPCHAR_DTC                 (1 << 14)
296
#define QH_EPCHAR_EPS_MASK            0x00003000
297
#define QH_EPCHAR_EPS_SH              12
298
#define EHCI_QH_EPS_FULL              0
299
#define EHCI_QH_EPS_LOW               1
300
#define EHCI_QH_EPS_HIGH              2
301
#define EHCI_QH_EPS_RESERVED          3
302

  
303
#define QH_EPCHAR_EP_MASK             0x00000f00
304
#define QH_EPCHAR_EP_SH               8
305
#define QH_EPCHAR_I                   (1 << 7)
306
#define QH_EPCHAR_DEVADDR_MASK        0x0000007f
307
#define QH_EPCHAR_DEVADDR_SH          0
308

  
309
    /* endpoint capabilities */
310
    uint32_t epcap;
311
#define QH_EPCAP_MULT_MASK            0xc0000000
312
#define QH_EPCAP_MULT_SH              30
313
#define QH_EPCAP_PORTNUM_MASK         0x3f800000
314
#define QH_EPCAP_PORTNUM_SH           23
315
#define QH_EPCAP_HUBADDR_MASK         0x007f0000
316
#define QH_EPCAP_HUBADDR_SH           16
317
#define QH_EPCAP_CMASK_MASK           0x0000ff00
318
#define QH_EPCAP_CMASK_SH             8
319
#define QH_EPCAP_SMASK_MASK           0x000000ff
320
#define QH_EPCAP_SMASK_SH             0
321

  
322
    uint32_t current_qtd;             // Standard next link pointer
323
    uint32_t next_qtd;                // Standard next link pointer
324
    uint32_t altnext_qtd;
325
#define QH_ALTNEXT_NAKCNT_MASK        0x0000001e
326
#define QH_ALTNEXT_NAKCNT_SH          1
327

  
328
    uint32_t token;                   // Same as QTD token
329
    uint32_t bufptr[5];               // Standard buffer pointer
330
#define BUFPTR_CPROGMASK_MASK         0x000000ff
331
#define BUFPTR_FRAMETAG_MASK          0x0000001f
332
#define BUFPTR_SBYTES_MASK            0x00000fe0
333
#define BUFPTR_SBYTES_SH              5
334
} EHCIqh;
335

  
336
/*  EHCI spec version 1.0 Section 3.7
337
 */
338
typedef struct EHCIfstn {
339
    uint32_t next;                    // Standard next link pointer
340
    uint32_t backptr;                 // Standard next link pointer
341
} EHCIfstn;
342

  
343
typedef struct {
344
    PCIDevice dev;
345
    qemu_irq irq;
346
    target_phys_addr_t mem_base;
347
    int mem;
348
    int num_ports;
349
    /*
350
     *  EHCI spec version 1.0 Section 2.3
351
     *  Host Controller Operational Registers
352
     */
353
    union {
354
        uint8_t mmio[MMIO_SIZE];
355
        struct {
356
            uint8_t cap[OPREGBASE];
357
            uint32_t usbcmd;
358
            uint32_t usbsts;
359
            uint32_t usbintr;
360
            uint32_t frindex;
361
            uint32_t ctrldssegment;
362
            uint32_t periodiclistbase;
363
            uint32_t asynclistaddr;
364
            uint32_t notused[9];
365
            uint32_t configflag;
366
            uint32_t portsc[NB_PORTS];
367
        };
368
    };
369
    /*
370
     *  Internal states, shadow registers, etc
371
     */
372
    uint32_t sofv;
373
    QEMUTimer *frame_timer;
374
    int attach_poll_counter;
375
    int astate;                        // Current state in asynchronous schedule
376
    int pstate;                        // Current state in periodic schedule
377
    USBPort ports[NB_PORTS];
378
    uint8_t buffer[BUFF_SIZE];
379
    uint32_t usbsts_pending;
380

  
381
    /* cached data from guest - needs to be flushed
382
     * when guest removes an entry (doorbell, handshake sequence)
383
     */
384
    EHCIqh qh;             // copy of current QH (being worked on)
385
    uint32_t qhaddr;       // address QH read from
386

  
387
    EHCIqtd qtd;           // copy of current QTD (being worked on)
388
    uint32_t qtdaddr;      // address QTD read from
389

  
390
    uint32_t itdaddr;      // current ITD
391

  
392
    uint32_t fetch_addr;   // which address to look at next
393

  
394
    USBBus bus;
395
    USBPacket usb_packet;
396
    int async_complete;
397
    uint32_t tbytes;
398
    int pid;
399
    int exec_status;
400
    int isoch_pause;
401
    uint32_t last_run_usec;
402
    uint32_t frame_end_usec;
403
} EHCIState;
404

  
405
#define SET_LAST_RUN_CLOCK(s) \
406
    (s)->last_run_usec = qemu_get_clock_ns(vm_clock) / 1000;
407

  
408
/* nifty macros from Arnon's EHCI version  */
409
#define get_field(data, field) \
410
    (((data) & field##_MASK) >> field##_SH)
411

  
412
#define set_field(data, newval, field) do { \
413
    uint32_t val = *data; \
414
    val &= ~ field##_MASK; \
415
    val |= ((newval) << field##_SH) & field##_MASK; \
416
    *data = val; \
417
    } while(0)
418

  
419

  
420
#if EHCI_DEBUG
421
static const char *addr2str(unsigned addr)
422
{
423
    const char *r            = "   unknown";
424
    const char *n[] = {
425
        [ CAPLENGTH ]        = " CAPLENGTH",
426
        [ HCIVERSION ]       = "HCIVERSION",
427
        [ HCSPARAMS ]        = " HCSPARAMS",
428
        [ HCCPARAMS ]        = " HCCPARAMS",
429
        [ USBCMD ]           = "   COMMAND",
430
        [ USBSTS ]           = "    STATUS",
431
        [ USBINTR ]          = " INTERRUPT",
432
        [ FRINDEX ]          = " FRAME IDX",
433
        [ PERIODICLISTBASE ] = "P-LIST BASE",
434
        [ ASYNCLISTADDR ]    = "A-LIST ADDR",
435
        [ PORTSC_BEGIN ...
436
          PORTSC_END ]       = "PORT STATUS",
437
        [ CONFIGFLAG ]       = "CONFIG FLAG",
438
    };
439

  
440
    if (addr < ARRAY_SIZE(n) && n[addr] != NULL) {
441
        return n[addr];
442
    } else {
443
        return r;
444
    }
445
}
446
#endif
447

  
448

  
449
static inline void ehci_set_interrupt(EHCIState *s, int intr)
450
{
451
    int level = 0;
452

  
453
    // TODO honour interrupt threshold requests
454

  
455
    s->usbsts |= intr;
456

  
457
    if ((s->usbsts & USBINTR_MASK) & s->usbintr) {
458
        level = 1;
459
    }
460

  
461
    qemu_set_irq(s->irq, level);
462
}
463

  
464
static inline void ehci_record_interrupt(EHCIState *s, int intr)
465
{
466
    s->usbsts_pending |= intr;
467
}
468

  
469
static inline void ehci_commit_interrupt(EHCIState *s)
470
{
471
    if (!s->usbsts_pending) {
472
        return;
473
    }
474
    ehci_set_interrupt(s, s->usbsts_pending);
475
    s->usbsts_pending = 0;
476
}
477

  
478
/* Attach or detach a device on root hub */
479

  
480
static void ehci_attach(USBPort *port)
481
{
482
    EHCIState *s = port->opaque;
483
    uint32_t *portsc = &s->portsc[port->index];
484

  
485
    DPRINTF("ehci_attach invoked for index %d, portsc 0x%x, desc %s\n",
486
           port->index, *portsc, port->dev->product_desc);
487

  
488
    *portsc |= PORTSC_CONNECT;
489
    *portsc |= PORTSC_CSC;
490

  
491
    /*
492
     *  If a high speed device is attached then we own this port(indicated
493
     *  by zero in the PORTSC_POWNER bit field) so set the status bit
494
     *  and set an interrupt if enabled.
495
     */
496
    if ( !(*portsc & PORTSC_POWNER)) {
497
        ehci_set_interrupt(s, USBSTS_PCD);
498
    }
499
}
500

  
501
static void ehci_detach(USBPort *port)
502
{
503
    EHCIState *s = port->opaque;
504
    uint32_t *portsc = &s->portsc[port->index];
505

  
506
    DPRINTF("ehci_attach invoked for index %d, portsc 0x%x\n",
507
           port->index, *portsc);
508

  
509
    *portsc &= ~PORTSC_CONNECT;
510
    *portsc |= PORTSC_CSC;
511

  
512
    /*
513
     *  If a high speed device is attached then we own this port(indicated
514
     *  by zero in the PORTSC_POWNER bit field) so set the status bit
515
     *  and set an interrupt if enabled.
516
     */
517
    if ( !(*portsc & PORTSC_POWNER)) {
518
        ehci_set_interrupt(s, USBSTS_PCD);
519
    }
520
}
521

  
522
/* 4.1 host controller initialization */
523
static void ehci_reset(void *opaque)
524
{
525
    EHCIState *s = opaque;
526
    uint8_t *pci_conf;
527
    int i;
528

  
529
    pci_conf = s->dev.config;
530

  
531
    memset(&s->mmio[OPREGBASE], 0x00, MMIO_SIZE - OPREGBASE);
532

  
533
    s->usbcmd = NB_MAXINTRATE << USBCMD_ITC_SH;
534
    s->usbsts = USBSTS_HALT;
535

  
536
    s->astate = EST_INACTIVE;
537
    s->pstate = EST_INACTIVE;
538
    s->async_complete = 0;
539
    s->isoch_pause = -1;
540
    s->attach_poll_counter = 0;
541

  
542
    for(i = 0; i < NB_PORTS; i++) {
543
        s->portsc[i] = PORTSC_POWNER | PORTSC_PPOWER;
544

  
545
        if (s->ports[i].dev) {
546
            usb_attach(&s->ports[i], s->ports[i].dev);
547
        }
548
    }
549
}
550

  
551
static uint32_t ehci_mem_readb(void *ptr, target_phys_addr_t addr)
552
{
553
    EHCIState *s = ptr;
554
    uint32_t val;
555

  
556
    val = s->mmio[addr];
557

  
558
    return val;
559
}
560

  
561
static uint32_t ehci_mem_readw(void *ptr, target_phys_addr_t addr)
562
{
563
    EHCIState *s = ptr;
564
    uint32_t val;
565

  
566
    val = s->mmio[addr] | (s->mmio[addr+1] << 8);
567

  
568
    return val;
569
}
570

  
571
static uint32_t ehci_mem_readl(void *ptr, target_phys_addr_t addr)
572
{
573
    EHCIState *s = ptr;
574
    uint32_t val;
575

  
576
    val = s->mmio[addr] | (s->mmio[addr+1] << 8) |
577
          (s->mmio[addr+2] << 16) | (s->mmio[addr+3] << 24);
578

  
579
    return val;
580
}
581

  
582
static void ehci_mem_writeb(void *ptr, target_phys_addr_t addr, uint32_t val)
583
{
584
    fprintf(stderr, "EHCI doesn't handle byte writes to MMIO\n");
585
    exit(1);
586
}
587

  
588
static void ehci_mem_writew(void *ptr, target_phys_addr_t addr, uint32_t val)
589
{
590
    fprintf(stderr, "EHCI doesn't handle 16-bit writes to MMIO\n");
591
    exit(1);
592
}
593

  
594
static void handle_port_status_write(EHCIState *s, int port, uint32_t val)
595
{
596
    uint32_t *portsc = &s->portsc[port];
597
    int rwc;
598
    USBDevice *dev = s->ports[port].dev;
599

  
600
    DPRINTF("port_status_write: "
601
            "PORTSC (port %d) curr %08X new %08X rw-clear %08X rw %08X\n",
602
            port, *portsc, val, (val & PORTSC_RWC_MASK), val & PORTSC_RO_MASK);
603

  
604
    rwc = val & PORTSC_RWC_MASK;
605
    val &= PORTSC_RO_MASK;
606

  
607
    // handle_read_write_clear(&val, portsc, PORTSC_PEDC | PORTSC_CSC);
608

  
609
    *portsc &= ~rwc;
610

  
611
    if ((val & PORTSC_PRESET) && !(*portsc & PORTSC_PRESET)) {
612
        DPRINTF("port_status_write: USBTRAN Port %d reset begin\n", port);
613
    }
614

  
615
    if (!(val & PORTSC_PRESET) &&(*portsc & PORTSC_PRESET)) {
616
        DPRINTF("port_status_write: USBTRAN Port %d reset done\n", port);
617
        usb_attach(&s->ports[port], dev);
618

  
619
        // TODO how to handle reset of ports with no device
620
        if (dev) {
621
            usb_send_msg(dev, USB_MSG_RESET);
622
        }
623

  
624
        if (s->ports[port].dev) {
625
            DPRINTF("port_status_write: "
626
                    "Device was connected before reset, clearing CSC bit\n");
627
            *portsc &= ~PORTSC_CSC;
628
        }
629

  
630
        /*  Table 2.16 Set the enable bit(and enable bit change) to indicate
631
         *  to SW that this port has a high speed device attached
632
         *
633
         *  TODO - when to disable?
634
         */
635
        val |= PORTSC_PED;
636
        val |= PORTSC_PEDC;
637
    }
638

  
639
    *portsc &= ~PORTSC_RO_MASK;
640
    *portsc |= val;
641
    DPRINTF("port_status_write: Port %d status set to 0x%08x\n", port, *portsc);
642
}
643

  
644
static void ehci_mem_writel(void *ptr, target_phys_addr_t addr, uint32_t val)
645
{
646
    EHCIState *s = ptr;
647
    int i;
648
#if EHCI_DEBUG
649
    const char *str;
650
#endif
651

  
652
    /* Only aligned reads are allowed on OHCI */
653
    if (addr & 3) {
654
        fprintf(stderr, "usb-ehci: Mis-aligned write to addr 0x"
655
                TARGET_FMT_plx "\n", addr);
656
        return;
657
    }
658

  
659
    if (addr >= PORTSC && addr < PORTSC + 4 * NB_PORTS) {
660
        handle_port_status_write(s, (addr-PORTSC)/4, val);
661
        return;
662
    }
663

  
664
    if (addr < OPREGBASE) {
665
        fprintf(stderr, "usb-ehci: write attempt to read-only register"
666
                TARGET_FMT_plx "\n", addr);
667
        return;
668
    }
669

  
670

  
671
    /* Do any register specific pre-write processing here.  */
672
#if EHCI_DEBUG
673
    str = addr2str((unsigned) addr);
674
#endif
675
    switch(addr) {
676
    case USBCMD:
677
        DPRINTF("ehci_mem_writel: USBCMD val=0x%08X, current cmd=0x%08X\n",
678
                val, s->usbcmd);
679

  
680
        if ((val & USBCMD_RUNSTOP) && !(s->usbcmd & USBCMD_RUNSTOP)) {
681
            DPRINTF("ehci_mem_writel: %s run, clear halt\n", str);
682
            qemu_mod_timer(s->frame_timer, qemu_get_clock_ns(vm_clock));
683
            SET_LAST_RUN_CLOCK(s);
684
            s->usbsts &= ~USBSTS_HALT;
685
        }
686

  
687
        if (!(val & USBCMD_RUNSTOP) && (s->usbcmd & USBCMD_RUNSTOP)) {
688
            DPRINTF("                         ** STOP **\n");
689
            qemu_del_timer(s->frame_timer);
690
            // TODO - should finish out some stuff before setting halt
691
            s->usbsts |= USBSTS_HALT;
692
        }
693

  
694
        if (val & USBCMD_HCRESET) {
695
            DPRINTF("ehci_mem_writel: %s run, resetting\n", str);
696
            ehci_reset(s);
697
            val &= ~USBCMD_HCRESET;
698
        }
699

  
700
        /* not supporting dynamic frame list size at the moment */
701
        if ((val & USBCMD_FLS) && !(s->usbcmd & USBCMD_FLS)) {
702
            fprintf(stderr, "attempt to set frame list size -- value %d\n",
703
                    val & USBCMD_FLS);
704
            val &= ~USBCMD_FLS;
705
        }
706
#if EHCI_DEBUG
707
        if ((val & USBCMD_PSE) && !(s->usbcmd & USBCMD_PSE)) {
708
            DPRINTF("periodic scheduling enabled\n");
709
        }
710
        if (!(val & USBCMD_PSE) && (s->usbcmd & USBCMD_PSE)) {
711
            DPRINTF("periodic scheduling disabled\n");
712
        }
713
        if ((val & USBCMD_ASE) && !(s->usbcmd & USBCMD_ASE)) {
714
            DPRINTF("asynchronous scheduling enabled\n");
715
        }
716
        if (!(val & USBCMD_ASE) && (s->usbcmd & USBCMD_ASE)) {
717
            DPRINTF("asynchronous scheduling disabled\n");
718
        }
719
        if ((val & USBCMD_IAAD) && !(s->usbcmd & USBCMD_IAAD)) {
720
            DPRINTF("doorbell request received\n");
721
        }
722
        if ((val & USBCMD_LHCR) && !(s->usbcmd & USBCMD_LHCR)) {
723
            DPRINTF("light host controller reset received\n");
724
        }
725
        if ((val & USBCMD_ITC) != (s->usbcmd & USBCMD_ITC)) {
726
            DPRINTF("interrupt threshold control set to %x\n",
727
                    (val & USBCMD_ITC)>>USBCMD_ITC_SH);
728
        }
729
#endif
730
        break;
731

  
732

  
733
    case USBSTS:
734
        val &= USBSTS_RO_MASK;              // bits 6 thru 31 are RO
735
        DPRINTF("ehci_mem_writel: %s RWC set to 0x%08X\n", str, val);
736

  
737
        val = (s->usbsts &= ~val);         // bits 0 thru 5 are R/WC
738

  
739
        DPRINTF("ehci_mem_writel: %s updating interrupt condition\n", str);
740
        ehci_set_interrupt(s, 0);
741
        break;
742

  
743

  
744
    case USBINTR:
745
        val &= USBINTR_MASK;
746
        DPRINTF("ehci_mem_writel: %s set to 0x%08X\n", str, val);
747
        break;
748

  
749
    case FRINDEX:
750
        s->sofv = val >> 3;
751
        DPRINTF("ehci_mem_writel: %s set to 0x%08X\n", str, val);
752
        break;
753

  
754
    case CONFIGFLAG:
755
        DPRINTF("ehci_mem_writel: %s set to 0x%08X\n", str, val);
756
        val &= 0x1;
757
        if (val) {
758
            for(i = 0; i < NB_PORTS; i++)
759
                s->portsc[i] &= ~PORTSC_POWNER;
760
        }
761
        break;
762

  
763
    case PERIODICLISTBASE:
764
        if ((s->usbcmd & USBCMD_PSE) && (s->usbcmd & USBCMD_RUNSTOP)) {
765
            fprintf(stderr,
766
              "ehci: PERIODIC list base register set while periodic schedule\n"
767
              "      is enabled and HC is enabled\n");
768
        }
769
        DPRINTF("ehci_mem_writel: P-LIST BASE set to 0x%08X\n", val);
770
        break;
771

  
772
    case ASYNCLISTADDR:
773
        if ((s->usbcmd & USBCMD_ASE) && (s->usbcmd & USBCMD_RUNSTOP)) {
774
            fprintf(stderr,
775
              "ehci: ASYNC list address register set while async schedule\n"
776
              "      is enabled and HC is enabled\n");
777
        }
778
        DPRINTF("ehci_mem_writel: A-LIST ADDR set to 0x%08X\n", val);
779
        break;
780
    }
781

  
782
    *(uint32_t *)(&s->mmio[addr]) = val;
783
}
784

  
785

  
786
// TODO : Put in common header file, duplication from usb-ohci.c
787

  
788
/* Get an array of dwords from main memory */
789
static inline int get_dwords(uint32_t addr, uint32_t *buf, int num)
790
{
791
    int i;
792

  
793
    for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
794
        cpu_physical_memory_rw(addr,(uint8_t *)buf, sizeof(*buf), 0);
795
        *buf = le32_to_cpu(*buf);
796
    }
797

  
798
    return 1;
799
}
800

  
801
/* Put an array of dwords in to main memory */
802
static inline int put_dwords(uint32_t addr, uint32_t *buf, int num)
803
{
804
    int i;
805

  
806
    for(i = 0; i < num; i++, buf++, addr += sizeof(*buf)) {
807
        uint32_t tmp = cpu_to_le32(*buf);
808
        cpu_physical_memory_rw(addr,(uint8_t *)&tmp, sizeof(tmp), 1);
809
    }
810

  
811
    return 1;
812
}
813

  
814
// 4.10.2
815

  
816
static int ehci_qh_do_overlay(EHCIState *ehci, EHCIqh *qh, EHCIqtd *qtd)
817
{
818
    int i;
819
    int dtoggle;
820
    int ping;
821
    int eps;
822
    int reload;
823

  
824
    // remember values in fields to preserve in qh after overlay
825

  
826
    dtoggle = qh->token & QTD_TOKEN_DTOGGLE;
827
    ping    = qh->token & QTD_TOKEN_PING;
828

  
829
    DPRINTF("setting qh.current from %08X to 0x%08X\n", qh->current_qtd,
830
            ehci->qtdaddr);
831
    qh->current_qtd = ehci->qtdaddr;
832
    qh->next_qtd    = qtd->next;
833
    qh->altnext_qtd = qtd->altnext;
834
    qh->token       = qtd->token;
835

  
836

  
837
    eps = get_field(qh->epchar, QH_EPCHAR_EPS);
838
    if (eps == EHCI_QH_EPS_HIGH) {
839
        qh->token &= ~QTD_TOKEN_PING;
840
        qh->token |= ping;
841
    }
842

  
843
    reload = get_field(qh->epchar, QH_EPCHAR_RL);
844
    set_field(&qh->altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
845

  
846
    for (i = 0; i < 5; i++) {
847
        qh->bufptr[i] = qtd->bufptr[i];
848
    }
849

  
850
    if (!(qh->epchar & QH_EPCHAR_DTC)) {
851
        // preserve QH DT bit
852
        qh->token &= ~QTD_TOKEN_DTOGGLE;
853
        qh->token |= dtoggle;
854
    }
855

  
856
    qh->bufptr[1] &= ~BUFPTR_CPROGMASK_MASK;
857
    qh->bufptr[2] &= ~BUFPTR_FRAMETAG_MASK;
858

  
859
    put_dwords(NLPTR_GET(ehci->qhaddr), (uint32_t *) qh, sizeof(EHCIqh) >> 2);
860

  
861
    return 0;
862
}
863

  
864
static int ehci_buffer_rw(uint8_t *buffer, EHCIqh *qh, int bytes, int rw)
865
{
866
    int bufpos = 0;
867
    int cpage, offset;
868
    uint32_t head;
869
    uint32_t tail;
870

  
871

  
872
    if (!bytes) {
873
        return 0;
874
    }
875

  
876
    cpage = get_field(qh->token, QTD_TOKEN_CPAGE);
877
    if (cpage > 4) {
878
        fprintf(stderr, "cpage out of range (%d)\n", cpage);
879
        return USB_RET_PROCERR;
880
    }
881

  
882
    offset = qh->bufptr[0] & ~QTD_BUFPTR_MASK;
883
    DPRINTF("ehci_buffer_rw: %sing %d bytes %08x cpage %d offset %d\n",
884
           rw ? "writ" : "read", bytes, qh->bufptr[0], cpage, offset);
885

  
886
    do {
887
        /* start and end of this page */
888
        head = qh->bufptr[cpage] & QTD_BUFPTR_MASK;
889
        tail = head + ~QTD_BUFPTR_MASK + 1;
890
        /* add offset into page */
891
        head |= offset;
892

  
893
        if (bytes <= (tail - head)) {
894
            tail = head + bytes;
895
        }
896

  
897
        DPRINTF("DATA %s cpage:%d head:%08X tail:%08X target:%08X\n",
898
                rw ? "WRITE" : "READ ", cpage, head, tail, bufpos);
899

  
900
        cpu_physical_memory_rw(head, &buffer[bufpos], tail - head, rw);
901

  
902
        bufpos += (tail - head);
903
        bytes -= (tail - head);
904

  
905
        if (bytes > 0) {
906
            cpage++;
907
            offset = 0;
908
        }
909
    } while (bytes > 0);
910

  
911
    /* save cpage */
912
    set_field(&qh->token, cpage, QTD_TOKEN_CPAGE);
913

  
914
    /* save offset into cpage */
915
    offset = tail - head;
916
    qh->bufptr[0] &= ~QTD_BUFPTR_MASK;
917
    qh->bufptr[0] |= offset;
918

  
919
    return 0;
920
}
921

  
922
static void ehci_async_complete_packet(USBDevice *dev, USBPacket *packet)
923
{
924
    EHCIState *ehci = container_of(packet, EHCIState, usb_packet);
925

  
926
    DPRINTF("Async packet complete\n");
927
    ehci->async_complete = 1;
928
    ehci->exec_status = packet->len;
929
}
930

  
931
static int ehci_execute_complete(EHCIState *ehci, EHCIqh *qh, int ret)
932
{
933
    int c_err, reload;
934

  
935
    if (ret == USB_RET_ASYNC && !ehci->async_complete) {
936
        DPRINTF("not done yet\n");
937
        return ret;
938
    }
939

  
940
    ehci->async_complete = 0;
941

  
942
    DPRINTF("execute_complete: qhaddr 0x%x, next %x, qtdaddr 0x%x, status %d\n",
943
            ehci->qhaddr, qh->next, ehci->qtdaddr, ret);
944

  
945
    if (ret < 0) {
946
err:
947
        /* TO-DO: put this is in a function that can be invoked below as well */
948
        c_err = get_field(qh->token, QTD_TOKEN_CERR);
949
        c_err--;
950
        set_field(&qh->token, c_err, QTD_TOKEN_CERR);
951

  
952
        switch(ret) {
953
        case USB_RET_NODEV:
954
            fprintf(stderr, "USB no device\n");
955
            break;
956
        case USB_RET_STALL:
957
            fprintf(stderr, "USB stall\n");
958
            qh->token |= QTD_TOKEN_HALT;
959
            ehci_record_interrupt(ehci, USBSTS_ERRINT);
960
            break;
961
        case USB_RET_NAK:
962
            /* 4.10.3 */
963
            reload = get_field(qh->epchar, QH_EPCHAR_RL);
964
            if ((ehci->pid == USB_TOKEN_IN) && reload) {
965
                int nakcnt = get_field(qh->altnext_qtd, QH_ALTNEXT_NAKCNT);
966
                nakcnt--;
967
                set_field(&qh->altnext_qtd, nakcnt, QH_ALTNEXT_NAKCNT);
968
            } else if (!reload) {
969
                return USB_RET_NAK;
970
            }
971
            break;
972
        case USB_RET_BABBLE:
973
            fprintf(stderr, "USB babble TODO\n");
974
            qh->token |= QTD_TOKEN_BABBLE;
975
            ehci_record_interrupt(ehci, USBSTS_ERRINT);
976
            break;
977
        default:
978
            fprintf(stderr, "USB invalid response %d to handle\n", ret);
979
            /* TO-DO: transaction error */
980
            ret = USB_RET_PROCERR;
981
            break;
982
        }
983
    } else {
984
        // DPRINTF("Short packet condition\n");
985
        // TODO check 4.12 for splits
986

  
987
        if ((ret > ehci->tbytes) && (ehci->pid == USB_TOKEN_IN)) {
988
            ret = USB_RET_BABBLE;
989
            goto err;
990
        }
991

  
992
        if (ehci->tbytes && ehci->pid == USB_TOKEN_IN) {
993
            if (ehci_buffer_rw(ehci->buffer, qh, ret, 1) != 0) {
994
                return USB_RET_PROCERR;
995
            }
996
            ehci->tbytes -= ret;
997
        } else {
998
            ehci->tbytes = 0;
999
        }
1000

  
1001
        DPRINTF("updating tbytes to %d\n", ehci->tbytes);
1002
        set_field(&qh->token, ehci->tbytes, QTD_TOKEN_TBYTES);
1003
    }
1004

  
1005
    qh->token ^= QTD_TOKEN_DTOGGLE;
1006
    qh->token &= ~QTD_TOKEN_ACTIVE;
1007

  
1008
    if ((ret >= 0) && (qh->token & QTD_TOKEN_IOC)) {
1009
        ehci_record_interrupt(ehci, USBSTS_INT);
1010
    }
1011

  
1012
    return ret;
1013
}
1014

  
1015
// 4.10.3
1016

  
1017
static int ehci_execute(EHCIState *ehci, EHCIqh *qh)
1018
{
1019
    USBPort *port;
1020
    USBDevice *dev;
1021
    int ret;
1022
    int i;
1023
    int endp;
1024
    int devadr;
1025

  
1026
    if ( !(qh->token & QTD_TOKEN_ACTIVE)) {
1027
        fprintf(stderr, "Attempting to execute inactive QH\n");
1028
        return USB_RET_PROCERR;
1029
    }
1030

  
1031
    ehci->tbytes = (qh->token & QTD_TOKEN_TBYTES_MASK) >> QTD_TOKEN_TBYTES_SH;
1032
    if (ehci->tbytes > BUFF_SIZE) {
1033
        fprintf(stderr, "Request for more bytes than allowed\n");
1034
        return USB_RET_PROCERR;
1035
    }
1036

  
1037
    ehci->pid = (qh->token & QTD_TOKEN_PID_MASK) >> QTD_TOKEN_PID_SH;
1038
    switch(ehci->pid) {
1039
        case 0: ehci->pid = USB_TOKEN_OUT; break;
1040
        case 1: ehci->pid = USB_TOKEN_IN; break;
1041
        case 2: ehci->pid = USB_TOKEN_SETUP; break;
1042
        default: fprintf(stderr, "bad token\n"); break;
1043
    }
1044

  
1045
    if ((ehci->tbytes && ehci->pid != USB_TOKEN_IN) &&
1046
        (ehci_buffer_rw(ehci->buffer, qh, ehci->tbytes, 0) != 0)) {
1047
        return USB_RET_PROCERR;
1048
    }
1049

  
1050
    endp = get_field(qh->epchar, QH_EPCHAR_EP);
1051
    devadr = get_field(qh->epchar, QH_EPCHAR_DEVADDR);
1052

  
1053
    ret = USB_RET_NODEV;
1054

  
1055
    // TO-DO: associating device with ehci port
1056
    for(i = 0; i < NB_PORTS; i++) {
1057
        port = &ehci->ports[i];
1058
        dev = port->dev;
1059

  
1060
        // TODO sometime we will also need to check if we are the port owner
1061

  
1062
        if (!(ehci->portsc[i] &(PORTSC_CONNECT))) {
1063
            DPRINTF("Port %d, no exec, not connected(%08X)\n",
1064
                    i, ehci->portsc[i]);
1065
            continue;
1066
        }
1067

  
1068
        ehci->usb_packet.pid = ehci->pid;
1069
        ehci->usb_packet.devaddr = devadr;
1070
        ehci->usb_packet.devep = endp;
1071
        ehci->usb_packet.data = ehci->buffer;
1072
        ehci->usb_packet.len = ehci->tbytes;
1073

  
1074
        ret = usb_handle_packet(dev, &ehci->usb_packet);
1075

  
1076
        DPRINTF("submit: qh %x next %x qtd %x pid %x len %d (total %d) endp %x ret %d\n",
1077
                ehci->qhaddr, qh->next, ehci->qtdaddr, ehci->pid,
1078
                ehci->usb_packet.len, ehci->tbytes, endp, ret);
1079

  
1080
        if (ret != USB_RET_NODEV) {
1081
            break;
1082
        }
1083
    }
1084

  
1085
    if (ret > BUFF_SIZE) {
1086
        fprintf(stderr, "ret from usb_handle_packet > BUFF_SIZE\n");
1087
        return USB_RET_PROCERR;
1088
    }
1089

  
1090
    if (ret == USB_RET_ASYNC) {
1091
        ehci->async_complete = 0;
1092
    }
1093

  
1094
    return ret;
1095
}
1096

  
1097
/*  4.7.2
1098
 */
1099

  
1100
static int ehci_process_itd(EHCIState *ehci,
1101
                            EHCIitd *itd)
1102
{
1103
    USBPort *port;
1104
    USBDevice *dev;
1105
    int ret;
1106
    int i, j;
1107
    int ptr;
1108
    int pid;
1109
    int pg;
1110
    int len;
1111
    int dir;
1112
    int devadr;
1113
    int endp;
1114
    int maxpkt;
1115

  
1116
    dir =(itd->bufptr[1] & ITD_BUFPTR_DIRECTION);
1117
    devadr = get_field(itd->bufptr[0], ITD_BUFPTR_DEVADDR);
1118
    endp = get_field(itd->bufptr[0], ITD_BUFPTR_EP);
1119
    maxpkt = get_field(itd->bufptr[1], ITD_BUFPTR_MAXPKT);
1120

  
1121
    for(i = 0; i < 8; i++) {
1122
        if (itd->transact[i] & ITD_XACT_ACTIVE) {
1123
            DPRINTF("ISOCHRONOUS active for frame %d, interval %d\n",
1124
                    ehci->frindex >> 3, i);
1125

  
1126
            pg = get_field(itd->transact[i], ITD_XACT_PGSEL);
1127
            ptr = (itd->bufptr[pg] & ITD_BUFPTR_MASK) |
1128
                (itd->transact[i] & ITD_XACT_OFFSET_MASK);
1129
            len = get_field(itd->transact[i], ITD_XACT_LENGTH);
1130

  
1131
            if (len > BUFF_SIZE) {
1132
                return USB_RET_PROCERR;
1133
            }
1134

  
1135
            DPRINTF("ISOCH: buffer %08X len %d\n", ptr, len);
1136

  
1137
            if (!dir) {
1138
                cpu_physical_memory_rw(ptr, &ehci->buffer[0], len, 0);
1139
                pid = USB_TOKEN_OUT;
1140
            } else
1141
                pid = USB_TOKEN_IN;
1142

  
1143
            ret = USB_RET_NODEV;
1144

  
1145
            for (j = 0; j < NB_PORTS; j++) {
1146
                port = &ehci->ports[j];
1147
                dev = port->dev;
1148

  
1149
                // TODO sometime we will also need to check if we are the port owner
1150

  
1151
                if (!(ehci->portsc[j] &(PORTSC_CONNECT))) {
1152
                    DPRINTF("Port %d, no exec, not connected(%08X)\n",
1153
                            j, ehci->portsc[j]);
1154
                    continue;
1155
                }
1156

  
1157
                ehci->usb_packet.pid = ehci->pid;
1158
                ehci->usb_packet.devaddr = devadr;
1159
                ehci->usb_packet.devep = endp;
1160
                ehci->usb_packet.data = ehci->buffer;
1161
                ehci->usb_packet.len = len;
1162

  
1163
                DPRINTF("calling usb_handle_packet\n");
1164
                ret = usb_handle_packet(dev, &ehci->usb_packet);
1165

  
1166
                if (ret != USB_RET_NODEV) {
1167
                    break;
1168
                }
1169
            }
1170

  
1171
            /*  In isoch, there is no facility to indicate a NAK so let's
1172
             *  instead just complete a zero-byte transaction.  Setting
1173
             *  DBERR seems too draconian.
1174
             */
1175

  
1176
            if (ret == USB_RET_NAK) {
1177
                if (ehci->isoch_pause > 0) {
1178
                    DPRINTF("ISOCH: received a NAK but paused so returning\n");
1179
                    ehci->isoch_pause--;
1180
                    return 0;
1181
                } else if (ehci->isoch_pause == -1) {
1182
                    DPRINTF("ISOCH: recv NAK & isoch pause inactive, setting\n");
1183
                    // Pause frindex for up to 50 msec waiting for data from
1184
                    // remote
1185
                    ehci->isoch_pause = 50;
1186
                    return 0;
1187
                } else {
1188
                    DPRINTF("ISOCH: isoch pause timeout! return 0\n");
1189
                    ret = 0;
1190
                }
1191
            } else {
1192
                DPRINTF("ISOCH: received ACK, clearing pause\n");
1193
                ehci->isoch_pause = -1;
1194
            }
1195

  
1196
            if (ret >= 0) {
1197
                itd->transact[i] &= ~ITD_XACT_ACTIVE;
1198

  
1199
                if (itd->transact[i] & ITD_XACT_IOC) {
1200
                    ehci_record_interrupt(ehci, USBSTS_INT);
1201
                }
1202
            }
1203

  
1204
            if (ret >= 0 && dir) {
1205
                cpu_physical_memory_rw(ptr, &ehci->buffer[0], len, 1);
1206

  
1207
                if (ret != len) {
1208
                    DPRINTF("ISOCH IN expected %d, got %d\n",
1209
                            len, ret);
1210
                    set_field(&itd->transact[i], ret, ITD_XACT_LENGTH);
1211
                }
1212
            }
1213
        }
1214
    }
1215
    return 0;
1216
}
1217

  
1218
/*  This state is the entry point for asynchronous schedule
1219
 *  processing.  Entry here consitutes a EHCI start event state (4.8.5)
1220
 */
1221
static int ehci_state_waitlisthead(EHCIState *ehci,  int async, int *state)
1222
{
1223
    EHCIqh *qh = &ehci->qh;
1224
    int i = 0;
1225
    int again = 0;
1226
    uint32_t entry = ehci->asynclistaddr;
1227

  
1228
    /* set reclamation flag at start event (4.8.6) */
1229
    if (async) {
1230
        ehci->usbsts |= USBSTS_REC;
1231
    }
1232

  
1233
    /*  Find the head of the list (4.9.1.1) */
1234
    for(i = 0; i < MAX_QH; i++) {
1235
        get_dwords(NLPTR_GET(entry), (uint32_t *) qh, sizeof(EHCIqh) >> 2);
1236

  
1237
        if (qh->epchar & QH_EPCHAR_H) {
1238
            DPRINTF_ST("WAITLISTHEAD: QH %08X is the HEAD of the list\n",
1239
                       entry);
1240
            if (async) {
1241
                entry |= (NLPTR_TYPE_QH << 1);
1242
            }
1243

  
1244
            ehci->fetch_addr = entry;
1245
            *state = EST_FETCHENTRY;
1246
            again = 1;
1247
            goto out;
1248
        }
1249

  
1250
        DPRINTF_ST("WAITLISTHEAD: QH %08X is NOT the HEAD of the list\n",
1251
                   entry);
1252
        entry = qh->next;
1253
        if (entry == ehci->asynclistaddr) {
1254
            DPRINTF("WAITLISTHEAD: reached beginning of QH list\n");
1255
            break;
1256
        }
1257
    }
1258

  
1259
    /* no head found for list. */
1260

  
1261
    *state = EST_ACTIVE;
1262

  
1263
out:
1264
    return again;
1265
}
1266

  
1267

  
1268
/*  This state is the entry point for periodic schedule processing as
1269
 *  well as being a continuation state for async processing.
1270
 */
1271
static int ehci_state_fetchentry(EHCIState *ehci, int async, int *state)
1272
{
1273
    int again = 0;
1274
    uint32_t entry = ehci->fetch_addr;
1275

  
1276
#if EHCI_DEBUG == 0
1277
    if (qemu_get_clock_ns(vm_clock) / 1000 >= ehci->frame_end_usec) {
1278
        if (async) {
1279
            DPRINTF("FETCHENTRY: FRAME timer elapsed, exit state machine\n");
1280
            goto out;
1281
        } else {
1282
            DPRINTF("FETCHENTRY: WARNING "
1283
                    "- frame timer elapsed during periodic\n");
1284
        }
1285
    }
1286
#endif
1287
    if (entry < 0x1000) {
1288
        DPRINTF("fetchentry: entry invalid (0x%08x)\n", entry);
1289
        *state = EST_ACTIVE;
1290
        goto out;
1291
    }
1292

  
1293
    /* section 4.8, only QH in async schedule */
1294
    if (async && (NLPTR_TYPE_GET(entry) != NLPTR_TYPE_QH)) {
1295
        fprintf(stderr, "non queue head request in async schedule\n");
1296
        return -1;
1297
    }
1298

  
1299
    switch (NLPTR_TYPE_GET(entry)) {
1300
    case NLPTR_TYPE_QH:
1301
        DPRINTF_ST("FETCHENTRY: entry %X is a Queue Head\n", entry);
1302
        *state = EST_FETCHQH;
1303
        ehci->qhaddr = entry;
1304
        again = 1;
1305
        break;
1306

  
1307
    case NLPTR_TYPE_ITD:
1308
        DPRINTF_ST("FETCHENTRY: entry %X is an ITD\n", entry);
1309
        *state = EST_FETCHITD;
1310
        ehci->itdaddr = entry;
1311
        again = 1;
1312
        break;
1313

  
1314
    default:
1315
        // TODO: handle siTD and FSTN types
1316
        fprintf(stderr, "FETCHENTRY: entry at %X is of type %d "
1317
                "which is not supported yet\n", entry, NLPTR_TYPE_GET(entry));
1318
        return -1;
1319
    }
1320

  
1321
out:
1322
    return again;
1323
}
1324

  
1325
static int ehci_state_fetchqh(EHCIState *ehci, int async, int *state)
1326
{
1327
    EHCIqh *qh = &ehci->qh;
1328
    int reload;
1329
    int again = 0;
1330

  
1331
    get_dwords(NLPTR_GET(ehci->qhaddr), (uint32_t *) qh, sizeof(EHCIqh) >> 2);
1332

  
1333
    if (async && (qh->epchar & QH_EPCHAR_H)) {
1334

  
1335
        /*  EHCI spec version 1.0 Section 4.8.3 & 4.10.1 */
1336
        if (ehci->usbsts & USBSTS_REC) {
1337
            ehci->usbsts &= ~USBSTS_REC;
1338
        } else {
1339
            DPRINTF("FETCHQH:  QH 0x%08x. H-bit set, reclamation status reset"
1340
                       " - done processing\n", ehci->qhaddr);
1341
            *state = EST_ACTIVE;
1342
            goto out;
1343
        }
1344
    }
1345

  
1346
#if EHCI_DEBUG
1347
    if (ehci->qhaddr != qh->next) {
1348
    DPRINTF("FETCHQH:  QH 0x%08x (h %x halt %x active %x) next 0x%08x\n",
1349
               ehci->qhaddr,
1350
               qh->epchar & QH_EPCHAR_H,
1351
               qh->token & QTD_TOKEN_HALT,
1352
               qh->token & QTD_TOKEN_ACTIVE,
1353
               qh->next);
1354
    }
1355
#endif
1356

  
1357
    reload = get_field(qh->epchar, QH_EPCHAR_RL);
1358
    if (reload) {
1359
        DPRINTF_ST("FETCHQH: reloading nakcnt to %d\n", reload);
1360
        set_field(&qh->altnext_qtd, reload, QH_ALTNEXT_NAKCNT);
1361
    }
1362

  
1363
    if (qh->token & QTD_TOKEN_HALT) {
1364
        DPRINTF_ST("FETCHQH: QH Halted, go horizontal\n");
1365
        *state = EST_HORIZONTALQH;
1366
        again = 1;
1367

  
1368
    } else if ((qh->token & QTD_TOKEN_ACTIVE) && (qh->current_qtd > 0x1000)) {
1369
        DPRINTF_ST("FETCHQH: Active, !Halt, execute - fetch qTD\n");
1370
        ehci->qtdaddr = qh->current_qtd;
1371
        *state = EST_FETCHQTD;
1372
        again = 1;
1373

  
1374
    } else {
1375
        /*  EHCI spec version 1.0 Section 4.10.2 */
1376
        DPRINTF_ST("FETCHQH: !Active, !Halt, advance queue\n");
1377
        *state = EST_ADVANCEQUEUE;
1378
        again = 1;
1379
    }
1380

  
1381
out:
1382
    return again;
1383
}
1384

  
1385
static int ehci_state_fetchitd(EHCIState *ehci, int async, int *state)
1386
{
1387
    EHCIitd itd;
1388

  
1389
    get_dwords(NLPTR_GET(ehci->itdaddr),(uint32_t *) &itd,
1390
               sizeof(EHCIitd) >> 2);
1391
    DPRINTF_ST("FETCHITD: Fetched ITD at address %08X " "(next is %08X)\n",
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff