1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale QUICC Engine USB Host Controller Driver
5 * Copyright (c) Freescale Semicondutor, Inc. 2006, 2011.
6 * Shlomi Gridish <gridish@freescale.com>
7 * Jerry Huang <Chang-Ming.Huang@freescale.com>
8 * Copyright (c) Logic Product Development, Inc. 2007
9 * Peter Barada <peterb@logicpd.com>
10 * Copyright (c) MontaVista Software, Inc. 2008.
11 * Anton Vorontsov <avorontsov@ru.mvista.com>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
20 #include <linux/interrupt.h>
22 #include <linux/usb.h>
23 #include <linux/usb/hcd.h>
24 #include <soc/fsl/qe/qe.h>
25 #include <asm/fsl_gtm.h>
28 static void recycle_frame(struct fhci_usb
*usb
, struct packet
*pkt
)
32 pkt
->status
= USB_TD_OK
;
34 pkt
->priv_data
= NULL
;
36 cq_put(&usb
->ep0
->empty_frame_Q
, pkt
);
39 /* confirm submitted packet */
40 void fhci_transaction_confirm(struct fhci_usb
*usb
, struct packet
*pkt
)
43 struct packet
*td_pkt
;
48 td
= fhci_remove_td_from_frame(usb
->actual_frame
);
51 td
->status
= pkt
->status
;
52 if (td
->type
== FHCI_TA_IN
&& td_pkt
->info
& PKT_DUMMY_PACKET
) {
53 if ((td
->data
+ td
->actual_len
) && trans_len
)
54 memcpy(td
->data
+ td
->actual_len
, pkt
->data
,
56 cq_put(&usb
->ep0
->dummy_packets_Q
, pkt
->data
);
59 recycle_frame(usb
, pkt
);
62 if (ed
->mode
== FHCI_TF_ISO
) {
63 if (ed
->td_list
.next
->next
!= &ed
->td_list
) {
65 list_entry(ed
->td_list
.next
->next
, struct td
,
68 td_next
->start_frame
= usb
->actual_frame
->frame_num
;
70 td
->actual_len
= trans_len
;
72 } else if ((td
->status
& USB_TD_ERROR
) &&
73 !(td
->status
& USB_TD_TX_ER_NAK
)) {
75 * There was an error on the transaction (but not NAK).
76 * If it is fatal error (data underrun, stall, bad pid or 3
77 * errors exceeded), mark this TD as done.
79 if ((td
->status
& USB_TD_RX_DATA_UNDERUN
) ||
80 (td
->status
& USB_TD_TX_ER_STALL
) ||
81 (td
->status
& USB_TD_RX_ER_PID
) ||
82 (++td
->error_cnt
>= 3)) {
83 ed
->state
= FHCI_ED_HALTED
;
86 if (td
->status
& USB_TD_RX_DATA_UNDERUN
) {
87 fhci_dbg(usb
->fhci
, "td err fu\n");
88 td
->toggle
= !td
->toggle
;
89 td
->actual_len
+= trans_len
;
91 fhci_dbg(usb
->fhci
, "td err f!u\n");
94 fhci_dbg(usb
->fhci
, "td err !f\n");
95 /* it is not a fatal error -retry this transaction */
98 td
->status
= USB_TD_OK
;
100 } else if (td
->status
& USB_TD_TX_ER_NAK
) {
101 /* there was a NAK response */
102 fhci_vdbg(usb
->fhci
, "td nack\n");
105 td
->status
= USB_TD_OK
;
107 /* there was no error on transaction */
110 td
->toggle
= !td
->toggle
;
111 td
->actual_len
+= trans_len
;
113 if (td
->len
== td
->actual_len
)
118 fhci_move_td_from_ed_to_done_list(usb
, ed
);
122 * Flush all transmitted packets from BDs
123 * This routine is called when disabling the USB port to flush all
124 * transmissions that are already scheduled in the BDs
126 void fhci_flush_all_transmissions(struct fhci_usb
*usb
)
131 mode
= in_8(&usb
->fhci
->regs
->usb_usmod
);
132 clrbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_EN
);
136 while ((td
= fhci_peek_td_from_frame(usb
->actual_frame
)) != NULL
) {
137 struct packet
*pkt
= td
->pkt
;
139 pkt
->status
= USB_TD_TX_ER_TIMEOUT
;
140 fhci_transaction_confirm(usb
, pkt
);
143 usb
->actual_frame
->frame_status
= FRAME_END_TRANSMISSION
;
145 /* reset the event register */
146 out_be16(&usb
->fhci
->regs
->usb_usber
, 0xffff);
147 /* enable the USB controller */
148 out_8(&usb
->fhci
->regs
->usb_usmod
, mode
| USB_MODE_EN
);
152 * This function forms the packet and transmit the packet. This function
153 * will handle all endpoint type:ISO,interrupt,control and bulk
155 static int add_packet(struct fhci_usb
*usb
, struct ed
*ed
, struct td
*td
)
157 u32 fw_transaction_time
, len
= 0;
161 /* calcalate data address,len and toggle and then add the transaction */
162 if (td
->toggle
== USB_TD_TOGGLE_CARRY
)
163 td
->toggle
= ed
->toggle_carry
;
168 if (td
->type
!= FHCI_TA_IN
)
173 len
= min(td
->len
- td
->actual_len
, ed
->max_pkt_size
);
174 if (!((td
->type
== FHCI_TA_IN
) &&
175 ((len
+ td
->actual_len
) == td
->len
)))
176 data
= td
->data
+ td
->actual_len
;
179 len
= min(td
->len
, ed
->max_pkt_size
);
180 if (!((td
->type
== FHCI_TA_IN
) &&
181 ((td
->len
+ CRC_SIZE
) >= ed
->max_pkt_size
)))
188 if (usb
->port_status
== FHCI_PORT_FULL
)
189 fw_transaction_time
= (((len
+ PROTOCOL_OVERHEAD
) * 11) >> 4);
191 fw_transaction_time
= ((len
+ PROTOCOL_OVERHEAD
) * 6);
193 /* check if there's enough space in this frame to submit this TD */
194 if (usb
->actual_frame
->total_bytes
+ len
+ PROTOCOL_OVERHEAD
>=
195 usb
->max_bytes_per_frame
) {
196 fhci_vdbg(usb
->fhci
, "not enough space in this frame: "
197 "%d %d %d\n", usb
->actual_frame
->total_bytes
, len
,
198 usb
->max_bytes_per_frame
);
202 /* check if there's enough time in this frame to submit this TD */
203 if (usb
->actual_frame
->frame_status
!= FRAME_IS_PREPARED
&&
204 (usb
->actual_frame
->frame_status
& FRAME_END_TRANSMISSION
||
205 (fw_transaction_time
+ usb
->sw_transaction_time
>=
206 1000 - fhci_get_sof_timer_count(usb
)))) {
207 fhci_dbg(usb
->fhci
, "not enough time in this frame\n");
211 /* update frame object fields before transmitting */
212 pkt
= cq_get(&usb
->ep0
->empty_frame_Q
);
214 fhci_dbg(usb
->fhci
, "there is no empty frame\n");
221 data
= cq_get(&usb
->ep0
->dummy_packets_Q
);
223 pkt
->info
= PKT_DUMMY_PACKET
;
227 pkt
->status
= USB_TD_OK
;
228 /* update TD status field before transmitting */
229 td
->status
= USB_TD_INPROGRESS
;
230 /* update actual frame time object with the actual transmission */
231 usb
->actual_frame
->total_bytes
+= (len
+ PROTOCOL_OVERHEAD
);
232 fhci_add_td_to_frame(usb
->actual_frame
, td
);
234 if (usb
->port_status
!= FHCI_PORT_FULL
&&
235 usb
->port_status
!= FHCI_PORT_LOW
) {
236 pkt
->status
= USB_TD_TX_ER_TIMEOUT
;
238 fhci_transaction_confirm(usb
, pkt
);
239 } else if (fhci_host_transaction(usb
, pkt
, td
->type
, ed
->dev_addr
,
240 ed
->ep_addr
, ed
->mode
, ed
->speed
, td
->toggle
)) {
241 /* remove TD from actual frame */
242 list_del_init(&td
->frame_lh
);
243 td
->status
= USB_TD_OK
;
244 if (pkt
->info
& PKT_DUMMY_PACKET
)
245 cq_put(&usb
->ep0
->dummy_packets_Q
, pkt
->data
);
246 recycle_frame(usb
, pkt
);
247 usb
->actual_frame
->total_bytes
-= (len
+ PROTOCOL_OVERHEAD
);
248 fhci_err(usb
->fhci
, "host transaction failed\n");
255 static void move_head_to_tail(struct list_head
*list
)
257 struct list_head
*node
= list
->next
;
259 if (!list_empty(list
)) {
260 list_move_tail(node
, list
);
265 * This function goes through the endpoint list and schedules the
266 * transactions within this list
268 static int scan_ed_list(struct fhci_usb
*usb
,
269 struct list_head
*list
, enum fhci_tf_mode list_type
)
271 static const int frame_part
[4] = {
272 [FHCI_TF_CTRL
] = MAX_BYTES_PER_FRAME
,
273 [FHCI_TF_ISO
] = (MAX_BYTES_PER_FRAME
*
274 MAX_PERIODIC_FRAME_USAGE
) / 100,
275 [FHCI_TF_BULK
] = MAX_BYTES_PER_FRAME
,
276 [FHCI_TF_INTR
] = (MAX_BYTES_PER_FRAME
*
277 MAX_PERIODIC_FRAME_USAGE
) / 100
282 u32 save_transaction_time
= usb
->sw_transaction_time
;
284 list_for_each_entry(ed
, list
, node
) {
287 if (!td
|| td
->status
== USB_TD_INPROGRESS
)
290 if (ed
->state
!= FHCI_ED_OPER
) {
291 if (ed
->state
== FHCI_ED_URB_DEL
) {
292 td
->status
= USB_TD_OK
;
293 fhci_move_td_from_ed_to_done_list(usb
, ed
);
294 ed
->state
= FHCI_ED_SKIP
;
300 * if it isn't interrupt pipe or it is not iso pipe and the
301 * interval time passed
303 if ((list_type
== FHCI_TF_INTR
|| list_type
== FHCI_TF_ISO
) &&
304 (((usb
->actual_frame
->frame_num
-
305 td
->start_frame
) & 0x7ff) < td
->interval
))
308 if (add_packet(usb
, ed
, td
) < 0)
311 /* update time stamps in the TD */
312 td
->start_frame
= usb
->actual_frame
->frame_num
;
313 usb
->sw_transaction_time
+= save_transaction_time
;
315 if (usb
->actual_frame
->total_bytes
>=
316 usb
->max_bytes_per_frame
) {
317 usb
->actual_frame
->frame_status
=
318 FRAME_DATA_END_TRANSMISSION
;
319 fhci_push_dummy_bd(usb
->ep0
);
324 if (usb
->actual_frame
->total_bytes
>= frame_part
[list_type
])
328 /* be fair to each ED(move list head around) */
329 move_head_to_tail(list
);
330 usb
->sw_transaction_time
= save_transaction_time
;
335 static u32
rotate_frames(struct fhci_usb
*usb
)
337 struct fhci_hcd
*fhci
= usb
->fhci
;
339 if (!list_empty(&usb
->actual_frame
->tds_list
)) {
340 if ((((in_be16(&fhci
->pram
->frame_num
) & 0x07ff) -
341 usb
->actual_frame
->frame_num
) & 0x7ff) > 5)
342 fhci_flush_actual_frame(usb
);
347 usb
->actual_frame
->frame_status
= FRAME_IS_PREPARED
;
348 usb
->actual_frame
->frame_num
= in_be16(&fhci
->pram
->frame_num
) & 0x7ff;
349 usb
->actual_frame
->total_bytes
= 0;
355 * This function schedule the USB transaction and will process the
356 * endpoint in the following order: iso, interrupt, control and bulk.
358 void fhci_schedule_transactions(struct fhci_usb
*usb
)
362 if (usb
->actual_frame
->frame_status
& FRAME_END_TRANSMISSION
)
363 if (rotate_frames(usb
) != 0)
366 if (usb
->actual_frame
->frame_status
& FRAME_END_TRANSMISSION
)
369 if (usb
->actual_frame
->total_bytes
== 0) {
371 * schedule the next available ISO transfer
372 *or next stage of the ISO transfer
374 scan_ed_list(usb
, &usb
->hc_list
->iso_list
, FHCI_TF_ISO
);
377 * schedule the next available interrupt transfer or
378 * the next stage of the interrupt transfer
380 scan_ed_list(usb
, &usb
->hc_list
->intr_list
, FHCI_TF_INTR
);
383 * schedule the next available control transfer
384 * or the next stage of the control transfer
386 left
= scan_ed_list(usb
, &usb
->hc_list
->ctrl_list
,
391 * schedule the next available bulk transfer or the next stage of the
395 scan_ed_list(usb
, &usb
->hc_list
->bulk_list
, FHCI_TF_BULK
);
398 /* Handles SOF interrupt */
399 static void sof_interrupt(struct fhci_hcd
*fhci
)
401 struct fhci_usb
*usb
= fhci
->usb_lld
;
403 if ((usb
->port_status
== FHCI_PORT_DISABLED
) &&
404 (usb
->vroot_hub
->port
.wPortStatus
& USB_PORT_STAT_CONNECTION
) &&
405 !(usb
->vroot_hub
->port
.wPortChange
& USB_PORT_STAT_C_CONNECTION
)) {
406 if (usb
->vroot_hub
->port
.wPortStatus
& USB_PORT_STAT_LOW_SPEED
)
407 usb
->port_status
= FHCI_PORT_LOW
;
409 usb
->port_status
= FHCI_PORT_FULL
;
411 usb
->saved_msk
&= ~USB_E_IDLE_MASK
;
412 out_be16(&usb
->fhci
->regs
->usb_usbmr
, usb
->saved_msk
);
415 gtm_set_exact_timer16(fhci
->timer
, usb
->max_frame_usage
, false);
417 fhci_host_transmit_actual_frame(usb
);
418 usb
->actual_frame
->frame_status
= FRAME_IS_TRANSMITTED
;
420 fhci_schedule_transactions(usb
);
423 /* Handles device disconnected interrupt on port */
424 void fhci_device_disconnected_interrupt(struct fhci_hcd
*fhci
)
426 struct fhci_usb
*usb
= fhci
->usb_lld
;
428 fhci_dbg(fhci
, "-> %s\n", __func__
);
430 fhci_usb_disable_interrupt(usb
);
431 clrbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_LSS
);
432 usb
->port_status
= FHCI_PORT_DISABLED
;
434 fhci_stop_sof_timer(fhci
);
436 /* Enable IDLE since we want to know if something comes along */
437 usb
->saved_msk
|= USB_E_IDLE_MASK
;
438 out_be16(&usb
->fhci
->regs
->usb_usbmr
, usb
->saved_msk
);
440 usb
->vroot_hub
->port
.wPortStatus
&= ~USB_PORT_STAT_CONNECTION
;
441 usb
->vroot_hub
->port
.wPortChange
|= USB_PORT_STAT_C_CONNECTION
;
442 usb
->max_bytes_per_frame
= 0;
443 fhci_usb_enable_interrupt(usb
);
445 fhci_dbg(fhci
, "<- %s\n", __func__
);
448 /* detect a new device connected on the USB port */
449 void fhci_device_connected_interrupt(struct fhci_hcd
*fhci
)
452 struct fhci_usb
*usb
= fhci
->usb_lld
;
456 fhci_dbg(fhci
, "-> %s\n", __func__
);
458 fhci_usb_disable_interrupt(usb
);
459 state
= fhci_ioports_check_bus_state(fhci
);
461 /* low-speed device was connected to the USB port */
463 ret
= qe_usb_clock_set(fhci
->lowspeed_clk
, USB_CLOCK
>> 3);
465 fhci_warn(fhci
, "Low-Speed device is not supported, "
470 usb
->port_status
= FHCI_PORT_LOW
;
471 setbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_LSS
);
472 usb
->vroot_hub
->port
.wPortStatus
|=
473 (USB_PORT_STAT_LOW_SPEED
|
474 USB_PORT_STAT_CONNECTION
);
475 usb
->vroot_hub
->port
.wPortChange
|=
476 USB_PORT_STAT_C_CONNECTION
;
477 usb
->max_bytes_per_frame
=
478 (MAX_BYTES_PER_FRAME
>> 3) - 7;
479 fhci_port_enable(usb
);
480 } else if (state
== 2) {
481 ret
= qe_usb_clock_set(fhci
->fullspeed_clk
, USB_CLOCK
);
483 fhci_warn(fhci
, "Full-Speed device is not supported, "
488 usb
->port_status
= FHCI_PORT_FULL
;
489 clrbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_LSS
);
490 usb
->vroot_hub
->port
.wPortStatus
&=
491 ~USB_PORT_STAT_LOW_SPEED
;
492 usb
->vroot_hub
->port
.wPortStatus
|=
493 USB_PORT_STAT_CONNECTION
;
494 usb
->vroot_hub
->port
.wPortChange
|=
495 USB_PORT_STAT_C_CONNECTION
;
496 usb
->max_bytes_per_frame
= (MAX_BYTES_PER_FRAME
- 15);
497 fhci_port_enable(usb
);
500 fhci_usb_enable_interrupt(usb
);
501 fhci_dbg(fhci
, "<- %s\n", __func__
);
504 irqreturn_t
fhci_frame_limit_timer_irq(int irq
, void *_hcd
)
506 struct usb_hcd
*hcd
= _hcd
;
507 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
508 struct fhci_usb
*usb
= fhci
->usb_lld
;
510 spin_lock(&fhci
->lock
);
512 gtm_set_exact_timer16(fhci
->timer
, 1000, false);
514 if (usb
->actual_frame
->frame_status
== FRAME_IS_TRANSMITTED
) {
515 usb
->actual_frame
->frame_status
= FRAME_TIMER_END_TRANSMISSION
;
516 fhci_push_dummy_bd(usb
->ep0
);
519 fhci_schedule_transactions(usb
);
521 spin_unlock(&fhci
->lock
);
526 /* Cancel transmission on the USB endpoint */
527 static void abort_transmission(struct fhci_usb
*usb
)
529 fhci_dbg(usb
->fhci
, "-> %s\n", __func__
);
530 /* issue stop Tx command */
531 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
, EP_ZERO
, 0);
533 out_8(&usb
->fhci
->regs
->usb_uscom
, USB_CMD_FLUSH_FIFO
| EP_ZERO
);
537 /* issue restart Tx command */
538 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
, EP_ZERO
, 0);
539 fhci_dbg(usb
->fhci
, "<- %s\n", __func__
);
542 irqreturn_t
fhci_irq(struct usb_hcd
*hcd
)
544 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
545 struct fhci_usb
*usb
;
549 spin_lock_irqsave(&fhci
->lock
, flags
);
553 usb_er
|= in_be16(&usb
->fhci
->regs
->usb_usber
) &
554 in_be16(&usb
->fhci
->regs
->usb_usbmr
);
556 /* clear event bits for next time */
557 out_be16(&usb
->fhci
->regs
->usb_usber
, usb_er
);
559 fhci_dbg_isr(fhci
, usb_er
);
561 if (usb_er
& USB_E_RESET_MASK
) {
562 if ((usb
->port_status
== FHCI_PORT_FULL
) ||
563 (usb
->port_status
== FHCI_PORT_LOW
)) {
564 fhci_device_disconnected_interrupt(fhci
);
565 usb_er
&= ~USB_E_IDLE_MASK
;
566 } else if (usb
->port_status
== FHCI_PORT_WAITING
) {
567 usb
->port_status
= FHCI_PORT_DISCONNECTING
;
569 /* Turn on IDLE since we want to disconnect */
570 usb
->saved_msk
|= USB_E_IDLE_MASK
;
571 out_be16(&usb
->fhci
->regs
->usb_usber
,
573 } else if (usb
->port_status
== FHCI_PORT_DISABLED
) {
574 if (fhci_ioports_check_bus_state(fhci
) == 1)
575 fhci_device_connected_interrupt(fhci
);
577 usb_er
&= ~USB_E_RESET_MASK
;
580 if (usb_er
& USB_E_MSF_MASK
) {
581 abort_transmission(fhci
->usb_lld
);
582 usb_er
&= ~USB_E_MSF_MASK
;
585 if (usb_er
& (USB_E_SOF_MASK
| USB_E_SFT_MASK
)) {
587 usb_er
&= ~(USB_E_SOF_MASK
| USB_E_SFT_MASK
);
590 if (usb_er
& USB_E_TXB_MASK
) {
591 fhci_tx_conf_interrupt(fhci
->usb_lld
);
592 usb_er
&= ~USB_E_TXB_MASK
;
595 if (usb_er
& USB_E_TXE1_MASK
) {
596 fhci_tx_conf_interrupt(fhci
->usb_lld
);
597 usb_er
&= ~USB_E_TXE1_MASK
;
600 if (usb_er
& USB_E_IDLE_MASK
) {
601 if (usb
->port_status
== FHCI_PORT_DISABLED
) {
602 usb_er
&= ~USB_E_RESET_MASK
;
603 fhci_device_connected_interrupt(fhci
);
604 } else if (usb
->port_status
==
605 FHCI_PORT_DISCONNECTING
) {
606 /* XXX usb->port_status = FHCI_PORT_WAITING; */
608 usb
->saved_msk
&= ~USB_E_IDLE_MASK
;
609 out_be16(&usb
->fhci
->regs
->usb_usbmr
,
612 fhci_dbg_isr(fhci
, -1);
615 usb_er
&= ~USB_E_IDLE_MASK
;
618 spin_unlock_irqrestore(&fhci
->lock
, flags
);
625 * Process normal completions(error or success) and clean the schedule.
627 * This is the main path for handing urbs back to drivers. The only other patth
628 * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
629 * the (re-reversed) done list as this does.
631 static void process_done_list(unsigned long data
)
636 struct urb_priv
*urb_priv
;
637 struct fhci_hcd
*fhci
= (struct fhci_hcd
*)data
;
639 disable_irq(fhci
->timer
->irq
);
640 disable_irq(fhci_to_hcd(fhci
)->irq
);
641 spin_lock(&fhci
->lock
);
643 td
= fhci_remove_td_from_done_list(fhci
->hc_list
);
646 urb_priv
= urb
->hcpriv
;
649 /* update URB's length and status from TD */
650 fhci_done_td(urb
, td
);
654 * if all this urb's TDs are done, call complete()
655 * Interrupt transfers are the onley special case:
656 * they are reissued,until "deleted" by usb_unlink_urb
657 * (real work done in a SOF intr, by process_del_list)
659 if (urb_priv
->tds_cnt
== urb_priv
->num_of_tds
) {
660 fhci_urb_complete_free(fhci
, urb
);
661 } else if (urb_priv
->state
== URB_DEL
&&
662 ed
->state
== FHCI_ED_SKIP
) {
663 fhci_del_ed_list(fhci
, ed
);
664 ed
->state
= FHCI_ED_OPER
;
665 } else if (ed
->state
== FHCI_ED_HALTED
) {
666 urb_priv
->state
= URB_DEL
;
667 ed
->state
= FHCI_ED_URB_DEL
;
668 fhci_del_ed_list(fhci
, ed
);
669 ed
->state
= FHCI_ED_OPER
;
672 td
= fhci_remove_td_from_done_list(fhci
->hc_list
);
675 spin_unlock(&fhci
->lock
);
676 enable_irq(fhci
->timer
->irq
);
677 enable_irq(fhci_to_hcd(fhci
)->irq
);
680 DECLARE_TASKLET(fhci_tasklet
, process_done_list
, 0);
682 /* transfer complted callback */
683 u32
fhci_transfer_confirm_callback(struct fhci_hcd
*fhci
)
685 if (!fhci
->process_done_task
->state
)
686 tasklet_schedule(fhci
->process_done_task
);
691 * adds urb to the endpoint descriptor list
693 * fhci data structure for the Low level host controller
694 * ep USB Host endpoint data structure
695 * urb USB request block data structure
697 void fhci_queue_urb(struct fhci_hcd
*fhci
, struct urb
*urb
)
699 struct ed
*ed
= urb
->ep
->hcpriv
;
700 struct urb_priv
*urb_priv
= urb
->hcpriv
;
701 u32 data_len
= urb
->transfer_buffer_length
;
709 ed
= fhci_get_empty_ed(fhci
);
710 ed
->dev_addr
= usb_pipedevice(urb
->pipe
);
711 ed
->ep_addr
= usb_pipeendpoint(urb
->pipe
);
712 switch (usb_pipetype(urb
->pipe
)) {
714 ed
->mode
= FHCI_TF_CTRL
;
717 ed
->mode
= FHCI_TF_BULK
;
720 ed
->mode
= FHCI_TF_INTR
;
722 case PIPE_ISOCHRONOUS
:
723 ed
->mode
= FHCI_TF_ISO
;
728 ed
->speed
= (urb
->dev
->speed
== USB_SPEED_LOW
) ?
729 FHCI_LOW_SPEED
: FHCI_FULL_SPEED
;
730 ed
->max_pkt_size
= usb_maxpacket(urb
->dev
,
731 urb
->pipe
, usb_pipeout(urb
->pipe
));
732 urb
->ep
->hcpriv
= ed
;
733 fhci_dbg(fhci
, "new ep speed=%d max_pkt_size=%d\n",
734 ed
->speed
, ed
->max_pkt_size
);
737 /* for ISO transfer calculate start frame index */
738 if (ed
->mode
== FHCI_TF_ISO
) {
739 /* Ignore the possibility of underruns */
740 urb
->start_frame
= ed
->td_head
? ed
->next_iso
:
742 ed
->next_iso
= (urb
->start_frame
+ urb
->interval
*
743 urb
->number_of_packets
) & 0x07ff;
747 * OHCI handles the DATA toggle itself,we just use the USB
750 if (usb_gettoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
751 usb_pipeout(urb
->pipe
)))
752 toggle
= USB_TD_TOGGLE_CARRY
;
754 toggle
= USB_TD_TOGGLE_DATA0
;
755 usb_settoggle(urb
->dev
, usb_pipeendpoint(urb
->pipe
),
756 usb_pipeout(urb
->pipe
), 1);
759 urb_priv
->tds_cnt
= 0;
762 data
= urb
->transfer_buffer
;
768 if (urb
->transfer_flags
& URB_ZERO_PACKET
&&
769 urb
->transfer_buffer_length
> 0 &&
770 ((urb
->transfer_buffer_length
%
771 usb_maxpacket(urb
->dev
, urb
->pipe
,
772 usb_pipeout(urb
->pipe
))) == 0))
773 urb_state
= US_BULK0
;
774 while (data_len
> 4096) {
775 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
,
776 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
:
778 cnt
? USB_TD_TOGGLE_CARRY
:
780 data
, 4096, 0, 0, true);
786 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
,
787 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
: FHCI_TA_IN
,
788 cnt
? USB_TD_TOGGLE_CARRY
: toggle
,
789 data
, data_len
, 0, 0, true);
792 if (urb
->transfer_flags
& URB_ZERO_PACKET
&&
793 cnt
< urb_priv
->num_of_tds
) {
794 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
,
795 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
:
797 USB_TD_TOGGLE_CARRY
, NULL
, 0, 0, 0, true);
802 urb
->start_frame
= get_frame_num(fhci
) + 1;
803 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
++,
804 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
: FHCI_TA_IN
,
805 USB_TD_TOGGLE_DATA0
, data
, data_len
,
806 urb
->interval
, urb
->start_frame
, true);
809 ed
->dev_addr
= usb_pipedevice(urb
->pipe
);
810 ed
->max_pkt_size
= usb_maxpacket(urb
->dev
, urb
->pipe
,
811 usb_pipeout(urb
->pipe
));
813 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
++, FHCI_TA_SETUP
,
814 USB_TD_TOGGLE_DATA0
, urb
->setup_packet
, 8, 0, 0, true);
818 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
++,
819 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
:
821 USB_TD_TOGGLE_DATA1
, data
, data_len
, 0, 0,
827 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
++,
828 (usb_pipeout(urb
->pipe
) ? FHCI_TA_IN
:
830 USB_TD_TOGGLE_DATA1
, data
, 0, 0, 0, true);
832 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
++,
834 USB_TD_TOGGLE_DATA1
, data
, 0, 0, 0, true);
836 urb_state
= US_CTRL_SETUP
;
839 for (cnt
= 0; cnt
< urb
->number_of_packets
; cnt
++) {
840 u16 frame
= urb
->start_frame
;
843 * FIXME scheduling should handle frame counter
844 * roll-around ... exotic case (and OHCI has
845 * a 2^16 iso range, vs other HCs max of 2^10)
847 frame
+= cnt
* urb
->interval
;
849 td
= fhci_td_fill(fhci
, urb
, urb_priv
, ed
, cnt
,
850 usb_pipeout(urb
->pipe
) ? FHCI_TA_OUT
:
853 data
+ urb
->iso_frame_desc
[cnt
].offset
,
854 urb
->iso_frame_desc
[cnt
].length
,
855 urb
->interval
, frame
, true);
863 * set the state of URB
864 * control pipe:3 states -- setup,data,status
865 * interrupt and bulk pipe:1 state -- data
868 urb
->pipe
|= urb_state
& 0x1f;
870 urb_priv
->state
= URB_INPROGRESS
;
873 ed
->state
= FHCI_ED_OPER
;
876 list_add(&ed
->node
, &fhci
->hc_list
->ctrl_list
);
879 list_add(&ed
->node
, &fhci
->hc_list
->bulk_list
);
882 list_add(&ed
->node
, &fhci
->hc_list
->intr_list
);
885 list_add(&ed
->node
, &fhci
->hc_list
->iso_list
);
892 fhci_add_tds_to_ed(ed
, urb_priv
->tds
, urb_priv
->num_of_tds
);