Subversion Repositories shark

Rev

Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 1
/*
2
 * OHCI HCD (Host Controller Driver) for USB.
3
 *
4
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5
 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6
 *
7
 * This file is licenced under the GPL.
8
 */
9
 
10
static void urb_free_priv (struct ohci_hcd *hc, urb_priv_t *urb_priv)
11
{
12
        int             last = urb_priv->length - 1;
13
 
14
        if (last >= 0) {
15
                int             i;
16
                struct td       *td;
17
 
18
                for (i = 0; i <= last; i++) {
19
                        td = urb_priv->td [i];
20
                        if (td)
21
                                td_free (hc, td);
22
                }
23
        }
24
 
25
        kfree (urb_priv);
26
}
27
 
28
/*-------------------------------------------------------------------------*/
29
 
30
/*
31
 * URB goes back to driver, and isn't reissued.
32
 * It's completely gone from HC data structures.
33
 * PRECONDITION:  no locks held, irqs blocked  (Giveback can call into HCD.)
34
 */
35
static void
36
finish_urb (struct ohci_hcd *ohci, struct urb *urb, struct pt_regs *regs)
37
{
38
        // ASSERT (urb->hcpriv != 0);
39
 
40
        urb_free_priv (ohci, urb->hcpriv);
41
        urb->hcpriv = NULL;
42
 
43
        spin_lock (&urb->lock);
44
        if (likely (urb->status == -EINPROGRESS))
45
                urb->status = 0;
46
        /* report short control reads right even though the data TD always
47
         * has TD_R set.  (much simpler, but creates the 1-td limit.)
48
         */
49
        if (unlikely (urb->transfer_flags & URB_SHORT_NOT_OK)
50
                        && unlikely (usb_pipecontrol (urb->pipe))
51
                        && urb->actual_length < urb->transfer_buffer_length
52
                        && usb_pipein (urb->pipe)
53
                        && urb->status == 0) {
54
                urb->status = -EREMOTEIO;
55
        }
56
        spin_unlock (&urb->lock);
57
 
58
        // what lock protects these?
59
        switch (usb_pipetype (urb->pipe)) {
60
        case PIPE_ISOCHRONOUS:
61
                hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs--;
62
                break;
63
        case PIPE_INTERRUPT:
64
                hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs--;
65
                break;
66
        }
67
 
68
#ifdef OHCI_VERBOSE_DEBUG
69
        urb_print (urb, "RET", usb_pipeout (urb->pipe));
70
#endif
71
        usb_hcd_giveback_urb (&ohci->hcd, urb, regs);
72
}
73
 
74
 
75
/*-------------------------------------------------------------------------*
76
 * ED handling functions
77
 *-------------------------------------------------------------------------*/  
78
 
79
/* search for the right schedule branch to use for a periodic ed.
80
 * does some load balancing; returns the branch, or negative errno.
81
 */
82
static int balance (struct ohci_hcd *ohci, int interval, int load)
83
{
84
        int     i, branch = -ENOSPC;
85
 
86
        /* iso periods can be huge; iso tds specify frame numbers */
87
        if (interval > NUM_INTS)
88
                interval = NUM_INTS;
89
 
90
        /* search for the least loaded schedule branch of that period
91
         * that has enough bandwidth left unreserved.
92
         */
93
        for (i = 0; i < interval ; i++) {
94
                if (branch < 0 || ohci->load [branch] > ohci->load [i]) {
95
#if 1   /* CONFIG_USB_BANDWIDTH */
96
                        int     j;
97
 
98
                        /* usb 1.1 says 90% of one frame */
99
                        for (j = i; j < NUM_INTS; j += interval) {
100
                                if ((ohci->load [j] + load) > 900)
101
                                        break;
102
                        }
103
                        if (j < NUM_INTS)
104
                                continue;
105
#endif
106
                        branch = i;
107
                }
108
        }
109
        return branch;
110
}
111
 
112
/*-------------------------------------------------------------------------*/
113
 
114
/* both iso and interrupt requests have periods; this routine puts them
115
 * into the schedule tree in the apppropriate place.  most iso devices use
116
 * 1msec periods, but that's not required.
117
 */
118
static void periodic_link (struct ohci_hcd *ohci, struct ed *ed)
119
{
120
        unsigned        i;
121
 
122
        ohci_vdbg (ohci, "link %sed %p branch %d [%dus.], interval %d\n",
123
                (ed->hwINFO & ED_ISO) ? "iso " : "",
124
                ed, ed->branch, ed->load, ed->interval);
125
 
126
        for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
127
                struct ed       **prev = &ohci->periodic [i];
128
                u32             *prev_p = &ohci->hcca->int_table [i];
129
                struct ed       *here = *prev;
130
 
131
                /* sorting each branch by period (slow before fast)
132
                 * lets us share the faster parts of the tree.
133
                 * (plus maybe: put interrupt eds before iso)
134
                 */
135
                while (here && ed != here) {
136
                        if (ed->interval > here->interval)
137
                                break;
138
                        prev = &here->ed_next;
139
                        prev_p = &here->hwNextED;
140
                        here = *prev;
141
                }
142
                if (ed != here) {
143
                        ed->ed_next = here;
144
                        if (here)
145
                                ed->hwNextED = *prev_p;
146
                        wmb ();
147
                        *prev = ed;
148
                        *prev_p = cpu_to_le32p (&ed->dma);
149
                }
150
                ohci->load [i] += ed->load;
151
        }
152
        hcd_to_bus (&ohci->hcd)->bandwidth_allocated += ed->load / ed->interval;
153
}
154
 
155
/* link an ed into one of the HC chains */
156
 
157
static int ed_schedule (struct ohci_hcd *ohci, struct ed *ed)
158
{        
159
        int     branch;
160
 
161
        ed->state = ED_OPER;
162
        ed->ed_prev = 0;
163
        ed->ed_next = 0;
164
        ed->hwNextED = 0;
165
        wmb ();
166
 
167
        /* we care about rm_list when setting CLE/BLE in case the HC was at
168
         * work on some TD when CLE/BLE was turned off, and isn't quiesced
169
         * yet.  finish_unlinks() restarts as needed, some upcoming INTR_SF.
170
         *
171
         * control and bulk EDs are doubly linked (ed_next, ed_prev), but
172
         * periodic ones are singly linked (ed_next). that's because the
173
         * periodic schedule encodes a tree like figure 3-5 in the ohci
174
         * spec:  each qh can have several "previous" nodes, and the tree
175
         * doesn't have unused/idle descriptors.
176
         */
177
        switch (ed->type) {
178
        case PIPE_CONTROL:
179
                if (ohci->ed_controltail == NULL) {
180
                        writel (ed->dma, &ohci->regs->ed_controlhead);
181
                } else {
182
                        ohci->ed_controltail->ed_next = ed;
183
                        ohci->ed_controltail->hwNextED = cpu_to_le32 (ed->dma);
184
                }
185
                ed->ed_prev = ohci->ed_controltail;
186
                if (!ohci->ed_controltail && !ohci->ed_rm_list) {
187
                        ohci->hc_control |= OHCI_CTRL_CLE;
188
                        writel (0, &ohci->regs->ed_controlcurrent);
189
                        writel (ohci->hc_control, &ohci->regs->control);
190
                }
191
                ohci->ed_controltail = ed;
192
                break;
193
 
194
        case PIPE_BULK:
195
                if (ohci->ed_bulktail == NULL) {
196
                        writel (ed->dma, &ohci->regs->ed_bulkhead);
197
                } else {
198
                        ohci->ed_bulktail->ed_next = ed;
199
                        ohci->ed_bulktail->hwNextED = cpu_to_le32 (ed->dma);
200
                }
201
                ed->ed_prev = ohci->ed_bulktail;
202
                if (!ohci->ed_bulktail && !ohci->ed_rm_list) {
203
                        ohci->hc_control |= OHCI_CTRL_BLE;
204
                        writel (0, &ohci->regs->ed_bulkcurrent);
205
                        writel (ohci->hc_control, &ohci->regs->control);
206
                }
207
                ohci->ed_bulktail = ed;
208
                break;
209
 
210
        // case PIPE_INTERRUPT:
211
        // case PIPE_ISOCHRONOUS:
212
        default:
213
                branch = balance (ohci, ed->interval, ed->load);
214
                if (branch < 0) {
215
                        ohci_dbg (ohci,
216
                                "ERR %d, interval %d msecs, load %d\n",
217
                                branch, ed->interval, ed->load);
218
                        // FIXME if there are TDs queued, fail them!
219
                        return branch;
220
                }
221
                ed->branch = branch;
222
                periodic_link (ohci, ed);
223
        }              
224
 
225
        /* the HC may not see the schedule updates yet, but if it does
226
         * then they'll be properly ordered.
227
         */
228
        return 0;
229
}
230
 
231
/*-------------------------------------------------------------------------*/
232
 
233
/* scan the periodic table to find and unlink this ED */
234
static void periodic_unlink (struct ohci_hcd *ohci, struct ed *ed)
235
{
236
        int     i;
237
 
238
        for (i = ed->branch; i < NUM_INTS; i += ed->interval) {
239
                struct ed       *temp;
240
                struct ed       **prev = &ohci->periodic [i];
241
                u32             *prev_p = &ohci->hcca->int_table [i];
242
 
243
                while (*prev && (temp = *prev) != ed) {
244
                        prev_p = &temp->hwNextED;
245
                        prev = &temp->ed_next;
246
                }
247
                if (*prev) {
248
                        *prev_p = ed->hwNextED;
249
                        *prev = ed->ed_next;
250
                }
251
                ohci->load [i] -= ed->load;
252
        }      
253
        hcd_to_bus (&ohci->hcd)->bandwidth_allocated -= ed->load / ed->interval;
254
 
255
        ohci_vdbg (ohci, "unlink %sed %p branch %d [%dus.], interval %d\n",
256
                (ed->hwINFO & ED_ISO) ? "iso " : "",
257
                ed, ed->branch, ed->load, ed->interval);
258
}
259
 
260
/* unlink an ed from one of the HC chains.
261
 * just the link to the ed is unlinked.
262
 * the link from the ed still points to another operational ed or 0
263
 * so the HC can eventually finish the processing of the unlinked ed
264
 */
265
static void ed_deschedule (struct ohci_hcd *ohci, struct ed *ed)
266
{
267
        ed->hwINFO |= ED_SKIP;
268
 
269
        switch (ed->type) {
270
        case PIPE_CONTROL:
271
                if (ed->ed_prev == NULL) {
272
                        if (!ed->hwNextED) {
273
                                ohci->hc_control &= ~OHCI_CTRL_CLE;
274
                                writel (ohci->hc_control, &ohci->regs->control);
275
                                writel (0, &ohci->regs->ed_controlcurrent);
276
                                // post those pci writes
277
                                (void) readl (&ohci->regs->control);
278
                        }
279
                        writel (le32_to_cpup (&ed->hwNextED),
280
                                &ohci->regs->ed_controlhead);
281
                } else {
282
                        ed->ed_prev->ed_next = ed->ed_next;
283
                        ed->ed_prev->hwNextED = ed->hwNextED;
284
                }
285
                if (ohci->ed_controltail == ed) {
286
                        ohci->ed_controltail = ed->ed_prev;
287
                        if (ohci->ed_controltail)
288
                                ohci->ed_controltail->ed_next = 0;
289
                } else if (ed->ed_next) {
290
                        ed->ed_next->ed_prev = ed->ed_prev;
291
                }
292
                break;
293
 
294
        case PIPE_BULK:
295
                if (ed->ed_prev == NULL) {
296
                        if (!ed->hwNextED) {
297
                                ohci->hc_control &= ~OHCI_CTRL_BLE;
298
                                writel (ohci->hc_control, &ohci->regs->control);
299
                                writel (0, &ohci->regs->ed_bulkcurrent);
300
                                // post those pci writes
301
                                (void) readl (&ohci->regs->control);
302
                        }
303
                        writel (le32_to_cpup (&ed->hwNextED),
304
                                &ohci->regs->ed_bulkhead);
305
                } else {
306
                        ed->ed_prev->ed_next = ed->ed_next;
307
                        ed->ed_prev->hwNextED = ed->hwNextED;
308
                }
309
                if (ohci->ed_bulktail == ed) {
310
                        ohci->ed_bulktail = ed->ed_prev;
311
                        if (ohci->ed_bulktail)
312
                                ohci->ed_bulktail->ed_next = 0;
313
                } else if (ed->ed_next) {
314
                        ed->ed_next->ed_prev = ed->ed_prev;
315
                }
316
                break;
317
 
318
        // case PIPE_INTERRUPT:
319
        // case PIPE_ISOCHRONOUS:
320
        default:
321
                periodic_unlink (ohci, ed);
322
                break;
323
        }
324
 
325
        /* NOTE: Except for a couple of exceptionally clean unlink cases
326
         * (like unlinking the only c/b ED, with no TDs) HCs may still be
327
         * caching this operational ED (or its address).  Safe unlinking
328
         * involves not marking it ED_IDLE till INTR_SF; we always do that
329
         * if td_list isn't empty.  Otherwise the race is small; but ...
330
         */
331
        if (ed->state == ED_OPER) {
332
                ed->state = ED_IDLE;
333
                ed->hwINFO &= ~(ED_SKIP | ED_DEQUEUE);
334
                ed->hwHeadP &= ~ED_H;
335
                wmb ();
336
        }
337
}
338
 
339
 
340
/*-------------------------------------------------------------------------*/
341
 
342
/* get and maybe (re)init an endpoint. init _should_ be done only as part
343
 * of usb_set_configuration() or usb_set_interface() ... but the USB stack
344
 * isn't very stateful, so we re-init whenever the HC isn't looking.
345
 */
346
static struct ed *ed_get (
347
        struct ohci_hcd         *ohci,
348
        struct usb_device       *udev,
349
        unsigned int            pipe,
350
        int                     interval
351
) {
352
        int                     is_out = !usb_pipein (pipe);
353
        int                     type = usb_pipetype (pipe);
354
        struct hcd_dev          *dev = (struct hcd_dev *) udev->hcpriv;
355
        struct ed               *ed;
356
        unsigned                ep;
357
        unsigned long           flags;
358
 
359
        ep = usb_pipeendpoint (pipe) << 1;
360
        if (type != PIPE_CONTROL && is_out)
361
                ep |= 1;
362
 
363
        spin_lock_irqsave (&ohci->lock, flags);
364
 
365
        if (!(ed = dev->ep [ep])) {
366
                struct td       *td;
367
 
368
                ed = ed_alloc (ohci, SLAB_ATOMIC);
369
                if (!ed) {
370
                        /* out of memory */
371
                        goto done;
372
                }
373
                dev->ep [ep] = ed;
374
 
375
                /* dummy td; end of td list for ed */
376
                td = td_alloc (ohci, SLAB_ATOMIC);
377
                if (!td) {
378
                        /* out of memory */
379
                        ed_free (ohci, ed);
380
                        ed = 0;
381
                        goto done;
382
                }
383
                ed->dummy = td;
384
                ed->hwTailP = cpu_to_le32 (td->td_dma);
385
                ed->hwHeadP = ed->hwTailP;      /* ED_C, ED_H zeroed */
386
                ed->state = ED_IDLE;
387
                ed->type = type;
388
        }
389
 
390
        /* NOTE: only ep0 currently needs this "re"init logic, during
391
         * enumeration (after set_address, or if ep0 maxpacket >8).
392
         */
393
        if (ed->state == ED_IDLE) {
394
                u32     info;
395
 
396
                info = usb_pipedevice (pipe);
397
                info |= (ep >> 1) << 7;
398
                info |= usb_maxpacket (udev, pipe, is_out) << 16;
399
                info = cpu_to_le32 (info);
400
                if (udev->speed == USB_SPEED_LOW)
401
                        info |= ED_LOWSPEED;
402
                /* only control transfers store pids in tds */
403
                if (type != PIPE_CONTROL) {
404
                        info |= is_out ? ED_OUT : ED_IN;
405
                        if (type != PIPE_BULK) {
406
                                /* periodic transfers... */
407
                                if (type == PIPE_ISOCHRONOUS)
408
                                        info |= ED_ISO;
409
                                else if (interval > 32) /* iso can be bigger */
410
                                        interval = 32;
411
                                ed->interval = interval;
412
                                ed->load = usb_calc_bus_time (
413
                                        udev->speed, !is_out,
414
                                        type == PIPE_ISOCHRONOUS,
415
                                        usb_maxpacket (udev, pipe, is_out))
416
                                                / 1000;
417
                        }
418
                }
419
                ed->hwINFO = info;
420
        }
421
 
422
done:
423
        spin_unlock_irqrestore (&ohci->lock, flags);
424
        return ed;
425
}
426
 
427
/*-------------------------------------------------------------------------*/
428
 
429
/* request unlinking of an endpoint from an operational HC.
430
 * put the ep on the rm_list
431
 * real work is done at the next start frame (SF) hardware interrupt
432
 */
433
static void start_urb_unlink (struct ohci_hcd *ohci, struct ed *ed)
434
{    
435
        ed->hwINFO |= ED_DEQUEUE;
436
        ed->state = ED_UNLINK;
437
        ed_deschedule (ohci, ed);
438
 
439
        /* SF interrupt might get delayed; record the frame counter value that
440
         * indicates when the HC isn't looking at it, so concurrent unlinks
441
         * behave.  frame_no wraps every 2^16 msec, and changes right before
442
         * SF is triggered.
443
         */
444
        ed->tick = le16_to_cpu (ohci->hcca->frame_no) + 1;
445
 
446
        /* rm_list is just singly linked, for simplicity */
447
        ed->ed_next = ohci->ed_rm_list;
448
        ed->ed_prev = 0;
449
        ohci->ed_rm_list = ed;
450
 
451
        /* enable SOF interrupt */
452
        if (HCD_IS_RUNNING (ohci->hcd.state)) {
453
                writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
454
                writel (OHCI_INTR_SF, &ohci->regs->intrenable);
455
                // flush those pci writes
456
                (void) readl (&ohci->regs->control);
457
        }
458
}
459
 
460
/*-------------------------------------------------------------------------*
461
 * TD handling functions
462
 *-------------------------------------------------------------------------*/
463
 
464
/* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
465
 
466
static void
467
td_fill (struct ohci_hcd *ohci, u32 info,
468
        dma_addr_t data, int len,
469
        struct urb *urb, int index)
470
{
471
        struct td               *td, *td_pt;
472
        struct urb_priv         *urb_priv = urb->hcpriv;
473
        int                     is_iso = info & TD_ISO;
474
        int                     hash;
475
 
476
        // ASSERT (index < urb_priv->length);
477
 
478
        /* aim for only one interrupt per urb.  mostly applies to control
479
         * and iso; other urbs rarely need more than one TD per urb.
480
         * this way, only final tds (or ones with an error) cause IRQs.
481
         * at least immediately; use DI=6 in case any control request is
482
         * tempted to die part way through.
483
         *
484
         * NOTE: could delay interrupts even for the last TD, and get fewer
485
         * interrupts ... increasing per-urb latency by sharing interrupts.
486
         * Drivers that queue bulk urbs may request that behavior.
487
         */
488
        if (index != (urb_priv->length - 1)
489
                        || (urb->transfer_flags & URB_NO_INTERRUPT))
490
                info |= TD_DI_SET (6);
491
 
492
        /* use this td as the next dummy */
493
        td_pt = urb_priv->td [index];
494
 
495
        /* fill the old dummy TD */
496
        td = urb_priv->td [index] = urb_priv->ed->dummy;
497
        urb_priv->ed->dummy = td_pt;
498
 
499
        td->ed = urb_priv->ed;
500
        td->next_dl_td = NULL;
501
        td->index = index;
502
        td->urb = urb;
503
        td->data_dma = data;
504
        if (!len)
505
                data = 0;
506
 
507
        td->hwINFO = cpu_to_le32 (info);
508
        if (is_iso) {
509
                td->hwCBP = cpu_to_le32 (data & 0xFFFFF000);
510
                td->hwPSW [0] = cpu_to_le16 ((data & 0x0FFF) | 0xE000);
511
                td->ed->last_iso = info & 0xffff;
512
        } else {
513
                td->hwCBP = cpu_to_le32 (data);
514
        }                      
515
        if (data)
516
                td->hwBE = cpu_to_le32 (data + len - 1);
517
        else
518
                td->hwBE = 0;
519
        td->hwNextTD = cpu_to_le32 (td_pt->td_dma);
520
 
521
        /* append to queue */
522
        list_add_tail (&td->td_list, &td->ed->td_list);
523
 
524
        /* hash it for later reverse mapping */
525
        hash = TD_HASH_FUNC (td->td_dma);
526
        td->td_hash = ohci->td_hash [hash];
527
        ohci->td_hash [hash] = td;
528
 
529
        /* HC might read the TD (or cachelines) right away ... */
530
        wmb ();
531
        td->ed->hwTailP = td->hwNextTD;
532
}
533
 
534
/*-------------------------------------------------------------------------*/
535
 
536
/* Prepare all TDs of a transfer, and queue them onto the ED.
537
 * Caller guarantees HC is active.
538
 * Usually the ED is already on the schedule, so TDs might be
539
 * processed as soon as they're queued.
540
 */
541
static void td_submit_urb (
542
        struct ohci_hcd *ohci,
543
        struct urb      *urb
544
) {
545
        struct urb_priv *urb_priv = urb->hcpriv;
546
        dma_addr_t      data;
547
        int             data_len = urb->transfer_buffer_length;
548
        int             cnt = 0;
549
        u32             info = 0;
550
        int             is_out = usb_pipeout (urb->pipe);
551
 
552
        /* OHCI handles the bulk/interrupt data toggles itself.  We just
553
         * use the device toggle bits for resetting, and rely on the fact
554
         * that resetting toggle is meaningless if the endpoint is active.
555
         */
556
        if (!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), is_out)) {
557
                usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe),
558
                        is_out, 1);
559
                urb_priv->ed->hwHeadP &= ~ED_C;
560
        }
561
 
562
        urb_priv->td_cnt = 0;
563
 
564
        if (data_len)
565
                data = urb->transfer_dma;
566
        else
567
                data = 0;
568
 
569
        /* NOTE:  TD_CC is set so we can tell which TDs the HC processed by
570
         * using TD_CC_GET, as well as by seeing them on the done list.
571
         * (CC = NotAccessed ... 0x0F, or 0x0E in PSWs for ISO.)
572
         */
573
        switch (urb_priv->ed->type) {
574
 
575
        /* Bulk and interrupt are identical except for where in the schedule
576
         * their EDs live.
577
         */
578
        case PIPE_INTERRUPT:
579
                /* ... and periodic urbs have extra accounting */
580
                hcd_to_bus (&ohci->hcd)->bandwidth_int_reqs++;
581
                /* FALLTHROUGH */
582
        case PIPE_BULK:
583
                info = is_out
584
                        ? TD_T_TOGGLE | TD_CC | TD_DP_OUT
585
                        : TD_T_TOGGLE | TD_CC | TD_DP_IN;
586
                /* TDs _could_ transfer up to 8K each */
587
                while (data_len > 4096) {
588
                        td_fill (ohci, info, data, 4096, urb, cnt);
589
                        data += 4096;
590
                        data_len -= 4096;
591
                        cnt++;
592
                }
593
                /* maybe avoid ED halt on final TD short read */
594
                if (!(urb->transfer_flags & URB_SHORT_NOT_OK))
595
                        info |= TD_R;
596
                td_fill (ohci, info, data, data_len, urb, cnt);
597
                cnt++;
598
                if ((urb->transfer_flags & URB_ZERO_PACKET)
599
                                && cnt < urb_priv->length) {
600
                        td_fill (ohci, info, 0, 0, urb, cnt);
601
                        cnt++;
602
                }
603
                /* maybe kickstart bulk list */
604
                if (urb_priv->ed->type == PIPE_BULK) {
605
                        wmb ();
606
                        writel (OHCI_BLF, &ohci->regs->cmdstatus);
607
                }
608
                break;
609
 
610
        /* control manages DATA0/DATA1 toggle per-request; SETUP resets it,
611
         * any DATA phase works normally, and the STATUS ack is special.
612
         */
613
        case PIPE_CONTROL:
614
                info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
615
                td_fill (ohci, info, urb->setup_dma, 8, urb, cnt++);
616
                if (data_len > 0) {
617
                        info = TD_CC | TD_R | TD_T_DATA1;
618
                        info |= is_out ? TD_DP_OUT : TD_DP_IN;
619
                        /* NOTE:  mishandles transfers >8K, some >4K */
620
                        td_fill (ohci, info, data, data_len, urb, cnt++);
621
                }
622
                info = is_out
623
                        ? TD_CC | TD_DP_IN | TD_T_DATA1
624
                        : TD_CC | TD_DP_OUT | TD_T_DATA1;
625
                td_fill (ohci, info, data, 0, urb, cnt++);
626
                /* maybe kickstart control list */
627
                wmb ();
628
                writel (OHCI_CLF, &ohci->regs->cmdstatus);
629
                break;
630
 
631
        /* ISO has no retransmit, so no toggle; and it uses special TDs.
632
         * Each TD could handle multiple consecutive frames (interval 1);
633
         * we could often reduce the number of TDs here.
634
         */
635
        case PIPE_ISOCHRONOUS:
636
                for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
637
                        int     frame = urb->start_frame;
638
 
639
                        // FIXME scheduling should handle frame counter
640
                        // roll-around ... exotic case (and OHCI has
641
                        // a 2^16 iso range, vs other HCs max of 2^10)
642
                        frame += cnt * urb->interval;
643
                        frame &= 0xffff;
644
                        td_fill (ohci, TD_CC | TD_ISO | frame,
645
                                data + urb->iso_frame_desc [cnt].offset,
646
                                urb->iso_frame_desc [cnt].length, urb, cnt);
647
                }
648
                hcd_to_bus (&ohci->hcd)->bandwidth_isoc_reqs++;
649
                break;
650
        }
651
        // ASSERT (urb_priv->length == cnt);
652
}
653
 
654
/*-------------------------------------------------------------------------*
655
 * Done List handling functions
656
 *-------------------------------------------------------------------------*/
657
 
658
/* calculate transfer length/status and update the urb
659
 * PRECONDITION:  irqsafe (only for urb->status locking)
660
 */
661
static void td_done (struct ohci_hcd *ohci, struct urb *urb, struct td *td)
662
{
663
        u32     tdINFO = le32_to_cpup (&td->hwINFO);
664
        int     cc = 0;
665
 
666
        list_del (&td->td_list);
667
 
668
        /* ISO ... drivers see per-TD length/status */
669
        if (tdINFO & TD_ISO) {
670
                u16     tdPSW = le16_to_cpu (td->hwPSW [0]);
671
                int     dlen = 0;
672
 
673
                /* NOTE:  assumes FC in tdINFO == 0 (and MAXPSW == 1) */
674
 
675
                cc = (tdPSW >> 12) & 0xF;
676
                if (tdINFO & TD_CC)     /* hc didn't touch? */
677
                        return;
678
 
679
                if (usb_pipeout (urb->pipe))
680
                        dlen = urb->iso_frame_desc [td->index].length;
681
                else {
682
                        /* short reads are always OK for ISO */
683
                        if (cc == TD_DATAUNDERRUN)
684
                                cc = TD_CC_NOERROR;
685
                        dlen = tdPSW & 0x3ff;
686
                }
687
                urb->actual_length += dlen;
688
                urb->iso_frame_desc [td->index].actual_length = dlen;
689
                urb->iso_frame_desc [td->index].status = cc_to_error [cc];
690
 
691
                if (cc != TD_CC_NOERROR)
692
                        ohci_vdbg (ohci,
693
                                "urb %p iso td %p (%d) len %d cc %d\n",
694
                                urb, td, 1 + td->index, dlen, cc);
695
 
696
        /* BULK, INT, CONTROL ... drivers see aggregate length/status,
697
         * except that "setup" bytes aren't counted and "short" transfers
698
         * might not be reported as errors.
699
         */
700
        } else {
701
                int     type = usb_pipetype (urb->pipe);
702
                u32     tdBE = le32_to_cpup (&td->hwBE);
703
 
704
                cc = TD_CC_GET (tdINFO);
705
 
706
                /* control endpoints only have soft stalls */
707
                if (type != PIPE_CONTROL && cc == TD_CC_STALL)
708
                        usb_endpoint_halt (urb->dev,
709
                                usb_pipeendpoint (urb->pipe),
710
                                usb_pipeout (urb->pipe));
711
 
712
                /* update packet status if needed (short is normally ok) */
713
                if (cc == TD_DATAUNDERRUN
714
                                && !(urb->transfer_flags & URB_SHORT_NOT_OK))
715
                        cc = TD_CC_NOERROR;
716
                if (cc != TD_CC_NOERROR && cc < 0x0E) {
717
                        spin_lock (&urb->lock);
718
                        if (urb->status == -EINPROGRESS)
719
                                urb->status = cc_to_error [cc];
720
                        spin_unlock (&urb->lock);
721
                }
722
 
723
                /* count all non-empty packets except control SETUP packet */
724
                if ((type != PIPE_CONTROL || td->index != 0) && tdBE != 0) {
725
                        if (td->hwCBP == 0)
726
                                urb->actual_length += tdBE - td->data_dma + 1;
727
                        else
728
                                urb->actual_length +=
729
                                          le32_to_cpup (&td->hwCBP)
730
                                        - td->data_dma;
731
                }
732
 
733
                if (cc != TD_CC_NOERROR && cc < 0x0E)
734
                        ohci_vdbg (ohci,
735
                                "urb %p td %p (%d) cc %d, len=%d/%d\n",
736
                                urb, td, 1 + td->index, cc,
737
                                urb->actual_length,
738
                                urb->transfer_buffer_length);
739
        }
740
}
741
 
742
/*-------------------------------------------------------------------------*/
743
 
744
static inline struct td *
745
ed_halted (struct ohci_hcd *ohci, struct td *td, int cc, struct td *rev)
746
{
747
        struct urb              *urb = td->urb;
748
        struct ed               *ed = td->ed;
749
        struct list_head        *tmp = td->td_list.next;
750
        u32                     toggle = ed->hwHeadP & ED_C;
751
 
752
        /* clear ed halt; this is the td that caused it, but keep it inactive
753
         * until its urb->complete() has a chance to clean up.
754
         */
755
        ed->hwINFO |= ED_SKIP;
756
        wmb ();
757
        ed->hwHeadP &= ~ED_H;
758
 
759
        /* put any later tds from this urb onto the donelist, after 'td',
760
         * order won't matter here: no errors, and nothing was transferred.
761
         * also patch the ed so it looks as if those tds completed normally.
762
         */
763
        while (tmp != &ed->td_list) {
764
                struct td       *next;
765
                u32             info;
766
 
767
                next = list_entry (tmp, struct td, td_list);
768
                tmp = next->td_list.next;
769
 
770
                if (next->urb != urb)
771
                        break;
772
 
773
                /* NOTE: if multi-td control DATA segments get supported,
774
                 * this urb had one of them, this td wasn't the last td
775
                 * in that segment (TD_R clear), this ed halted because
776
                 * of a short read, _and_ URB_SHORT_NOT_OK is clear ...
777
                 * then we need to leave the control STATUS packet queued
778
                 * and clear ED_SKIP.
779
                 */
780
                info = next->hwINFO;
781
                info |= cpu_to_le32 (TD_DONE);
782
                info &= ~cpu_to_le32 (TD_CC);
783
                next->hwINFO = info;
784
 
785
                next->next_dl_td = rev;
786
                rev = next;
787
 
788
                if (ed->hwTailP == cpu_to_le32 (next->td_dma))
789
                        ed->hwTailP = next->hwNextTD;
790
                ed->hwHeadP = next->hwNextTD | toggle;
791
        }
792
 
793
        /* help for troubleshooting:  report anything that
794
         * looks odd ... that doesn't include protocol stalls
795
         * (or maybe some other things)
796
         */
797
        switch (cc) {
798
        case TD_DATAUNDERRUN:
799
                if ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)
800
                        break;
801
                /* fallthrough */
802
        case TD_CC_STALL:
803
                if (usb_pipecontrol (urb->pipe))
804
                        break;
805
                /* fallthrough */
806
        default:
807
                ohci_dbg (ohci,
808
                        "urb %p path %s ep%d%s %08x cc %d --> status %d\n",
809
                        urb, urb->dev->devpath,
810
                        usb_pipeendpoint (urb->pipe),
811
                        usb_pipein (urb->pipe) ? "in" : "out",
812
                        le32_to_cpu (td->hwINFO),
813
                        cc, cc_to_error [cc]);
814
        }
815
 
816
        return rev;
817
}
818
 
819
/* replies to the request have to be on a FIFO basis so
820
 * we unreverse the hc-reversed done-list
821
 */
822
static struct td *dl_reverse_done_list (struct ohci_hcd *ohci)
823
{
824
        u32             td_dma;
825
        struct td       *td_rev = NULL;
826
        struct td       *td = NULL;
827
        unsigned long   flags;
828
 
829
        spin_lock_irqsave (&ohci->lock, flags);
830
 
831
        td_dma = le32_to_cpup (&ohci->hcca->done_head);
832
        ohci->hcca->done_head = 0;
833
 
834
        /* get TD from hc's singly linked list, and
835
         * prepend to ours.  ed->td_list changes later.
836
         */
837
        while (td_dma) {               
838
                int             cc;
839
 
840
                td = dma_to_td (ohci, td_dma);
841
                if (!td) {
842
                        ohci_err (ohci, "bad entry %8x\n", td_dma);
843
                        break;
844
                }
845
 
846
                td->hwINFO |= cpu_to_le32 (TD_DONE);
847
                cc = TD_CC_GET (le32_to_cpup (&td->hwINFO));
848
 
849
                /* Non-iso endpoints can halt on error; un-halt,
850
                 * and dequeue any other TDs from this urb.
851
                 * No other TD could have caused the halt.
852
                 */
853
                if (cc != TD_CC_NOERROR && (td->ed->hwHeadP & ED_H))
854
                        td_rev = ed_halted (ohci, td, cc, td_rev);
855
 
856
                td->next_dl_td = td_rev;       
857
                td_rev = td;
858
                td_dma = le32_to_cpup (&td->hwNextTD);
859
        }      
860
        spin_unlock_irqrestore (&ohci->lock, flags);
861
        return td_rev;
862
}
863
 
864
/*-------------------------------------------------------------------------*/
865
 
866
/* wrap-aware logic stolen from <linux/jiffies26.h> */
867
#define tick_before(t1,t2) ((((s16)(t1))-((s16)(t2))) < 0)
868
 
869
/* there are some urbs/eds to unlink; called in_irq(), with HCD locked */
870
static void
871
finish_unlinks (struct ohci_hcd *ohci, u16 tick, struct pt_regs *regs)
872
{
873
        struct ed       *ed, **last;
874
 
875
rescan_all:
876
        for (last = &ohci->ed_rm_list, ed = *last; ed != NULL; ed = *last) {
877
                struct list_head        *entry, *tmp;
878
                int                     completed, modified;
879
                u32                     *prev;
880
 
881
                /* only take off EDs that the HC isn't using, accounting for
882
                 * frame counter wraps.
883
                 */
884
                if (tick_before (tick, ed->tick)
885
                                && HCD_IS_RUNNING(ohci->hcd.state)) {
886
                        last = &ed->ed_next;
887
                        continue;
888
                }
889
 
890
                /* reentrancy:  if we drop the schedule lock, someone might
891
                 * have modified this list.  normally it's just prepending
892
                 * entries (which we'd ignore), but paranoia won't hurt.
893
                 */
894
                *last = ed->ed_next;
895
                ed->ed_next = 0;
896
                modified = 0;
897
 
898
                /* unlink urbs as requested, but rescan the list after
899
                 * we call a completion since it might have unlinked
900
                 * another (earlier) urb
901
                 */
902
rescan_this:
903
                completed = 0;
904
                prev = &ed->hwHeadP;
905
                list_for_each_safe (entry, tmp, &ed->td_list) {
906
                        struct td       *td;
907
                        struct urb      *urb;
908
                        urb_priv_t      *urb_priv;
909
                        u32             savebits;
910
 
911
                        td = list_entry (entry, struct td, td_list);
912
                        urb = td->urb;
913
                        urb_priv = td->urb->hcpriv;
914
 
915
                        if (urb->status == -EINPROGRESS) {
916
                                prev = &td->hwNextTD;
917
                                continue;
918
                        }
919
 
920
                        /* patch pointers hc uses ... tail, if we're removing
921
                         * an otherwise active td, and whatever td pointer
922
                         * points to this td
923
                         */
924
                        if (ed->hwTailP == cpu_to_le32 (td->td_dma))
925
                                ed->hwTailP = td->hwNextTD;
926
                        savebits = *prev & ~cpu_to_le32 (TD_MASK);
927
                        *prev = td->hwNextTD | savebits;
928
 
929
                        /* HC may have partly processed this TD */
930
                        td_done (ohci, urb, td);
931
                        urb_priv->td_cnt++;
932
 
933
                        /* if URB is done, clean up */
934
                        if (urb_priv->td_cnt == urb_priv->length) {
935
                                modified = completed = 1;
936
                                spin_unlock (&ohci->lock);
937
                                finish_urb (ohci, urb, regs);
938
                                spin_lock (&ohci->lock);
939
                        }
940
                }
941
                if (completed && !list_empty (&ed->td_list))
942
                        goto rescan_this;
943
 
944
                /* ED's now officially unlinked, hc doesn't see */
945
                ed->state = ED_IDLE;
946
                ed->hwINFO &= ~(ED_SKIP | ED_DEQUEUE);
947
                ed->hwHeadP &= ~ED_H;
948
                ed->hwNextED = 0;
949
 
950
                /* but if there's work queued, reschedule */
951
                if (!list_empty (&ed->td_list)) {
952
                        if (HCD_IS_RUNNING(ohci->hcd.state))
953
                                ed_schedule (ohci, ed);
954
                }
955
 
956
                if (modified)
957
                        goto rescan_all;
958
        }
959
 
960
        /* maybe reenable control and bulk lists */
961
        if (HCD_IS_RUNNING(ohci->hcd.state) && !ohci->ed_rm_list) {
962
                u32     command = 0, control = 0;
963
 
964
                if (ohci->ed_controltail) {
965
                        command |= OHCI_CLF;
966
                        if (!(ohci->hc_control & OHCI_CTRL_CLE)) {
967
                                control |= OHCI_CTRL_CLE;
968
                                writel (0, &ohci->regs->ed_controlcurrent);
969
                        }
970
                }
971
                if (ohci->ed_bulktail) {
972
                        command |= OHCI_BLF;
973
                        if (!(ohci->hc_control & OHCI_CTRL_BLE)) {
974
                                control |= OHCI_CTRL_BLE;
975
                                writel (0, &ohci->regs->ed_bulkcurrent);
976
                        }
977
                }
978
 
979
                /* CLE/BLE to enable, CLF/BLF to (maybe) kickstart */
980
                if (control) {
981
                        ohci->hc_control |= control;
982
                        writel (ohci->hc_control, &ohci->regs->control);  
983
                }
984
                if (command)
985
                        writel (command, &ohci->regs->cmdstatus);  
986
        }
987
}
988
 
989
 
990
 
991
/*-------------------------------------------------------------------------*/
992
 
993
/*
994
 * Process normal completions (error or success) and clean the schedules.
995
 *
996
 * This is the main path for handing urbs back to drivers.  The only other
997
 * path is finish_unlinks(), which unlinks URBs using ed_rm_list, instead of
998
 * scanning the (re-reversed) donelist as this does.
999
 */
1000
static void
1001
dl_done_list (struct ohci_hcd *ohci, struct td *td, struct pt_regs *regs)
1002
{
1003
        unsigned long   flags;
1004
 
1005
        spin_lock_irqsave (&ohci->lock, flags);
1006
        while (td) {
1007
                struct td       *td_next = td->next_dl_td;
1008
                struct urb      *urb = td->urb;
1009
                urb_priv_t      *urb_priv = urb->hcpriv;
1010
                struct ed       *ed = td->ed;
1011
 
1012
                /* update URB's length and status from TD */
1013
                td_done (ohci, urb, td);
1014
                urb_priv->td_cnt++;
1015
 
1016
                /* If all this urb's TDs are done, call complete() */
1017
                if (urb_priv->td_cnt == urb_priv->length) {
1018
                        spin_unlock (&ohci->lock);
1019
                        finish_urb (ohci, urb, regs);
1020
                        spin_lock (&ohci->lock);
1021
                }
1022
 
1023
                /* clean schedule:  unlink EDs that are no longer busy */
1024
                if (list_empty (&ed->td_list))
1025
                        ed_deschedule (ohci, ed);
1026
                /* ... reenabling halted EDs only after fault cleanup */
1027
                else if ((ed->hwINFO & (ED_SKIP | ED_DEQUEUE)) == ED_SKIP) {
1028
                        td = list_entry (ed->td_list.next, struct td, td_list);
1029
                        if (!(td->hwINFO & TD_DONE)) {
1030
                                ed->hwINFO &= ~ED_SKIP;
1031
                                /* ... hc may need waking-up */
1032
                                switch (ed->type) {
1033
                                case PIPE_CONTROL:
1034
                                        writel (OHCI_CLF,
1035
                                                &ohci->regs->cmdstatus);  
1036
                                        break;
1037
                                case PIPE_BULK:
1038
                                        writel (OHCI_BLF,
1039
                                                &ohci->regs->cmdstatus);  
1040
                                        break;
1041
                                }
1042
                        }
1043
                }
1044
 
1045
                td = td_next;
1046
        }  
1047
        spin_unlock_irqrestore (&ohci->lock, flags);
1048
}