Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3520 clevermous 1
; Code for OHCI controllers.
2
; Note: it should be moved to an external driver,
3
; it was convenient to have this code compiled into the kernel during initial
4
; development, but there are no reasons to keep it here.
5
 
6
; =============================================================================
7
; ================================= Constants =================================
8
; =============================================================================
9
; OHCI register declarations
10
; All of the registers should be read and written as Dwords.
11
; Partition 1. Control and Status registers.
12
OhciRevisionReg         = 0
13
OhciControlReg          = 4
14
OhciCommandStatusReg    = 8
15
OhciInterruptStatusReg  = 0Ch
16
OhciInterruptEnableReg  = 10h
17
OhciInterruptDisableReg = 14h
18
; Partition 2. Memory Pointer registers.
19
OhciHCCAReg             = 18h
20
OhciPeriodCurrentEDReg  = 1Ch
21
OhciControlHeadEDReg    = 20h
22
OhciControlCurrentEDReg = 24h
23
OhciBulkHeadEDReg       = 28h
24
OhciBulkCurrentEDReg    = 2Ch
25
OhciDoneHeadReg         = 30h
26
; Partition 3. Frame Counter registers.
27
OhciFmIntervalReg       = 34h
28
OhciFmRemainingReg      = 38h
29
OhciFmNumberReg         = 3Ch
30
OhciPeriodicStartReg    = 40h
31
OhciLSThresholdReg      = 44h
32
; Partition 4. Root Hub registers.
33
OhciRhDescriptorAReg    = 48h
34
OhciRhDescriptorBReg    = 4Ch
35
OhciRhStatusReg         = 50h
36
OhciRhPortStatusReg     = 54h
37
 
38
; =============================================================================
39
; ================================ Structures =================================
40
; =============================================================================
41
 
42
; OHCI-specific part of a pipe descriptor.
43
; * This structure corresponds to the Endpoint Descriptor aka ED from the OHCI
44
;   specification.
45
; * The hardware requires 16-bytes alignment of the hardware part.
46
;   Since the allocator (usb_allocate_common) allocates memory sequentially
47
;   from page start (aligned on 0x1000 bytes), size of the structure must be
48
;   divisible by 16.
49
struct ohci_pipe
50
; All addresses are physical.
51
Flags           dd      ?
52
; 1. Lower 7 bits (bits 0-6) are FunctionAddress. This is the USB address of
53
;    the function containing the endpoint that this ED controls.
54
; 2. Next 4 bits (bits 7-10) are EndpointNumber. This is the USB address of
55
;    the endpoint within the function.
56
; 3. Next 2 bits (bits 11-12) are Direction. This 2-bit field indicates the
57
;    direction of data flow: 1 = IN, 2 = OUT. If neither IN nor OUT is
58
;    specified, then the direction is determined from the PID field of the TD.
59
;    For CONTROL endpoints, the transfer direction is different
60
;    for different transfers, so the value of this field is 0
61
;    (3 would have the same effect) and the actual direction
62
;    of one transfer is encoded in the Transfer Descriptor.
63
; 4. Next bit (bit 13) is Speed bit. It indicates the speed of the endpoint:
64
;    full-speed (S = 0) or low-speed (S = 1).
65
; 5. Next bit (bit 14) is sKip bit. When this bit is set, the hardware
66
;    continues on to the next ED on the list without attempting access
67
;    to the TD queue or issuing any USB token for the endpoint.
68
;    Always cleared.
69
; 6. Next bit (bit 15) is Format bit. It must be 0 for Control, Bulk and
70
;    Interrupt endpoints and 1 for Isochronous endpoints.
71
; 7. Next 11 bits (bits 16-26) are MaximumPacketSize. This field indicates
72
;    the maximum number of bytes that can be sent to or received from the
73
;    endpoint in a single data packet.
74
TailP           dd      ?
75
; Physical address of the tail descriptor in the TD queue.
76
; The descriptor itself is not in the queue. See also HeadP.
77
HeadP           dd      ?
78
; 1. First bit (bit 0) is Halted bit. This bit is set by the hardware to
79
;    indicate that processing of the TD queue on the endpoint is halted.
80
; 2. Second bit (bit 1) is toggleCarry bit. Whenever a TD is retired, this
81
;    bit is written to contain the last data toggle value from the retired TD.
82
; 3. Next two bits (bits 2-3) are reserved and always zero.
83
; 4. With masked 4 lower bits, this is HeadP itself: physical address of the
84
;    head descriptor in the TD queue, that is, next TD to be processed for this
85
;    endpoint. Note that a TD must be 16-bytes aligned.
86
;    Empty queue is characterized by the condition HeadP == TailP.
87
NextED          dd      ?
88
; If nonzero, then this entry is a physical address of the next ED to be
89
; processed. See also the description before NextVirt field of the usb_pipe
90
; structure. Additionally to that description, the following is specific for
91
; the OHCI controller:
92
; * n=5, N=32, there are 32 "leaf" periodic lists.
93
; * The 1ms periodic list also serves Isochronous endpoints, which should be
94
;   in the end of the list.
95
; * There is no "next" list for Bulk and Control lists, they are processed
96
;   separately from others.
97
; * There is no "next" list for Periodic list for 1ms interval.
98
SoftwarePart    rd      sizeof.usb_pipe/4
99
; Software part, common for all controllers.
100
ends
101
 
102
if sizeof.ohci_pipe mod 16
103
.err ohci_pipe must be 16-bytes aligned
104
end if
105
 
106
; This structure describes the static head of every list of pipes.
107
; The hardware requires 16-bytes alignment of this structure.
108
; All instances of this structure are located sequentially in uhci_controller,
109
; uhci_controller is page-aligned, so it is sufficient to make this structure
110
; 16-bytes aligned and verify that the first instance is 16-bytes aligned
111
; inside uhci_controller.
112
struct ohci_static_ep
113
Flags           dd      ?
114
; Same as ohci_pipe.Flags.
115
; sKip bit is set, so the hardware ignores other fields except NextED.
116
                dd      ?
117
; Corresponds to ohci_pipe.TailP. Not used.
118
NextList        dd      ?
119
; Virtual address of the next list.
120
NextED          dd      ?
121
; Same as ohci_pipe.NextED.
122
SoftwarePart    rd      sizeof.usb_static_ep/4
123
; Software part, common for all controllers.
124
                dd      ?
125
; Padding for 16-bytes alignment.
126
ends
127
 
128
if sizeof.ohci_static_ep mod 16
129
.err ohci_static_ep must be 16-bytes aligned
130
end if
131
 
132
; OHCI-specific part of controller data.
133
; * The structure describes the memory area used for controller data,
134
;   additionally to the registers of the controller.
135
; * The structure includes two parts, the hardware part and the software part.
136
; * The hardware part consists of first 256 bytes and corresponds to
137
;   the HCCA from OHCI specification.
138
; * The hardware requires 256-bytes alignment of the hardware part, so
139
;   the entire descriptor must be 256-bytes aligned.
140
;   This structure is allocated with kernel_alloc (see usb_init_controller),
141
;   this gives page-aligned data.
142
; * The controller is described by both ohci_controller and usb_controller
143
;   structures, for each controller there is one ohci_controller and one
144
;   usb_controller structure. These structures are located sequentially
145
;   in the memory: beginning from some page start, there is ohci_controller
146
;   structure - this enforces hardware alignment requirements - and then
147
;   usb_controller structure.
148
; * The code keeps pointer to usb_controller structure. The ohci_controller
149
;   structure is addressed as [ptr + ohci_controller.field - sizeof.ohci_controller].
150
struct ohci_controller
151
; ------------------------------ hardware fields ------------------------------
152
InterruptTable  rd      32
153
; Pointers to interrupt EDs. The hardware starts processing of periodic lists
154
; within the frame N from the ED pointed to by [InterruptTable+(N and 31)*4].
155
; See also the description of periodic lists inside ohci_pipe structure.
156
FrameNumber     dw      ?
157
; The current frame number. This field is written by hardware only.
158
; This field is read by ohci_process_deferred and ohci_irq to
159
; communicate when control/bulk processing needs to be temporarily
160
; stopped/restarted.
161
                dw      ?
162
; Padding. Written as zero at every update of FrameNumber.
163
DoneHead        dd      ?
164
; Physical pointer to the start of Done Queue.
165
; When the hardware updates this field, it sets bit 0 to one if there is
166
; unmasked interrupt pending.
167
                rb      120
168
; Reserved for the hardware.
169
; ------------------------------ software fields ------------------------------
170
IntEDs          ohci_static_ep
171
                rb      62 * sizeof.ohci_static_ep
172
; Heads of 63 Periodic lists, see the description in usb_pipe.
173
ControlED       ohci_static_ep
174
; Head of Control list, see the description in usb_pipe.
175
BulkED          ohci_static_ep
176
; Head of Bulk list, see the description in usb_pipe.
177
MMIOBase        dd      ?
178
; Virtual address of memory-mapped area with OHCI registers OhciXxxReg.
179
PoweredUp       db      ?
180
; 1 in normal work, 0 during early phases of the initialization.
181
; This field is initialized to zero during memory allocation
182
; (see usb_init_controller), set to one by ohci_init when ports of the root hub
183
; are powered up, so connect/disconnect events can be handled.
184
                rb      3 ; alignment
185
DoneList        dd      ?
186
; List of descriptors which were processed by the controller and now need
187
; to be finalized.
188
DoneListEndPtr  dd      ?
189
; Pointer to dword which should receive a pointer to the next item in DoneList.
190
; If DoneList is empty, this is a pointer to DoneList itself;
191
; otherwise, this is a pointer to NextTD field of the last item in DoneList.
192
ends
193
 
194
if ohci_controller.IntEDs mod 16
195
.err Static endpoint descriptors must be 16-bytes aligned inside ohci_controller
196
end if
197
 
198
; OHCI general transfer descriptor.
199
; * The structure describes transfers to be performed on Control, Bulk or
200
;   Interrupt endpoints.
201
; * The structure includes two parts, the hardware part and the software part.
202
; * The hardware part consists of first 16 bytes and corresponds to
203
;   the General Transfer Descriptor aka general TD from OHCI specification.
204
; * The hardware requires 16-bytes alignment of the hardware part, so
205
;   the entire descriptor must be 16-bytes aligned. Since the allocator
206
;   (usb_allocate_common) allocates memory sequentially from page start
207
;   (aligned on 0x1000 bytes), size of the structure must be divisible by 16.
208
struct ohci_gtd
209
; ------------------------------ hardware fields ------------------------------
210
; All addresses in this part are physical.
211
Flags           dd      ?
212
; 1. Lower 18 bits (bits 0-17) are ignored and not modified by the hardware.
213
; 2. Next bit (bit 18) is bufferRounding bit. If this bit is 0, then the last
214
;    data packet must exactly fill the defined data buffer. If this bit is 1,
215
;    then the last data packet may be smaller than the defined buffer without
216
;    causing an error condition on the TD.
217
; 3. Next 2 bits (bits 19-20) are Direction field. This field indicates the
218
;    direction of data flow. If the Direction field in the ED is OUT or IN,
219
;    this field is ignored and the direction from the ED is used instead.
220
;    Otherwise, 0 = SETUP, 1 = OUT, 2 = IN, 3 is reserved.
221
; 4. Next 3 bits (bits 21-23) are DelayInterrupt field. This field contains
222
;    the interrupt delay count for this TD. When a TD is complete, the hardware
223
;    may wait up to DelayInterrupt frames before generating an interrupt.
224
;    If DelayInterrupt is 7 (maximum possible), then there is no interrupt
225
;    associated with completion of this TD.
226
; 5. Next 2 bits (bits 24-25) are DataToggle field. This field is used to
227
;    generate/compare the data PID value (DATA0 or DATA1). It is updated after
228
;    each successful transmission/reception of a data packet. The bit 25
229
;    is 0 when the data toggle value is acquired from the toggleCarry field in
230
;    the ED and 1 when the data toggle value is taken from the bit 24.
231
; 6. Next 2 bits (bits 26-27) are ErrorCount field. For each transmission
232
;    error, this value is incremented. If ErrorCount is 2 and another error
233
;    occurs, the TD is retired with error. When a transaction completes without
234
;    error, ErrorCount is reset to 0.
235
; 7. Upper 4 bits (bits 28-31) are ConditionCode field. This field contains
236
;    the status of the last attempted transaction, one of USB_STATUS_* values.
237
CurBufPtr       dd      ?
238
; Physical address of the next memory location that will be accessed for
239
; transfer to/from the endpoint. 0 means zero-length data packet or that all
240
; bytes have been transferred.
241
NextTD          dd      ?
242
; This field has different meanings depending on the status of the descriptor.
243
; When the descriptor is queued for processing, but not yet processed:
244
;   Physical address of the next TD for the endpoint.
245
; When the descriptor is processed by hardware, but not yet by software:
246
;   Physical address of the previous processed TD.
247
; When the descriptor is processed by the IRQ handler, but not yet completed:
248
;   Virtual pointer to the next processed TD.
249
BufEnd          dd      ?
250
; Physical address of the last byte in the buffer for this TD.
251
                dd      ?       ; padding for 16-bytes alignment
252
SoftwarePart    rd      sizeof.usb_gtd/4
253
; Common part for all controllers.
254
ends
255
 
256
if sizeof.ohci_gtd mod 16
257
.err ohci_gtd must be 16-bytes aligned
258
end if
259
 
260
; OHCI isochronous transfer descriptor.
261
; * The structure describes transfers to be performed on Isochronous endpoints.
262
; * The structure includes two parts, the hardware part and the software part.
263
; * The hardware part consists of first 32 bytes and corresponds to
264
;   the Isochronous Transfer Descriptor aka isochronous TD from OHCI
265
;   specification.
266
; * The hardware requires 32-bytes alignment of the hardware part, so
267
;   the entire descriptor must be 32-bytes aligned.
268
; * The isochronous endpoints are not supported yet, so only hardware part is
269
;   defined at the moment.
270
struct ohci_itd
271
StartingFrame   dw      ?
272
; This field contains the low order 16 bits of the frame number in which the
273
; first data packet of the Isochronous TD is to be sent.
274
Flags           dw      ?
275
; 1. Lower 5 bits (bits 0-4) are ignored and not modified by the hardware.
276
; 2. Next 3 bits (bits 5-7) are DelayInterrupt field.
277
; 3. Next 3 bits (bits 8-10) are FrameCount field. The TD describes
278
;    FrameCount+1 data packets.
279
; 4. Next bit (bit 11) is ignored and not modified by the hardware.
280
; 5. Upper 4 bits (bits 12-15) are ConditionCode field. This field contains
281
;    the completion code, one of USB_STATUS_* values, when the TD is moved to
282
;    the Done Queue.
283
BufPage0        dd      ?
284
; Lower 12 bits are ignored and not modified by the hardware.
285
; With masked 12 bits this field is the physical page containing all buffers.
286
NextTD          dd      ?
287
; Physical address of the next TD in the transfer queue.
288
BufEnd          dd      ?
289
; Physical address of the last byte in the buffer.
290
OffsetArray     rw      8
291
; Initialized by software, read by hardware: Offset for packet 0..7.
292
; Used to determine size and starting address of an isochronous data packet.
293
; Written by hardware, read by software: PacketStatusWord for packet 0..7.
294
; Contains completion code and, if applicable, size received for an isochronous
295
; data packet.
296
ends
297
 
298
; Description of OHCI-specific data and functions for
299
; controller-independent code.
300
; Implements the structure usb_hardware_func from hccommon.inc for OHCI.
301
iglobal
302
align 4
303
ohci_hardware_func:
304
        dd      'OHCI'
305
        dd      sizeof.ohci_controller
306
        dd      ohci_init
307
        dd      ohci_process_deferred
308
        dd      ohci_set_device_address
309
        dd      ohci_get_device_address
310
        dd      ohci_port_disable
311
        dd      ohci_new_port.reset
312
        dd      ohci_set_endpoint_packet_size
313
        dd      usb1_allocate_endpoint
314
        dd      usb1_free_endpoint
315
        dd      ohci_init_pipe
316
        dd      ohci_unlink_pipe
317
        dd      usb1_allocate_general_td
318
        dd      usb1_free_general_td
319
        dd      ohci_alloc_transfer
320
        dd      ohci_insert_transfer
321
        dd      ohci_new_device
322
endg
323
 
324
; =============================================================================
325
; =================================== Code ====================================
326
; =============================================================================
327
 
328
; Controller-specific initialization function.
329
; Called from usb_init_controller. Initializes the hardware and
330
; OHCI-specific parts of software structures.
331
; eax = pointer to ohci_controller to be initialized
332
; [ebp-4] = pcidevice
333
proc ohci_init
334
; inherit some variables from the parent (usb_init_controller)
335
.devfn   equ ebp - 4
336
.bus     equ ebp - 3
337
; 1. Store pointer to ohci_controller for further use.
338
        push    eax
339
        mov     edi, eax
340
; 2. Initialize hardware fields of ohci_controller.
341
; Namely, InterruptTable needs to be initialized with
342
; physical addresses of heads of first 32 Periodic lists.
343
; Note that all static heads fit in one page, so one call
344
; to get_pg_addr is sufficient.
345
if (ohci_controller.IntEDs / 0x1000) <> (ohci_controller.BulkED / 0x1000)
346
.err assertion failed
347
end if
348
if ohci_controller.IntEDs >= 0x1000
349
.err assertion failed
350
end if
351
        lea     esi, [eax+ohci_controller.IntEDs+32*sizeof.ohci_static_ep]
352
        call    get_pg_addr
353
        add     eax, ohci_controller.IntEDs
354
        push    32
355
        pop     ecx
356
        mov     edx, ecx
357
@@:
358
        stosd
359
        add     eax, sizeof.ohci_static_ep
360
        loop    @b
361
; 3. Initialize static heads ohci_controller.IntEDs, .ControlED, .BulkED.
362
; Use the loop over groups: first group consists of first 32 Periodic
363
; descriptors, next group consists of next 16 Periodic descriptors,
364
; ..., last group consists of the last Periodic descriptor.
365
; 3a. Prepare for the loop.
366
; make edi point to start of ohci_controller.IntEDs,
367
; other registers are already set.
368
; -128 fits in one byte, +128 does not fit.
369
        sub     edi, -128
370
; 3b. Loop over groups. On every iteration:
371
; edx = size of group, edi = pointer to the current group,
372
; esi = pointer to the next group, eax = physical address of the next group.
373
.init_static_eds:
374
; 3c. Get the size of the next group.
375
        shr     edx, 1
376
; 3d. Exit the loop if there is no next group.
377
        jz      .init_static_eds_done
378
; 3e. Initialize the first half of the current group.
379
; Advance edi to the second half.
380
        push    eax esi
381
        call    ohci_init_static_ep_group
382
        pop     esi eax
383
; 3f. Initialize the second half of the current group
384
; with the same values.
385
; Advance edi to the next group, esi/eax to the next of the next group.
386
        call    ohci_init_static_ep_group
387
        jmp     .init_static_eds
388
.init_static_eds_done:
389
; 3g. Initialize the head of the last Periodic list.
390
        xor     eax, eax
391
        xor     esi, esi
392
        call    ohci_init_static_endpoint
393
; 3i. Initialize the heads of Control and Bulk lists.
394
        call    ohci_init_static_endpoint
395
        call    ohci_init_static_endpoint
396
; 4. Create a virtual memory area to talk with the controller.
397
; 4a. Enable memory & bus master access.
398
        mov     ch, [.bus]
399
        mov     cl, 0
400
        mov     eax, ecx
401
        mov     bh, [.devfn]
402
        mov     bl, 4
403
        call    pci_read_reg
404
        or      al, 6
405
        xchg    eax, ecx
406
        call    pci_write_reg
407
; 4b. Read memory base address.
408
        mov     ah, [.bus]
409
        mov     al, 2
410
        mov     bl, 10h
411
        call    pci_read_reg
412
        and     al, not 0Fh
413
; 4c. Create mapping for physical memory. 256 bytes are sufficient.
414
        stdcall map_io_mem, eax, 100h, PG_SW+PG_NOCACHE
415
        test    eax, eax
416
        jz      .fail
417
        stosd   ; fill ohci_controller.MMIOBase
418
        xchg    eax, edi
419
; now edi = MMIOBase
420
; 5. Reset the controller if needed.
421
; 5a. Check operational state.
422
; 0 = reset, 1 = resume, 2 = operational, 3 = suspended
423
        mov     eax, [edi+OhciControlReg]
424
        and     al, 3 shl 6
425
        cmp     al, 2 shl 6
426
        jz      .operational
427
; 5b. State is not operational, reset is needed.
428
.reset:
429
; 5c. Save FmInterval register.
430
        pushd   [edi+OhciFmIntervalReg]
431
; 5d. Issue software reset and wait up to 10ms, checking status every 1 ms.
432
        push    1
433
        pop     ecx
434
        push    10
435
        pop     edx
436
        mov     [edi+OhciCommandStatusReg], ecx
437
@@:
438
        mov     esi, ecx
439
        call    delay_ms
440
        test    [edi+OhciCommandStatusReg], ecx
441
        jz      .resetdone
442
        dec     edx
443
        jnz     @b
444
        pop     eax
445
        dbgstr 'controller reset timeout'
446
        jmp     .fail_unmap
447
.resetdone:
448
; 5e. Restore FmInterval register.
449
        pop     eax
450
        mov     edx, eax
451
        and     edx, 3FFFh
452
        jz      .setfminterval
453
        cmp     dx, 2EDFh       ; default value?
454
        jnz     @f              ; assume that BIOS has configured the value
455
.setfminterval:
456
        mov     eax, 27792EDFh  ; default value
457
@@:
458
        mov     [edi+OhciFmIntervalReg], eax
459
; 5f. Set PeriodicStart to 90% of FmInterval.
460
        movzx   eax, ax
461
; Two following lines are equivalent to eax = floor(eax * 0.9)
462
; for any 0 <= eax < 1C71C71Dh, which of course is far from maximum 0FFFFh.
463
        mov     edx, 0E6666667h
464
        mul     edx
465
        mov     [edi+OhciPeriodicStartReg], edx
466
.operational:
467
; 6. Setup controller registers.
468
        pop     esi     ; restore pointer to ohci_controller saved in step 1
469
; 6a. Physical address of HCCA.
470
        mov     eax, esi
471
        call    get_pg_addr
472
        mov     [edi+OhciHCCAReg], eax
473
; 6b. Transition to operational state and clear all Enable bits.
474
        mov     cl, 2 shl 6
475
        mov     [edi+OhciControlReg], ecx
476
; 6c. Physical addresses of head of Control and Bulk lists.
477
if ohci_controller.BulkED >= 0x1000
478
.err assertion failed
479
end if
480
        add     eax, ohci_controller.ControlED
481
        mov     [edi+OhciControlHeadEDReg], eax
482
        add     eax, ohci_controller.BulkED - ohci_controller.ControlED
483
        mov     [edi+OhciBulkHeadEDReg], eax
484
; 6d. Zero Head registers: there are no active Control and Bulk descriptors yet.
485
        xor     eax, eax
486
;       mov     [edi+OhciPeriodCurrentEDReg], eax
487
        mov     [edi+OhciControlCurrentEDReg], eax
488
        mov     [edi+OhciBulkCurrentEDReg], eax
489
;       mov     [edi+OhciDoneHeadReg], eax
490
; 6e. Enable processing of all lists with control:bulk ratio = 1:1.
491
        mov     dword [edi+OhciControlReg], 10111100b
492
; 7. Get number of ports.
493
        add     esi, sizeof.ohci_controller
494
        mov     eax, [edi+OhciRhDescriptorAReg]
495
        and     eax, 0xF
496
        mov     [esi+usb_controller.NumPorts], eax
497
; 8. Initialize DoneListEndPtr to point to DoneList.
498
        lea     eax, [esi+ohci_controller.DoneList-sizeof.ohci_controller]
499
        mov     [esi+ohci_controller.DoneListEndPtr-sizeof.ohci_controller], eax
500
; 9. Hook interrupt.
501
        mov     ah, [.bus]
502
        mov     al, 0
503
        mov     bh, [.devfn]
504
        mov     bl, 3Ch
505
        call    pci_read_reg
506
; al = IRQ
507
        movzx   eax, al
508
        stdcall attach_int_handler, eax, ohci_irq, esi
509
; 10. Enable controller interrupt on HcDoneHead writeback and RootHubStatusChange.
510
        mov     dword [edi+OhciInterruptEnableReg], 80000042h
511
        DEBUGF 1,'K : OHCI controller at %x:%x with %d ports initialized\n',[.bus]:2,[.devfn]:2,[esi+usb_controller.NumPorts]
512
; 11. Initialize ports of the controller.
513
; 11a. Initiate power up, disable all ports, clear all "changed" bits.
514
        mov     dword [edi+OhciRhStatusReg], 10000h     ; SetGlobalPower
515
        xor     ecx, ecx
516
@@:
517
        mov     dword [edi+OhciRhPortStatusReg+ecx*4], 1F0101h  ; SetPortPower+ClearPortEnable+clear "changed" bits
518
        inc     ecx
519
        cmp     ecx, [esi+usb_controller.NumPorts]
520
        jb      @b
521
; 11b. Wait for power up.
522
; VirtualBox has AReg == 0, delay_ms doesn't like zero value; ignore zero delay
523
        push    esi
524
        mov     esi, [edi+OhciRhDescriptorAReg]
525
        shr     esi, 24
526
        add     esi, esi
527
        jz      @f
528
        call    delay_ms
529
@@:
530
        pop     esi
531
; 11c. Ports are powered up; now it is ok to process connect/disconnect events.
532
        mov     [esi+ohci_controller.PoweredUp-sizeof.ohci_controller], 1
533
                ; IRQ handler doesn't accept connect/disconnect events before this point
534
; 11d. We could miss some events while waiting for powering up;
535
; scan all ports manually and check for connected devices.
536
        xor     ecx, ecx
537
.port_loop:
538
        test    dword [edi+OhciRhPortStatusReg+ecx*4], 1
539
        jz      .next_port
540
; There is a connected device; mark the port as 'connected'
541
; and save the connected time.
542
; Note that ConnectedTime must be set before 'connected' mark,
543
; otherwise the code in ohci_process_deferred could use incorrect time.
544
        mov     eax, [timer_ticks]
545
        mov     [esi+usb_controller.ConnectedTime+ecx*4], eax
546
        lock bts [esi+usb_controller.NewConnected], ecx
547
.next_port:
548
        inc     ecx
549
        cmp     ecx, [esi+usb_controller.NumPorts]
550
        jb      .port_loop
551
; 12. Return pointer to usb_controller.
552
        xchg    eax, esi
553
        ret
554
.fail_unmap:
555
; On error after step 4, release the virtual memory area.
556
        stdcall free_kernel_space, edi
557
.fail:
558
; On error, free the ohci_controller structure and return zero.
559
; Note that the pointer was placed in the stack at step 1.
560
; Note also that there can be no errors after step 8,
561
; where that pointer is popped from the stack.
562
        pop     ecx
563
.nothing:
564
        xor     eax, eax
565
        ret
566
endp
567
 
568
; Helper procedure for step 3 of ohci_init.
569
; Initializes the static head of one list.
570
; eax = physical address of the "next" list, esi = pointer to the "next" list,
571
; edi = pointer to head to initialize.
572
; Advances edi to the next head, keeps eax/esi.
573
proc ohci_init_static_endpoint
574
        mov     byte [edi+ohci_static_ep.Flags+1], 1 shl (14 - 8)       ; sKip this endpoint
575
        mov     [edi+ohci_static_ep.NextED], eax
576
        mov     [edi+ohci_static_ep.NextList], esi
577
        add     edi, ohci_static_ep.SoftwarePart
578
        call    usb_init_static_endpoint
579
        add     edi, sizeof.ohci_static_ep - ohci_static_ep.SoftwarePart
580
        ret
581
endp
582
 
583
; Helper procedure for step 3 of ohci_init.
584
; Initializes one half of group of static heads.
585
; edx = size of the next group = half of size of the group,
586
; edi = pointer to the group, eax = physical address of the next group,
587
; esi = pointer to the next group.
588
; Advances eax, esi, edi to next group, keeps edx.
589
proc ohci_init_static_ep_group
590
        push    edx
591
@@:
592
        call    ohci_init_static_endpoint
593
        add     eax, sizeof.ohci_static_ep
594
        add     esi, sizeof.ohci_static_ep
595
        dec     edx
596
        jnz     @b
597
        pop     edx
598
        ret
599
endp
600
 
601
; Controller-specific pre-initialization function: take ownership from BIOS.
602
; Some BIOSes, although not all of them, provide legacy emulation
603
; for USB keyboard and/or mice as PS/2-devices. In this case,
604
; we must notify the BIOS that we don't need that emulation and know how to
605
; deal with USB devices.
606
proc ohci_kickoff_bios
607
; 1. Get the physical address of MMIO registers.
608
        mov     ah, [esi+PCIDEV.bus]
609
        mov     bh, [esi+PCIDEV.devfn]
610
        mov     al, 2
611
        mov     bl, 10h
612
        call    pci_read_reg
613
        and     al, not 0Fh
614
; 2. Create mapping for physical memory. 256 bytes are sufficient.
615
        stdcall map_io_mem, eax, 100h, PG_SW+PG_NOCACHE
616
        test    eax, eax
617
        jz      .nothing
618
; 3. Some BIOSes enable controller interrupts as a result of giving
619
; controller away. At this point the system knows nothing about how to serve
620
; OHCI interrupts, so such an interrupt will send the system into an infinite
621
; loop handling the same IRQ again and again. Thus, we need to block OHCI
622
; interrupts. We can't do this at the controller level until step 5,
623
; because the controller is currently owned by BIOS, so we block all hardware
624
; interrupts on this processor until step 5.
625
        pushf
626
        cli
627
; 4. Take the ownership over the controller.
628
; 4a. Check whether BIOS handles this controller at all.
629
        mov     edx, 100h
630
        test    dword [eax+OhciControlReg], edx
631
        jz      .has_ownership
632
; 4b. Send "take ownership" command to the BIOS.
633
; (This should generate SMI, BIOS should release its ownership in SMI handler.)
634
        mov     dword [eax+OhciCommandStatusReg], 8
635
; 4c. Wait for result no more than 50 ms, checking for status every 1 ms.
636
        push    50
637
        pop     ecx
638
@@:
639
        test    dword [eax+OhciControlReg], edx
640
        jz      .has_ownership
641
        push    esi
642
        push    1
643
        pop     esi
644
        call    delay_ms
645
        pop     esi
646
        loop    @b
647
        dbgstr 'warning: taking OHCI ownership from BIOS timeout'
648
.has_ownership:
649
; 5. Disable all controller interrupts until the system will be ready to
650
; process them.
651
        mov     dword [eax+OhciInterruptDisableReg], 0C000007Fh
652
; 6. Now we can unblock interrupts in the processor.
653
        popf
654
; 7. Release memory mapping created in step 2 and return.
655
        stdcall free_kernel_space, eax
656
.nothing:
657
        ret
658
endp
659
 
660
; IRQ handler for OHCI controllers.
661
ohci_irq.noint:
662
; Not our interrupt: restore registers and return zero.
663
        xor     eax, eax
664
        pop     edi esi ebx
665
        ret
666
 
667
proc ohci_irq
668
        push    ebx esi edi     ; save used registers to be cdecl
669
virtual at esp
670
                rd      3       ; saved registers
671
                dd      ?       ; return address
672
.controller     dd      ?
673
end virtual
674
; 1. ebx will hold whether some deferred processing is needed,
675
; that cannot be done from the interrupt handler. Initialize to zero.
676
        xor     ebx, ebx
677
; 2. Get the mask of events which should be processed.
678
        mov     esi, [.controller]
679
        mov     edi, [esi+ohci_controller.MMIOBase-sizeof.ohci_controller]
680
        mov     eax, [edi+OhciInterruptStatusReg]
681
; 3. Check whether that interrupt has been generated by our controller.
682
; (One IRQ can be shared by several devices.)
683
        and     eax, [edi+OhciInterruptEnableReg]
684
        jz      .noint
685
; 4. Get the physical pointer to the last processed descriptor.
686
; All processed descriptors form single-linked list from last to first
687
; with the help of NextTD field. The list is restarted every time when
688
; the controller writes to DoneHead, so grab the pointer now (before the next
689
; step) or it could be lost (the controller could write new value to DoneHead
690
; any time after WorkDone bit is cleared in OhciInterruptStatusReg).
691
        mov     ecx, [esi+ohci_controller.DoneHead-sizeof.ohci_controller]
692
        and     ecx, not 1
693
; 5. Clear the events we know of.
694
; Note that this should be done before processing of events:
695
; new events could arise while we are processing those, this way we won't lose
696
; them (the controller would generate another interrupt
697
; after completion of this one).
698
        mov     [edi+OhciInterruptStatusReg], eax
699
; 6. Save the mask of events for further reference.
700
        push    eax
701
; 7. Handle 'transfer is done' events.
702
; 7a. Test whether there are such events.
703
        test    al, 2
704
        jz      .skip_donehead
705
; There are some 'transfer is done' events, processed descriptors are linked
706
; through physical addresses in the reverse order.
707
; We can't do much in an interrupt handler, since callbacks could require
708
; waiting for locks and that can't be done in an interrupt handler.
709
; However, we can't also just defer all work to the USB thread, since
710
; it is possible that previous lists are not yet processed and it is hard
711
; to store unlimited number of list heads. Thus, we reverse the current list,
712
; append it to end of the previous list (if there is one) and defer other
713
; processing to the USB thread; this way there always is no more than one list
714
; (possibly joined from several controller-reported lists).
715
; The list traversal requires converting physical addresses to virtual pointers,
716
; so we may as well store pointers instead of physical addresses.
717
; 7b. Prepare for the reversing loop.
718
        push    ebx
719
        xor     ebx, ebx
720
        test    ecx, ecx
721
        jz      .tddone
722
        call    usb_td_to_virt
723
        test    eax, eax
724
        jz      .tddone
725
        lea     edx, [eax+ohci_gtd.NextTD]
726
; 7c. Reverse the list, converting physical to virtual. On every iteration:
727
; ecx = physical address of the current item
728
; eax = virtual pointer to the current item
729
; edx = virtual pointer to the last item.NextTD (first in the reverse list)
730
; ebx = virtual pointer to the next item (previous in the reverse list)
731
.tdloop:
732
        mov     ecx, [eax+ohci_gtd.NextTD]
733
        mov     [eax+ohci_gtd.NextTD], ebx
734
        lea     ebx, [eax+ohci_gtd.SoftwarePart]
735
        test    ecx, ecx
736
        jz      .tddone
737
        call    usb_td_to_virt
738
        test    eax, eax
739
        jnz     .tdloop
740
.tddone:
741
        mov     ecx, ebx
742
        pop     ebx
743
; 7d. The list is reversed,
744
; ecx = pointer to the first item, edx = pointer to the last item.NextTD.
745
; If the list is empty (unusual case), step 7 is done.
746
        test    ecx, ecx
747
        jz      .skip_donehead
748
; 7e. Otherwise, append this list to the end of previous one.
749
; Note that in theory the interrupt handler and the USB thread
750
; could execute in parallel.
751
.append_restart:
752
; Atomically get DoneListEndPtr in eax and set it to edx.
753
        mov     eax, [esi+ohci_controller.DoneListEndPtr-sizeof.ohci_controller]
754
        lock cmpxchg [esi+ohci_controller.DoneListEndPtr-sizeof.ohci_controller], edx
755
        jnz     .append_restart
756
; Store pointer to the new list.
757
; Note: we cannot perform any operations with [DoneListEndPtr]
758
; until we switch DoneListEndPtr to a new descriptor:
759
; it is possible that after first line of .append_restart loop
760
; ohci_process_deferred obtains the control, finishes processing
761
; of the old list, sets DoneListEndPtr to address of DoneList,
762
; frees all old descriptors, so eax would point to invalid location.
763
; This way, .append_restart loop would detect that DoneListEndPtr
764
; has changed, so eax needs to be re-read.
765
        mov     [eax], ecx
766
; 7f. Notify the USB thread that there is new work.
767
        inc     ebx
768
.skip_donehead:
769
; 8. Handle start-of-frame events.
770
; 8a. Test whether there are such events.
771
        test    byte [esp], 4
772
        jz      .skip_sof
773
; We enable SOF interrupt only when some pipes are waiting after changes.
774
        spin_lock_irqsave [esi+usb_controller.WaitSpinlock]
775
; 8b. Make sure that there was at least one frame update
776
; since the request. If not, wait for the next SOF.
777
        movzx   eax, [esi+ohci_controller.FrameNumber-sizeof.ohci_controller]
778
        cmp     eax, [esi+usb_controller.StartWaitFrame]
779
        jz      .sof_unlock
780
; 8c. Copy WaitPipeRequest* to ReadyPipeHead*.
781
        mov     eax, [esi+usb_controller.WaitPipeRequestAsync]
782
        mov     [esi+usb_controller.ReadyPipeHeadAsync], eax
783
        mov     eax, [esi+usb_controller.WaitPipeRequestPeriodic]
784
        mov     [esi+usb_controller.ReadyPipeHeadPeriodic], eax
785
; 8d. It is possible that pipe change is due to removal and
786
; Control/BulkCurrentED registers still point to one of pipes to be removed.
787
; The code responsible for disconnect events has temporarily stopped
788
; Control/Bulk processing, so it is safe to clear Control/BulkCurrentED.
789
; After that, restart processing.
790
        xor     edx, edx
791
        mov     [edi+OhciControlCurrentEDReg], edx
792
        mov     [edi+OhciBulkCurrentEDReg], edx
793
        mov     dword [edi+OhciCommandStatusReg], 6
794
        or      dword [edi+OhciControlReg], 30h
795
; 8e. Disable further interrupts on SOF.
796
; Note: OhciInterruptEnableReg/OhciInterruptDisableReg have unusual semantics.
797
        mov     dword [edi+OhciInterruptDisableReg], 4
798
; Notify the USB thread that there is new work (with pipes from ReadyPipeHead*).
799
        inc     ebx
800
.sof_unlock:
801
        spin_unlock_irqrestore [esi+usb_controller.RemoveSpinlock]
802
.skip_sof:
803
; Handle roothub events.
804
; 9. Test whether there are such events.
805
        test    byte [esp], 40h
806
        jz      .skip_roothub
807
; 10. Check the status of the roothub itself.
808
; 10a. Global overcurrent?
809
        test    dword [edi+OhciRhStatusReg], 2
810
        jz      @f
811
; Note: this needs work.
812
        dbgstr 'global overcurrent'
813
@@:
814
; 10b. Clear roothub events.
815
        mov     dword [edi+OhciRhStatusReg], 80020000h
816
; 11. Check the status of individual ports.
817
; Look for connect/disconnect and reset events.
818
; 11a. Prepare for the loop: start from port 0.
819
        xor     ecx, ecx
820
.portloop:
821
; 11b. Get the port status and changes of it.
822
; Accumulate change information.
823
; Look to "11.12.3 Port Change Information Processing" of the USB2 spec.
824
        xor     eax, eax
825
.accloop:
826
        mov     edx, [edi+OhciRhPortStatusReg+ecx*4]
827
        xor     ax, ax
828
        or      eax, edx
829
        test    edx, 1F0000h
830
        jz      .accdone
831
        mov     dword [edi+OhciRhPortStatusReg+ecx*4], 1F0000h
832
        jmp     .accloop
833
.accdone:
834
; debugging output, not needed for work
835
;       test    eax, 1F0000h
836
;       jz      @f
837
;       DEBUGF 1,'K : ohci irq [%d] status of port %d is %x\n',[timer_ticks],ecx,eax
838
;@@:
839
; 11c. Ignore any events until all ports are powered up.
840
; They will be processed by ohci_init.
841
        cmp     [esi+ohci_controller.PoweredUp-sizeof.ohci_controller], 0
842
        jz      .nextport
843
; Handle changing of connection status.
844
        test    eax, 10000h
845
        jz      .nocsc
846
; There was a connect or disconnect event at this port.
847
; 11d. Disconnect the old device on this port, if any.
848
; if the port was resetting, indicate fail and signal
849
        cmp     cl, [esi+usb_controller.ResettingPort]
850
        jnz     @f
851
        mov     [esi+usb_controller.ResettingStatus], -1
852
        inc     ebx
853
@@:
854
        lock bts [esi+usb_controller.NewDisconnected], ecx
855
; notify the USB thread that new work is waiting
856
        inc     ebx
857
; 11e. Change connected status. For the connection event, also
858
; store the connection time; any further processing is permitted only
859
; after USB_CONNECT_DELAY ticks.
860
        test    al, 1
861
        jz      .disconnect
862
; Note: ConnectedTime must be stored before setting the 'connected' bit,
863
; otherwise ohci_process_deferred could use an old time.
864
        mov     eax, [timer_ticks]
865
        mov     [esi+usb_controller.ConnectedTime+ecx*4], eax
866
        lock bts [esi+usb_controller.NewConnected], ecx
867
        jmp     .nextport
868
.disconnect:
869
        lock btr [esi+usb_controller.NewConnected], ecx
870
        jmp     .nextport
871
.nocsc:
872
; 11f. Process 'reset done' events.
873
        test    eax, 100000h
874
        jz      .nextport
875
        test    al, 10h
876
        jnz     .nextport
877
        mov     edx, [timer_ticks]
878
        mov     [esi+usb_controller.ResetTime], edx
879
        mov     [esi+usb_controller.ResettingStatus], 2
880
        inc     ebx
881
.nextport:
882
; 11g. Continue the loop for the next port.
883
        inc     ecx
884
        cmp     ecx, [esi+usb_controller.NumPorts]
885
        jb      .portloop
886
.skip_roothub:
887
; 12. Restore the stack after step 6.
888
        pop     eax
889
; 13. Notify the USB thread if some deferred processing is required.
890
        call    usb_wakeup_if_needed
891
; 14. Interrupt processed; return something non-zero.
892
        mov     al, 1
893
        pop     edi esi ebx     ; restore used registers to be stdcall
894
        ret
895
endp
896
 
897
; This procedure is called from usb_set_address_callback
898
; and stores USB device address in the ohci_pipe structure.
899
; in: esi -> usb_controller, ebx -> usb_pipe, cl = address
900
proc ohci_set_device_address
901
        mov     byte [ebx+ohci_pipe.Flags-ohci_pipe.SoftwarePart], cl
902
; Wait until the hardware will forget the old value.
903
        call    usb_subscribe_control
904
        ret
905
endp
906
 
907
; This procedure returns USB device address from the usb_pipe structure.
908
; in: esi -> usb_controller, ebx -> usb_pipe
909
; out: eax = endpoint address
910
proc ohci_get_device_address
911
        mov     eax, [ebx+ohci_pipe.Flags-ohci_pipe.SoftwarePart]
912
        and     eax, 7Fh
913
        ret
914
endp
915
 
916
; This procedure is called from usb_set_address_callback
917
; if the device does not accept SET_ADDRESS command and needs
918
; to be disabled at the port level.
919
; in: esi -> usb_controller, ecx = port
920
proc ohci_port_disable
921
        mov     edx, [esi+ohci_controller.MMIOBase-sizeof.ohci_controller]
922
        mov     dword [edx+OhciRhPortStatusReg+ecx*4], 1
923
        ret
924
endp
925
 
926
; This procedure is called from usb_get_descr8_callback when
927
; the packet size for zero endpoint becomes known and
928
; stores the packet size in ohci_pipe structure.
929
; in: esi -> usb_controller, ebx -> usb_pipe, ecx = packet size
930
proc ohci_set_endpoint_packet_size
931
        mov     byte [ebx+ohci_pipe.Flags+2-ohci_pipe.SoftwarePart], cl
932
; Wait until the hardware will forget the old value.
933
        call    usb_subscribe_control
934
        ret
935
endp
936
 
937
; This procedure is called from API usb_open_pipe and processes
938
; the controller-specific part of this API. See docs.
939
; in: edi -> usb_pipe for target, ecx -> usb_pipe for config pipe,
940
; esi -> usb_controller, eax -> usb_gtd for the first TD,
941
; [ebp+12] = endpoint, [ebp+16] = maxpacket, [ebp+20] = type
942
proc ohci_init_pipe
943
virtual at ebp+8
944
.config_pipe    dd      ?
945
.endpoint       dd      ?
946
.maxpacket      dd      ?
947
.type           dd      ?
948
.interval       dd      ?
949
end virtual
950
; 1. Initialize the queue of transfer descriptors: empty.
951
        sub     eax, ohci_gtd.SoftwarePart
952
        call    get_phys_addr
953
        mov     [edi+ohci_pipe.TailP-ohci_pipe.SoftwarePart], eax
954
        mov     [edi+ohci_pipe.HeadP-ohci_pipe.SoftwarePart], eax
955
; 2. Generate ohci_pipe.Flags, see the description in ohci_pipe.
956
        mov     eax, [ecx+ohci_pipe.Flags-ohci_pipe.SoftwarePart]
957
        and     eax, 0x207F     ; keep Speed bit and FunctionAddress
958
        mov     edx, [.endpoint]
959
        and     edx, 15
960
        shl     edx, 7
961
        or      eax, edx
962
        mov     [edi+ohci_pipe.Flags-ohci_pipe.SoftwarePart], eax
963
        mov     eax, [.maxpacket]
964
        mov     word [edi+ohci_pipe.Flags+2-ohci_pipe.SoftwarePart], ax
965
        cmp     [.type], CONTROL_PIPE
966
        jz      @f
967
        test    byte [.endpoint], 80h
968
        setnz   al
969
        inc     eax
970
        shl     al, 3
971
        or      byte [edi+ohci_pipe.Flags+1-ohci_pipe.SoftwarePart], al
972
@@:
973
; 3. Insert the new pipe to the corresponding list of endpoints.
974
; 3a. Use Control list for control pipes, Bulk list for bulk pipes.
975
        lea     edx, [esi+ohci_controller.ControlED.SoftwarePart-sizeof.ohci_controller]
976
        cmp     [.type], BULK_PIPE
977
        jb      .insert ; control pipe
978
        lea     edx, [esi+ohci_controller.BulkED.SoftwarePart-sizeof.ohci_controller]
979
        jz      .insert ; bulk pipe
980
.interrupt_pipe:
981
; 3b. For interrupt pipes, let the scheduler select the appropriate list
982
; based on the current bandwidth distribution and the requested bandwidth.
983
; This could fail if the requested bandwidth is not available;
984
; if so, return an error.
985
        lea     edx, [esi + ohci_controller.IntEDs - sizeof.ohci_controller]
986
        lea     eax, [esi + ohci_controller.IntEDs + 32*sizeof.ohci_static_ep - sizeof.ohci_controller]
987
        push    64
988
        pop     ecx
989
        call    usb1_select_interrupt_list
990
        test    edx, edx
991
        jz      .return0
992
; 3c. Insert endpoint at edi to the head of list in edx.
993
; Inserting to tail would work as well,
994
; but let's be consistent with other controllers.
995
.insert:
996
        mov     ecx, [edx+usb_pipe.NextVirt]
997
        mov     [edi+usb_pipe.NextVirt], ecx
998
        mov     [edi+usb_pipe.PrevVirt], edx
999
        mov     [ecx+usb_pipe.PrevVirt], edi
1000
        mov     [edx+usb_pipe.NextVirt], edi
1001
        mov     ecx, [edx+ohci_pipe.NextED-ohci_pipe.SoftwarePart]
1002
        mov     [edi+ohci_pipe.NextED-ohci_pipe.SoftwarePart], ecx
1003
        lea     eax, [edi-ohci_pipe.SoftwarePart]
1004
        call    get_phys_addr
1005
        mov     [edx+ohci_pipe.NextED-ohci_pipe.SoftwarePart], eax
1006
; 4. Return something non-zero.
1007
        ret
1008
.return0:
1009
        xor     eax, eax
1010
        ret
1011
endp
1012
 
1013
; This function is called from ohci_process_deferred when
1014
; a new device was connected at least USB_CONNECT_DELAY ticks
1015
; and therefore is ready to be configured.
1016
; ecx = port, esi -> usb_controller
1017
proc ohci_new_port
1018
; test whether we are configuring another port
1019
; if so, postpone configuring and return
1020
        bts     [esi+usb_controller.PendingPorts], ecx
1021
        cmp     [esi+usb_controller.ResettingPort], -1
1022
        jnz     .nothing
1023
        btr     [esi+usb_controller.PendingPorts], ecx
1024
; fall through to ohci_new_port.reset
1025
 
1026
; This function is called from usb_test_pending_port.
1027
; It starts reset signalling for the port. Note that in USB first stages
1028
; of configuration can not be done for several ports in parallel.
1029
.reset:
1030
; reset port
1031
        and     [esi+usb_controller.ResettingHub], 0
1032
        mov     [esi+usb_controller.ResettingPort], cl
1033
; Note: setting status must be the last action:
1034
; it is possible that the device has been disconnected
1035
; after timeout of USB_CONNECT_DELAY but before call to ohci_new_port.
1036
; In this case, ohci_irq would not set reset status to 'failed',
1037
; because ohci_irq would not know that this port is to be reset.
1038
; However, the hardware would generate another interrupt
1039
; in a response to reset a disconnected port, and this time
1040
; ohci_irq knows that it needs to generate 'reset failed' event
1041
; (because ResettingPort is now filled).
1042
        push    edi
1043
        mov     edi, [esi+ohci_controller.MMIOBase-sizeof.ohci_controller]
1044
        mov     dword [edi+OhciRhPortStatusReg+ecx*4], 10h
1045
        pop     edi
1046
.nothing:
1047
        ret
1048
endp
1049
 
1050
; This procedure is called from the several places in main USB code
1051
; and allocates required packets for the given transfer.
1052
; ebx = pipe, other parameters are passed through the stack:
1053
; buffer,size = data to transfer
1054
; flags = same as in usb_open_pipe: bit 0 = allow short transfer, other bits reserved
1055
; td = pointer to the current end-of-queue descriptor
1056
; direction =
1057
;   0000b for normal transfers,
1058
;   1000b for control SETUP transfer,
1059
;   1101b for control OUT transfer,
1060
;   1110b for control IN transfer
1061
; returns eax = pointer to the new end-of-queue descriptor
1062
; (not included in the queue itself) or 0 on error
1063
proc ohci_alloc_transfer stdcall uses edi, \
1064
        buffer:dword, size:dword, flags:dword, td:dword, direction:dword
1065
locals
1066
origTD          dd      ?
1067
packetSize      dd      ?       ; must be the last variable, see usb_init_transfer
1068
endl
1069
; 1. Save original value of td:
1070
; it will be useful for rollback if something would fail.
1071
        mov     eax, [td]
1072
        mov     [origTD], eax
1073
; One transfer descriptor can describe up to two pages.
1074
; In the worst case (when the buffer is something*1000h+0FFFh)
1075
; this corresponds to 1001h bytes. If the requested size is
1076
; greater, we should split the transfer into several descriptors.
1077
; Boundaries to split must be multiples of endpoint transfer size
1078
; to avoid short packets except in the end of the transfer,
1079
; 1000h is always a good value.
1080
; 2. While the remaining data cannot fit in one packet,
1081
; allocate page-sized descriptors.
1082
        mov     edi, 1000h
1083
        mov     [packetSize], edi
1084
.fullpackets:
1085
        cmp     [size], edi
1086
        jbe     .lastpacket
1087
        call    ohci_alloc_packet
1088
        test    eax, eax
1089
        jz      .fail
1090
        mov     [td], eax
1091
        add     [buffer], edi
1092
        sub     [size], edi
1093
        jmp     .fullpackets
1094
; 3. The remaining data can fit in one descriptor;
1095
; allocate the last descriptor with size = size of remaining data.
1096
.lastpacket:
1097
        mov     eax, [size]
1098
        mov     [packetSize], eax
1099
        call    ohci_alloc_packet
1100
        test    eax, eax
1101
        jz      .fail
1102
; 4. Enable an immediate interrupt on completion of the last packet.
1103
        and     byte [ecx+ohci_gtd.Flags+2-ohci_gtd.SoftwarePart], not (7 shl (21-16))
1104
; 5. If a short transfer is ok for a caller, set the corresponding bit in
1105
; the last descriptor, but not in others.
1106
; Note: even if the caller says that short transfers are ok,
1107
; all packets except the last one are marked as 'must be complete':
1108
; if one of them will be short, the software intervention is needed
1109
; to skip remaining packets; ohci_process_finalized_td will handle this
1110
; transparently to the caller.
1111
        test    [flags], 1
1112
        jz      @f
1113
        or      byte [ecx+ohci_gtd.Flags+2-ohci_gtd.SoftwarePart], 1 shl (18-16)
1114
@@:
1115
        ret
1116
.fail:
1117
        mov     edi, ohci_hardware_func
1118
        mov     eax, [td]
1119
        stdcall usb_undo_tds, [origTD]
1120
        xor     eax, eax
1121
        ret
1122
endp
1123
 
1124
; Helper procedure for ohci_alloc_transfer.
1125
; Allocates and initializes one transfer descriptor.
1126
; ebx = pipe, other parameters are passed through the stack;
1127
; fills the current last descriptor and
1128
; returns eax = next descriptor (not filled).
1129
proc ohci_alloc_packet
1130
; inherit some variables from the parent ohci_alloc_transfer
1131
virtual at ebp-8
1132
.origTD         dd      ?
1133
.packetSize     dd      ?
1134
                rd      2
1135
.buffer         dd      ?
1136
.transferSize   dd      ?
1137
.Flags          dd      ?
1138
.td             dd      ?
1139
.direction      dd      ?
1140
end virtual
1141
; 1. Allocate the next TD.
1142
        call    usb1_allocate_general_td
1143
        test    eax, eax
1144
        jz      .nothing
1145
; 2. Initialize controller-independent parts of both TDs.
1146
        push    eax
1147
        call    usb_init_transfer
1148
        pop     eax
1149
; 3. Save the returned value (next descriptor).
1150
        push    eax
1151
; 4. Store the physical address of the next descriptor.
1152
        sub     eax, ohci_gtd.SoftwarePart
1153
        call    get_phys_addr
1154
        mov     [ecx+ohci_gtd.NextTD-ohci_gtd.SoftwarePart], eax
1155
; 5. For zero-length transfers, store zero in both fields for buffer addresses.
1156
; Otherwise, fill them with real values.
1157
        xor     eax, eax
1158
        mov     [ecx+ohci_gtd.CurBufPtr-ohci_gtd.SoftwarePart], eax
1159
        mov     [ecx+ohci_gtd.BufEnd-ohci_gtd.SoftwarePart], eax
1160
        cmp     [.packetSize], eax
1161
        jz      @f
1162
        mov     eax, [.buffer]
1163
        call    get_phys_addr
1164
        mov     [ecx+ohci_gtd.CurBufPtr-ohci_gtd.SoftwarePart], eax
1165
        mov     eax, [.buffer]
1166
        add     eax, [.packetSize]
1167
        dec     eax
1168
        call    get_phys_addr
1169
        mov     [ecx+ohci_gtd.BufEnd-ohci_gtd.SoftwarePart], eax
1170
@@:
1171
; 6. Generate Flags field:
1172
; - set bufferRounding (bit 18) to zero = disallow short transfers;
1173
;   for the last transfer in a row, ohci_alloc_transfer would set the real value;
1174
; - set Direction (bits 19-20) to lower 2 bits of [.direction];
1175
; - set DelayInterrupt (bits 21-23) to 7 = do not generate interrupt;
1176
;   for the last transfer in a row, ohci_alloc_transfer would set the real value;
1177
; - set DataToggle (bits 24-25) to next 2 bits of [.direction];
1178
; - set ConditionCode (bits 28-31) to 1111b as a indicator that there was no
1179
;   attempts to perform this transfer yet;
1180
; - zero all other bits.
1181
        mov     eax, [.direction]
1182
        mov     edx, eax
1183
        and     eax, 3
1184
        shl     eax, 19
1185
        and     edx, (3 shl 2)
1186
        shl     edx, 24 - 2
1187
        lea     eax, [eax + edx + (7 shl 21) + (15 shl 28)]
1188
        mov     [ecx+ohci_gtd.Flags-ohci_gtd.SoftwarePart], eax
1189
; 7. Restore the returned value saved in step 3.
1190
        pop     eax
1191
.nothing:
1192
        ret
1193
endp
1194
 
1195
; This procedure is called from the several places in main USB code
1196
; and activates the transfer which was previously allocated by
1197
; ohci_alloc_transfer.
1198
; ecx -> last descriptor for the transfer, ebx -> usb_pipe
1199
proc ohci_insert_transfer
1200
; 1. Advance the queue of transfer descriptors.
1201
        mov     eax, [ecx+ohci_gtd.NextTD-ohci_gtd.SoftwarePart]
1202
        mov     [ebx+ohci_pipe.TailP-ohci_pipe.SoftwarePart], eax
1203
; 2. For control and bulk pipes, notify the controller that
1204
; there is new work in control/bulk queue respectively.
1205
ohci_notify_new_work:
1206
        mov     edx, [ebx+usb_pipe.Controller]
1207
        mov     edx, [edx+ohci_controller.MMIOBase-sizeof.ohci_controller]
1208
        cmp     [ebx+usb_pipe.Type], CONTROL_PIPE
1209
        jz      .control
1210
        cmp     [ebx+usb_pipe.Type], BULK_PIPE
1211
        jnz     .nothing
1212
.bulk:
1213
        mov     dword [edx+OhciCommandStatusReg], 4
1214
        jmp     .nothing
1215
.control:
1216
        mov     dword [edx+OhciCommandStatusReg], 2
1217
.nothing:
1218
        ret
1219
endp
1220
 
1221
; This function is called from ohci_process_deferred when
1222
; a new device has been reset and needs to be configured.
1223
proc ohci_port_after_reset
1224
; 1. Get the status.
1225
; If reset has been failed (device disconnected during reset),
1226
; continue to next device (if there is one).
1227
        xor     eax, eax
1228
        xchg    al, [esi+usb_controller.ResettingStatus]
1229
        test    al, al
1230
        js      usb_test_pending_port
1231
; If the controller has disabled the port (e.g. overcurrent),
1232
; continue to next device (if there is one).
1233
        movzx   ecx, [esi+usb_controller.ResettingPort]
1234
        mov     eax, [edi+OhciRhPortStatusReg+ecx*4]
1235
        test    al, 2
1236
        jnz     @f
1237
        DEBUGF 1,'K : USB port disabled after reset, status=%x\n',eax
1238
        jmp     usb_test_pending_port
1239
@@:
1240
        push    ecx
1241
; 2. Get LowSpeed bit to bit 0 of eax and call the worker procedure
1242
; to notify the protocol layer about new OHCI device.
1243
        mov     eax, [edi+OhciRhPortStatusReg+ecx*4]
1244
        DEBUGF 1,'K : port_after_reset [%d] status of port %d is %x\n',[timer_ticks],ecx,eax
1245
        shr     eax, 9
1246
        call    ohci_new_device
1247
        pop     ecx
1248
; 3. If something at the protocol layer has failed
1249
; (no memory, no bus address), disable the port and stop the initialization.
1250
        test    eax, eax
1251
        jnz     .nothing
1252
.disable_exit:
1253
        mov     dword [edi+OhciRhPortStatusReg+ecx*4], 1
1254
        jmp     usb_test_pending_port
1255
.nothing:
1256
        ret
1257
endp
1258
 
1259
; This procedure is called from uhci_port_init and from hub support code
1260
; when a new device is connected and has been reset.
1261
; It calls usb_new_device at the protocol layer with correct parameters.
1262
; in: esi -> usb_controller, eax = speed;
1263
; OHCI is USB1 device, so only low bit of eax (LowSpeed) is used.
1264
proc ohci_new_device
1265
; 1. Clear all bits of speed except bit 0.
1266
        and     eax, 1
1267
; 2. Store the speed for the protocol layer.
1268
        mov     [esi+usb_controller.ResettingSpeed], al
1269
; 3. Create pseudo-pipe in the stack.
1270
; See ohci_init_pipe: only .Controller and .Flags fields are used.
1271
        shl     eax, 13
1272
        push    esi     ; .Controller
1273
        mov     ecx, esp
1274
        sub     esp, 12 ; ignored fields
1275
        push    eax     ; .Flags
1276
; 4. Notify the protocol layer.
1277
        call    usb_new_device
1278
; 5. Cleanup the stack after step 3 and return.
1279
        add     esp, 20
1280
        ret
1281
endp
1282
 
1283
; This procedure is called in the USB thread from usb_thread_proc,
1284
; processes regular actions and those actions which can't be safely done
1285
; from interrupt handler.
1286
; Returns maximal time delta before the next call.
1287
proc ohci_process_deferred
1288
        push    ebx edi         ; save used registers to be stdcall
1289
; 1. Initialize the return value.
1290
        push    -1
1291
; 2. Process disconnect events.
1292
        call    usb_disconnect_stage2
1293
; 3. Check for connected devices.
1294
; If there is a connected device which was connected less than
1295
; USB_CONNECT_DELAY ticks ago, plan to wake up when the delay will be over.
1296
; Otherwise, call ohci_new_port.
1297
        mov     edi, [esi+ohci_controller.MMIOBase-sizeof.ohci_controller]
1298
        xor     ecx, ecx
1299
        cmp     [esi+usb_controller.NewConnected], ecx
1300
        jz      .skip_newconnected
1301
.portloop:
1302
        bt      [esi+usb_controller.NewConnected], ecx
1303
        jnc     .noconnect
1304
        mov     eax, [timer_ticks]
1305
        sub     eax, [esi+usb_controller.ConnectedTime+ecx*4]
1306
        sub     eax, USB_CONNECT_DELAY
1307
        jge     .connected
1308
        neg     eax
1309
        cmp     [esp], eax
1310
        jb      .nextport
1311
        mov     [esp], eax
1312
        jmp     .nextport
1313
.connected:
1314
        lock btr [esi+usb_controller.NewConnected], ecx
1315
        jnc     .nextport
1316
        call    ohci_new_port
1317
.noconnect:
1318
.nextport:
1319
        inc     ecx
1320
        cmp     ecx, [esi+usb_controller.NumPorts]
1321
        jb      .portloop
1322
.skip_newconnected:
1323
; 4. Check for end of reset signalling. If so, call ohci_port_after_reset.
1324
        cmp     [esi+usb_controller.ResettingStatus], 2
1325
        jnz     .no_reset_recovery
1326
        mov     eax, [timer_ticks]
1327
        sub     eax, [esi+usb_controller.ResetTime]
1328
        sub     eax, USB_RESET_RECOVERY_TIME
1329
        jge     .reset_done
1330
        neg     eax
1331
        cmp     [esp], eax
1332
        jb      .skip_roothub
1333
        mov     [esp], eax
1334
        jmp     .skip_roothub
1335
.no_reset_recovery:
1336
        cmp     [esi+usb_controller.ResettingStatus], 0
1337
        jz      .skip_roothub
1338
.reset_done:
1339
        call    ohci_port_after_reset
1340
.skip_roothub:
1341
; 5. Finalize transfers processed by hardware.
1342
; It is better to perform this step after processing disconnect events,
1343
; although not strictly obligatory. This way, an active transfer aborted
1344
; due to disconnect would be handled with more specific USB_STATUS_CLOSED,
1345
; not USB_STATUS_NORESPONSE.
1346
; Loop over all items in DoneList, call ohci_process_finalized_td for each.
1347
        xor     ebx, ebx
1348
        xchg    ebx, [esi+ohci_controller.DoneList-sizeof.ohci_controller]
1349
.tdloop:
1350
        test    ebx, ebx
1351
        jz      .tddone
1352
        call    ohci_process_finalized_td
1353
        jmp     .tdloop
1354
.tddone:
1355
; 6. Process wait-done notifications, test for new wait requests.
1356
; Note: that must be done after steps 2 and 5 which could create new requests.
1357
; 6a. Call the worker function from main USB code.
1358
        call    usb_process_wait_lists
1359
; 6b. If no new requests, skip the rest of this step.
1360
        test    eax, eax
1361
        jz      @f
1362
; 6c. OHCI is not allowed to cache anything; we don't know what is
1363
; processed right now, but we can be sure that the controller will not
1364
; use any removed structure starting from the next frame.
1365
; Schedule SOF event.
1366
        spin_lock_irq [esi+usb_controller.RemoveSpinlock]
1367
        mov     eax, [esi+usb_controller.WaitPipeListAsync]
1368
        mov     [esi+usb_controller.WaitPipeRequestAsync], eax
1369
        mov     eax, [esi+usb_controller.WaitPipeListPeriodic]
1370
        mov     [esi+usb_controller.WaitPipeRequestPeriodic], eax
1371
; temporarily stop bulk and interrupt processing;
1372
; this is required for handler of SOF event
1373
        and     dword [edi+OhciControlReg], not 30h
1374
; remember the frame number when processing has been stopped
1375
; (needs to be done after stopping)
1376
        movzx   eax, [esi+ohci_controller.FrameNumber-sizeof.ohci_controller]
1377
        mov     [esi+usb_controller.StartWaitFrame], eax
1378
; make sure that the next SOF will happen after the request
1379
        mov     dword [edi+OhciInterruptStatusReg], 4
1380
; enable interrupt on SOF
1381
; Note: OhciInterruptEnableReg/OhciInterruptDisableReg have unusual semantics,
1382
; so there should be 'mov' here, not 'or'
1383
        mov     dword [edi+OhciInterruptEnableReg], 4
1384
        spin_unlock_irq [esi+usb_controller.RemoveSpinlock]
1385
@@:
1386
; 7. Restore the return value and return.
1387
        pop     eax
1388
        pop     edi ebx         ; restore used registers to be stdcall
1389
        ret
1390
endp
1391
 
1392
; Helper procedure for ohci_process_deferred. Processes one completed TD.
1393
; in: esi -> usb_controller, ebx -> usb_gtd, out: ebx -> next usb_gtd.
1394
proc ohci_process_finalized_td
1395
;       DEBUGF 1,'K : processing %x\n',ebx
1396
; 1. Check whether the pipe has been closed, either due to API call or due to
1397
; disconnect; if so, the callback will be called by usb_pipe_closed with
1398
; correct status, so go to step 6 with ebx = 0 (do not free the TD).
1399
        mov     edx, [ebx+usb_gtd.Pipe]
1400
        test    [edx+usb_pipe.Flags], USB_FLAG_CLOSED
1401
        jz      @f
1402
        lea     eax, [ebx+ohci_gtd.NextTD-ohci_gtd.SoftwarePart]
1403
        xor     ebx, ebx
1404
        jmp     .next_td2
1405
@@:
1406
; 2. Remove the descriptor from the descriptors queue.
1407
        call    usb_unlink_td
1408
; 3. Get number of bytes that remain to be transferred.
1409
; If CurBufPtr is zero, everything was transferred.
1410
        xor     edx, edx
1411
        cmp     [ebx+ohci_gtd.CurBufPtr-ohci_gtd.SoftwarePart], edx
1412
        jz      .gotlen
1413
; Otherwise, the remaining length is
1414
; (BufEnd and 0xFFF) - (CurBufPtr and 0xFFF) + 1,
1415
; plus 0x1000 if BufEnd and CurBufPtr are in different pages.
1416
        mov     edx, [ebx+ohci_gtd.BufEnd-ohci_gtd.SoftwarePart]
1417
        mov     eax, [ebx+ohci_gtd.CurBufPtr-ohci_gtd.SoftwarePart]
1418
        mov     ecx, edx
1419
        and     edx, 0xFFF
1420
        inc     edx
1421
        xor     ecx, eax
1422
        and     ecx, -0x1000
1423
        jz      @f
1424
        add     edx, 0x1000
1425
@@:
1426
        and     eax, 0xFFF
1427
        sub     edx, eax
1428
.gotlen:
1429
; The actual length is Length - (remaining length).
1430
        sub     edx, [ebx+usb_gtd.Length]
1431
        neg     edx
1432
; 4. Check for error. If so, go to 7.
1433
        push    ebx
1434
        mov     eax, [ebx+ohci_gtd.Flags-ohci_gtd.SoftwarePart]
1435
        shr     eax, 28
1436
        jnz     .error
1437
.notify:
1438
; 5. Successful completion.
1439
; 5a. Check whether this descriptor has an associated callback.
1440
        mov     ecx, [ebx+usb_gtd.Callback]
1441
        test    ecx, ecx
1442
        jz      .ok_nocallback
1443
; 5b. If so, call the callback.
1444
        stdcall_verify ecx, [ebx+usb_gtd.Pipe], eax, \
1445
                [ebx+usb_gtd.Buffer], edx, [ebx+usb_gtd.UserData]
1446
        jmp     .next_td
1447
.ok_nocallback:
1448
; 5c. Otherwise, add length of the current descriptor to the next descriptor.
1449
        mov     eax, [ebx+usb_gtd.NextVirt]
1450
        add     [eax+usb_gtd.Length], edx
1451
.next_td:
1452
; 6. Free the current descriptor and advance to the next item.
1453
; If the current item is the last in the list,
1454
; set DoneListEndPtr to pointer to DoneList.
1455
        cmp     ebx, [esp]
1456
        jz      @f
1457
        stdcall usb1_free_general_td, ebx
1458
@@:
1459
        pop     ebx
1460
        lea     eax, [ebx+ohci_gtd.NextTD-ohci_gtd.SoftwarePart]
1461
.next_td2:
1462
        push    ebx
1463
        mov     ebx, eax
1464
        lea     edx, [esi+ohci_controller.DoneList-sizeof.ohci_controller]
1465
        xor     ecx, ecx        ; no next item
1466
        lock cmpxchg [esi+ohci_controller.DoneListEndPtr-sizeof.ohci_controller], edx
1467
        jz      .last
1468
; The current item is not the last.
1469
; It is possible, although very rare, that ohci_irq has already advanced
1470
; DoneListEndPtr, but not yet written NextTD. Wait until NextTD is nonzero.
1471
@@:
1472
        mov     ecx, [ebx]
1473
        test    ecx, ecx
1474
        jz      @b
1475
.last:
1476
        pop     ebx
1477
; ecx = the next item
1478
        push    ecx
1479
; Free the current item, set ebx to the next item, continue to 5a.
1480
        test    ebx, ebx
1481
        jz      @f
1482
        stdcall usb1_free_general_td, ebx
1483
@@:
1484
        pop     ebx
1485
        ret
1486
.error:
1487
; 7. There was an error while processing this descriptor.
1488
; The hardware has stopped processing the queue.
1489
; 7a. Save status and length.
1490
        push    eax
1491
        push    edx
1492
;       DEBUGF 1,'K : TD failed:\n'
1493
;       DEBUGF 1,'K : %x %x %x %x\n',[ebx-ohci_gtd.SoftwarePart],[ebx-ohci_gtd.SoftwarePart+4],[ebx-ohci_gtd.SoftwarePart+8],[ebx-ohci_gtd.SoftwarePart+12]
1494
;       DEBUGF 1,'K : %x %x %x %x\n',[ebx-ohci_gtd.SoftwarePart+16],[ebx-ohci_gtd.SoftwarePart+20],[ebx-ohci_gtd.SoftwarePart+24],[ebx-ohci_gtd.SoftwarePart+28]
1495
;       mov     eax, [ebx+usb_gtd.Pipe]
1496
;       DEBUGF 1,'K : pipe: %x %x %x %x\n',[eax-ohci_pipe.SoftwarePart],[eax-ohci_pipe.SoftwarePart+4],[eax-ohci_pipe.SoftwarePart+8],[eax-ohci_pipe.SoftwarePart+12]
1497
; 7b. Traverse the list of descriptors looking for the final packet
1498
; for this transfer.
1499
; Free and unlink non-final descriptors, except the current one.
1500
; Final descriptor will be freed in step 6.
1501
        call    usb_is_final_packet
1502
        jnc     .found_final
1503
        mov     ebx, [ebx+usb_gtd.NextVirt]
1504
virtual at esp
1505
.length         dd      ?
1506
.error_code     dd      ?
1507
.current_item   dd      ?
1508
end virtual
1509
.look_final:
1510
        call    usb_unlink_td
1511
        call    usb_is_final_packet
1512
        jnc     .found_final
1513
        push    [ebx+usb_gtd.NextVirt]
1514
        stdcall usb1_free_general_td, ebx
1515
        pop     ebx
1516
        jmp     .look_final
1517
.found_final:
1518
; 7c. If error code is USB_STATUS_UNDERRUN and the last TD allows short packets,
1519
; it is not an error.
1520
; Note: all TDs except the last one in any transfer stage are marked
1521
; as short-packet-is-error to stop controller from further processing
1522
; of that stage; we need to restart processing from a TD following the last.
1523
; After that, go to step 5 with eax = 0 (no error).
1524
        cmp     dword [.error_code], USB_STATUS_UNDERRUN
1525
        jnz     .no_underrun
1526
        test    byte [ebx+ohci_gtd.Flags+2-ohci_gtd.SoftwarePart], 1 shl (18-16)
1527
        jz      .no_underrun
1528
        and     dword [.error_code], 0
1529
        mov     ecx, [ebx+usb_gtd.Pipe]
1530
        mov     edx, [ecx+ohci_pipe.HeadP-ohci_pipe.SoftwarePart]
1531
        and     edx, 2
1532
.advance_queue:
1533
        mov     eax, [ebx+usb_gtd.NextVirt]
1534
        sub     eax, ohci_gtd.SoftwarePart
1535
        call    get_phys_addr
1536
        or      eax, edx
1537
        mov     [ecx+ohci_pipe.HeadP-ohci_pipe.SoftwarePart], eax
1538
        push    ebx
1539
        mov     ebx, ecx
1540
        call    ohci_notify_new_work
1541
        pop     ebx
1542
        pop     edx eax
1543
        jmp     .notify
1544
; 7d. Abort the entire transfer.
1545
; There are two cases: either there is only one transfer stage
1546
; (everything except control transfers), then ebx points to the last TD and
1547
; all previous TD were unlinked and dismissed (if possible),
1548
; or there are several stages (a control transfer) and ebx points to the last
1549
; TD of Data or Status stage (usb_is_final_packet does not stop in Setup stage,
1550
; because Setup stage can not produce short packets); for Data stage, we need
1551
; to unlink and free (if possible) one more TD and advance ebx to the next one.
1552
.no_underrun:
1553
        cmp     [ebx+usb_gtd.Callback], 0
1554
        jnz     .halted
1555
        cmp     ebx, [.current_item]
1556
        push    [ebx+usb_gtd.NextVirt]
1557
        jz      @f
1558
        stdcall usb1_free_general_td, ebx
1559
@@:
1560
        pop     ebx
1561
        call    usb_unlink_td
1562
.halted:
1563
; 7e. For bulk/interrupt transfers we have no choice but halt the queue,
1564
; the driver should intercede (through some API which is not written yet).
1565
; Control pipes normally recover at the next SETUP transaction (first stage
1566
; of any control transfer), so we hope on the best and just advance the queue
1567
; to the next transfer. (According to the standard, "A control pipe may also
1568
; support functional stall as well, but this is not recommended.").
1569
; Advance the transfer queue to the next descriptor.
1570
        mov     ecx, [ebx+usb_gtd.Pipe]
1571
        mov     edx, [ecx+ohci_pipe.HeadP-ohci_pipe.SoftwarePart]
1572
        and     edx, 2  ; keep toggleCarry bit
1573
        cmp     [ecx+usb_pipe.Type], CONTROL_PIPE
1574
        jnz     @f
1575
        inc     edx     ; set Halted bit
1576
@@:
1577
        jmp     .advance_queue
1578
endp
1579
 
1580
; This procedure is called when a pipe is closing (either due to API call
1581
; or due to disconnect); it unlinks the pipe from the corresponding list.
1582
; esi -> usb_controller, ebx -> usb_pipe
1583
proc ohci_unlink_pipe
1584
        cmp     [ebx+usb_pipe.Type], INTERRUPT_PIPE
1585
        jnz     @f
1586
        mov     eax, [ebx+ohci_pipe.Flags-ohci_pipe.SoftwarePart]
1587
        bt      eax, 13
1588
        setc    cl
1589
        bt      eax, 11
1590
        setc    ch
1591
        shr     eax, 16
1592
        stdcall usb1_interrupt_list_unlink, eax, ecx
1593
@@:
1594
        mov     edx, [ebx+usb_pipe.NextVirt]
1595
        mov     eax, [ebx+usb_pipe.PrevVirt]
1596
        mov     [edx+usb_pipe.PrevVirt], eax
1597
        mov     [eax+usb_pipe.NextVirt], edx
1598
        mov     edx, [ebx+ohci_pipe.NextED-ohci_pipe.SoftwarePart]
1599
        mov     [eax+ohci_pipe.NextED-ohci_pipe.SoftwarePart], edx
1600
        ret
1601
endp