xref: /openbmc/linux/drivers/usb/host/fhci-sched.c (revision 027c5215)
1 /*
2  * Freescale QUICC Engine USB Host Controller Driver
3  *
4  * Copyright (c) Freescale Semicondutor, Inc. 2006, 2011.
5  *               Shlomi Gridish <gridish@freescale.com>
6  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
7  * Copyright (c) Logic Product Development, Inc. 2007
8  *               Peter Barada <peterb@logicpd.com>
9  * Copyright (c) MontaVista Software, Inc. 2008.
10  *               Anton Vorontsov <avorontsov@ru.mvista.com>
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  */
17 
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/spinlock.h>
21 #include <linux/delay.h>
22 #include <linux/errno.h>
23 #include <linux/list.h>
24 #include <linux/interrupt.h>
25 #include <linux/io.h>
26 #include <linux/usb.h>
27 #include <linux/usb/hcd.h>
28 #include <asm/qe.h>
29 #include <asm/fsl_gtm.h>
30 #include "fhci.h"
31 
32 static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
33 {
34 	pkt->data = NULL;
35 	pkt->len = 0;
36 	pkt->status = USB_TD_OK;
37 	pkt->info = 0;
38 	pkt->priv_data = NULL;
39 
40 	cq_put(&usb->ep0->empty_frame_Q, pkt);
41 }
42 
43 /* confirm submitted packet */
44 void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
45 {
46 	struct td *td;
47 	struct packet *td_pkt;
48 	struct ed *ed;
49 	u32 trans_len;
50 	bool td_done = false;
51 
52 	td = fhci_remove_td_from_frame(usb->actual_frame);
53 	td_pkt = td->pkt;
54 	trans_len = pkt->len;
55 	td->status = pkt->status;
56 	if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
57 		if ((td->data + td->actual_len) && trans_len)
58 			memcpy(td->data + td->actual_len, pkt->data,
59 			       trans_len);
60 		cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
61 	}
62 
63 	recycle_frame(usb, pkt);
64 
65 	ed = td->ed;
66 	if (ed->mode == FHCI_TF_ISO) {
67 		if (ed->td_list.next->next != &ed->td_list) {
68 			struct td *td_next =
69 			    list_entry(ed->td_list.next->next, struct td,
70 				       node);
71 
72 			td_next->start_frame = usb->actual_frame->frame_num;
73 		}
74 		td->actual_len = trans_len;
75 		td_done = true;
76 	} else if ((td->status & USB_TD_ERROR) &&
77 			!(td->status & USB_TD_TX_ER_NAK)) {
78 		/*
79 		 * There was an error on the transaction (but not NAK).
80 		 * If it is fatal error (data underrun, stall, bad pid or 3
81 		 * errors exceeded), mark this TD as done.
82 		 */
83 		if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
84 				(td->status & USB_TD_TX_ER_STALL) ||
85 				(td->status & USB_TD_RX_ER_PID) ||
86 				(++td->error_cnt >= 3)) {
87 			ed->state = FHCI_ED_HALTED;
88 			td_done = true;
89 
90 			if (td->status & USB_TD_RX_DATA_UNDERUN) {
91 				fhci_dbg(usb->fhci, "td err fu\n");
92 				td->toggle = !td->toggle;
93 				td->actual_len += trans_len;
94 			} else {
95 				fhci_dbg(usb->fhci, "td err f!u\n");
96 			}
97 		} else {
98 			fhci_dbg(usb->fhci, "td err !f\n");
99 			/* it is not a fatal error -retry this transaction */
100 			td->nak_cnt = 0;
101 			td->error_cnt++;
102 			td->status = USB_TD_OK;
103 		}
104 	} else if (td->status & USB_TD_TX_ER_NAK) {
105 		/* there was a NAK response */
106 		fhci_vdbg(usb->fhci, "td nack\n");
107 		td->nak_cnt++;
108 		td->error_cnt = 0;
109 		td->status = USB_TD_OK;
110 	} else {
111 		/* there was no error on transaction */
112 		td->error_cnt = 0;
113 		td->nak_cnt = 0;
114 		td->toggle = !td->toggle;
115 		td->actual_len += trans_len;
116 
117 		if (td->len == td->actual_len)
118 			td_done = true;
119 	}
120 
121 	if (td_done)
122 		fhci_move_td_from_ed_to_done_list(usb, ed);
123 }
124 
125 /*
126  * Flush all transmitted packets from BDs
127  * This routine is called when disabling the USB port to flush all
128  * transmissions that are already scheduled in the BDs
129  */
130 void fhci_flush_all_transmissions(struct fhci_usb *usb)
131 {
132 	u8 mode;
133 	struct td *td;
134 
135 	mode = in_8(&usb->fhci->regs->usb_usmod);
136 	clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
137 
138 	fhci_flush_bds(usb);
139 
140 	while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
141 		struct packet *pkt = td->pkt;
142 
143 		pkt->status = USB_TD_TX_ER_TIMEOUT;
144 		fhci_transaction_confirm(usb, pkt);
145 	}
146 
147 	usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;
148 
149 	/* reset the event register */
150 	out_be16(&usb->fhci->regs->usb_usber, 0xffff);
151 	/* enable the USB controller */
152 	out_8(&usb->fhci->regs->usb_usmod, mode | USB_MODE_EN);
153 }
154 
155 /*
156  * This function forms the packet and transmit the packet. This function
157  * will handle all endpoint type:ISO,interrupt,control and bulk
158  */
159 static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
160 {
161 	u32 fw_transaction_time, len = 0;
162 	struct packet *pkt;
163 	u8 *data = NULL;
164 
165 	/* calcalate data address,len and toggle and then add the transaction */
166 	if (td->toggle == USB_TD_TOGGLE_CARRY)
167 		td->toggle = ed->toggle_carry;
168 
169 	switch (ed->mode) {
170 	case FHCI_TF_ISO:
171 		len = td->len;
172 		if (td->type != FHCI_TA_IN)
173 			data = td->data;
174 		break;
175 	case FHCI_TF_CTRL:
176 	case FHCI_TF_BULK:
177 		len = min(td->len - td->actual_len, ed->max_pkt_size);
178 		if (!((td->type == FHCI_TA_IN) &&
179 		      ((len + td->actual_len) == td->len)))
180 			data = td->data + td->actual_len;
181 		break;
182 	case FHCI_TF_INTR:
183 		len = min(td->len, ed->max_pkt_size);
184 		if (!((td->type == FHCI_TA_IN) &&
185 		      ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
186 			data = td->data;
187 		break;
188 	default:
189 		break;
190 	}
191 
192 	if (usb->port_status == FHCI_PORT_FULL)
193 		fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
194 	else
195 		fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);
196 
197 	/* check if there's enough space in this frame to submit this TD */
198 	if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
199 			usb->max_bytes_per_frame) {
200 		fhci_vdbg(usb->fhci, "not enough space in this frame: "
201 			  "%d %d %d\n", usb->actual_frame->total_bytes, len,
202 			  usb->max_bytes_per_frame);
203 		return -1;
204 	}
205 
206 	/* check if there's enough time in this frame to submit this TD */
207 	if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
208 	    (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
209 	     (fw_transaction_time + usb->sw_transaction_time >=
210 	      1000 - fhci_get_sof_timer_count(usb)))) {
211 		fhci_dbg(usb->fhci, "not enough time in this frame\n");
212 		return -1;
213 	}
214 
215 	/* update frame object fields before transmitting */
216 	pkt = cq_get(&usb->ep0->empty_frame_Q);
217 	if (!pkt) {
218 		fhci_dbg(usb->fhci, "there is no empty frame\n");
219 		return -1;
220 	}
221 	td->pkt = pkt;
222 
223 	pkt->info = 0;
224 	if (data == NULL) {
225 		data = cq_get(&usb->ep0->dummy_packets_Q);
226 		BUG_ON(!data);
227 		pkt->info = PKT_DUMMY_PACKET;
228 	}
229 	pkt->data = data;
230 	pkt->len = len;
231 	pkt->status = USB_TD_OK;
232 	/* update TD status field before transmitting */
233 	td->status = USB_TD_INPROGRESS;
234 	/* update actual frame time object with the actual transmission */
235 	usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
236 	fhci_add_td_to_frame(usb->actual_frame, td);
237 
238 	if (usb->port_status != FHCI_PORT_FULL &&
239 			usb->port_status != FHCI_PORT_LOW) {
240 		pkt->status = USB_TD_TX_ER_TIMEOUT;
241 		pkt->len = 0;
242 		fhci_transaction_confirm(usb, pkt);
243 	} else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
244 			ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
245 		/* remove TD from actual frame */
246 		list_del_init(&td->frame_lh);
247 		td->status = USB_TD_OK;
248 		if (pkt->info & PKT_DUMMY_PACKET)
249 			cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
250 		recycle_frame(usb, pkt);
251 		usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
252 		fhci_err(usb->fhci, "host transaction failed\n");
253 		return -1;
254 	}
255 
256 	return len;
257 }
258 
259 static void move_head_to_tail(struct list_head *list)
260 {
261 	struct list_head *node = list->next;
262 
263 	if (!list_empty(list)) {
264 		list_move_tail(node, list);
265 	}
266 }
267 
268 /*
269  * This function goes through the endpoint list and schedules the
270  * transactions within this list
271  */
272 static int scan_ed_list(struct fhci_usb *usb,
273 			struct list_head *list, enum fhci_tf_mode list_type)
274 {
275 	static const int frame_part[4] = {
276 		[FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
277 		[FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
278 				 MAX_PERIODIC_FRAME_USAGE) / 100,
279 		[FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
280 		[FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
281 				  MAX_PERIODIC_FRAME_USAGE) / 100
282 	};
283 	struct ed *ed;
284 	struct td *td;
285 	int ans = 1;
286 	u32 save_transaction_time = usb->sw_transaction_time;
287 
288 	list_for_each_entry(ed, list, node) {
289 		td = ed->td_head;
290 
291 		if (!td || (td && td->status == USB_TD_INPROGRESS))
292 			continue;
293 
294 		if (ed->state != FHCI_ED_OPER) {
295 			if (ed->state == FHCI_ED_URB_DEL) {
296 				td->status = USB_TD_OK;
297 				fhci_move_td_from_ed_to_done_list(usb, ed);
298 				ed->state = FHCI_ED_SKIP;
299 			}
300 			continue;
301 		}
302 
303 		/*
304 		 * if it isn't interrupt pipe or it is not iso pipe and the
305 		 * interval time passed
306 		 */
307 		if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
308 				(((usb->actual_frame->frame_num -
309 				   td->start_frame) & 0x7ff) < td->interval))
310 			continue;
311 
312 		if (add_packet(usb, ed, td) < 0)
313 			continue;
314 
315 		/* update time stamps in the TD */
316 		td->start_frame = usb->actual_frame->frame_num;
317 		usb->sw_transaction_time += save_transaction_time;
318 
319 		if (usb->actual_frame->total_bytes >=
320 					usb->max_bytes_per_frame) {
321 			usb->actual_frame->frame_status =
322 				FRAME_DATA_END_TRANSMISSION;
323 			fhci_push_dummy_bd(usb->ep0);
324 			ans = 0;
325 			break;
326 		}
327 
328 		if (usb->actual_frame->total_bytes >= frame_part[list_type])
329 			break;
330 	}
331 
332 	/* be fair to each ED(move list head around) */
333 	move_head_to_tail(list);
334 	usb->sw_transaction_time = save_transaction_time;
335 
336 	return ans;
337 }
338 
339 static u32 rotate_frames(struct fhci_usb *usb)
340 {
341 	struct fhci_hcd *fhci = usb->fhci;
342 
343 	if (!list_empty(&usb->actual_frame->tds_list)) {
344 		if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
345 		      usb->actual_frame->frame_num) & 0x7ff) > 5)
346 			fhci_flush_actual_frame(usb);
347 		else
348 			return -EINVAL;
349 	}
350 
351 	usb->actual_frame->frame_status = FRAME_IS_PREPARED;
352 	usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
353 	usb->actual_frame->total_bytes = 0;
354 
355 	return 0;
356 }
357 
358 /*
359  * This function schedule the USB transaction and will process the
360  * endpoint in the following order: iso, interrupt, control and bulk.
361  */
362 void fhci_schedule_transactions(struct fhci_usb *usb)
363 {
364 	int left = 1;
365 
366 	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
367 		if (rotate_frames(usb) != 0)
368 			return;
369 
370 	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
371 		return;
372 
373 	if (usb->actual_frame->total_bytes == 0) {
374 		/*
375 		 * schedule the next available ISO transfer
376 		 *or next stage of the ISO transfer
377 		 */
378 		scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
379 
380 		/*
381 		 * schedule the next available interrupt transfer or
382 		 * the next stage of the interrupt transfer
383 		 */
384 		scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
385 
386 		/*
387 		 * schedule the next available control transfer
388 		 * or the next stage of the control transfer
389 		 */
390 		left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
391 				    FHCI_TF_CTRL);
392 	}
393 
394 	/*
395 	 * schedule the next available bulk transfer or the next stage of the
396 	 * bulk transfer
397 	 */
398 	if (left > 0)
399 		scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
400 }
401 
402 /* Handles SOF interrupt */
403 static void sof_interrupt(struct fhci_hcd *fhci)
404 {
405 	struct fhci_usb *usb = fhci->usb_lld;
406 
407 	if ((usb->port_status == FHCI_PORT_DISABLED) &&
408 	    (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
409 	    !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
410 		if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
411 			usb->port_status = FHCI_PORT_LOW;
412 		else
413 			usb->port_status = FHCI_PORT_FULL;
414 		/* Disable IDLE */
415 		usb->saved_msk &= ~USB_E_IDLE_MASK;
416 		out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
417 	}
418 
419 	gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
420 
421 	fhci_host_transmit_actual_frame(usb);
422 	usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
423 
424 	fhci_schedule_transactions(usb);
425 }
426 
427 /* Handles device disconnected interrupt on port */
428 void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
429 {
430 	struct fhci_usb *usb = fhci->usb_lld;
431 
432 	fhci_dbg(fhci, "-> %s\n", __func__);
433 
434 	fhci_usb_disable_interrupt(usb);
435 	clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
436 	usb->port_status = FHCI_PORT_DISABLED;
437 
438 	fhci_stop_sof_timer(fhci);
439 
440 	/* Enable IDLE since we want to know if something comes along */
441 	usb->saved_msk |= USB_E_IDLE_MASK;
442 	out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
443 
444 	usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
445 	usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
446 	usb->max_bytes_per_frame = 0;
447 	fhci_usb_enable_interrupt(usb);
448 
449 	fhci_dbg(fhci, "<- %s\n", __func__);
450 }
451 
452 /* detect a new device connected on the USB port */
453 void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
454 {
455 
456 	struct fhci_usb *usb = fhci->usb_lld;
457 	int state;
458 	int ret;
459 
460 	fhci_dbg(fhci, "-> %s\n", __func__);
461 
462 	fhci_usb_disable_interrupt(usb);
463 	state = fhci_ioports_check_bus_state(fhci);
464 
465 	/* low-speed device was connected to the USB port */
466 	if (state == 1) {
467 		ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
468 		if (ret) {
469 			fhci_warn(fhci, "Low-Speed device is not supported, "
470 				  "try use BRGx\n");
471 			goto out;
472 		}
473 
474 		usb->port_status = FHCI_PORT_LOW;
475 		setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
476 		usb->vroot_hub->port.wPortStatus |=
477 		    (USB_PORT_STAT_LOW_SPEED |
478 		     USB_PORT_STAT_CONNECTION);
479 		usb->vroot_hub->port.wPortChange |=
480 		    USB_PORT_STAT_C_CONNECTION;
481 		usb->max_bytes_per_frame =
482 		    (MAX_BYTES_PER_FRAME >> 3) - 7;
483 		fhci_port_enable(usb);
484 	} else if (state == 2) {
485 		ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
486 		if (ret) {
487 			fhci_warn(fhci, "Full-Speed device is not supported, "
488 				  "try use CLKx\n");
489 			goto out;
490 		}
491 
492 		usb->port_status = FHCI_PORT_FULL;
493 		clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
494 		usb->vroot_hub->port.wPortStatus &=
495 		    ~USB_PORT_STAT_LOW_SPEED;
496 		usb->vroot_hub->port.wPortStatus |=
497 		    USB_PORT_STAT_CONNECTION;
498 		usb->vroot_hub->port.wPortChange |=
499 		    USB_PORT_STAT_C_CONNECTION;
500 		usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
501 		fhci_port_enable(usb);
502 	}
503 out:
504 	fhci_usb_enable_interrupt(usb);
505 	fhci_dbg(fhci, "<- %s\n", __func__);
506 }
507 
508 irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
509 {
510 	struct usb_hcd *hcd = _hcd;
511 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
512 	struct fhci_usb *usb = fhci->usb_lld;
513 
514 	spin_lock(&fhci->lock);
515 
516 	gtm_set_exact_timer16(fhci->timer, 1000, false);
517 
518 	if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
519 		usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
520 		fhci_push_dummy_bd(usb->ep0);
521 	}
522 
523 	fhci_schedule_transactions(usb);
524 
525 	spin_unlock(&fhci->lock);
526 
527 	return IRQ_HANDLED;
528 }
529 
530 /* Cancel transmission on the USB endpoint */
531 static void abort_transmission(struct fhci_usb *usb)
532 {
533 	fhci_dbg(usb->fhci, "-> %s\n", __func__);
534 	/* issue stop Tx command */
535 	qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
536 	/* flush Tx FIFOs */
537 	out_8(&usb->fhci->regs->usb_uscom, USB_CMD_FLUSH_FIFO | EP_ZERO);
538 	udelay(1000);
539 	/* reset Tx BDs */
540 	fhci_flush_bds(usb);
541 	/* issue restart Tx command */
542 	qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
543 	fhci_dbg(usb->fhci, "<- %s\n", __func__);
544 }
545 
546 irqreturn_t fhci_irq(struct usb_hcd *hcd)
547 {
548 	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
549 	struct fhci_usb *usb;
550 	u16 usb_er = 0;
551 	unsigned long flags;
552 
553 	spin_lock_irqsave(&fhci->lock, flags);
554 
555 	usb = fhci->usb_lld;
556 
557 	usb_er |= in_be16(&usb->fhci->regs->usb_usber) &
558 		  in_be16(&usb->fhci->regs->usb_usbmr);
559 
560 	/* clear event bits for next time */
561 	out_be16(&usb->fhci->regs->usb_usber, usb_er);
562 
563 	fhci_dbg_isr(fhci, usb_er);
564 
565 	if (usb_er & USB_E_RESET_MASK) {
566 		if ((usb->port_status == FHCI_PORT_FULL) ||
567 				(usb->port_status == FHCI_PORT_LOW)) {
568 			fhci_device_disconnected_interrupt(fhci);
569 			usb_er &= ~USB_E_IDLE_MASK;
570 		} else if (usb->port_status == FHCI_PORT_WAITING) {
571 			usb->port_status = FHCI_PORT_DISCONNECTING;
572 
573 			/* Turn on IDLE since we want to disconnect */
574 			usb->saved_msk |= USB_E_IDLE_MASK;
575 			out_be16(&usb->fhci->regs->usb_usber,
576 				 usb->saved_msk);
577 		} else if (usb->port_status == FHCI_PORT_DISABLED) {
578 			if (fhci_ioports_check_bus_state(fhci) == 1)
579 				fhci_device_connected_interrupt(fhci);
580 		}
581 		usb_er &= ~USB_E_RESET_MASK;
582 	}
583 
584 	if (usb_er & USB_E_MSF_MASK) {
585 		abort_transmission(fhci->usb_lld);
586 		usb_er &= ~USB_E_MSF_MASK;
587 	}
588 
589 	if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
590 		sof_interrupt(fhci);
591 		usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
592 	}
593 
594 	if (usb_er & USB_E_TXB_MASK) {
595 		fhci_tx_conf_interrupt(fhci->usb_lld);
596 		usb_er &= ~USB_E_TXB_MASK;
597 	}
598 
599 	if (usb_er & USB_E_TXE1_MASK) {
600 		fhci_tx_conf_interrupt(fhci->usb_lld);
601 		usb_er &= ~USB_E_TXE1_MASK;
602 	}
603 
604 	if (usb_er & USB_E_IDLE_MASK) {
605 		if (usb->port_status == FHCI_PORT_DISABLED) {
606 			usb_er &= ~USB_E_RESET_MASK;
607 			fhci_device_connected_interrupt(fhci);
608 		} else if (usb->port_status ==
609 				FHCI_PORT_DISCONNECTING) {
610 			/* XXX usb->port_status = FHCI_PORT_WAITING; */
611 			/* Disable IDLE */
612 			usb->saved_msk &= ~USB_E_IDLE_MASK;
613 			out_be16(&usb->fhci->regs->usb_usbmr,
614 				 usb->saved_msk);
615 		} else {
616 			fhci_dbg_isr(fhci, -1);
617 		}
618 
619 		usb_er &= ~USB_E_IDLE_MASK;
620 	}
621 
622 	spin_unlock_irqrestore(&fhci->lock, flags);
623 
624 	return IRQ_HANDLED;
625 }
626 
627 
628 /*
629  * Process normal completions(error or success) and clean the schedule.
630  *
631  * This is the main path for handing urbs back to drivers. The only other patth
632  * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
633  * the (re-reversed) done list as this does.
634  */
635 static void process_done_list(unsigned long data)
636 {
637 	struct urb *urb;
638 	struct ed *ed;
639 	struct td *td;
640 	struct urb_priv *urb_priv;
641 	struct fhci_hcd *fhci = (struct fhci_hcd *)data;
642 
643 	disable_irq(fhci->timer->irq);
644 	disable_irq(fhci_to_hcd(fhci)->irq);
645 	spin_lock(&fhci->lock);
646 
647 	td = fhci_remove_td_from_done_list(fhci->hc_list);
648 	while (td != NULL) {
649 		urb = td->urb;
650 		urb_priv = urb->hcpriv;
651 		ed = td->ed;
652 
653 		/* update URB's length and status from TD */
654 		fhci_done_td(urb, td);
655 		urb_priv->tds_cnt++;
656 
657 		/*
658 		 * if all this urb's TDs are done, call complete()
659 		 * Interrupt transfers are the onley special case:
660 		 * they are reissued,until "deleted" by usb_unlink_urb
661 		 * (real work done in a SOF intr, by process_del_list)
662 		 */
663 		if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
664 			fhci_urb_complete_free(fhci, urb);
665 		} else if (urb_priv->state == URB_DEL &&
666 				ed->state == FHCI_ED_SKIP) {
667 			fhci_del_ed_list(fhci, ed);
668 			ed->state = FHCI_ED_OPER;
669 		} else if (ed->state == FHCI_ED_HALTED) {
670 			urb_priv->state = URB_DEL;
671 			ed->state = FHCI_ED_URB_DEL;
672 			fhci_del_ed_list(fhci, ed);
673 			ed->state = FHCI_ED_OPER;
674 		}
675 
676 		td = fhci_remove_td_from_done_list(fhci->hc_list);
677 	}
678 
679 	spin_unlock(&fhci->lock);
680 	enable_irq(fhci->timer->irq);
681 	enable_irq(fhci_to_hcd(fhci)->irq);
682 }
683 
684 DECLARE_TASKLET(fhci_tasklet, process_done_list, 0);
685 
686 /* transfer complted callback */
687 u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
688 {
689 	if (!fhci->process_done_task->state)
690 		tasklet_schedule(fhci->process_done_task);
691 	return 0;
692 }
693 
694 /*
695  * adds urb to the endpoint descriptor list
696  * arguments:
697  * fhci		data structure for the Low level host controller
698  * ep		USB Host endpoint data structure
699  * urb		USB request block data structure
700  */
701 void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
702 {
703 	struct ed *ed = urb->ep->hcpriv;
704 	struct urb_priv *urb_priv = urb->hcpriv;
705 	u32 data_len = urb->transfer_buffer_length;
706 	int urb_state = 0;
707 	int toggle = 0;
708 	struct td *td;
709 	u8 *data;
710 	u16 cnt = 0;
711 
712 	if (ed == NULL) {
713 		ed = fhci_get_empty_ed(fhci);
714 		ed->dev_addr = usb_pipedevice(urb->pipe);
715 		ed->ep_addr = usb_pipeendpoint(urb->pipe);
716 		switch (usb_pipetype(urb->pipe)) {
717 		case PIPE_CONTROL:
718 			ed->mode = FHCI_TF_CTRL;
719 			break;
720 		case PIPE_BULK:
721 			ed->mode = FHCI_TF_BULK;
722 			break;
723 		case PIPE_INTERRUPT:
724 			ed->mode = FHCI_TF_INTR;
725 			break;
726 		case PIPE_ISOCHRONOUS:
727 			ed->mode = FHCI_TF_ISO;
728 			break;
729 		default:
730 			break;
731 		}
732 		ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
733 			FHCI_LOW_SPEED : FHCI_FULL_SPEED;
734 		ed->max_pkt_size = usb_maxpacket(urb->dev,
735 			urb->pipe, usb_pipeout(urb->pipe));
736 		urb->ep->hcpriv = ed;
737 		fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
738 			 ed->speed, ed->max_pkt_size);
739 	}
740 
741 	/* for ISO transfer calculate start frame index */
742 	if (ed->mode == FHCI_TF_ISO && urb->transfer_flags & URB_ISO_ASAP)
743 		urb->start_frame = ed->td_head ? ed->last_iso + 1 :
744 						 get_frame_num(fhci);
745 
746 	/*
747 	 * OHCI handles the DATA toggle itself,we just use the USB
748 	 * toggle bits
749 	 */
750 	if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
751 			  usb_pipeout(urb->pipe)))
752 		toggle = USB_TD_TOGGLE_CARRY;
753 	else {
754 		toggle = USB_TD_TOGGLE_DATA0;
755 		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
756 			      usb_pipeout(urb->pipe), 1);
757 	}
758 
759 	urb_priv->tds_cnt = 0;
760 	urb_priv->ed = ed;
761 	if (data_len > 0)
762 		data = urb->transfer_buffer;
763 	else
764 		data = NULL;
765 
766 	switch (ed->mode) {
767 	case FHCI_TF_BULK:
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 :
777 							 FHCI_TA_IN,
778 				cnt ? USB_TD_TOGGLE_CARRY :
779 				      toggle,
780 				data, 4096, 0, 0, true);
781 			data += 4096;
782 			data_len -= 4096;
783 			cnt++;
784 		}
785 
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);
790 		cnt++;
791 
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 :
796 							 FHCI_TA_IN,
797 				USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
798 			cnt++;
799 		}
800 		break;
801 	case FHCI_TF_INTR:
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);
807 		break;
808 	case FHCI_TF_CTRL:
809 		ed->dev_addr = usb_pipedevice(urb->pipe);
810 		ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
811 			usb_pipeout(urb->pipe));
812 		/* setup stage */
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);
815 
816 		/* data stage */
817 		if (data_len > 0) {
818 			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
819 				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
820 							 FHCI_TA_IN,
821 				USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
822 				true);
823 		}
824 
825 		/* status stage */
826 		if (data_len > 0)
827 			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
828 				(usb_pipeout(urb->pipe) ? FHCI_TA_IN :
829 							  FHCI_TA_OUT),
830 				USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
831 		else
832 			 td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
833 				FHCI_TA_IN,
834 				USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
835 
836 		urb_state = US_CTRL_SETUP;
837 		break;
838 	case FHCI_TF_ISO:
839 		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
840 			u16 frame = urb->start_frame;
841 
842 			/*
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)
846 			 */
847 			frame += cnt * urb->interval;
848 			frame &= 0x07ff;
849 			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
850 				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
851 							 FHCI_TA_IN,
852 				USB_TD_TOGGLE_DATA0,
853 				data + urb->iso_frame_desc[cnt].offset,
854 				urb->iso_frame_desc[cnt].length,
855 				urb->interval, frame, true);
856 		}
857 		break;
858 	default:
859 		break;
860 	}
861 
862 	/*
863 	 * set the state of URB
864 	 * control pipe:3 states -- setup,data,status
865 	 * interrupt and bulk pipe:1 state -- data
866 	 */
867 	urb->pipe &= ~0x1f;
868 	urb->pipe |= urb_state & 0x1f;
869 
870 	urb_priv->state = URB_INPROGRESS;
871 
872 	if (!ed->td_head) {
873 		ed->state = FHCI_ED_OPER;
874 		switch (ed->mode) {
875 		case FHCI_TF_CTRL:
876 			list_add(&ed->node, &fhci->hc_list->ctrl_list);
877 			break;
878 		case FHCI_TF_BULK:
879 			list_add(&ed->node, &fhci->hc_list->bulk_list);
880 			break;
881 		case FHCI_TF_INTR:
882 			list_add(&ed->node, &fhci->hc_list->intr_list);
883 			break;
884 		case FHCI_TF_ISO:
885 			list_add(&ed->node, &fhci->hc_list->iso_list);
886 			break;
887 		default:
888 			break;
889 		}
890 	}
891 
892 	fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
893 	fhci->active_urbs++;
894 }
895