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, 2011.
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/delay.h>
0018 #include <linux/errno.h>
0019 #include <linux/list.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/io.h>
0022 #include <linux/usb.h>
0023 #include <linux/usb/hcd.h>
0024 #include <soc/fsl/qe/qe.h>
0025 #include <asm/fsl_gtm.h>
0026 #include "fhci.h"
0027 
0028 static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
0029 {
0030     pkt->data = NULL;
0031     pkt->len = 0;
0032     pkt->status = USB_TD_OK;
0033     pkt->info = 0;
0034     pkt->priv_data = NULL;
0035 
0036     cq_put(&usb->ep0->empty_frame_Q, pkt);
0037 }
0038 
0039 /* confirm submitted packet */
0040 void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
0041 {
0042     struct td *td;
0043     struct packet *td_pkt;
0044     struct ed *ed;
0045     u32 trans_len;
0046     bool td_done = false;
0047 
0048     td = fhci_remove_td_from_frame(usb->actual_frame);
0049     td_pkt = td->pkt;
0050     trans_len = pkt->len;
0051     td->status = pkt->status;
0052     if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
0053         if ((td->data + td->actual_len) && trans_len)
0054             memcpy(td->data + td->actual_len, pkt->data,
0055                    trans_len);
0056         cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
0057     }
0058 
0059     recycle_frame(usb, pkt);
0060 
0061     ed = td->ed;
0062     if (ed->mode == FHCI_TF_ISO) {
0063         if (ed->td_list.next->next != &ed->td_list) {
0064             struct td *td_next =
0065                 list_entry(ed->td_list.next->next, struct td,
0066                        node);
0067 
0068             td_next->start_frame = usb->actual_frame->frame_num;
0069         }
0070         td->actual_len = trans_len;
0071         td_done = true;
0072     } else if ((td->status & USB_TD_ERROR) &&
0073             !(td->status & USB_TD_TX_ER_NAK)) {
0074         /*
0075          * There was an error on the transaction (but not NAK).
0076          * If it is fatal error (data underrun, stall, bad pid or 3
0077          * errors exceeded), mark this TD as done.
0078          */
0079         if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
0080                 (td->status & USB_TD_TX_ER_STALL) ||
0081                 (td->status & USB_TD_RX_ER_PID) ||
0082                 (++td->error_cnt >= 3)) {
0083             ed->state = FHCI_ED_HALTED;
0084             td_done = true;
0085 
0086             if (td->status & USB_TD_RX_DATA_UNDERUN) {
0087                 fhci_dbg(usb->fhci, "td err fu\n");
0088                 td->toggle = !td->toggle;
0089                 td->actual_len += trans_len;
0090             } else {
0091                 fhci_dbg(usb->fhci, "td err f!u\n");
0092             }
0093         } else {
0094             fhci_dbg(usb->fhci, "td err !f\n");
0095             /* it is not a fatal error -retry this transaction */
0096             td->nak_cnt = 0;
0097             td->error_cnt++;
0098             td->status = USB_TD_OK;
0099         }
0100     } else if (td->status & USB_TD_TX_ER_NAK) {
0101         /* there was a NAK response */
0102         fhci_vdbg(usb->fhci, "td nack\n");
0103         td->nak_cnt++;
0104         td->error_cnt = 0;
0105         td->status = USB_TD_OK;
0106     } else {
0107         /* there was no error on transaction */
0108         td->error_cnt = 0;
0109         td->nak_cnt = 0;
0110         td->toggle = !td->toggle;
0111         td->actual_len += trans_len;
0112 
0113         if (td->len == td->actual_len)
0114             td_done = true;
0115     }
0116 
0117     if (td_done)
0118         fhci_move_td_from_ed_to_done_list(usb, ed);
0119 }
0120 
0121 /*
0122  * Flush all transmitted packets from BDs
0123  * This routine is called when disabling the USB port to flush all
0124  * transmissions that are already scheduled in the BDs
0125  */
0126 void fhci_flush_all_transmissions(struct fhci_usb *usb)
0127 {
0128     u8 mode;
0129     struct td *td;
0130 
0131     mode = in_8(&usb->fhci->regs->usb_usmod);
0132     clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
0133 
0134     fhci_flush_bds(usb);
0135 
0136     while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
0137         struct packet *pkt = td->pkt;
0138 
0139         pkt->status = USB_TD_TX_ER_TIMEOUT;
0140         fhci_transaction_confirm(usb, pkt);
0141     }
0142 
0143     usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;
0144 
0145     /* reset the event register */
0146     out_be16(&usb->fhci->regs->usb_usber, 0xffff);
0147     /* enable the USB controller */
0148     out_8(&usb->fhci->regs->usb_usmod, mode | USB_MODE_EN);
0149 }
0150 
0151 /*
0152  * This function forms the packet and transmit the packet. This function
0153  * will handle all endpoint type:ISO,interrupt,control and bulk
0154  */
0155 static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
0156 {
0157     u32 fw_transaction_time, len = 0;
0158     struct packet *pkt;
0159     u8 *data = NULL;
0160 
0161     /* calcalate data address,len and toggle and then add the transaction */
0162     if (td->toggle == USB_TD_TOGGLE_CARRY)
0163         td->toggle = ed->toggle_carry;
0164 
0165     switch (ed->mode) {
0166     case FHCI_TF_ISO:
0167         len = td->len;
0168         if (td->type != FHCI_TA_IN)
0169             data = td->data;
0170         break;
0171     case FHCI_TF_CTRL:
0172     case FHCI_TF_BULK:
0173         len = min(td->len - td->actual_len, ed->max_pkt_size);
0174         if (!((td->type == FHCI_TA_IN) &&
0175               ((len + td->actual_len) == td->len)))
0176             data = td->data + td->actual_len;
0177         break;
0178     case FHCI_TF_INTR:
0179         len = min(td->len, ed->max_pkt_size);
0180         if (!((td->type == FHCI_TA_IN) &&
0181               ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
0182             data = td->data;
0183         break;
0184     default:
0185         break;
0186     }
0187 
0188     if (usb->port_status == FHCI_PORT_FULL)
0189         fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
0190     else
0191         fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);
0192 
0193     /* check if there's enough space in this frame to submit this TD */
0194     if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
0195             usb->max_bytes_per_frame) {
0196         fhci_vdbg(usb->fhci, "not enough space in this frame: "
0197               "%d %d %d\n", usb->actual_frame->total_bytes, len,
0198               usb->max_bytes_per_frame);
0199         return -1;
0200     }
0201 
0202     /* check if there's enough time in this frame to submit this TD */
0203     if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
0204         (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
0205          (fw_transaction_time + usb->sw_transaction_time >=
0206           1000 - fhci_get_sof_timer_count(usb)))) {
0207         fhci_dbg(usb->fhci, "not enough time in this frame\n");
0208         return -1;
0209     }
0210 
0211     /* update frame object fields before transmitting */
0212     pkt = cq_get(&usb->ep0->empty_frame_Q);
0213     if (!pkt) {
0214         fhci_dbg(usb->fhci, "there is no empty frame\n");
0215         return -1;
0216     }
0217     td->pkt = pkt;
0218 
0219     pkt->info = 0;
0220     if (data == NULL) {
0221         data = cq_get(&usb->ep0->dummy_packets_Q);
0222         BUG_ON(!data);
0223         pkt->info = PKT_DUMMY_PACKET;
0224     }
0225     pkt->data = data;
0226     pkt->len = len;
0227     pkt->status = USB_TD_OK;
0228     /* update TD status field before transmitting */
0229     td->status = USB_TD_INPROGRESS;
0230     /* update actual frame time object with the actual transmission */
0231     usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
0232     fhci_add_td_to_frame(usb->actual_frame, td);
0233 
0234     if (usb->port_status != FHCI_PORT_FULL &&
0235             usb->port_status != FHCI_PORT_LOW) {
0236         pkt->status = USB_TD_TX_ER_TIMEOUT;
0237         pkt->len = 0;
0238         fhci_transaction_confirm(usb, pkt);
0239     } else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
0240             ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
0241         /* remove TD from actual frame */
0242         list_del_init(&td->frame_lh);
0243         td->status = USB_TD_OK;
0244         if (pkt->info & PKT_DUMMY_PACKET)
0245             cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
0246         recycle_frame(usb, pkt);
0247         usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
0248         fhci_err(usb->fhci, "host transaction failed\n");
0249         return -1;
0250     }
0251 
0252     return len;
0253 }
0254 
0255 static void move_head_to_tail(struct list_head *list)
0256 {
0257     struct list_head *node = list->next;
0258 
0259     if (!list_empty(list)) {
0260         list_move_tail(node, list);
0261     }
0262 }
0263 
0264 /*
0265  * This function goes through the endpoint list and schedules the
0266  * transactions within this list
0267  */
0268 static int scan_ed_list(struct fhci_usb *usb,
0269             struct list_head *list, enum fhci_tf_mode list_type)
0270 {
0271     static const int frame_part[4] = {
0272         [FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
0273         [FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
0274                  MAX_PERIODIC_FRAME_USAGE) / 100,
0275         [FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
0276         [FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
0277                   MAX_PERIODIC_FRAME_USAGE) / 100
0278     };
0279     struct ed *ed;
0280     struct td *td;
0281     int ans = 1;
0282     u32 save_transaction_time = usb->sw_transaction_time;
0283 
0284     list_for_each_entry(ed, list, node) {
0285         td = ed->td_head;
0286 
0287         if (!td || td->status == USB_TD_INPROGRESS)
0288             continue;
0289 
0290         if (ed->state != FHCI_ED_OPER) {
0291             if (ed->state == FHCI_ED_URB_DEL) {
0292                 td->status = USB_TD_OK;
0293                 fhci_move_td_from_ed_to_done_list(usb, ed);
0294                 ed->state = FHCI_ED_SKIP;
0295             }
0296             continue;
0297         }
0298 
0299         /*
0300          * if it isn't interrupt pipe or it is not iso pipe and the
0301          * interval time passed
0302          */
0303         if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
0304                 (((usb->actual_frame->frame_num -
0305                    td->start_frame) & 0x7ff) < td->interval))
0306             continue;
0307 
0308         if (add_packet(usb, ed, td) < 0)
0309             continue;
0310 
0311         /* update time stamps in the TD */
0312         td->start_frame = usb->actual_frame->frame_num;
0313         usb->sw_transaction_time += save_transaction_time;
0314 
0315         if (usb->actual_frame->total_bytes >=
0316                     usb->max_bytes_per_frame) {
0317             usb->actual_frame->frame_status =
0318                 FRAME_DATA_END_TRANSMISSION;
0319             fhci_push_dummy_bd(usb->ep0);
0320             ans = 0;
0321             break;
0322         }
0323 
0324         if (usb->actual_frame->total_bytes >= frame_part[list_type])
0325             break;
0326     }
0327 
0328     /* be fair to each ED(move list head around) */
0329     move_head_to_tail(list);
0330     usb->sw_transaction_time = save_transaction_time;
0331 
0332     return ans;
0333 }
0334 
0335 static u32 rotate_frames(struct fhci_usb *usb)
0336 {
0337     struct fhci_hcd *fhci = usb->fhci;
0338 
0339     if (!list_empty(&usb->actual_frame->tds_list)) {
0340         if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
0341               usb->actual_frame->frame_num) & 0x7ff) > 5)
0342             fhci_flush_actual_frame(usb);
0343         else
0344             return -EINVAL;
0345     }
0346 
0347     usb->actual_frame->frame_status = FRAME_IS_PREPARED;
0348     usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
0349     usb->actual_frame->total_bytes = 0;
0350 
0351     return 0;
0352 }
0353 
0354 /*
0355  * This function schedule the USB transaction and will process the
0356  * endpoint in the following order: iso, interrupt, control and bulk.
0357  */
0358 void fhci_schedule_transactions(struct fhci_usb *usb)
0359 {
0360     int left = 1;
0361 
0362     if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
0363         if (rotate_frames(usb) != 0)
0364             return;
0365 
0366     if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
0367         return;
0368 
0369     if (usb->actual_frame->total_bytes == 0) {
0370         /*
0371          * schedule the next available ISO transfer
0372          *or next stage of the ISO transfer
0373          */
0374         scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
0375 
0376         /*
0377          * schedule the next available interrupt transfer or
0378          * the next stage of the interrupt transfer
0379          */
0380         scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
0381 
0382         /*
0383          * schedule the next available control transfer
0384          * or the next stage of the control transfer
0385          */
0386         left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
0387                     FHCI_TF_CTRL);
0388     }
0389 
0390     /*
0391      * schedule the next available bulk transfer or the next stage of the
0392      * bulk transfer
0393      */
0394     if (left > 0)
0395         scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
0396 }
0397 
0398 /* Handles SOF interrupt */
0399 static void sof_interrupt(struct fhci_hcd *fhci)
0400 {
0401     struct fhci_usb *usb = fhci->usb_lld;
0402 
0403     if ((usb->port_status == FHCI_PORT_DISABLED) &&
0404         (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
0405         !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
0406         if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
0407             usb->port_status = FHCI_PORT_LOW;
0408         else
0409             usb->port_status = FHCI_PORT_FULL;
0410         /* Disable IDLE */
0411         usb->saved_msk &= ~USB_E_IDLE_MASK;
0412         out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
0413     }
0414 
0415     gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
0416 
0417     fhci_host_transmit_actual_frame(usb);
0418     usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
0419 
0420     fhci_schedule_transactions(usb);
0421 }
0422 
0423 /* Handles device disconnected interrupt on port */
0424 void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
0425 {
0426     struct fhci_usb *usb = fhci->usb_lld;
0427 
0428     fhci_dbg(fhci, "-> %s\n", __func__);
0429 
0430     fhci_usb_disable_interrupt(usb);
0431     clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
0432     usb->port_status = FHCI_PORT_DISABLED;
0433 
0434     fhci_stop_sof_timer(fhci);
0435 
0436     /* Enable IDLE since we want to know if something comes along */
0437     usb->saved_msk |= USB_E_IDLE_MASK;
0438     out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
0439 
0440     usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
0441     usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
0442     usb->max_bytes_per_frame = 0;
0443     fhci_usb_enable_interrupt(usb);
0444 
0445     fhci_dbg(fhci, "<- %s\n", __func__);
0446 }
0447 
0448 /* detect a new device connected on the USB port */
0449 void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
0450 {
0451 
0452     struct fhci_usb *usb = fhci->usb_lld;
0453     int state;
0454     int ret;
0455 
0456     fhci_dbg(fhci, "-> %s\n", __func__);
0457 
0458     fhci_usb_disable_interrupt(usb);
0459     state = fhci_ioports_check_bus_state(fhci);
0460 
0461     /* low-speed device was connected to the USB port */
0462     if (state == 1) {
0463         ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
0464         if (ret) {
0465             fhci_warn(fhci, "Low-Speed device is not supported, "
0466                   "try use BRGx\n");
0467             goto out;
0468         }
0469 
0470         usb->port_status = FHCI_PORT_LOW;
0471         setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
0472         usb->vroot_hub->port.wPortStatus |=
0473             (USB_PORT_STAT_LOW_SPEED |
0474              USB_PORT_STAT_CONNECTION);
0475         usb->vroot_hub->port.wPortChange |=
0476             USB_PORT_STAT_C_CONNECTION;
0477         usb->max_bytes_per_frame =
0478             (MAX_BYTES_PER_FRAME >> 3) - 7;
0479         fhci_port_enable(usb);
0480     } else if (state == 2) {
0481         ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
0482         if (ret) {
0483             fhci_warn(fhci, "Full-Speed device is not supported, "
0484                   "try use CLKx\n");
0485             goto out;
0486         }
0487 
0488         usb->port_status = FHCI_PORT_FULL;
0489         clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
0490         usb->vroot_hub->port.wPortStatus &=
0491             ~USB_PORT_STAT_LOW_SPEED;
0492         usb->vroot_hub->port.wPortStatus |=
0493             USB_PORT_STAT_CONNECTION;
0494         usb->vroot_hub->port.wPortChange |=
0495             USB_PORT_STAT_C_CONNECTION;
0496         usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
0497         fhci_port_enable(usb);
0498     }
0499 out:
0500     fhci_usb_enable_interrupt(usb);
0501     fhci_dbg(fhci, "<- %s\n", __func__);
0502 }
0503 
0504 irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
0505 {
0506     struct usb_hcd *hcd = _hcd;
0507     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0508     struct fhci_usb *usb = fhci->usb_lld;
0509 
0510     spin_lock(&fhci->lock);
0511 
0512     gtm_set_exact_timer16(fhci->timer, 1000, false);
0513 
0514     if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
0515         usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
0516         fhci_push_dummy_bd(usb->ep0);
0517     }
0518 
0519     fhci_schedule_transactions(usb);
0520 
0521     spin_unlock(&fhci->lock);
0522 
0523     return IRQ_HANDLED;
0524 }
0525 
0526 /* Cancel transmission on the USB endpoint */
0527 static void abort_transmission(struct fhci_usb *usb)
0528 {
0529     fhci_dbg(usb->fhci, "-> %s\n", __func__);
0530     /* issue stop Tx command */
0531     qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
0532     /* flush Tx FIFOs */
0533     out_8(&usb->fhci->regs->usb_uscom, USB_CMD_FLUSH_FIFO | EP_ZERO);
0534     udelay(1000);
0535     /* reset Tx BDs */
0536     fhci_flush_bds(usb);
0537     /* issue restart Tx command */
0538     qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
0539     fhci_dbg(usb->fhci, "<- %s\n", __func__);
0540 }
0541 
0542 irqreturn_t fhci_irq(struct usb_hcd *hcd)
0543 {
0544     struct fhci_hcd *fhci = hcd_to_fhci(hcd);
0545     struct fhci_usb *usb;
0546     u16 usb_er = 0;
0547     unsigned long flags;
0548 
0549     spin_lock_irqsave(&fhci->lock, flags);
0550 
0551     usb = fhci->usb_lld;
0552 
0553     usb_er |= in_be16(&usb->fhci->regs->usb_usber) &
0554           in_be16(&usb->fhci->regs->usb_usbmr);
0555 
0556     /* clear event bits for next time */
0557     out_be16(&usb->fhci->regs->usb_usber, usb_er);
0558 
0559     fhci_dbg_isr(fhci, usb_er);
0560 
0561     if (usb_er & USB_E_RESET_MASK) {
0562         if ((usb->port_status == FHCI_PORT_FULL) ||
0563                 (usb->port_status == FHCI_PORT_LOW)) {
0564             fhci_device_disconnected_interrupt(fhci);
0565             usb_er &= ~USB_E_IDLE_MASK;
0566         } else if (usb->port_status == FHCI_PORT_WAITING) {
0567             usb->port_status = FHCI_PORT_DISCONNECTING;
0568 
0569             /* Turn on IDLE since we want to disconnect */
0570             usb->saved_msk |= USB_E_IDLE_MASK;
0571             out_be16(&usb->fhci->regs->usb_usber,
0572                  usb->saved_msk);
0573         } else if (usb->port_status == FHCI_PORT_DISABLED) {
0574             if (fhci_ioports_check_bus_state(fhci) == 1)
0575                 fhci_device_connected_interrupt(fhci);
0576         }
0577         usb_er &= ~USB_E_RESET_MASK;
0578     }
0579 
0580     if (usb_er & USB_E_MSF_MASK) {
0581         abort_transmission(fhci->usb_lld);
0582         usb_er &= ~USB_E_MSF_MASK;
0583     }
0584 
0585     if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
0586         sof_interrupt(fhci);
0587         usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
0588     }
0589 
0590     if (usb_er & USB_E_TXB_MASK) {
0591         fhci_tx_conf_interrupt(fhci->usb_lld);
0592         usb_er &= ~USB_E_TXB_MASK;
0593     }
0594 
0595     if (usb_er & USB_E_TXE1_MASK) {
0596         fhci_tx_conf_interrupt(fhci->usb_lld);
0597         usb_er &= ~USB_E_TXE1_MASK;
0598     }
0599 
0600     if (usb_er & USB_E_IDLE_MASK) {
0601         if (usb->port_status == FHCI_PORT_DISABLED) {
0602             usb_er &= ~USB_E_RESET_MASK;
0603             fhci_device_connected_interrupt(fhci);
0604         } else if (usb->port_status ==
0605                 FHCI_PORT_DISCONNECTING) {
0606             /* XXX usb->port_status = FHCI_PORT_WAITING; */
0607             /* Disable IDLE */
0608             usb->saved_msk &= ~USB_E_IDLE_MASK;
0609             out_be16(&usb->fhci->regs->usb_usbmr,
0610                  usb->saved_msk);
0611         } else {
0612             fhci_dbg_isr(fhci, -1);
0613         }
0614 
0615         usb_er &= ~USB_E_IDLE_MASK;
0616     }
0617 
0618     spin_unlock_irqrestore(&fhci->lock, flags);
0619 
0620     return IRQ_HANDLED;
0621 }
0622 
0623 
0624 /*
0625  * Process normal completions(error or success) and clean the schedule.
0626  *
0627  * This is the main path for handing urbs back to drivers. The only other patth
0628  * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
0629  * the (re-reversed) done list as this does.
0630  */
0631 static void process_done_list(unsigned long data)
0632 {
0633     struct urb *urb;
0634     struct ed *ed;
0635     struct td *td;
0636     struct urb_priv *urb_priv;
0637     struct fhci_hcd *fhci = (struct fhci_hcd *)data;
0638 
0639     disable_irq(fhci->timer->irq);
0640     disable_irq(fhci_to_hcd(fhci)->irq);
0641     spin_lock(&fhci->lock);
0642 
0643     td = fhci_remove_td_from_done_list(fhci->hc_list);
0644     while (td != NULL) {
0645         urb = td->urb;
0646         urb_priv = urb->hcpriv;
0647         ed = td->ed;
0648 
0649         /* update URB's length and status from TD */
0650         fhci_done_td(urb, td);
0651         urb_priv->tds_cnt++;
0652 
0653         /*
0654          * if all this urb's TDs are done, call complete()
0655          * Interrupt transfers are the onley special case:
0656          * they are reissued,until "deleted" by usb_unlink_urb
0657          * (real work done in a SOF intr, by process_del_list)
0658          */
0659         if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
0660             fhci_urb_complete_free(fhci, urb);
0661         } else if (urb_priv->state == URB_DEL &&
0662                 ed->state == FHCI_ED_SKIP) {
0663             fhci_del_ed_list(fhci, ed);
0664             ed->state = FHCI_ED_OPER;
0665         } else if (ed->state == FHCI_ED_HALTED) {
0666             urb_priv->state = URB_DEL;
0667             ed->state = FHCI_ED_URB_DEL;
0668             fhci_del_ed_list(fhci, ed);
0669             ed->state = FHCI_ED_OPER;
0670         }
0671 
0672         td = fhci_remove_td_from_done_list(fhci->hc_list);
0673     }
0674 
0675     spin_unlock(&fhci->lock);
0676     enable_irq(fhci->timer->irq);
0677     enable_irq(fhci_to_hcd(fhci)->irq);
0678 }
0679 
0680 DECLARE_TASKLET_OLD(fhci_tasklet, process_done_list);
0681 
0682 /* transfer complted callback */
0683 u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
0684 {
0685     if (!fhci->process_done_task->state)
0686         tasklet_schedule(fhci->process_done_task);
0687     return 0;
0688 }
0689 
0690 /*
0691  * adds urb to the endpoint descriptor list
0692  * arguments:
0693  * fhci     data structure for the Low level host controller
0694  * ep       USB Host endpoint data structure
0695  * urb      USB request block data structure
0696  */
0697 void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
0698 {
0699     struct ed *ed = urb->ep->hcpriv;
0700     struct urb_priv *urb_priv = urb->hcpriv;
0701     u32 data_len = urb->transfer_buffer_length;
0702     int urb_state = 0;
0703     int toggle = 0;
0704     u8 *data;
0705     u16 cnt = 0;
0706 
0707     if (ed == NULL) {
0708         ed = fhci_get_empty_ed(fhci);
0709         ed->dev_addr = usb_pipedevice(urb->pipe);
0710         ed->ep_addr = usb_pipeendpoint(urb->pipe);
0711         switch (usb_pipetype(urb->pipe)) {
0712         case PIPE_CONTROL:
0713             ed->mode = FHCI_TF_CTRL;
0714             break;
0715         case PIPE_BULK:
0716             ed->mode = FHCI_TF_BULK;
0717             break;
0718         case PIPE_INTERRUPT:
0719             ed->mode = FHCI_TF_INTR;
0720             break;
0721         case PIPE_ISOCHRONOUS:
0722             ed->mode = FHCI_TF_ISO;
0723             break;
0724         default:
0725             break;
0726         }
0727         ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
0728             FHCI_LOW_SPEED : FHCI_FULL_SPEED;
0729         ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
0730         urb->ep->hcpriv = ed;
0731         fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
0732              ed->speed, ed->max_pkt_size);
0733     }
0734 
0735     /* for ISO transfer calculate start frame index */
0736     if (ed->mode == FHCI_TF_ISO) {
0737         /* Ignore the possibility of underruns */
0738         urb->start_frame = ed->td_head ? ed->next_iso :
0739                          get_frame_num(fhci);
0740         ed->next_iso = (urb->start_frame + urb->interval *
0741                 urb->number_of_packets) & 0x07ff;
0742     }
0743 
0744     /*
0745      * OHCI handles the DATA toggle itself,we just use the USB
0746      * toggle bits
0747      */
0748     if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
0749               usb_pipeout(urb->pipe)))
0750         toggle = USB_TD_TOGGLE_CARRY;
0751     else {
0752         toggle = USB_TD_TOGGLE_DATA0;
0753         usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
0754                   usb_pipeout(urb->pipe), 1);
0755     }
0756 
0757     urb_priv->tds_cnt = 0;
0758     urb_priv->ed = ed;
0759     if (data_len > 0)
0760         data = urb->transfer_buffer;
0761     else
0762         data = NULL;
0763 
0764     switch (ed->mode) {
0765     case FHCI_TF_BULK:
0766         if (urb->transfer_flags & URB_ZERO_PACKET &&
0767                 urb->transfer_buffer_length > 0 &&
0768                 ((urb->transfer_buffer_length %
0769                 usb_endpoint_maxp(&urb->ep->desc)) == 0))
0770             urb_state = US_BULK0;
0771         while (data_len > 4096) {
0772             fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
0773                 usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
0774                              FHCI_TA_IN,
0775                 cnt ? USB_TD_TOGGLE_CARRY :
0776                       toggle,
0777                 data, 4096, 0, 0, true);
0778             data += 4096;
0779             data_len -= 4096;
0780             cnt++;
0781         }
0782 
0783         fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
0784             usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
0785             cnt ? USB_TD_TOGGLE_CARRY : toggle,
0786             data, data_len, 0, 0, true);
0787         cnt++;
0788 
0789         if (urb->transfer_flags & URB_ZERO_PACKET &&
0790                 cnt < urb_priv->num_of_tds) {
0791             fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
0792                 usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
0793                              FHCI_TA_IN,
0794                 USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
0795             cnt++;
0796         }
0797         break;
0798     case FHCI_TF_INTR:
0799         urb->start_frame = get_frame_num(fhci) + 1;
0800         fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
0801             usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
0802             USB_TD_TOGGLE_DATA0, data, data_len,
0803             urb->interval, urb->start_frame, true);
0804         break;
0805     case FHCI_TF_CTRL:
0806         ed->dev_addr = usb_pipedevice(urb->pipe);
0807         ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
0808 
0809         /* setup stage */
0810         fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
0811             USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
0812 
0813         /* data stage */
0814         if (data_len > 0) {
0815             fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
0816                 usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
0817                              FHCI_TA_IN,
0818                 USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
0819                 true);
0820         }
0821 
0822         /* status stage */
0823         if (data_len > 0)
0824             fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
0825                 (usb_pipeout(urb->pipe) ? FHCI_TA_IN :
0826                               FHCI_TA_OUT),
0827                 USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
0828         else
0829              fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
0830                 FHCI_TA_IN,
0831                 USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
0832 
0833         urb_state = US_CTRL_SETUP;
0834         break;
0835     case FHCI_TF_ISO:
0836         for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
0837             u16 frame = urb->start_frame;
0838 
0839             /*
0840              * FIXME scheduling should handle frame counter
0841              * roll-around ... exotic case (and OHCI has
0842              * a 2^16 iso range, vs other HCs max of 2^10)
0843              */
0844             frame += cnt * urb->interval;
0845             frame &= 0x07ff;
0846             fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
0847                 usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
0848                              FHCI_TA_IN,
0849                 USB_TD_TOGGLE_DATA0,
0850                 data + urb->iso_frame_desc[cnt].offset,
0851                 urb->iso_frame_desc[cnt].length,
0852                 urb->interval, frame, true);
0853         }
0854         break;
0855     default:
0856         break;
0857     }
0858 
0859     /*
0860      * set the state of URB
0861      * control pipe:3 states -- setup,data,status
0862      * interrupt and bulk pipe:1 state -- data
0863      */
0864     urb->pipe &= ~0x1f;
0865     urb->pipe |= urb_state & 0x1f;
0866 
0867     urb_priv->state = URB_INPROGRESS;
0868 
0869     if (!ed->td_head) {
0870         ed->state = FHCI_ED_OPER;
0871         switch (ed->mode) {
0872         case FHCI_TF_CTRL:
0873             list_add(&ed->node, &fhci->hc_list->ctrl_list);
0874             break;
0875         case FHCI_TF_BULK:
0876             list_add(&ed->node, &fhci->hc_list->bulk_list);
0877             break;
0878         case FHCI_TF_INTR:
0879             list_add(&ed->node, &fhci->hc_list->intr_list);
0880             break;
0881         case FHCI_TF_ISO:
0882             list_add(&ed->node, &fhci->hc_list->iso_list);
0883             break;
0884         default:
0885             break;
0886         }
0887     }
0888 
0889     fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
0890     fhci->active_urbs++;
0891 }