Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Freescale QUICC Engine USB Host Controller Driver
0004  *
0005  * Copyright (c) Freescale Semicondutor, Inc. 2006.
0006  *               Shlomi Gridish <gridish@freescale.com>
0007  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
0008  * Copyright (c) Logic Product Development, Inc. 2007
0009  *               Peter Barada <peterb@logicpd.com>
0010  * Copyright (c) MontaVista Software, Inc. 2008.
0011  *               Anton Vorontsov <avorontsov@ru.mvista.com>
0012  */
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/types.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/errno.h>
0018 #include <linux/slab.h>
0019 #include <linux/list.h>
0020 #include <linux/usb.h>
0021 #include <linux/usb/hcd.h>
0022 #include "fhci.h"
0023 
0024 /* maps the hardware error code to the USB error code */
0025 static int status_to_error(u32 status)
0026 {
0027     if (status == USB_TD_OK)
0028         return 0;
0029     else if (status & USB_TD_RX_ER_CRC)
0030         return -EILSEQ;
0031     else if (status & USB_TD_RX_ER_NONOCT)
0032         return -EPROTO;
0033     else if (status & USB_TD_RX_ER_OVERUN)
0034         return -ECOMM;
0035     else if (status & USB_TD_RX_ER_BITSTUFF)
0036         return -EPROTO;
0037     else if (status & USB_TD_RX_ER_PID)
0038         return -EILSEQ;
0039     else if (status & (USB_TD_TX_ER_NAK | USB_TD_TX_ER_TIMEOUT))
0040         return -ETIMEDOUT;
0041     else if (status & USB_TD_TX_ER_STALL)
0042         return -EPIPE;
0043     else if (status & USB_TD_TX_ER_UNDERUN)
0044         return -ENOSR;
0045     else if (status & USB_TD_RX_DATA_UNDERUN)
0046         return -EREMOTEIO;
0047     else if (status & USB_TD_RX_DATA_OVERUN)
0048         return -EOVERFLOW;
0049     else
0050         return -EINVAL;
0051 }
0052 
0053 void fhci_add_td_to_frame(struct fhci_time_frame *frame, struct td *td)
0054 {
0055     list_add_tail(&td->frame_lh, &frame->tds_list);
0056 }
0057 
0058 void fhci_add_tds_to_ed(struct ed *ed, struct td **td_list, int number)
0059 {
0060     int i;
0061 
0062     for (i = 0; i < number; i++) {
0063         struct td *td = td_list[i];
0064         list_add_tail(&td->node, &ed->td_list);
0065     }
0066     if (ed->td_head == NULL)
0067         ed->td_head = td_list[0];
0068 }
0069 
0070 static struct td *peek_td_from_ed(struct ed *ed)
0071 {
0072     struct td *td;
0073 
0074     if (!list_empty(&ed->td_list))
0075         td = list_entry(ed->td_list.next, struct td, node);
0076     else
0077         td = NULL;
0078 
0079     return td;
0080 }
0081 
0082 struct td *fhci_remove_td_from_frame(struct fhci_time_frame *frame)
0083 {
0084     struct td *td;
0085 
0086     if (!list_empty(&frame->tds_list)) {
0087         td = list_entry(frame->tds_list.next, struct td, frame_lh);
0088         list_del_init(frame->tds_list.next);
0089     } else
0090         td = NULL;
0091 
0092     return td;
0093 }
0094 
0095 struct td *fhci_peek_td_from_frame(struct fhci_time_frame *frame)
0096 {
0097     struct td *td;
0098 
0099     if (!list_empty(&frame->tds_list))
0100         td = list_entry(frame->tds_list.next, struct td, frame_lh);
0101     else
0102         td = NULL;
0103 
0104     return td;
0105 }
0106 
0107 struct td *fhci_remove_td_from_ed(struct ed *ed)
0108 {
0109     struct td *td;
0110 
0111     if (!list_empty(&ed->td_list)) {
0112         td = list_entry(ed->td_list.next, struct td, node);
0113         list_del_init(ed->td_list.next);
0114 
0115         /* if this TD was the ED's head, find next TD */
0116         if (!list_empty(&ed->td_list))
0117             ed->td_head = list_entry(ed->td_list.next, struct td,
0118                          node);
0119         else
0120             ed->td_head = NULL;
0121     } else
0122         td = NULL;
0123 
0124     return td;
0125 }
0126 
0127 struct td *fhci_remove_td_from_done_list(struct fhci_controller_list *p_list)
0128 {
0129     struct td *td;
0130 
0131     if (!list_empty(&p_list->done_list)) {
0132         td = list_entry(p_list->done_list.next, struct td, node);
0133         list_del_init(p_list->done_list.next);
0134     } else
0135         td = NULL;
0136 
0137     return td;
0138 }
0139 
0140 void fhci_move_td_from_ed_to_done_list(struct fhci_usb *usb, struct ed *ed)
0141 {
0142     struct td *td;
0143 
0144     td = ed->td_head;
0145     list_del_init(&td->node);
0146 
0147     /* If this TD was the ED's head,find next TD */
0148     if (!list_empty(&ed->td_list))
0149         ed->td_head = list_entry(ed->td_list.next, struct td, node);
0150     else {
0151         ed->td_head = NULL;
0152         ed->state = FHCI_ED_SKIP;
0153     }
0154     ed->toggle_carry = td->toggle;
0155     list_add_tail(&td->node, &usb->hc_list->done_list);
0156     if (td->ioc)
0157         usb->transfer_confirm(usb->fhci);
0158 }
0159 
0160 /* free done FHCI URB resource such as ED and TD */
0161 static void free_urb_priv(struct fhci_hcd *fhci, struct urb *urb)
0162 {
0163     int i;
0164     struct urb_priv *urb_priv = urb->hcpriv;
0165     struct ed *ed = urb_priv->ed;
0166 
0167     for (i = 0; i < urb_priv->num_of_tds; i++) {
0168         list_del_init(&urb_priv->tds[i]->node);
0169         fhci_recycle_empty_td(fhci, urb_priv->tds[i]);
0170     }
0171 
0172     /* if this TD was the ED's head,find the next TD */
0173     if (!list_empty(&ed->td_list))
0174         ed->td_head = list_entry(ed->td_list.next, struct td, node);
0175     else
0176         ed->td_head = NULL;
0177 
0178     kfree(urb_priv->tds);
0179     kfree(urb_priv);
0180     urb->hcpriv = NULL;
0181 
0182     /* if this TD was the ED's head,find next TD */
0183     if (ed->td_head == NULL)
0184         list_del_init(&ed->node);
0185     fhci->active_urbs--;
0186 }
0187 
0188 /* this routine called to complete and free done URB */
0189 void fhci_urb_complete_free(struct fhci_hcd *fhci, struct urb *urb)
0190 {
0191     free_urb_priv(fhci, urb);
0192 
0193     if (urb->status == -EINPROGRESS) {
0194         if (urb->actual_length != urb->transfer_buffer_length &&
0195                 urb->transfer_flags & URB_SHORT_NOT_OK)
0196             urb->status = -EREMOTEIO;
0197         else
0198             urb->status = 0;
0199     }
0200 
0201     usb_hcd_unlink_urb_from_ep(fhci_to_hcd(fhci), urb);
0202 
0203     spin_unlock(&fhci->lock);
0204 
0205     usb_hcd_giveback_urb(fhci_to_hcd(fhci), urb, urb->status);
0206 
0207     spin_lock(&fhci->lock);
0208 }
0209 
0210 /*
0211  * caculate transfer length/stats and update the urb
0212  * Precondition: irqsafe(only for urb-?status locking)
0213  */
0214 void fhci_done_td(struct urb *urb, struct td *td)
0215 {
0216     struct ed *ed = td->ed;
0217     u32 cc = td->status;
0218 
0219     /* ISO...drivers see per-TD length/status */
0220     if (ed->mode == FHCI_TF_ISO) {
0221         u32 len;
0222         if (!(urb->transfer_flags & URB_SHORT_NOT_OK &&
0223                 cc == USB_TD_RX_DATA_UNDERUN))
0224             cc = USB_TD_OK;
0225 
0226         if (usb_pipeout(urb->pipe))
0227             len = urb->iso_frame_desc[td->iso_index].length;
0228         else
0229             len = td->actual_len;
0230 
0231         urb->actual_length += len;
0232         urb->iso_frame_desc[td->iso_index].actual_length = len;
0233         urb->iso_frame_desc[td->iso_index].status =
0234             status_to_error(cc);
0235     }
0236 
0237     /* BULK,INT,CONTROL... drivers see aggregate length/status,
0238      * except that "setup" bytes aren't counted and "short" transfers
0239      * might not be reported as errors.
0240      */
0241     else {
0242         if (td->error_cnt >= 3)
0243             urb->error_count = 3;
0244 
0245         /* control endpoint only have soft stalls */
0246 
0247         /* update packet status if needed(short may be ok) */
0248         if (!(urb->transfer_flags & URB_SHORT_NOT_OK) &&
0249                 cc == USB_TD_RX_DATA_UNDERUN) {
0250             ed->state = FHCI_ED_OPER;
0251             cc = USB_TD_OK;
0252         }
0253         if (cc != USB_TD_OK) {
0254             if (urb->status == -EINPROGRESS)
0255                 urb->status = status_to_error(cc);
0256         }
0257 
0258         /* count all non-empty packets except control SETUP packet */
0259         if (td->type != FHCI_TA_SETUP || td->iso_index != 0)
0260             urb->actual_length += td->actual_len;
0261     }
0262 }
0263 
0264 /* there are some pedning request to unlink */
0265 void fhci_del_ed_list(struct fhci_hcd *fhci, struct ed *ed)
0266 {
0267     struct td *td = peek_td_from_ed(ed);
0268     struct urb *urb = td->urb;
0269     struct urb_priv *urb_priv = urb->hcpriv;
0270 
0271     if (urb_priv->state == URB_DEL) {
0272         td = fhci_remove_td_from_ed(ed);
0273         /* HC may have partly processed this TD */
0274         if (td->status != USB_TD_INPROGRESS)
0275             fhci_done_td(urb, td);
0276 
0277         /* URB is done;clean up */
0278         if (++(urb_priv->tds_cnt) == urb_priv->num_of_tds)
0279             fhci_urb_complete_free(fhci, urb);
0280     }
0281 }