ehci-sched.c
来自「linux-2.4.29操作系统的源码」· C语言 代码 · 共 1,350 行 · 第 1/3 页
C
1,350 行
goto ready; } } while (++start < max); /* no room in the schedule */ ehci_dbg (ehci, "iso %ssched full %p (now %d end %d max %d)\n", list_empty (&stream->itd_list) ? "" : "re", urb, now, end, max); status = -ENOSPC;fail: itd_sched_free (stream, itd_sched); urb->hcpriv = 0; return status;ready: urb->start_frame = stream->next_uframe; return 0;}/*-------------------------------------------------------------------------*/static inline voiditd_init (struct ehci_iso_stream *stream, struct ehci_itd *itd){ int i; itd->hw_next = EHCI_LIST_END; itd->hw_bufp [0] = stream->buf0; itd->hw_bufp [1] = stream->buf1; itd->hw_bufp [2] = stream->buf2; for (i = 0; i < 8; i++) itd->index[i] = -1; /* All other fields are filled when scheduling */}static inline voiditd_patch ( struct ehci_itd *itd, struct ehci_itd_sched *itd_sched, unsigned index, u16 uframe, int first){ struct ehci_iso_uframe *uf = &itd_sched->packet [index]; unsigned pg = itd->pg; // BUG_ON (pg == 6 && uf->cross); uframe &= 0x07; itd->index [uframe] = index; itd->hw_transaction [uframe] = uf->transaction; itd->hw_transaction [uframe] |= cpu_to_le32 (pg << 12); itd->hw_bufp [pg] |= cpu_to_le32 (uf->bufp & ~(u32)0); itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(uf->bufp >> 32)); /* iso_frame_desc[].offset must be strictly increasing */ if (unlikely (!first && uf->cross)) { u64 bufp = uf->bufp + 4096; itd->pg = ++pg; itd->hw_bufp [pg] |= cpu_to_le32 (bufp & ~(u32)0); itd->hw_bufp_hi [pg] |= cpu_to_le32 ((u32)(bufp >> 32)); }}static inline voiditd_link (struct ehci_hcd *ehci, unsigned frame, struct ehci_itd *itd){ /* always prepend ITD/SITD ... only QH tree is order-sensitive */ itd->itd_next = ehci->pshadow [frame]; itd->hw_next = ehci->periodic [frame]; ehci->pshadow [frame].itd = itd; itd->frame = frame; wmb (); ehci->periodic [frame] = cpu_to_le32 (itd->itd_dma) | Q_TYPE_ITD;}/* fit urb's itds into the selected schedule slot; activate as needed */static intitd_link_urb ( struct ehci_hcd *ehci, struct urb *urb, unsigned mod, struct ehci_iso_stream *stream){ int packet, first = 1; unsigned next_uframe, uframe, frame; struct ehci_itd_sched *itd_sched = urb->hcpriv; struct ehci_itd *itd; next_uframe = stream->next_uframe % mod; if (unlikely (list_empty(&stream->itd_list))) { hcd_to_bus (&ehci->hcd)->bandwidth_allocated += stream->bandwidth; ehci_vdbg (ehci, "schedule devp %s ep%d%s-iso period %d start %d.%d\n", urb->dev->devpath, stream->bEndpointAddress & 0x0f, (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out", urb->interval, next_uframe >> 3, next_uframe & 0x7); stream->start = jiffies; } hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs++; /* fill iTDs uframe by uframe */ for (packet = 0, itd = 0; packet < urb->number_of_packets; ) { if (itd == 0) { /* ASSERT: we have all necessary itds */ // BUG_ON (list_empty (&itd_sched->itd_list)); /* ASSERT: no itds for this endpoint in this uframe */ itd = list_entry (itd_sched->itd_list.next, struct ehci_itd, itd_list); list_move_tail (&itd->itd_list, &stream->itd_list); itd->stream = iso_stream_get (stream); itd->urb = usb_get_urb (urb); first = 1; itd_init (stream, itd); } uframe = next_uframe & 0x07; frame = next_uframe >> 3; itd->usecs [uframe] = stream->usecs; itd_patch (itd, itd_sched, packet, uframe, first); first = 0; next_uframe += stream->interval; next_uframe %= mod; packet++; /* link completed itds into the schedule */ if (((next_uframe >> 3) != frame) || packet == urb->number_of_packets) { itd_link (ehci, frame % ehci->periodic_size, itd); itd = 0; } } stream->next_uframe = next_uframe; /* don't need that schedule data any more */ itd_sched_free (stream, itd_sched); urb->hcpriv = 0; if (unlikely (!ehci->periodic_sched++)) return enable_periodic (ehci); return 0;}#define ISO_ERRS (EHCI_ISOC_BUF_ERR | EHCI_ISOC_BABBLE | EHCI_ISOC_XACTERR)static unsigneditd_complete ( struct ehci_hcd *ehci, struct ehci_itd *itd, struct pt_regs *regs) { struct urb *urb = itd->urb; struct usb_iso_packet_descriptor *desc; u32 t; unsigned uframe; int urb_index = -1; struct ehci_iso_stream *stream = itd->stream; struct usb_device *dev; /* for each uframe with a packet */ for (uframe = 0; uframe < 8; uframe++) { if (likely (itd->index[uframe] == -1)) continue; urb_index = itd->index[uframe]; desc = &urb->iso_frame_desc [urb_index]; t = le32_to_cpup (&itd->hw_transaction [uframe]); itd->hw_transaction [uframe] = 0; /* report transfer status */ if (unlikely (t & ISO_ERRS)) { urb->error_count++; if (t & EHCI_ISOC_BUF_ERR) desc->status = usb_pipein (urb->pipe) ? -ENOSR /* hc couldn't read */ : -ECOMM; /* hc couldn't write */ else if (t & EHCI_ISOC_BABBLE) desc->status = -EOVERFLOW; else /* (t & EHCI_ISOC_XACTERR) */ desc->status = -EPROTO; /* HC need not update length with this error */ if (!(t & EHCI_ISOC_BABBLE)) desc->actual_length = EHCI_ITD_LENGTH (t); } else if (likely ((t & EHCI_ISOC_ACTIVE) == 0)) { desc->status = 0; desc->actual_length = EHCI_ITD_LENGTH (t); } } usb_put_urb (urb); itd->urb = 0; itd->stream = 0; list_move (&itd->itd_list, &stream->free_itd_list); iso_stream_put (ehci, stream); /* handle completion now? */ if (likely ((urb_index + 1) != urb->number_of_packets)) return 0; /* ASSERT: it's really the last itd for this urb list_for_each_entry (itd, &stream->itd_list, itd_list) BUG_ON (itd->urb == urb); */ /* give urb back to the driver ... can be out-of-order */ //dev = usb_get_dev (urb->dev); dev = urb->dev; ehci_urb_done (ehci, urb, regs); urb = 0; /* defer stopping schedule; completion can submit */ ehci->periodic_sched--; if (unlikely (!ehci->periodic_sched)) (void) disable_periodic (ehci); hcd_to_bus (&ehci->hcd)->bandwidth_isoc_reqs--; if (unlikely (list_empty (&stream->itd_list))) { hcd_to_bus (&ehci->hcd)->bandwidth_allocated -= stream->bandwidth; ehci_vdbg (ehci, "deschedule devp %s ep%d%s-iso\n", dev->devpath, stream->bEndpointAddress & 0x0f, (stream->bEndpointAddress & USB_DIR_IN) ? "in" : "out"); } iso_stream_put (ehci, stream); //usb_put_dev (dev); return 1;}/*-------------------------------------------------------------------------*/static int itd_submit (struct ehci_hcd *ehci, struct urb *urb, int mem_flags){ int status = -EINVAL; unsigned long flags; struct ehci_iso_stream *stream; /* Get iso_stream head */ stream = iso_stream_find (ehci, urb); if (unlikely (stream == 0)) { ehci_dbg (ehci, "can't get iso stream\n"); return -ENOMEM; } if (unlikely (urb->interval != stream->interval)) { ehci_dbg (ehci, "can't change iso interval %d --> %d\n", stream->interval, urb->interval); goto done; }#ifdef EHCI_URB_TRACE ehci_dbg (ehci, "%s %s urb %p ep%d%s len %d, %d pkts %d uframes [%p]\n", __FUNCTION__, urb->dev->devpath, urb, usb_pipeendpoint (urb->pipe), usb_pipein (urb->pipe) ? "in" : "out", urb->transfer_buffer_length, urb->number_of_packets, urb->interval, stream);#endif /* allocate ITDs w/o locking anything */ status = itd_urb_transaction (stream, ehci, urb, mem_flags); if (unlikely (status < 0)) { ehci_dbg (ehci, "can't init itds\n"); goto done; } /* schedule ... need to lock */ spin_lock_irqsave (&ehci->lock, flags); status = itd_stream_schedule (ehci, urb, stream); if (likely (status == 0)) itd_link_urb (ehci, urb, ehci->periodic_size << 3, stream); spin_unlock_irqrestore (&ehci->lock, flags);done: if (unlikely (status < 0)) iso_stream_put (ehci, stream); return status;}#ifdef have_split_iso/*-------------------------------------------------------------------------*//* * "Split ISO TDs" ... used for USB 1.1 devices going through * the TTs in USB 2.0 hubs. * * FIXME not yet implemented */#endif /* have_split_iso *//*-------------------------------------------------------------------------*/static voidscan_periodic (struct ehci_hcd *ehci, struct pt_regs *regs){ unsigned frame, clock, now_uframe, mod; unsigned modified; mod = ehci->periodic_size << 3; /* * When running, scan from last scan point up to "now" * else clean up by scanning everything that's left. * Touches as few pages as possible: cache-friendly. */ now_uframe = ehci->next_uframe; if (HCD_IS_RUNNING (ehci->hcd.state)) clock = readl (&ehci->regs->frame_index); else clock = now_uframe + mod - 1; clock %= mod; for (;;) { union ehci_shadow q, *q_p; u32 type, *hw_p; unsigned uframes; /* don't scan past the live uframe */ frame = now_uframe >> 3; if (frame == (clock >> 3)) uframes = now_uframe & 0x07; else { /* safe to scan the whole frame at once */ now_uframe |= 0x07; uframes = 8; }restart: /* scan each element in frame's queue for completions */ q_p = &ehci->pshadow [frame]; hw_p = &ehci->periodic [frame]; q.ptr = q_p->ptr; type = Q_NEXT_TYPE (*hw_p); modified = 0; while (q.ptr != 0) { unsigned uf; union ehci_shadow temp; switch (type) { case Q_TYPE_QH: /* handle any completions */ temp.qh = qh_get (q.qh); type = Q_NEXT_TYPE (q.qh->hw_next); q = q.qh->qh_next; modified = qh_completions (ehci, temp.qh, regs); if (unlikely (list_empty (&temp.qh->qtd_list))) intr_deschedule (ehci, temp.qh, 0); qh_put (ehci, temp.qh); break; case Q_TYPE_FSTN: /* for "save place" FSTNs, look at QH entries * in the previous frame for completions. */ if (q.fstn->hw_prev != EHCI_LIST_END) { dbg ("ignoring completions from FSTNs"); } type = Q_NEXT_TYPE (q.fstn->hw_next); q = q.fstn->fstn_next; break; case Q_TYPE_ITD: /* skip itds for later in the frame */ rmb (); for (uf = uframes; uf < 8; uf++) { if (0 == (q.itd->hw_transaction [uf] & ISO_ACTIVE)) continue; q_p = &q.itd->itd_next; hw_p = &q.itd->hw_next; type = Q_NEXT_TYPE (q.itd->hw_next); q = *q_p; break; } if (uf != 8) break; /* this one's ready ... HC won't cache the * pointer for much longer, if at all. */ *q_p = q.itd->itd_next; *hw_p = q.itd->hw_next; type = Q_NEXT_TYPE (q.itd->hw_next); wmb(); modified = itd_complete (ehci, q.itd, regs); q = *q_p; break;#ifdef have_split_iso case Q_TYPE_SITD: // nyet!#endif /* have_split_iso */ default: dbg ("corrupt type %d frame %d shadow %p", type, frame, q.ptr); // BUG (); q.ptr = 0; } /* assume completion callbacks modify the queue */ if (unlikely (modified)) goto restart; } /* stop when we catch up to the HC */ // FIXME: this assumes we won't get lapped when // latencies climb; that should be rare, but... // detect it, and just go all the way around. // FLR might help detect this case, so long as latencies // don't exceed periodic_size msec (default 1.024 sec). // FIXME: likewise assumes HC doesn't halt mid-scan if (now_uframe == clock) { unsigned now; if (!HCD_IS_RUNNING (ehci->hcd.state)) break; ehci->next_uframe = now_uframe; now = readl (&ehci->regs->frame_index) % mod; if (now_uframe == now) break; /* rescan the rest of this frame, then ... */ clock = now; } else { now_uframe++; now_uframe %= mod; } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?