aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/host/fhci-sched.c
blob: 00a29855d0c41f777760e7efc16c7dde83746b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*
 * Freescale QUICC Engine USB Host Controller Driver
 *
 * Copyright (c) Freescale Semicondutor, Inc. 2006.
 *               Shlomi Gridish <gridish@freescale.com>
 *               Jerry Huang <Chang-Ming.Huang@freescale.com>
 * Copyright (c) Logic Product Development, Inc. 2007
 *               Peter Barada <peterb@logicpd.com>
 * Copyright (c) MontaVista Software, Inc. 2008.
 *               Anton Vorontsov <avorontsov@ru.mvista.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/usb.h>
#include <asm/qe.h>
#include <asm/fsl_gtm.h>
#include "../core/hcd.h"
#include "fhci.h"

static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
{
	pkt->data = NULL;
	pkt->len = 0;
	pkt->status = USB_TD_OK;
	pkt->info = 0;
	pkt->priv_data = NULL;

	cq_put(usb->ep0->empty_frame_Q, pkt);
}

/* confirm submitted packet */
void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
{
	struct td *td;
	struct packet *td_pkt;
	struct ed *ed;
	u32 trans_len;
	bool td_done = false;

	td = fhci_remove_td_from_frame(usb->actual_frame);
	td_pkt = td->pkt;
	trans_len = pkt->len;
	td->status = pkt->status;
	if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
		if ((td->data + td->actual_len) && trans_len)
			memcpy(td->data + td->actual_len, pkt->data,
			       trans_len);
		cq_put(usb->ep0->dummy_packets_Q, pkt->data);
	}

	recycle_frame(usb, pkt);

	ed = td->ed;
	if (ed->mode == FHCI_TF_ISO) {
		if (ed->td_list.next->next != &ed->td_list) {
			struct td *td_next =
			    list_entry(ed->td_list.next->next, struct td,
				       node);

			td_next->start_frame = usb->actual_frame->frame_num;
		}
		td->actual_len = trans_len;
		td_done = true;
	} else if ((td->status & USB_TD_ERROR) &&
			!(td->status & USB_TD_TX_ER_NAK)) {
		/*
		 * There was an error on the transaction (but not NAK).
		 * If it is fatal error (data underrun, stall, bad pid or 3
		 * errors exceeded), mark this TD as done.
		 */
		if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
				(td->status & USB_TD_TX_ER_STALL) ||
				(td->status & USB_TD_RX_ER_PID) ||
				(++td->error_cnt >= 3)) {
			ed->state = FHCI_ED_HALTED;
			td_done = true;

			if (td->status & USB_TD_RX_DATA_UNDERUN) {
				fhci_dbg(usb->fhci, "td err fu\n");
				td->toggle = !td->toggle;
				td->actual_len += trans_len;
			} else {
				fhci_dbg(usb->fhci, "td err f!u\n");
			}
		} else {
			fhci_dbg(usb->fhci, "td err !f\n");
			/* it is not a fatal error -retry this transaction */
			td->nak_cnt = 0;
			td->error_cnt++;
			td->status = USB_TD_OK;
		}
	} else if (td->status & USB_TD_TX_ER_NAK) {
		/* there was a NAK response */
		fhci_vdbg(usb->fhci, "td nack\n");
		td->nak_cnt++;
		td->error_cnt = 0;
		td->status = USB_TD_OK;
	} else {
		/* there was no error on transaction */
		td->error_cnt = 0;
		td->nak_cnt = 0;
		td->toggle = !td->toggle;
		td->actual_len += trans_len;

		if (td->len == td->actual_len)
			td_done = true;
	}

	if (td_done)
		fhci_move_td_from_ed_to_done_list(usb, ed);
}

/*
 * Flush all transmitted packets from BDs
 * This routine is called when disabling the USB port to flush all
 * transmissions that are allready scheduled in the BDs
 */
void fhci_flush_all_transmissions(struct fhci_usb *usb)
{
	u8 mode;
	struct td *td;

	mode = in_8(&usb->fhci->regs->usb_mod);
	clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);

	fhci_flush_bds(usb);

	while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
		struct packet *pkt = td->pkt;

		pkt->status = USB_TD_TX_ER_TIMEOUT;
		fhci_transaction_confirm(usb, pkt);
	}

	usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;

	/* reset the event register */
	out_be16(&usb->fhci->regs->usb_event, 0xffff);
	/* enable the USB controller */
	out_8(&usb->fhci->regs->usb_mod, mode | USB_MODE_EN);
}

/*
 * This function forms the packet and transmit the packet. This function
 * will handle all endpoint type:ISO,interrupt,control and bulk
 */
static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
{
	u32 fw_transaction_time, len = 0;
	struct packet *pkt;
	u8 *data = NULL;

	/* calcalate data address,len and toggle and then add the transaction */
	if (td->toggle == USB_TD_TOGGLE_CARRY)
		td->toggle = ed->toggle_carry;

	switch (ed->mode) {
	case FHCI_TF_ISO:
		len = td->len;
		if (td->type != FHCI_TA_IN)
			data = td->data;
		break;
	case FHCI_TF_CTRL:
	case FHCI_TF_BULK:
		len = min(td->len - td->actual_len, ed->max_pkt_size);
		if (!((td->type == FHCI_TA_IN) &&
		      ((len + td->actual_len) == td->len)))
			data = td->data + td->actual_len;
		break;
	case FHCI_TF_INTR:
		len = min(td->len, ed->max_pkt_size);
		if (!((td->type == FHCI_TA_IN) &&
		      ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
			data = td->data;
		break;
	default:
		break;
	}

	if (usb->port_status == FHCI_PORT_FULL)
		fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
	else
		fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);

	/* check if there's enough space in this frame to submit this TD */
	if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
			usb->max_bytes_per_frame) {
		fhci_vdbg(usb->fhci, "not enough space in this frame: "
			  "%d %d %d\n", usb->actual_frame->total_bytes, len,
			  usb->max_bytes_per_frame);
		return -1;
	}

	/* check if there's enough time in this frame to submit this TD */
	if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
	    (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
	     (fw_transaction_time + usb->sw_transaction_time >=
	      1000 - fhci_get_sof_timer_count(usb)))) {
		fhci_dbg(usb->fhci, "not enough time in this frame\n");
		return -1;
	}

	/* update frame object fields before transmitting */
	pkt = cq_get(usb->ep0->empty_frame_Q);
	if (!pkt) {
		fhci_dbg(usb->fhci, "there is no empty frame\n");
		return -1;
	}
	td->pkt = pkt;

	pkt->info = 0;
	if (data == NULL) {
		data = cq_get(usb->ep0->dummy_packets_Q);
		BUG_ON(!data);
		pkt->info = PKT_DUMMY_PACKET;
	}
	pkt->data = data;
	pkt->len = len;
	pkt->status = USB_TD_OK;
	/* update TD status field before transmitting */
	td->status = USB_TD_INPROGRESS;
	/* update actual frame time object with the actual transmission */
	usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
	fhci_add_td_to_frame(usb->actual_frame, td);

	if (usb->port_status != FHCI_PORT_FULL &&
			usb->port_status != FHCI_PORT_LOW) {
		pkt->status = USB_TD_TX_ER_TIMEOUT;
		pkt->len = 0;
		fhci_transaction_confirm(usb, pkt);
	} else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
			ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
		/* remove TD from actual frame */
		list_del_init(&td->frame_lh);
		td->status = USB_TD_OK;
		if (pkt->info & PKT_DUMMY_PACKET)
			cq_put(usb->ep0->dummy_packets_Q, pkt->data);
		recycle_frame(usb, pkt);
		usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
		fhci_err(usb->fhci, "host transaction failed\n");
		return -1;
	}

	return len;
}

static void move_head_to_tail(struct list_head *list)
{
	struct list_head *node = list->next;

	if (!list_empty(list)) {
		list_del(node);
		list_add_tail(node, list);
	}
}

/*
 * This function goes through the endpoint list and schedules the
 * transactions within this list
 */
static int scan_ed_list(struct fhci_usb *usb,
			struct list_head *list, enum fhci_tf_mode list_type)
{
	static const int frame_part[4] = {
		[FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
		[FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
				 MAX_PERIODIC_FRAME_USAGE) / 100,
		[FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
		[FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
				  MAX_PERIODIC_FRAME_USAGE) / 100
	};
	struct ed *ed;
	struct td *td;
	int ans = 1;
	u32 save_transaction_time = usb->sw_transaction_time;

	list_for_each_entry(ed, list, node) {
		td = ed->td_head;

		if (!td || (td && td->status == USB_TD_INPROGRESS))
			continue;

		if (ed->state != FHCI_ED_OPER) {
			if (ed->state == FHCI_ED_URB_DEL) {
				td->status = USB_TD_OK;
				fhci_move_td_from_ed_to_done_list(usb, ed);
				ed->state = FHCI_ED_SKIP;
			}
			continue;
		}

		/*
		 * if it isn't interrupt pipe or it is not iso pipe and the
		 * interval time passed
		 */
		if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
				(((usb->actual_frame->frame_num -
				   td->start_frame) & 0x7ff) < td->interval))
			continue;

		if (add_packet(usb, ed, td) < 0)
			continue;

		/* update time stamps in the TD */
		td->start_frame = usb->actual_frame->frame_num;
		usb->sw_transaction_time += save_transaction_time;

		if (usb->actual_frame->total_bytes >=
					usb->max_bytes_per_frame) {
			usb->actual_frame->frame_status =
				FRAME_DATA_END_TRANSMISSION;
			fhci_push_dummy_bd(usb->ep0);
			ans = 0;
			break;
		}

		if (usb->actual_frame->total_bytes >= frame_part[list_type])
			break;
	}

	/* be fair to each ED(move list head around) */
	move_head_to_tail(list);
	usb->sw_transaction_time = save_transaction_time;

	return ans;
}

static u32 rotate_frames(struct fhci_usb *usb)
{
	struct fhci_hcd *fhci = usb->fhci;

	if (!list_empty(&usb->actual_frame->tds_list)) {
		if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
		      usb->actual_frame->frame_num) & 0x7ff) > 5)
			fhci_flush_actual_frame(usb);
		else
			return -EINVAL;
	}

	usb->actual_frame->frame_status = FRAME_IS_PREPARED;
	usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
	usb->actual_frame->total_bytes = 0;

	return 0;
}

/*
 * This function schedule the USB transaction and will process the
 * endpoint in the following order: iso, interrupt, control and bulk.
 */
void fhci_schedule_transactions(struct fhci_usb *usb)
{
	int left = 1;

	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
		if (rotate_frames(usb) != 0)
			return;

	if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
		return;

	if (usb->actual_frame->total_bytes == 0) {
		/*
		 * schedule the next available ISO transfer
		 *or next stage of the ISO transfer
		 */
		scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);

		/*
		 * schedule the next available interrupt transfer or
		 * the next stage of the interrupt transfer
		 */
		scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);

		/*
		 * schedule the next available control transfer
		 * or the next stage of the control transfer
		 */
		left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
				    FHCI_TF_CTRL);
	}

	/*
	 * schedule the next available bulk transfer or the next stage of the
	 * bulk transfer
	 */
	if (left > 0)
		scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
}

/* Handles SOF interrupt */
static void sof_interrupt(struct fhci_hcd *fhci)
{
	struct fhci_usb *usb = fhci->usb_lld;

	if ((usb->port_status == FHCI_PORT_DISABLED) &&
	    (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
	    !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
		if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
			usb->port_status = FHCI_PORT_LOW;
		else
			usb->port_status = FHCI_PORT_FULL;
		/* Disable IDLE */
		usb->saved_msk &= ~USB_E_IDLE_MASK;
		out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
	}

	gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);

	fhci_host_transmit_actual_frame(usb);
	usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;

	fhci_schedule_transactions(usb);
}

/* Handles device disconnected interrupt on port */
void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
{
	struct fhci_usb *usb = fhci->usb_lld;

	fhci_dbg(fhci, "-> %s\n", __func__);

	fhci_usb_disable_interrupt(usb);
	clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
	usb->port_status = FHCI_PORT_DISABLED;

	fhci_stop_sof_timer(fhci);

	/* Enable IDLE since we want to know if something comes along */
	usb->saved_msk |= USB_E_IDLE_MASK;
	out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);

	usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
	usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
	usb->max_bytes_per_frame = 0;
	fhci_usb_enable_interrupt(usb);

	fhci_dbg(fhci, "<- %s\n", __func__);
}

/* detect a new device connected on the USB port */
void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
{

	struct fhci_usb *usb = fhci->usb_lld;
	int state;
	int ret;

	fhci_dbg(fhci, "-> %s\n", __func__);

	fhci_usb_disable_interrupt(usb);
	state = fhci_ioports_check_bus_state(fhci);

	/* low-speed device was connected to the USB port */
	if (state == 1) {
		ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
		if (ret) {
			fhci_warn(fhci, "Low-Speed device is not supported, "
				  "try use BRGx\n");
			goto out;
		}

		usb->port_status = FHCI_PORT_LOW;
		setbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
		usb->vroot_hub->port.wPortStatus |=
		    (USB_PORT_STAT_LOW_SPEED |
		     USB_PORT_STAT_CONNECTION);
		usb->vroot_hub->port.wPortChange |=
		    USB_PORT_STAT_C_CONNECTION;
		usb->max_bytes_per_frame =
		    (MAX_BYTES_PER_FRAME >> 3) - 7;
		fhci_port_enable(usb);
	} else if (state == 2) {
		ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
		if (ret) {
			fhci_warn(fhci, "Full-Speed device is not supported, "
				  "try use CLKx\n");
			goto out;
		}

		usb->port_status = FHCI_PORT_FULL;
		clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
		usb->vroot_hub->port.wPortStatus &=
		    ~USB_PORT_STAT_LOW_SPEED;
		usb->vroot_hub->port.wPortStatus |=
		    USB_PORT_STAT_CONNECTION;
		usb->vroot_hub->port.wPortChange |=
		    USB_PORT_STAT_C_CONNECTION;
		usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
		fhci_port_enable(usb);
	}
out:
	fhci_usb_enable_interrupt(usb);
	fhci_dbg(fhci, "<- %s\n", __func__);
}

irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
{
	struct usb_hcd *hcd = _hcd;
	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
	struct fhci_usb *usb = fhci->usb_lld;

	spin_lock(&fhci->lock);

	gtm_set_exact_timer16(fhci->timer, 1000, false);

	if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
		usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
		fhci_push_dummy_bd(usb->ep0);
	}

	fhci_schedule_transactions(usb);

	spin_unlock(&fhci->lock);

	return IRQ_HANDLED;
}

/* Cancel transmission on the USB endpoint */
static void abort_transmission(struct fhci_usb *usb)
{
	fhci_dbg(usb->fhci, "-> %s\n", __func__);
	/* issue stop Tx command */
	qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
	/* flush Tx FIFOs */
	out_8(&usb->fhci->regs->usb_comm, USB_CMD_FLUSH_FIFO | EP_ZERO);
	udelay(1000);
	/* reset Tx BDs */
	fhci_flush_bds(usb);
	/* issue restart Tx command */
	qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
	fhci_dbg(usb->fhci, "<- %s\n", __func__);
}

irqreturn_t fhci_irq(struct usb_hcd *hcd)
{
	struct fhci_hcd *fhci = hcd_to_fhci(hcd);
	struct fhci_usb *usb;
	u16 usb_er = 0;
	unsigned long flags;

	spin_lock_irqsave(&fhci->lock, flags);

	usb = fhci->usb_lld;

	usb_er |= in_be16(&usb->fhci->regs->usb_event) &
		  in_be16(&usb->fhci->regs->usb_mask);

	/* clear event bits for next time */
	out_be16(&usb->fhci->regs->usb_event, usb_er);

	fhci_dbg_isr(fhci, usb_er);

	if (usb_er & USB_E_RESET_MASK) {
		if ((usb->port_status == FHCI_PORT_FULL) ||
				(usb->port_status == FHCI_PORT_LOW)) {
			fhci_device_disconnected_interrupt(fhci);
			usb_er &= ~USB_E_IDLE_MASK;
		} else if (usb->port_status == FHCI_PORT_WAITING) {
			usb->port_status = FHCI_PORT_DISCONNECTING;

			/* Turn on IDLE since we want to disconnect */
			usb->saved_msk |= USB_E_IDLE_MASK;
			out_be16(&usb->fhci->regs->usb_event,
				 usb->saved_msk);
		} else if (usb->port_status == FHCI_PORT_DISABLED) {
			if (fhci_ioports_check_bus_state(fhci) == 1)
				fhci_device_connected_interrupt(fhci);
		}
		usb_er &= ~USB_E_RESET_MASK;
	}

	if (usb_er & USB_E_MSF_MASK) {
		abort_transmission(fhci->usb_lld);
		usb_er &= ~USB_E_MSF_MASK;
	}

	if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
		sof_interrupt(fhci);
		usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
	}

	if (usb_er & USB_E_TXB_MASK) {
		fhci_tx_conf_interrupt(fhci->usb_lld);
		usb_er &= ~USB_E_TXB_MASK;
	}

	if (usb_er & USB_E_TXE1_MASK) {
		fhci_tx_conf_interrupt(fhci->usb_lld);
		usb_er &= ~USB_E_TXE1_MASK;
	}

	if (usb_er & USB_E_IDLE_MASK) {
		if (usb->port_status == FHCI_PORT_DISABLED) {
			usb_er &= ~USB_E_RESET_MASK;
			fhci_device_connected_interrupt(fhci);
		} else if (usb->port_status ==
				FHCI_PORT_DISCONNECTING) {
			/* XXX usb->port_status = FHCI_PORT_WAITING; */
			/* Disable IDLE */
			usb->saved_msk &= ~USB_E_IDLE_MASK;
			out_be16(&usb->fhci->regs->usb_mask,
				 usb->saved_msk);
		} else {
			fhci_dbg_isr(fhci, -1);
		}

		usb_er &= ~USB_E_IDLE_MASK;
	}

	spin_unlock_irqrestore(&fhci->lock, flags);

	return IRQ_HANDLED;
}


/*
 * Process normal completions(error or success) and clean the schedule.
 *
 * This is the main path for handing urbs back to drivers. The only other patth
 * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
 * the (re-reversed) done list as this does.
 */
static void process_done_list(unsigned long data)
{
	struct urb *urb;
	struct ed *ed;
	struct td *td;
	struct urb_priv *urb_priv;
	struct fhci_hcd *fhci = (struct fhci_hcd *)data;

	disable_irq(fhci->timer->irq);
	disable_irq(fhci_to_hcd(fhci)->irq);
	spin_lock(&fhci->lock);

	td = fhci_remove_td_from_done_list(fhci->hc_list);
	while (td != NULL) {
		urb = td->urb;
		urb_priv = urb->hcpriv;
		ed = td->ed;

		/* update URB's length and status from TD */
		fhci_done_td(urb, td);
		urb_priv->tds_cnt++;

		/*
		 * if all this urb's TDs are done, call complete()
		 * Interrupt transfers are the onley special case:
		 * they are reissued,until "deleted" by usb_unlink_urb
		 * (real work done in a SOF intr, by process_del_list)
		 */
		if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
			fhci_urb_complete_free(fhci, urb);
		} else if (urb_priv->state == URB_DEL &&
				ed->state == FHCI_ED_SKIP) {
			fhci_del_ed_list(fhci, ed);
			ed->state = FHCI_ED_OPER;
		} else if (ed->state == FHCI_ED_HALTED) {
			urb_priv->state = URB_DEL;
			ed->state = FHCI_ED_URB_DEL;
			fhci_del_ed_list(fhci, ed);
			ed->state = FHCI_ED_OPER;
		}

		td = fhci_remove_td_from_done_list(fhci->hc_list);
	}

	spin_unlock(&fhci->lock);
	enable_irq(fhci->timer->irq);
	enable_irq(fhci_to_hcd(fhci)->irq);
}

DECLARE_TASKLET(fhci_tasklet, process_done_list, 0);

/* transfer complted callback */
u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
{
	if (!fhci->process_done_task->state)
		tasklet_schedule(fhci->process_done_task);
	return 0;
}

/*
 * adds urb to the endpoint descriptor list
 * arguments:
 * fhci		data structure for the Low level host controller
 * ep		USB Host endpoint data structure
 * urb		USB request block data structure
 */
void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
{
	struct ed *ed = urb->ep->hcpriv;
	struct urb_priv *urb_priv = urb->hcpriv;
	u32 data_len = urb->transfer_buffer_length;
	int urb_state = 0;
	int toggle = 0;
	struct td *td;
	u8 *data;
	u16 cnt = 0;

	if (ed == NULL) {
		ed = fhci_get_empty_ed(fhci);
		ed->dev_addr = usb_pipedevice(urb->pipe);
		ed->ep_addr = usb_pipeendpoint(urb->pipe);
		switch (usb_pipetype(urb->pipe)) {
		case PIPE_CONTROL:
			ed->mode = FHCI_TF_CTRL;
			break;
		case PIPE_BULK:
			ed->mode = FHCI_TF_BULK;
			break;
		case PIPE_INTERRUPT:
			ed->mode = FHCI_TF_INTR;
			break;
		case PIPE_ISOCHRONOUS:
			ed->mode = FHCI_TF_ISO;
			break;
		default:
			break;
		}
		ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
			FHCI_LOW_SPEED : FHCI_FULL_SPEED;
		ed->max_pkt_size = usb_maxpacket(urb->dev,
			urb->pipe, usb_pipeout(urb->pipe));
		urb->ep->hcpriv = ed;
		fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
			 ed->speed, ed->max_pkt_size);
	}

	/* for ISO transfer calculate start frame index */
	if (ed->mode == FHCI_TF_ISO && urb->transfer_flags & URB_ISO_ASAP)
		urb->start_frame = ed->td_head ? ed->last_iso + 1 :
						 get_frame_num(fhci);

	/*
	 * OHCI handles the DATA toggle itself,we just use the USB
	 * toggle bits
	 */
	if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
			  usb_pipeout(urb->pipe)))
		toggle = USB_TD_TOGGLE_CARRY;
	else {
		toggle = USB_TD_TOGGLE_DATA0;
		usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
			      usb_pipeout(urb->pipe), 1);
	}

	urb_priv->tds_cnt = 0;
	urb_priv->ed = ed;
	if (data_len > 0)
		data = urb->transfer_buffer;
	else
		data = NULL;

	switch (ed->mode) {
	case FHCI_TF_BULK:
		if (urb->transfer_flags & URB_ZERO_PACKET &&
				urb->transfer_buffer_length > 0 &&
				((urb->transfer_buffer_length %
				usb_maxpacket(urb->dev, urb->pipe,
				usb_pipeout(urb->pipe))) == 0))
			urb_state = US_BULK0;
		while (data_len > 4096) {
			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
							 FHCI_TA_IN,
				cnt ? USB_TD_TOGGLE_CARRY :
				      toggle,
				data, 4096, 0, 0, true);
			data += 4096;
			data_len -= 4096;
			cnt++;
		}

		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
			cnt ? USB_TD_TOGGLE_CARRY : toggle,
			data, data_len, 0, 0, true);
		cnt++;

		if (urb->transfer_flags & URB_ZERO_PACKET &&
				cnt < urb_priv->num_of_tds) {
			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
							 FHCI_TA_IN,
				USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
			cnt++;
		}
		break;
	case FHCI_TF_INTR:
		urb->start_frame = get_frame_num(fhci) + 1;
		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
			usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
			USB_TD_TOGGLE_DATA0, data, data_len,
			urb->interval, urb->start_frame, true);
		break;
	case FHCI_TF_CTRL:
		ed->dev_addr = usb_pipedevice(urb->pipe);
		ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
			usb_pipeout(urb->pipe));
		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
			USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);

		if (data_len > 0) {
			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
							 FHCI_TA_IN,
				USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
				true);
		}
		td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
			usb_pipeout(urb->pipe) ? FHCI_TA_IN : FHCI_TA_OUT,
			USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
		urb_state = US_CTRL_SETUP;
		break;
	case FHCI_TF_ISO:
		for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
			u16 frame = urb->start_frame;

			/*
			 * FIXME scheduling should handle frame counter
			 * roll-around ... exotic case (and OHCI has
			 * a 2^16 iso range, vs other HCs max of 2^10)
			 */
			frame += cnt * urb->interval;
			frame &= 0x07ff;
			td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
				usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
							 FHCI_TA_IN,
				USB_TD_TOGGLE_DATA0,
				data + urb->iso_frame_desc[cnt].offset,
				urb->iso_frame_desc[cnt].length,
				urb->interval, frame, true);
		}
		break;
	default:
		break;
	}

	/*
	 * set the state of URB
	 * control pipe:3 states -- setup,data,status
	 * interrupt and bulk pipe:1 state -- data
	 */
	urb->pipe &= ~0x1f;
	urb->pipe |= urb_state & 0x1f;

	urb_priv->state = URB_INPROGRESS;

	if (!ed->td_head) {
		ed->state = FHCI_ED_OPER;
		switch (ed->mode) {
		case FHCI_TF_CTRL:
			list_add(&ed->node, &fhci->hc_list->ctrl_list);
			break;
		case FHCI_TF_BULK:
			list_add(&ed->node, &fhci->hc_list->bulk_list);
			break;
		case FHCI_TF_INTR:
			list_add(&ed->node, &fhci->hc_list->intr_list);
			break;
		case FHCI_TF_ISO:
			list_add(&ed->node, &fhci->hc_list->iso_list);
			break;
		default:
			break;
		}
	}

	fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
	fhci->active_urbs++;
}