📄 usbdcore_mpc8xx.c
字号:
* TODO: fit a pull-up resistor to drive SE0 for > 2.5us */ if (udc_state != STATE_ERROR) { udc_state = STATE_READY; usbp->usmod |= USMOD_EN; }}/* udc_disconnect * * Disconnect is not used but, is included for completeness */void udc_disconnect (void){ /* Disable pull-up resistor on D- * TODO: fix a pullup resistor to control this */ if (udc_state != STATE_ERROR) { udc_state = STATE_NOT_READY; } usbp->usmod &= ~USMOD_EN;}/* udc_enable * * Grab an EP0 URB, register interest in a subset of USB events */void udc_enable (struct usb_device_instance *device){ if (udc_state == STATE_ERROR) { return; } udc_device = device; if (!ep_ref[0].urb) { ep_ref[0].urb = usbd_alloc_urb (device, device->bus->endpoint_array); } /* Register interest in all events except SOF, enable transceiver */ usbp->usber = 0x03FF; usbp->usbmr = 0x02F7; return;}/* udc_disable * * disable the currently hooked device */void udc_disable (void){ int i = 0; if (udc_state == STATE_ERROR) { DBG ("Won't disable UDC. udc_state==STATE_ERROR !\n"); return; } udc_device = 0; for (; i < MAX_ENDPOINTS; i++) { if (ep_ref[i].urb) { usbd_dealloc_urb (ep_ref[i].urb); ep_ref[i].urb = 0; } } usbp->usbmr = 0x00; usbp->usmod = ~USMOD_EN; udc_state = STATE_NOT_READY;}/* udc_startup_events * * Enable the specified device */void udc_startup_events (struct usb_device_instance *device){ udc_enable (device); if (udc_state == STATE_READY) { usbd_device_event_irq (device, DEVICE_CREATE, 0); }}/* udc_set_nak * * Allow upper layers to signal lower layers should not accept more RX data * */void udc_set_nak (int epid){ if (epid) { mpc8xx_udc_set_nak (epid); }}/* udc_unset_nak * * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint. * Switch off NAKing on this endpoint to accept more data output from host. * */void udc_unset_nak (int epid){ if (epid > MAX_ENDPOINTS) { return; } if (usbp->usep[epid] & (USEP_THS_NAK | USEP_RHS_NAK)) { usbp->usep[epid] &= ~(USEP_THS_NAK | USEP_RHS_NAK); __asm__ ("eieio"); }}/****************************************************************************** Static Linkage******************************************************************************//* udc_state_transition_up * udc_state_transition_down * * Helper functions to implement device state changes. The device states and * the events that transition between them are: * * STATE_ATTACHED * || /\ * \/ || * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET * || /\ * \/ || * STATE_POWERED * || /\ * \/ || * DEVICE_RESET DEVICE_POWER_INTERRUPTION * || /\ * \/ || * STATE_DEFAULT * || /\ * \/ || * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET * || /\ * \/ || * STATE_ADDRESSED * || /\ * \/ || * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED * || /\ * \/ || * STATE_CONFIGURED * * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED * to STATE_CONFIGURED) from the specified initial state to the specified final * state, passing through each intermediate state on the way. If the initial * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then * no state transitions will take place. * * udc_state_transition_down transitions down (in the direction from * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the * specified final state, passing through each intermediate state on the way. * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final * state, then no state transitions will take place. * */static void mpc8xx_udc_state_transition_up (usb_device_state_t initial, usb_device_state_t final){ if (initial < final) { switch (initial) { case STATE_ATTACHED: usbd_device_event_irq (udc_device, DEVICE_HUB_CONFIGURED, 0); if (final == STATE_POWERED) break; case STATE_POWERED: usbd_device_event_irq (udc_device, DEVICE_RESET, 0); if (final == STATE_DEFAULT) break; case STATE_DEFAULT: usbd_device_event_irq (udc_device, DEVICE_ADDRESS_ASSIGNED, 0); if (final == STATE_ADDRESSED) break; case STATE_ADDRESSED: usbd_device_event_irq (udc_device, DEVICE_CONFIGURED, 0); case STATE_CONFIGURED: break; default: break; } }}static void mpc8xx_udc_state_transition_down (usb_device_state_t initial, usb_device_state_t final){ if (initial > final) { switch (initial) { case STATE_CONFIGURED: usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0); if (final == STATE_ADDRESSED) break; case STATE_ADDRESSED: usbd_device_event_irq (udc_device, DEVICE_RESET, 0); if (final == STATE_DEFAULT) break; case STATE_DEFAULT: usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0); if (final == STATE_POWERED) break; case STATE_POWERED: usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0); case STATE_ATTACHED: break; default: break; } }}/* mpc8xx_udc_stall * * Force returning of STALL tokens on the given endpoint. Protocol or function * STALL conditions are permissable here */static void mpc8xx_udc_stall (unsigned int ep){ usbp->usep[ep] |= STALL_BITMASK;}/* mpc8xx_udc_set_nak * * Force returning of NAK responses for the given endpoint as a kind of very * simple flow control */static void mpc8xx_udc_set_nak (unsigned int ep){ usbp->usep[ep] |= NAK_BITMASK; __asm__ ("eieio");}/* mpc8xx_udc_handle_txerr * * Handle errors relevant to TX. Return a status code to allow calling * indicative of what if anything happened */static short mpc8xx_udc_handle_txerr (){ short ep = 0, ret = 0; for (; ep < TX_RING_SIZE; ep++) { if (usbp->usber & (0x10 << ep)) { /* Timeout or underrun */ if (tx_cbd[ep]->cbd_sc & 0x06) { ret = 1; mpc8xx_udc_flush_tx_fifo (ep); } else { if (usbp->usep[ep] & STALL_BITMASK) { if (!ep) { usbp->usep[ep] &= ~STALL_BITMASK; } } /* else NAK */ } usbp->usber |= (0x10 << ep); } } return ret;}/* mpc8xx_udc_advance_rx * * Advance cbd rx */static void mpc8xx_udc_advance_rx (volatile cbd_t ** rx_cbdp, int epid){ if ((*rx_cbdp)->cbd_sc & RX_BD_W) { *rx_cbdp = (volatile cbd_t *) (endpoints[epid]->rbase + CFG_IMMR); } else { (*rx_cbdp)++; }}/* mpc8xx_udc_flush_tx_fifo * * Flush a given TX fifo. Assumes one tx cbd per endpoint */static void mpc8xx_udc_flush_tx_fifo (int epid){ volatile cbd_t *tx_cbdp = 0; if (epid > MAX_ENDPOINTS) { return; } /* TX stop */ immr->im_cpm.cp_cpcr = ((epid << 2) | 0x1D01); __asm__ ("eieio"); while (immr->im_cpm.cp_cpcr & 0x01); usbp->uscom = 0x40 | 0; /* reset ring */ tx_cbdp = (cbd_t *) (endpoints[epid]->tbptr + CFG_IMMR); tx_cbdp->cbd_sc = (TX_BD_I | TX_BD_W); endpoints[epid]->tptr = endpoints[epid]->tbase; endpoints[epid]->tstate = 0x00; endpoints[epid]->tbcnt = 0x00; /* TX start */ immr->im_cpm.cp_cpcr = ((epid << 2) | 0x2D01); __asm__ ("eieio"); while (immr->im_cpm.cp_cpcr & 0x01); return;}/* mpc8xx_udc_flush_rx_fifo * * For the sake of completeness of the namespace, it seems like * a good-design-decision (tm) to include mpc8xx_udc_flush_rx_fifo(); * If RX_BD_E is true => a driver bug either here or in an upper layer * not polling frequently enough. If RX_BD_E is true we have told the host * we have accepted data but, the CPM found it had no-where to put that data * which needless to say would be a bad thing. */static void mpc8xx_udc_flush_rx_fifo (){ int i = 0; for (i = 0; i < RX_RING_SIZE; i++) { if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) { ERR ("buf %p used rx data len = 0x%x sc=0x%x!\n", rx_cbd[i], rx_cbd[i]->cbd_datlen, rx_cbd[i]->cbd_sc); } } ERR ("BUG : Input over-run\n");}/* mpc8xx_udc_clear_rxbd * * Release control of RX CBD to CP. */static void mpc8xx_udc_clear_rxbd (volatile cbd_t * rx_cbdp){ rx_cbdp->cbd_datlen = 0x0000; rx_cbdp->cbd_sc = ((rx_cbdp->cbd_sc & RX_BD_W) | (RX_BD_E | RX_BD_I)); __asm__ ("eieio");}/* mpc8xx_udc_tx_irq * * Parse for tx timeout, control RX or USB reset/busy conditions * Return -1 on timeout, -2 on fatal error, else return zero */static int mpc8xx_udc_tx_irq (int ep){ int i = 0; if (usbp->usber & (USB_TX_ERRMASK)) { if (mpc8xx_udc_handle_txerr ()) { /* Timeout, controlling function must retry send */ return -1; } } if (usbp->usber & (USB_E_RESET | USB_E_BSY)) { /* Fatal, abandon TX transaction */ return -2; } if (usbp->usber & USB_E_RXB) { for (i = 0; i < RX_RING_SIZE; i++) { if (!(rx_cbd[i]->cbd_sc & RX_BD_E)) { if ((rx_cbd[i] == ep_ref[0].prx) || ep) { return -2; } } } } return 0;}/* mpc8xx_udc_ep_tx * * Transmit in a re-entrant fashion outbound USB packets. * Implement retry/timeout mechanism described in USB specification * Toggle DATA0/DATA1 pids as necessary * Introduces non-standard tx_retry. The USB standard has no scope for slave * devices to give up TX, however tx_retry stops us getting stuck in an endless * TX loop. */static int mpc8xx_udc_ep_tx (struct usb_endpoint_instance *epi){ struct urb *urb = epi->tx_urb; volatile cbd_t *tx_cbdp = 0; unsigned int ep = 0, pkt_len = 0, x = 0, tx_retry = 0; int ret = 0; if (!epi || (epi->endpoint_address & 0x03) >= MAX_ENDPOINTS || !urb) { return -1; } ep = epi->endpoint_address & 0x03; tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CFG_IMMR); if (tx_cbdp->cbd_sc & TX_BD_R || usbp->usber & USB_E_TXB) { mpc8xx_udc_flush_tx_fifo (ep); usbp->usber |= USB_E_TXB; }; while (tx_retry++ < 100) { ret = mpc8xx_udc_tx_irq (ep); if (ret == -1) { /* ignore timeout here */ } else if (ret == -2) { /* Abandon TX */ mpc8xx_udc_flush_tx_fifo (ep); return -1; } tx_cbdp = (cbd_t *) (endpoints[ep]->tbptr + CFG_IMMR); while (tx_cbdp->cbd_sc & TX_BD_R) { }; tx_cbdp->cbd_sc = (tx_cbdp->cbd_sc & TX_BD_W); pkt_len = urb->actual_length - epi->sent; if (pkt_len > epi->tx_packetSize || pkt_len > EP_MAX_PKT) { pkt_len = MIN (epi->tx_packetSize, EP_MAX_PKT); } for (x = 0; x < pkt_len; x++) { *((unsigned char *) (tx_cbdp->cbd_bufaddr + x)) = urb->buffer[epi->sent + x]; } tx_cbdp->cbd_datlen = pkt_len; tx_cbdp->cbd_sc |= (CBD_TX_BITMASK | ep_ref[ep].pid); __asm__ ("eieio");#ifdef __SIMULATE_ERROR__ if (++err_poison_test == 2) { err_poison_test = 0; tx_cbdp->cbd_sc &= ~TX_BD_TC; }#endif usbp->uscom = (USCOM_STR | ep); while (!(usbp->usber & USB_E_TXB)) { ret = mpc8xx_udc_tx_irq (ep); if (ret == -1) { /* TX timeout */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -