/
isp1362-hcd.c
2882 lines (2500 loc) · 87.2 KB
/
isp1362-hcd.c
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ISP1362 HCD (Host Controller Driver) for USB.
*
* Copyright (C) 2005 Lothar Wassmann <LW@KARO-electronics.de>
*
* Derived from the SL811 HCD, rewritten for ISP116x.
* Copyright (C) 2005 Olav Kongas <ok@artecdesign.ee>
*
* Portions:
* Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
* Copyright (C) 2004 David Brownell
*/
/*
* The ISP1362 chip requires a large delay (300ns and 462ns) between
* accesses to the address and data register.
* The following timing options exist:
*
* 1. Configure your memory controller to add such delays if it can (the best)
* 2. Implement platform-specific delay function possibly
* combined with configuring the memory controller; see
* include/linux/usb_isp1362.h for more info.
* 3. Use ndelay (easiest, poorest).
*
* Use the corresponding macros USE_PLATFORM_DELAY and USE_NDELAY in the
* platform specific section of isp1362.h to select the appropriate variant.
*
* Also note that according to the Philips "ISP1362 Errata" document
* Rev 1.00 from 27 May data corruption may occur when the #WR signal
* is reasserted (even with #CS deasserted) within 132ns after a
* write cycle to any controller register. If the hardware doesn't
* implement the recommended fix (gating the #WR with #CS) software
* must ensure that no further write cycle (not necessarily to the chip!)
* is issued by the CPU within this interval.
* For PXA25x this can be ensured by using VLIO with the maximum
* recovery time (MSCx = 0x7f8c) with a memory clock of 99.53 MHz.
*/
#ifdef CONFIG_USB_DEBUG
# define ISP1362_DEBUG
#else
# undef ISP1362_DEBUG
#endif
/*
* The PXA255 UDC apparently doesn't handle GET_STATUS, GET_CONFIG and
* GET_INTERFACE requests correctly when the SETUP and DATA stages of the
* requests are carried out in separate frames. This will delay any SETUP
* packets until the start of the next frame so that this situation is
* unlikely to occur (and makes usbtest happy running with a PXA255 target
* device).
*/
#undef BUGGY_PXA2XX_UDC_USBTEST
#undef PTD_TRACE
#undef URB_TRACE
#undef VERBOSE
#undef REGISTERS
/* This enables a memory test on the ISP1362 chip memory to make sure the
* chip access timing is correct.
*/
#undef CHIP_BUFFER_TEST
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/usb.h>
#include <linux/usb/isp1362.h>
#include <linux/usb/hcd.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/io.h>
#include <linux/bitmap.h>
#include <linux/prefetch.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/byteorder.h>
#include <asm/unaligned.h>
static int dbg_level;
#ifdef ISP1362_DEBUG
module_param(dbg_level, int, 0644);
#else
module_param(dbg_level, int, 0);
#define STUB_DEBUG_FILE
#endif
#include "../core/usb.h"
#include "isp1362.h"
#define DRIVER_VERSION "2005-04-04"
#define DRIVER_DESC "ISP1362 USB Host Controller Driver"
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
static const char hcd_name[] = "isp1362-hcd";
static void isp1362_hc_stop(struct usb_hcd *hcd);
static int isp1362_hc_start(struct usb_hcd *hcd);
/*-------------------------------------------------------------------------*/
/*
* When called from the interrupthandler only isp1362_hcd->irqenb is modified,
* since the interrupt handler will write isp1362_hcd->irqenb to HCuPINT upon
* completion.
* We don't need a 'disable' counterpart, since interrupts will be disabled
* only by the interrupt handler.
*/
static inline void isp1362_enable_int(struct isp1362_hcd *isp1362_hcd, u16 mask)
{
if ((isp1362_hcd->irqenb | mask) == isp1362_hcd->irqenb)
return;
if (mask & ~isp1362_hcd->irqenb)
isp1362_write_reg16(isp1362_hcd, HCuPINT, mask & ~isp1362_hcd->irqenb);
isp1362_hcd->irqenb |= mask;
if (isp1362_hcd->irq_active)
return;
isp1362_write_reg16(isp1362_hcd, HCuPINTENB, isp1362_hcd->irqenb);
}
/*-------------------------------------------------------------------------*/
static inline struct isp1362_ep_queue *get_ptd_queue(struct isp1362_hcd *isp1362_hcd,
u16 offset)
{
struct isp1362_ep_queue *epq = NULL;
if (offset < isp1362_hcd->istl_queue[1].buf_start)
epq = &isp1362_hcd->istl_queue[0];
else if (offset < isp1362_hcd->intl_queue.buf_start)
epq = &isp1362_hcd->istl_queue[1];
else if (offset < isp1362_hcd->atl_queue.buf_start)
epq = &isp1362_hcd->intl_queue;
else if (offset < isp1362_hcd->atl_queue.buf_start +
isp1362_hcd->atl_queue.buf_size)
epq = &isp1362_hcd->atl_queue;
if (epq)
DBG(1, "%s: PTD $%04x is on %s queue\n", __func__, offset, epq->name);
else
pr_warning("%s: invalid PTD $%04x\n", __func__, offset);
return epq;
}
static inline int get_ptd_offset(struct isp1362_ep_queue *epq, u8 index)
{
int offset;
if (index * epq->blk_size > epq->buf_size) {
pr_warning("%s: Bad %s index %d(%d)\n", __func__, epq->name, index,
epq->buf_size / epq->blk_size);
return -EINVAL;
}
offset = epq->buf_start + index * epq->blk_size;
DBG(3, "%s: %s PTD[%02x] # %04x\n", __func__, epq->name, index, offset);
return offset;
}
/*-------------------------------------------------------------------------*/
static inline u16 max_transfer_size(struct isp1362_ep_queue *epq, size_t size,
int mps)
{
u16 xfer_size = min_t(size_t, MAX_XFER_SIZE, size);
xfer_size = min_t(size_t, xfer_size, epq->buf_avail * epq->blk_size - PTD_HEADER_SIZE);
if (xfer_size < size && xfer_size % mps)
xfer_size -= xfer_size % mps;
return xfer_size;
}
static int claim_ptd_buffers(struct isp1362_ep_queue *epq,
struct isp1362_ep *ep, u16 len)
{
int ptd_offset = -EINVAL;
int num_ptds = ((len + PTD_HEADER_SIZE - 1) / epq->blk_size) + 1;
int found;
BUG_ON(len > epq->buf_size);
if (!epq->buf_avail)
return -ENOMEM;
if (ep->num_ptds)
pr_err("%s: %s len %d/%d num_ptds %d buf_map %08lx skip_map %08lx\n", __func__,
epq->name, len, epq->blk_size, num_ptds, epq->buf_map, epq->skip_map);
BUG_ON(ep->num_ptds != 0);
found = bitmap_find_next_zero_area(&epq->buf_map, epq->buf_count, 0,
num_ptds, 0);
if (found >= epq->buf_count)
return -EOVERFLOW;
DBG(1, "%s: Found %d PTDs[%d] for %d/%d byte\n", __func__,
num_ptds, found, len, (int)(epq->blk_size - PTD_HEADER_SIZE));
ptd_offset = get_ptd_offset(epq, found);
WARN_ON(ptd_offset < 0);
ep->ptd_offset = ptd_offset;
ep->num_ptds += num_ptds;
epq->buf_avail -= num_ptds;
BUG_ON(epq->buf_avail > epq->buf_count);
ep->ptd_index = found;
bitmap_set(&epq->buf_map, found, num_ptds);
DBG(1, "%s: Done %s PTD[%d] $%04x, avail %d count %d claimed %d %08lx:%08lx\n",
__func__, epq->name, ep->ptd_index, ep->ptd_offset,
epq->buf_avail, epq->buf_count, num_ptds, epq->buf_map, epq->skip_map);
return found;
}
static inline void release_ptd_buffers(struct isp1362_ep_queue *epq, struct isp1362_ep *ep)
{
int last = ep->ptd_index + ep->num_ptds;
if (last > epq->buf_count)
pr_err("%s: ep %p req %d len %d %s PTD[%d] $%04x num_ptds %d buf_count %d buf_avail %d buf_map %08lx skip_map %08lx\n",
__func__, ep, ep->num_req, ep->length, epq->name, ep->ptd_index,
ep->ptd_offset, ep->num_ptds, epq->buf_count, epq->buf_avail,
epq->buf_map, epq->skip_map);
BUG_ON(last > epq->buf_count);
bitmap_clear(&epq->buf_map, ep->ptd_index, ep->num_ptds);
bitmap_set(&epq->skip_map, ep->ptd_index, ep->num_ptds);
epq->buf_avail += ep->num_ptds;
epq->ptd_count--;
BUG_ON(epq->buf_avail > epq->buf_count);
BUG_ON(epq->ptd_count > epq->buf_count);
DBG(1, "%s: Done %s PTDs $%04x released %d avail %d count %d\n",
__func__, epq->name,
ep->ptd_offset, ep->num_ptds, epq->buf_avail, epq->buf_count);
DBG(1, "%s: buf_map %08lx skip_map %08lx\n", __func__,
epq->buf_map, epq->skip_map);
ep->num_ptds = 0;
ep->ptd_offset = -EINVAL;
ep->ptd_index = -EINVAL;
}
/*-------------------------------------------------------------------------*/
/*
Set up PTD's.
*/
static void prepare_ptd(struct isp1362_hcd *isp1362_hcd, struct urb *urb,
struct isp1362_ep *ep, struct isp1362_ep_queue *epq,
u16 fno)
{
struct ptd *ptd;
int toggle;
int dir;
u16 len;
size_t buf_len = urb->transfer_buffer_length - urb->actual_length;
DBG(3, "%s: %s ep %p\n", __func__, epq->name, ep);
ptd = &ep->ptd;
ep->data = (unsigned char *)urb->transfer_buffer + urb->actual_length;
switch (ep->nextpid) {
case USB_PID_IN:
toggle = usb_gettoggle(urb->dev, ep->epnum, 0);
dir = PTD_DIR_IN;
if (usb_pipecontrol(urb->pipe)) {
len = min_t(size_t, ep->maxpacket, buf_len);
} else if (usb_pipeisoc(urb->pipe)) {
len = min_t(size_t, urb->iso_frame_desc[fno].length, MAX_XFER_SIZE);
ep->data = urb->transfer_buffer + urb->iso_frame_desc[fno].offset;
} else
len = max_transfer_size(epq, buf_len, ep->maxpacket);
DBG(1, "%s: IN len %d/%d/%d from URB\n", __func__, len, ep->maxpacket,
(int)buf_len);
break;
case USB_PID_OUT:
toggle = usb_gettoggle(urb->dev, ep->epnum, 1);
dir = PTD_DIR_OUT;
if (usb_pipecontrol(urb->pipe))
len = min_t(size_t, ep->maxpacket, buf_len);
else if (usb_pipeisoc(urb->pipe))
len = min_t(size_t, urb->iso_frame_desc[0].length, MAX_XFER_SIZE);
else
len = max_transfer_size(epq, buf_len, ep->maxpacket);
if (len == 0)
pr_info("%s: Sending ZERO packet: %d\n", __func__,
urb->transfer_flags & URB_ZERO_PACKET);
DBG(1, "%s: OUT len %d/%d/%d from URB\n", __func__, len, ep->maxpacket,
(int)buf_len);
break;
case USB_PID_SETUP:
toggle = 0;
dir = PTD_DIR_SETUP;
len = sizeof(struct usb_ctrlrequest);
DBG(1, "%s: SETUP len %d\n", __func__, len);
ep->data = urb->setup_packet;
break;
case USB_PID_ACK:
toggle = 1;
len = 0;
dir = (urb->transfer_buffer_length && usb_pipein(urb->pipe)) ?
PTD_DIR_OUT : PTD_DIR_IN;
DBG(1, "%s: ACK len %d\n", __func__, len);
break;
default:
toggle = dir = len = 0;
pr_err("%s@%d: ep->nextpid %02x\n", __func__, __LINE__, ep->nextpid);
BUG_ON(1);
}
ep->length = len;
if (!len)
ep->data = NULL;
ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK | PTD_TOGGLE(toggle);
ptd->mps = PTD_MPS(ep->maxpacket) | PTD_SPD(urb->dev->speed == USB_SPEED_LOW) |
PTD_EP(ep->epnum);
ptd->len = PTD_LEN(len) | PTD_DIR(dir);
ptd->faddr = PTD_FA(usb_pipedevice(urb->pipe));
if (usb_pipeint(urb->pipe)) {
ptd->faddr |= PTD_SF_INT(ep->branch);
ptd->faddr |= PTD_PR(ep->interval ? __ffs(ep->interval) : 0);
}
if (usb_pipeisoc(urb->pipe))
ptd->faddr |= PTD_SF_ISO(fno);
DBG(1, "%s: Finished\n", __func__);
}
static void isp1362_write_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
struct isp1362_ep_queue *epq)
{
struct ptd *ptd = &ep->ptd;
int len = PTD_GET_DIR(ptd) == PTD_DIR_IN ? 0 : ep->length;
_BUG_ON(ep->ptd_offset < 0);
prefetch(ptd);
isp1362_write_buffer(isp1362_hcd, ptd, ep->ptd_offset, PTD_HEADER_SIZE);
if (len)
isp1362_write_buffer(isp1362_hcd, ep->data,
ep->ptd_offset + PTD_HEADER_SIZE, len);
dump_ptd(ptd);
dump_ptd_out_data(ptd, ep->data);
}
static void isp1362_read_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
struct isp1362_ep_queue *epq)
{
struct ptd *ptd = &ep->ptd;
int act_len;
WARN_ON(list_empty(&ep->active));
BUG_ON(ep->ptd_offset < 0);
list_del_init(&ep->active);
DBG(1, "%s: ep %p removed from active list %p\n", __func__, ep, &epq->active);
prefetchw(ptd);
isp1362_read_buffer(isp1362_hcd, ptd, ep->ptd_offset, PTD_HEADER_SIZE);
dump_ptd(ptd);
act_len = PTD_GET_COUNT(ptd);
if (PTD_GET_DIR(ptd) != PTD_DIR_IN || act_len == 0)
return;
if (act_len > ep->length)
pr_err("%s: ep %p PTD $%04x act_len %d ep->length %d\n", __func__, ep,
ep->ptd_offset, act_len, ep->length);
BUG_ON(act_len > ep->length);
/* Only transfer the amount of data that has actually been overwritten
* in the chip buffer. We don't want any data that doesn't belong to the
* transfer to leak out of the chip to the callers transfer buffer!
*/
prefetchw(ep->data);
isp1362_read_buffer(isp1362_hcd, ep->data,
ep->ptd_offset + PTD_HEADER_SIZE, act_len);
dump_ptd_in_data(ptd, ep->data);
}
/*
* INT PTDs will stay in the chip until data is available.
* This function will remove a PTD from the chip when the URB is dequeued.
* Must be called with the spinlock held and IRQs disabled
*/
static void remove_ptd(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep)
{
int index;
struct isp1362_ep_queue *epq;
DBG(1, "%s: ep %p PTD[%d] $%04x\n", __func__, ep, ep->ptd_index, ep->ptd_offset);
BUG_ON(ep->ptd_offset < 0);
epq = get_ptd_queue(isp1362_hcd, ep->ptd_offset);
BUG_ON(!epq);
/* put ep in remove_list for cleanup */
WARN_ON(!list_empty(&ep->remove_list));
list_add_tail(&ep->remove_list, &isp1362_hcd->remove_list);
/* let SOF interrupt handle the cleanup */
isp1362_enable_int(isp1362_hcd, HCuPINT_SOF);
index = ep->ptd_index;
if (index < 0)
/* ISO queues don't have SKIP registers */
return;
DBG(1, "%s: Disabling PTD[%02x] $%04x %08lx|%08x\n", __func__,
index, ep->ptd_offset, epq->skip_map, 1 << index);
/* prevent further processing of PTD (will be effective after next SOF) */
epq->skip_map |= 1 << index;
if (epq == &isp1362_hcd->atl_queue) {
DBG(2, "%s: ATLSKIP = %08x -> %08lx\n", __func__,
isp1362_read_reg32(isp1362_hcd, HCATLSKIP), epq->skip_map);
isp1362_write_reg32(isp1362_hcd, HCATLSKIP, epq->skip_map);
if (~epq->skip_map == 0)
isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
} else if (epq == &isp1362_hcd->intl_queue) {
DBG(2, "%s: INTLSKIP = %08x -> %08lx\n", __func__,
isp1362_read_reg32(isp1362_hcd, HCINTLSKIP), epq->skip_map);
isp1362_write_reg32(isp1362_hcd, HCINTLSKIP, epq->skip_map);
if (~epq->skip_map == 0)
isp1362_clr_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_INTL_ACTIVE);
}
}
/*
Take done or failed requests out of schedule. Give back
processed urbs.
*/
static void finish_request(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep,
struct urb *urb, int status)
__releases(isp1362_hcd->lock)
__acquires(isp1362_hcd->lock)
{
urb->hcpriv = NULL;
ep->error_count = 0;
if (usb_pipecontrol(urb->pipe))
ep->nextpid = USB_PID_SETUP;
URB_DBG("%s: req %d FA %d ep%d%s %s: len %d/%d %s stat %d\n", __func__,
ep->num_req, usb_pipedevice(urb->pipe),
usb_pipeendpoint(urb->pipe),
!usb_pipein(urb->pipe) ? "out" : "in",
usb_pipecontrol(urb->pipe) ? "ctrl" :
usb_pipeint(urb->pipe) ? "int" :
usb_pipebulk(urb->pipe) ? "bulk" :
"iso",
urb->actual_length, urb->transfer_buffer_length,
!(urb->transfer_flags & URB_SHORT_NOT_OK) ?
"short_ok" : "", urb->status);
usb_hcd_unlink_urb_from_ep(isp1362_hcd_to_hcd(isp1362_hcd), urb);
spin_unlock(&isp1362_hcd->lock);
usb_hcd_giveback_urb(isp1362_hcd_to_hcd(isp1362_hcd), urb, status);
spin_lock(&isp1362_hcd->lock);
/* take idle endpoints out of the schedule right away */
if (!list_empty(&ep->hep->urb_list))
return;
/* async deschedule */
if (!list_empty(&ep->schedule)) {
list_del_init(&ep->schedule);
return;
}
if (ep->interval) {
/* periodic deschedule */
DBG(1, "deschedule qh%d/%p branch %d load %d bandwidth %d -> %d\n", ep->interval,
ep, ep->branch, ep->load,
isp1362_hcd->load[ep->branch],
isp1362_hcd->load[ep->branch] - ep->load);
isp1362_hcd->load[ep->branch] -= ep->load;
ep->branch = PERIODIC_SIZE;
}
}
/*
* Analyze transfer results, handle partial transfers and errors
*/
static void postproc_ep(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep *ep)
{
struct urb *urb = get_urb(ep);
struct usb_device *udev;
struct ptd *ptd;
int short_ok;
u16 len;
int urbstat = -EINPROGRESS;
u8 cc;
DBG(2, "%s: ep %p req %d\n", __func__, ep, ep->num_req);
udev = urb->dev;
ptd = &ep->ptd;
cc = PTD_GET_CC(ptd);
if (cc == PTD_NOTACCESSED) {
pr_err("%s: req %d PTD %p Untouched by ISP1362\n", __func__,
ep->num_req, ptd);
cc = PTD_DEVNOTRESP;
}
short_ok = !(urb->transfer_flags & URB_SHORT_NOT_OK);
len = urb->transfer_buffer_length - urb->actual_length;
/* Data underrun is special. For allowed underrun
we clear the error and continue as normal. For
forbidden underrun we finish the DATA stage
immediately while for control transfer,
we do a STATUS stage.
*/
if (cc == PTD_DATAUNDERRUN) {
if (short_ok) {
DBG(1, "%s: req %d Allowed data underrun short_%sok %d/%d/%d byte\n",
__func__, ep->num_req, short_ok ? "" : "not_",
PTD_GET_COUNT(ptd), ep->maxpacket, len);
cc = PTD_CC_NOERROR;
urbstat = 0;
} else {
DBG(1, "%s: req %d Data Underrun %s nextpid %02x short_%sok %d/%d/%d byte\n",
__func__, ep->num_req,
usb_pipein(urb->pipe) ? "IN" : "OUT", ep->nextpid,
short_ok ? "" : "not_",
PTD_GET_COUNT(ptd), ep->maxpacket, len);
if (usb_pipecontrol(urb->pipe)) {
ep->nextpid = USB_PID_ACK;
/* save the data underrun error code for later and
* proceed with the status stage
*/
urb->actual_length += PTD_GET_COUNT(ptd);
BUG_ON(urb->actual_length > urb->transfer_buffer_length);
if (urb->status == -EINPROGRESS)
urb->status = cc_to_error[PTD_DATAUNDERRUN];
} else {
usb_settoggle(udev, ep->epnum, ep->nextpid == USB_PID_OUT,
PTD_GET_TOGGLE(ptd));
urbstat = cc_to_error[PTD_DATAUNDERRUN];
}
goto out;
}
}
if (cc != PTD_CC_NOERROR) {
if (++ep->error_count >= 3 || cc == PTD_CC_STALL || cc == PTD_DATAOVERRUN) {
urbstat = cc_to_error[cc];
DBG(1, "%s: req %d nextpid %02x, status %d, error %d, error_count %d\n",
__func__, ep->num_req, ep->nextpid, urbstat, cc,
ep->error_count);
}
goto out;
}
switch (ep->nextpid) {
case USB_PID_OUT:
if (PTD_GET_COUNT(ptd) != ep->length)
pr_err("%s: count=%d len=%d\n", __func__,
PTD_GET_COUNT(ptd), ep->length);
BUG_ON(PTD_GET_COUNT(ptd) != ep->length);
urb->actual_length += ep->length;
BUG_ON(urb->actual_length > urb->transfer_buffer_length);
usb_settoggle(udev, ep->epnum, 1, PTD_GET_TOGGLE(ptd));
if (urb->actual_length == urb->transfer_buffer_length) {
DBG(3, "%s: req %d xfer complete %d/%d status %d -> 0\n", __func__,
ep->num_req, len, ep->maxpacket, urbstat);
if (usb_pipecontrol(urb->pipe)) {
DBG(3, "%s: req %d %s Wait for ACK\n", __func__,
ep->num_req,
usb_pipein(urb->pipe) ? "IN" : "OUT");
ep->nextpid = USB_PID_ACK;
} else {
if (len % ep->maxpacket ||
!(urb->transfer_flags & URB_ZERO_PACKET)) {
urbstat = 0;
DBG(3, "%s: req %d URB %s status %d count %d/%d/%d\n",
__func__, ep->num_req, usb_pipein(urb->pipe) ? "IN" : "OUT",
urbstat, len, ep->maxpacket, urb->actual_length);
}
}
}
break;
case USB_PID_IN:
len = PTD_GET_COUNT(ptd);
BUG_ON(len > ep->length);
urb->actual_length += len;
BUG_ON(urb->actual_length > urb->transfer_buffer_length);
usb_settoggle(udev, ep->epnum, 0, PTD_GET_TOGGLE(ptd));
/* if transfer completed or (allowed) data underrun */
if ((urb->transfer_buffer_length == urb->actual_length) ||
len % ep->maxpacket) {
DBG(3, "%s: req %d xfer complete %d/%d status %d -> 0\n", __func__,
ep->num_req, len, ep->maxpacket, urbstat);
if (usb_pipecontrol(urb->pipe)) {
DBG(3, "%s: req %d %s Wait for ACK\n", __func__,
ep->num_req,
usb_pipein(urb->pipe) ? "IN" : "OUT");
ep->nextpid = USB_PID_ACK;
} else {
urbstat = 0;
DBG(3, "%s: req %d URB %s status %d count %d/%d/%d\n",
__func__, ep->num_req, usb_pipein(urb->pipe) ? "IN" : "OUT",
urbstat, len, ep->maxpacket, urb->actual_length);
}
}
break;
case USB_PID_SETUP:
if (urb->transfer_buffer_length == urb->actual_length) {
ep->nextpid = USB_PID_ACK;
} else if (usb_pipeout(urb->pipe)) {
usb_settoggle(udev, 0, 1, 1);
ep->nextpid = USB_PID_OUT;
} else {
usb_settoggle(udev, 0, 0, 1);
ep->nextpid = USB_PID_IN;
}
break;
case USB_PID_ACK:
DBG(3, "%s: req %d got ACK %d -> 0\n", __func__, ep->num_req,
urbstat);
WARN_ON(urbstat != -EINPROGRESS);
urbstat = 0;
ep->nextpid = 0;
break;
default:
BUG_ON(1);
}
out:
if (urbstat != -EINPROGRESS) {
DBG(2, "%s: Finishing ep %p req %d urb %p status %d\n", __func__,
ep, ep->num_req, urb, urbstat);
finish_request(isp1362_hcd, ep, urb, urbstat);
}
}
static void finish_unlinks(struct isp1362_hcd *isp1362_hcd)
{
struct isp1362_ep *ep;
struct isp1362_ep *tmp;
list_for_each_entry_safe(ep, tmp, &isp1362_hcd->remove_list, remove_list) {
struct isp1362_ep_queue *epq =
get_ptd_queue(isp1362_hcd, ep->ptd_offset);
int index = ep->ptd_index;
BUG_ON(epq == NULL);
if (index >= 0) {
DBG(1, "%s: remove PTD[%d] $%04x\n", __func__, index, ep->ptd_offset);
BUG_ON(ep->num_ptds == 0);
release_ptd_buffers(epq, ep);
}
if (!list_empty(&ep->hep->urb_list)) {
struct urb *urb = get_urb(ep);
DBG(1, "%s: Finishing req %d ep %p from remove_list\n", __func__,
ep->num_req, ep);
finish_request(isp1362_hcd, ep, urb, -ESHUTDOWN);
}
WARN_ON(list_empty(&ep->active));
if (!list_empty(&ep->active)) {
list_del_init(&ep->active);
DBG(1, "%s: ep %p removed from active list\n", __func__, ep);
}
list_del_init(&ep->remove_list);
DBG(1, "%s: ep %p removed from remove_list\n", __func__, ep);
}
DBG(1, "%s: Done\n", __func__);
}
static inline void enable_atl_transfers(struct isp1362_hcd *isp1362_hcd, int count)
{
if (count > 0) {
if (count < isp1362_hcd->atl_queue.ptd_count)
isp1362_write_reg16(isp1362_hcd, HCATLDTC, count);
isp1362_enable_int(isp1362_hcd, HCuPINT_ATL);
isp1362_write_reg32(isp1362_hcd, HCATLSKIP, isp1362_hcd->atl_queue.skip_map);
isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_ATL_ACTIVE);
} else
isp1362_enable_int(isp1362_hcd, HCuPINT_SOF);
}
static inline void enable_intl_transfers(struct isp1362_hcd *isp1362_hcd)
{
isp1362_enable_int(isp1362_hcd, HCuPINT_INTL);
isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, HCBUFSTAT_INTL_ACTIVE);
isp1362_write_reg32(isp1362_hcd, HCINTLSKIP, isp1362_hcd->intl_queue.skip_map);
}
static inline void enable_istl_transfers(struct isp1362_hcd *isp1362_hcd, int flip)
{
isp1362_enable_int(isp1362_hcd, flip ? HCuPINT_ISTL1 : HCuPINT_ISTL0);
isp1362_set_mask16(isp1362_hcd, HCBUFSTAT, flip ?
HCBUFSTAT_ISTL1_FULL : HCBUFSTAT_ISTL0_FULL);
}
static int submit_req(struct isp1362_hcd *isp1362_hcd, struct urb *urb,
struct isp1362_ep *ep, struct isp1362_ep_queue *epq)
{
int index = epq->free_ptd;
prepare_ptd(isp1362_hcd, urb, ep, epq, 0);
index = claim_ptd_buffers(epq, ep, ep->length);
if (index == -ENOMEM) {
DBG(1, "%s: req %d No free %s PTD available: %d, %08lx:%08lx\n", __func__,
ep->num_req, epq->name, ep->num_ptds, epq->buf_map, epq->skip_map);
return index;
} else if (index == -EOVERFLOW) {
DBG(1, "%s: req %d Not enough space for %d byte %s PTD %d %08lx:%08lx\n",
__func__, ep->num_req, ep->length, epq->name, ep->num_ptds,
epq->buf_map, epq->skip_map);
return index;
} else
BUG_ON(index < 0);
list_add_tail(&ep->active, &epq->active);
DBG(1, "%s: ep %p req %d len %d added to active list %p\n", __func__,
ep, ep->num_req, ep->length, &epq->active);
DBG(1, "%s: Submitting %s PTD $%04x for ep %p req %d\n", __func__, epq->name,
ep->ptd_offset, ep, ep->num_req);
isp1362_write_ptd(isp1362_hcd, ep, epq);
__clear_bit(ep->ptd_index, &epq->skip_map);
return 0;
}
static void start_atl_transfers(struct isp1362_hcd *isp1362_hcd)
{
int ptd_count = 0;
struct isp1362_ep_queue *epq = &isp1362_hcd->atl_queue;
struct isp1362_ep *ep;
int defer = 0;
if (atomic_read(&epq->finishing)) {
DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
return;
}
list_for_each_entry(ep, &isp1362_hcd->async, schedule) {
struct urb *urb = get_urb(ep);
int ret;
if (!list_empty(&ep->active)) {
DBG(2, "%s: Skipping active %s ep %p\n", __func__, epq->name, ep);
continue;
}
DBG(1, "%s: Processing %s ep %p req %d\n", __func__, epq->name,
ep, ep->num_req);
ret = submit_req(isp1362_hcd, urb, ep, epq);
if (ret == -ENOMEM) {
defer = 1;
break;
} else if (ret == -EOVERFLOW) {
defer = 1;
continue;
}
#ifdef BUGGY_PXA2XX_UDC_USBTEST
defer = ep->nextpid == USB_PID_SETUP;
#endif
ptd_count++;
}
/* Avoid starving of endpoints */
if (isp1362_hcd->async.next != isp1362_hcd->async.prev) {
DBG(2, "%s: Cycling ASYNC schedule %d\n", __func__, ptd_count);
list_move(&isp1362_hcd->async, isp1362_hcd->async.next);
}
if (ptd_count || defer)
enable_atl_transfers(isp1362_hcd, defer ? 0 : ptd_count);
epq->ptd_count += ptd_count;
if (epq->ptd_count > epq->stat_maxptds) {
epq->stat_maxptds = epq->ptd_count;
DBG(0, "%s: max_ptds: %d\n", __func__, epq->stat_maxptds);
}
}
static void start_intl_transfers(struct isp1362_hcd *isp1362_hcd)
{
int ptd_count = 0;
struct isp1362_ep_queue *epq = &isp1362_hcd->intl_queue;
struct isp1362_ep *ep;
if (atomic_read(&epq->finishing)) {
DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
return;
}
list_for_each_entry(ep, &isp1362_hcd->periodic, schedule) {
struct urb *urb = get_urb(ep);
int ret;
if (!list_empty(&ep->active)) {
DBG(1, "%s: Skipping active %s ep %p\n", __func__,
epq->name, ep);
continue;
}
DBG(1, "%s: Processing %s ep %p req %d\n", __func__,
epq->name, ep, ep->num_req);
ret = submit_req(isp1362_hcd, urb, ep, epq);
if (ret == -ENOMEM)
break;
else if (ret == -EOVERFLOW)
continue;
ptd_count++;
}
if (ptd_count) {
static int last_count;
if (ptd_count != last_count) {
DBG(0, "%s: ptd_count: %d\n", __func__, ptd_count);
last_count = ptd_count;
}
enable_intl_transfers(isp1362_hcd);
}
epq->ptd_count += ptd_count;
if (epq->ptd_count > epq->stat_maxptds)
epq->stat_maxptds = epq->ptd_count;
}
static inline int next_ptd(struct isp1362_ep_queue *epq, struct isp1362_ep *ep)
{
u16 ptd_offset = ep->ptd_offset;
int num_ptds = (ep->length + PTD_HEADER_SIZE + (epq->blk_size - 1)) / epq->blk_size;
DBG(2, "%s: PTD offset $%04x + %04x => %d * %04x -> $%04x\n", __func__, ptd_offset,
ep->length, num_ptds, epq->blk_size, ptd_offset + num_ptds * epq->blk_size);
ptd_offset += num_ptds * epq->blk_size;
if (ptd_offset < epq->buf_start + epq->buf_size)
return ptd_offset;
else
return -ENOMEM;
}
static void start_iso_transfers(struct isp1362_hcd *isp1362_hcd)
{
int ptd_count = 0;
int flip = isp1362_hcd->istl_flip;
struct isp1362_ep_queue *epq;
int ptd_offset;
struct isp1362_ep *ep;
struct isp1362_ep *tmp;
u16 fno = isp1362_read_reg32(isp1362_hcd, HCFMNUM);
fill2:
epq = &isp1362_hcd->istl_queue[flip];
if (atomic_read(&epq->finishing)) {
DBG(1, "%s: finish_transfers is active for %s\n", __func__, epq->name);
return;
}
if (!list_empty(&epq->active))
return;
ptd_offset = epq->buf_start;
list_for_each_entry_safe(ep, tmp, &isp1362_hcd->isoc, schedule) {
struct urb *urb = get_urb(ep);
s16 diff = fno - (u16)urb->start_frame;
DBG(1, "%s: Processing %s ep %p\n", __func__, epq->name, ep);
if (diff > urb->number_of_packets) {
/* time frame for this URB has elapsed */
finish_request(isp1362_hcd, ep, urb, -EOVERFLOW);
continue;
} else if (diff < -1) {
/* URB is not due in this frame or the next one.
* Comparing with '-1' instead of '0' accounts for double
* buffering in the ISP1362 which enables us to queue the PTD
* one frame ahead of time
*/
} else if (diff == -1) {
/* submit PTD's that are due in the next frame */
prepare_ptd(isp1362_hcd, urb, ep, epq, fno);
if (ptd_offset + PTD_HEADER_SIZE + ep->length >
epq->buf_start + epq->buf_size) {
pr_err("%s: Not enough ISO buffer space for %d byte PTD\n",
__func__, ep->length);
continue;
}
ep->ptd_offset = ptd_offset;
list_add_tail(&ep->active, &epq->active);
ptd_offset = next_ptd(epq, ep);
if (ptd_offset < 0) {
pr_warning("%s: req %d No more %s PTD buffers available\n", __func__,
ep->num_req, epq->name);
break;
}
}
}
list_for_each_entry(ep, &epq->active, active) {
if (epq->active.next == &ep->active)
ep->ptd.mps |= PTD_LAST_MSK;
isp1362_write_ptd(isp1362_hcd, ep, epq);
ptd_count++;
}
if (ptd_count)
enable_istl_transfers(isp1362_hcd, flip);
epq->ptd_count += ptd_count;
if (epq->ptd_count > epq->stat_maxptds)
epq->stat_maxptds = epq->ptd_count;
/* check, whether the second ISTL buffer may also be filled */
if (!(isp1362_read_reg16(isp1362_hcd, HCBUFSTAT) &
(flip ? HCBUFSTAT_ISTL0_FULL : HCBUFSTAT_ISTL1_FULL))) {
fno++;
ptd_count = 0;
flip = 1 - flip;
goto fill2;
}
}
static void finish_transfers(struct isp1362_hcd *isp1362_hcd, unsigned long done_map,
struct isp1362_ep_queue *epq)
{
struct isp1362_ep *ep;
struct isp1362_ep *tmp;
if (list_empty(&epq->active)) {
DBG(1, "%s: Nothing to do for %s queue\n", __func__, epq->name);
return;
}
DBG(1, "%s: Finishing %s transfers %08lx\n", __func__, epq->name, done_map);
atomic_inc(&epq->finishing);
list_for_each_entry_safe(ep, tmp, &epq->active, active) {
int index = ep->ptd_index;
DBG(1, "%s: Checking %s PTD[%02x] $%04x\n", __func__, epq->name,
index, ep->ptd_offset);
BUG_ON(index < 0);
if (__test_and_clear_bit(index, &done_map)) {
isp1362_read_ptd(isp1362_hcd, ep, epq);
epq->free_ptd = index;
BUG_ON(ep->num_ptds == 0);
release_ptd_buffers(epq, ep);
DBG(1, "%s: ep %p req %d removed from active list\n", __func__,
ep, ep->num_req);
if (!list_empty(&ep->remove_list)) {
list_del_init(&ep->remove_list);
DBG(1, "%s: ep %p removed from remove list\n", __func__, ep);
}
DBG(1, "%s: Postprocessing %s ep %p req %d\n", __func__, epq->name,
ep, ep->num_req);
postproc_ep(isp1362_hcd, ep);
}
if (!done_map)
break;
}
if (done_map)
pr_warning("%s: done_map not clear: %08lx:%08lx\n", __func__, done_map,
epq->skip_map);
atomic_dec(&epq->finishing);
}
static void finish_iso_transfers(struct isp1362_hcd *isp1362_hcd, struct isp1362_ep_queue *epq)
{
struct isp1362_ep *ep;
struct isp1362_ep *tmp;
if (list_empty(&epq->active)) {
DBG(1, "%s: Nothing to do for %s queue\n", __func__, epq->name);
return;
}
DBG(1, "%s: Finishing %s transfers\n", __func__, epq->name);
atomic_inc(&epq->finishing);