-
Notifications
You must be signed in to change notification settings - Fork 1
/
isp116x-hcd.c
2054 lines (1742 loc) · 51.6 KB
/
isp116x-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
/*
* NetUSBee USB driver for TOS
*
* Modified for TOS-only version of NetUSBee
* Copyright (C) 2018 Roger Burrows
*
* Modified for Atari-NetUSBee by David Gálvez. 2010 - 2011
*
* ISP116x HCD (Host Controller Driver) for u-boot.
*
* Copyright (C) 2006-2007 Rodolfo Giometti <[email protected]>
* Copyright (C) 2006-2007 Eurotech S.p.A. <[email protected]>
*
* This file 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.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
* Derived in part from the SL811 HCD driver "u-boot/drivers/usb/sl811_usb.c"
* (original copyright message follows):
*
* (C) Copyright 2004
* Wolfgang Denk, DENX Software Engineering, [email protected].
*
* This code is based on linux driver for sl811hs chip, source at
* drivers/usb/host/sl811.c:
*
* SL811 Host Controller Interface driver for USB.
*
* Copyright (c) 2003/06, Courage Co., Ltd.
*
* Based on:
* 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
* Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
* Adam Richter, Gregory P. Smith;
* 2.Original SL811 driver (hc_sl811.o) by Pei Liu <[email protected]>
* 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
*
* [[GNU/GPL disclaimer]]
*
* and in part from AU1x00 OHCI HCD driver "u-boot/cpu/mips/au1x00_usb_ohci.c"
* (original copyright message follows):
*
* URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
*
* (C) Copyright 2003
* Gary Jennejohn, DENX Software Engineering <[email protected]>
*
* [[GNU/GPL disclaimer]]
*
* Note: Part of this code has been derived from linux
*/
#include <stdio.h>
#include <string.h>
#include <osbind.h>
#define __CDECL /* for usb_api.h */
#include "usb.h"
#include "usb_api.h"
#include "delay.h"
#include "netusbee.h"
#include "netusbee_int.h"
#define ALERT(x)
#define DEBUG(x)
/*
* specific to NetUSBee
*/
#define VER_MAJOR 0
#define VER_MINOR 2
#define MSG_VERSION "TOS"
#define MSG_BUILDDATE __DATE__
#define MSG_BOOT \
"\033p NetUSBee USB controller driver for " MSG_VERSION " \033q\r\n"
#define MSG_GREET \
"Ported, mixed and shaken by David Galvez.\r\n" \
"Brought to TOS by Claude Labelle & Roger Burrows.\r\n" \
"Compiled " MSG_BUILDDATE ".\r\n\r\n"
/*
* global variables
*/
#ifndef SLOW_MACHINE
static ulong delay_150ns;
static ulong delay_300ns;
#endif
#include "isp116x.h" /* here because static inlines use above global variables */
ulong delay_1us;
extern unsigned long _PgmSize;
struct usb_module_api *api;
static struct usb_device *root_hub_dev = NULL;
static struct isp116x isp116x_dev;
struct isp116x_platform_data isp116x_board;
static long got_rhsc; /* root hub status change */
struct usb_device *devgone; /* device which was disconnected */
static long rh_devnum; /* address of Root Hub endpoint */
static char job_in_progress = 0;
/*
*Function prototypes
*/
static long isp116x_check_id (struct isp116x *);
static long isp116x_reset (struct isp116x *);
static long submit_bulk_msg (struct usb_device *, unsigned long , void *, long, long);
static long submit_control_msg (struct usb_device *, unsigned long, void *,
long, struct devrequest *);
static long submit_int_msg (struct usb_device *, unsigned long, void *, long, long);
/*
* USB controller interface
*/
static long _cdecl netusbee_open (struct ucdif *);
static long _cdecl netusbee_close (struct ucdif *);
static long _cdecl netusbee_ioctl (struct ucdif *, short, long);
static char lname[] = "NetUSBee USB driver\0";
static struct ucdif netusbee_uif =
{
0, /* *next */
USB_API_VERSION, /* API */
USB_CONTRLL, /* class */
lname, /* lname */
"netusbee", /* name */
0, /* unit */
0, /* flags */
netusbee_open, /* open */
netusbee_close, /* close */
0, /* resrvd1 */
netusbee_ioctl, /* ioctl */
0, /* resrvd2 */
};
/* ------------------------------------------------------------------------- */
#define min1_t(type,x,y) \
({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
/* Galvez: added to avoid shadow warnings */
#define min2_t(type,x,y) \
({ type __a = (x); type __b = (y); __a < __b ? __a : __b; })
/* ------------------------------------------------------------------------- */
/* --- Debugging functions ------------------------------------------------- */
#define isp116x_show_reg(d, r) { \
if ((r) < 0x20) \
{ \
DEBUG(("%12s[%02x]: %08lx", #r, \
r, isp116x_read_reg32(d, r))); \
} \
else \
{ \
DEBUG(("%12s[%02x]: %04x", #r, \
r, isp116x_read_reg16(d, r))); \
} \
}
#define isp116x_show_regs(d) { \
isp116x_show_reg(d, HCREVISION); \
isp116x_show_reg(d, HCCONTROL); \
isp116x_show_reg(d, HCCMDSTAT); \
isp116x_show_reg(d, HCINTSTAT); \
isp116x_show_reg(d, HCINTENB); \
isp116x_show_reg(d, HCFMINTVL); \
isp116x_show_reg(d, HCFMREM); \
isp116x_show_reg(d, HCFMNUM); \
isp116x_show_reg(d, HCLSTHRESH); \
isp116x_show_reg(d, HCRHDESCA); \
isp116x_show_reg(d, HCRHDESCB); \
isp116x_show_reg(d, HCRHSTATUS); \
isp116x_show_reg(d, HCRHPORT1); \
isp116x_show_reg(d, HCRHPORT2); \
isp116x_show_reg(d, HCHWCFG); \
isp116x_show_reg(d, HCDMACFG); \
isp116x_show_reg(d, HCXFERCTR); \
isp116x_show_reg(d, HCuPINT); \
isp116x_show_reg(d, HCuPINTENB); \
isp116x_show_reg(d, HCCHIPID); \
isp116x_show_reg(d, HCSCRATCH); \
isp116x_show_reg(d, HCITLBUFLEN); \
isp116x_show_reg(d, HCATLBUFLEN); \
isp116x_show_reg(d, HCBUFSTAT); \
isp116x_show_reg(d, HCRDITL0LEN); \
isp116x_show_reg(d, HCRDITL1LEN); \
}
#if defined(TRACE_EXTRA)
static long
isp116x_get_current_frame_number(struct usb_device *usb_dev)
{
struct isp116x *isp116x = &isp116x_dev;
return isp116x_read_reg32(isp116x, HCFMNUM);
}
static void
dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
long len, char *str)
{
#if defined(VERBOSE)
long i;
char build_str[64];
char buf[(len * 4) + 24 + 6];
#endif
DEBUG(("%s URB:[%4lx] dev:%2ld,ep:%2ld-%c,type:%s,len:%ld stat:0x%lx",
str, isp116x_get_current_frame_number(dev),
usb_pipedevice(pipe), usb_pipeendpoint(pipe),
usb_pipeout(pipe) ? 'O' : 'I',
usb_pipetype(pipe) < 2 ?
(usb_pipeint(pipe) ? "INTR" : "ISOC") :
(usb_pipecontrol(pipe) ? "CTRL" : "BULK"),
len, dev->status));
#if defined(VERBOSE)
sprintf(buf, sizeof(buf),"\0");
if (len > 0 && buffer)
{
sprintf(build_str, sizeof(build_str), __FILE__ ": data(%ld):", len);
strcat(buf, build_str);
for (i = 0; i < 16 && i < len; i++)
{
sprintf(build_str, sizeof(build_str), " %02x", ((unsigned char *) buffer)[i]);
strcat(buf, build_str);
}
sprintf(build_str, sizeof(build_str), "%s\r\n", i < len ? "..." : "");
strcat(buf, build_str);
DEBUG((buf));
}
#endif
}
# define PTD_DIR_STR(ptd) ({char __c; \
switch(PTD_GET_DIR(ptd)){ \
case 0: __c = 's'; break; \
case 1: __c = 'o'; break; \
default: __c = 'i'; break; \
}; __c;})
/*
Dump PTD info. The code documents the format
perfectly, right :)
*/
static inline void
dump_ptd(PTD *ptd)
{
#if defined(VERBOSE)
long k;
char build_str[64];
char buf[64 + 4 * sizeof(PTD)];
#endif
DEBUG(("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
PTD_GET_CC(ptd),
PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
PTD_GET_TOGGLE(ptd),
PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd)));
#if defined(VERBOSE)
sprintf(buf, sizeof(buf),"\0");
sprintf(build_str, sizeof(build_str), "isp116x: %s: PTD(byte): ", __FUNCTION__);
strcat(buf, build_str);
for (k = 0; k < sizeof(PTD); ++k) /* Galvez: note that bytes in the words are shown swapped */
{
sprintf(build_str, sizeof(build_str),"%02x ", ((unsigned char *) ptd)[k]);
strcat(buf, build_str);
}
DEBUG((buf));
#endif
}
static inline void
dump_ptd_data(PTD *ptd, unsigned char * buffer, long type)
{
#if defined(VERBOSE)
long k;
char build_str[64];
char buf[64 + 4 * PTD_GET_LEN(ptd)];
sprintf(buf, sizeof(buf),"\0");
if (type == 0 /* 0ut data */ )
{
sprintf(build_str, sizeof(build_str), "isp116x: %s: out data: ", __FUNCTION__);
strcat(buf, build_str);
for (k = 0; k < PTD_GET_LEN(ptd); ++k)
{
sprintf(build_str, sizeof(build_str), "%02x ", ((unsigned char *) buffer)[k]);
strcat(buf, build_str);
}
DEBUG((buf));
}
if (type == 1 /* 1n data */ )
{
sprintf(build_str, sizeof(build_str), "isp116x: %s: in data: ", __FUNCTION__);
strcat(buf, build_str);
for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
{
sprintf(build_str, sizeof(build_str), "%02x ", ((unsigned char *) buffer)[k]);
strcat(buf, build_str);
}
DEBUG((buf));
}
if (PTD_GET_LAST(ptd))
{
DEBUG(("--- last PTD ---"));
}
#endif
}
#else
# define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
# define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
# define dump_ptd(ptd) do {} while (0)
# define dump_ptd_data(ptd, buf, type) do {} while (0)
#endif /* TRACE_EXTRA */
/* --- Virtual Root Hub ---------------------------------------------------- */
/* Device descriptor */
static unsigned char root_hub_dev_des[] =
{
0x12, /* unsigned char bLength; */
0x01, /* unsigned char bDescriptorType; Device */
0x10, /* unsigned short bcdUSB; v1.1 */
0x01,
0x09, /* unsigned char bDeviceClass; HUB_CLASSCODE */
0x00, /* unsigned char bDeviceSubClass; */
0x00, /* unsigned char bDeviceProtocol; */
0x08, /* unsigned char bMaxPacketSize0; 8 Bytes */
0x00, /* unsigned short idVendor; */
0x00,
0x00, /* unsigned short idProduct; */
0x00,
0x00, /* unsigned short bcdDevice; */
0x00,
0x00, /* unsigned char iManufacturer; */
0x01, /* unsigned char iProduct; */
0x00, /* unsigned char iSerialNumber; */
0x01 /* unsigned char bNumConfigurations; */
};
/* Configuration descriptor */
static unsigned char root_hub_config_des[] =
{
0x09, /* unsigned char bLength; */
0x02, /* unsigned char bDescriptorType; Configuration */
0x19, /* unsigned short wTotalLength; */
0x00,
0x01, /* unsigned char bNumInterfaces; */
0x01, /* unsigned char bConfigurationValue; */
0x00, /* unsigned char iConfiguration; */
0x40, /* unsigned char bmAttributes;
Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
0x00, /* unsigned char MaxPower; */
/* interface */
0x09, /* unsigned char if_bLength; */
0x04, /* unsigned char if_bDescriptorType; Interface */
0x00, /* unsigned char if_bInterfaceNumber; */
0x00, /* unsigned char if_bAlternateSetting; */
0x01, /* unsigned char if_bNumEndpoints; */
0x09, /* unsigned char if_bInterfaceClass; HUB_CLASSCODE */
0x00, /* unsigned char if_bInterfaceSubClass; */
0x00, /* unsigned char if_bInterfaceProtocol; */
0x00, /* unsigned char if_iInterface; */
/* endpoint */
0x07, /* unsigned char ep_bLength; */
0x05, /* unsigned char ep_bDescriptorType; Endpoint */
0x81, /* unsigned char ep_bEndpointAddress; IN Endpoint 1 */
0x03, /* unsigned char ep_bmAttributes; Interrupt */
0x00, /* unsigned short ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
0x02,
0xff /* unsigned char ep_bInterval; 255 ms */
};
static unsigned char root_hub_str_index0[] =
{
0x04, /* unsigned char bLength; */
0x03, /* unsigned char bDescriptorType; String-descriptor */
0x09, /* unsigned char lang ID */
0x04, /* unsigned char lang ID */
};
static unsigned char root_hub_str_index1[] =
{
0x24, /* unsigned char bLength; */
0x03, /* unsigned char bDescriptorType; String-descriptor */
'N', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'e', /* unsigned char Unicode */
0, /* unsigned char Unicode */
't', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'U', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'S', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'B', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'e', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'e', /* unsigned char Unicode */
0, /* unsigned char Unicode */
' ', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'R', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'o', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'o', /* unsigned char Unicode */
0, /* unsigned char Unicode */
't', /* unsigned char Unicode */
0, /* unsigned char Unicode */
' ', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'H', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'u', /* unsigned char Unicode */
0, /* unsigned char Unicode */
'b', /* unsigned char Unicode */
0, /* unsigned char Unicode */
};
/*
* Hub class-specific descriptor is constructed dynamically
*/
/* --- Virtual root hub management functions ------------------------------- */
static long
rh_check_port_status(struct isp116x *isp116x)
{
unsigned long temp, ndp, i;
long res;
res = -1;
temp = isp116x_read_reg32(isp116x, HCRHDESCA);
ndp = (temp & RH_A_NDP);
for (i = 0; i < ndp; i++)
{
temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
/* check for a device disconnect */
if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
(RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0))
{
res = i;
break;
}
}
return res;
}
/* --- HC management functions --------------------------------------------- */
/* locking functions */
inline static char lock_usb(char *lock) {
char ret = 0;
__asm("tas %1\n\t"
"seq %0"
: "=d" (ret) /* outputs */
: "m" (*lock) /* inputs, note that this really has to be "*lock", not "lock" */
: "cc"); /* clobbers condition codes */
return ret;
}
inline static void unlock_usb(char *lock) {
*lock = 0;
}
/*
* Check if there is a pending interrupt request from the keyboard ACIA.
* We use this while the CPU priority is set to 6, causing interrupts to
* be disabled. The major problem with this is that some keyboard/mouse
* interrupt data is lost, which typically results in mouse movements
* being interpreted as keyclicks, then repeating keys and other nasties.
*
* We call this routine to poll for ikbd interrupts, which are then serviced
* by calling the keyboard interrupt routine ourselves.
*
* Returns != 0 if there is a pending interrupt request.
*/
static inline int ikbd_int_pending(void)
{
unsigned char keyctl = *(volatile unsigned char *)0xFFFFFC00UL;
return keyctl & 0x80;
}
/* Write len bytes to fifo, pad till 32-bit boundary
*
* Note that we unroll big loops to allow us to check for pending ikbd
* activity frequently, but not too frequently. It may also provide
* a marginal speed improvement.
*/
static void
write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, long len)
{
unsigned char *dp = (unsigned char *) buf;
unsigned short *dp2 = (unsigned short *) buf;
unsigned short w;
long quot = len % 4;
/*
* because of little-endian considerations, the FIFO buffer has the
* bytes swapped from a big-endian POV. we swap them here.
*
* for an unaligned buffer, it's easier to use the raw write function
* and explicitly swap the bytes.
*/
if ((unsigned long)dp2 & 1)
{
/* not aligned */
for (; len >= 16; len -= 16) { /* unrolled loop */
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
if (ikbd_int_pending())
fake_ikbd_int();
}
for (; len > 1; len -= 2)
{
w = *dp++;
w |= *dp++ << 8;
isp116x_raw_write_data16(isp116x, w);
}
}
else
{
/* aligned */
for (; len >= 16; len -= 16) { /* unrolled loop */
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
isp116x_write_data16(isp116x, *dp2++);
if (ikbd_int_pending())
fake_ikbd_int();
}
for (; len > 1; len -= 2)
isp116x_write_data16(isp116x, *dp2++);
dp = (unsigned char *)dp2;
}
if (len)
isp116x_raw_write_data16(isp116x, (unsigned short) *dp);
/*
* since we throw the data away, we just use the raw write here for
* consistency with read_ptddata_from_fifo() [in fact, the generated
* code should be the same for either]
*/
if (quot == 1 || quot == 2)
isp116x_raw_write_data16(isp116x, 0);
}
/* Read len bytes from fifo and then read till 32-bit boundary
*
* Note that we unroll big loops to allow us to check for pending ikbd
* activity frequently, but not too frequently. It may also provide
* a marginal speed improvement.
*/
static void
read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, long len)
{
unsigned char *dp = (unsigned char *) buf;
unsigned short *dp2 = (unsigned short *) buf;
unsigned short w;
long quot = len % 4;
/*
* because of little-endian considerations, the FIFO buffer has the
* bytes swapped from a big-endian POV. we swap them back here.
*
* for an unaligned buffer, it's easier to use the raw read function
* and explicitly swap the bytes.
*/
if ((unsigned long)dp2 & 1)
{
/* not aligned */
for (; len >= 16; len -= 16) { /* unrolled loop */
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
if (ikbd_int_pending())
fake_ikbd_int();
}
for (; len > 1; len -= 2)
{
w = isp116x_raw_read_data16(isp116x);
*dp++ = w & 0xff;
*dp++ = (w >> 8) & 0xff;
}
}
else
{
/* aligned */
for (; len >= 16; len -= 16) { /* unrolled loop */
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
*dp2++ = isp116x_read_data16(isp116x);
if (ikbd_int_pending())
fake_ikbd_int();
}
for (; len > 1; len -= 2)
*dp2++ = isp116x_read_data16(isp116x);
dp = (unsigned char *)dp2;
}
if (len)
*dp = 0xff & isp116x_raw_read_data16(isp116x);
/*
* since we throw the data away, we can just use the raw read here
*/
if (quot == 1 || quot == 2)
isp116x_raw_read_data16(isp116x);
}
/* Write PTD's and data for scheduled transfers into the fifo ram.
* Fifo must be empty and ready
*/
static void
pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
unsigned long pipe, PTD *ptd, void *data,
long len)
{
int write_data = FALSE;
long buflen = sizeof(PTD);
if ((PTD_GET_DIR(ptd) == PTD_DIR_OUT) || (PTD_GET_DIR(ptd) == PTD_DIR_SETUP)) {
write_data = TRUE;
buflen += len;
}
DEBUG(("--- pack buffer %p - %ld bytes (fifo %ld) ---", data, len, buflen));
isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
/* For NetUSBee, use raw_write to don't swap bytes */
dump_ptd(ptd);
isp116x_raw_write_data16(isp116x, ptd->count);
isp116x_raw_write_data16(isp116x, ptd->mps);
isp116x_raw_write_data16(isp116x, ptd->len);
isp116x_raw_write_data16(isp116x, ptd->faddr);
dump_ptd_data(ptd, (unsigned char *) data, 0);
if (write_data) {
/* disable interrupts for critical section */
unsigned long oldmode = ENSURE_SUPER;
TOS_INT_OFF;
write_ptddata_to_fifo(isp116x,
(unsigned char *) data,
PTD_GET_LEN(ptd));
TOS_INT_ON;
RESTORE_MODE(oldmode);
}
}
/* Read the processed PTD's and data from fifo ram back to URBs' buffers.
* Fifo must be full and done
*/
static long
unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
unsigned long pipe, PTD *ptd, void *data,
long len)
{
int read_data = FALSE;
long buflen = sizeof(PTD);
long cc, ret;
if (PTD_GET_DIR(ptd) == PTD_DIR_IN) {
read_data = TRUE;
buflen += len;
}
isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
isp116x_write_addr(isp116x, HCATLPORT);
ret = TD_CC_NOERROR;
/* For NetUSBee, use raw_read to don't swap bytes */
ptd->count = isp116x_raw_read_data16(isp116x);
ptd->mps = isp116x_raw_read_data16(isp116x);
ptd->len = isp116x_raw_read_data16(isp116x);
ptd->faddr = isp116x_raw_read_data16(isp116x);
dump_ptd(ptd);
if (read_data) {
/* disable interrupts for critical section */
unsigned long oldmode = ENSURE_SUPER;
TOS_INT_OFF;
read_ptddata_from_fifo(isp116x,
(unsigned char *) data,
PTD_GET_LEN(ptd));
TOS_INT_ON;
RESTORE_MODE(oldmode);
}
dump_ptd_data(ptd, (unsigned char *) data, 1);
cc = PTD_GET_CC(ptd);
/* Data underrun means basically that we had more buffer space than
* the function had data. It is perfectly normal but upper levels have
* to know how much we actually transferred.
*/
if (cc == TD_NOTACCESSED ||
(cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
ret = cc;
DEBUG(("--- unpack buffer %p - %ld bytes (fifo %ld) count: %d ---", data, len, buflen, PTD_GET_COUNT(ptd)));
return ret;
}
/* Interrupt handling
*/
static long
isp116x_interrupt(struct isp116x *isp116x)
{
unsigned short irqstat;
unsigned long intstat;
long ret = 0;
isp116x_write_reg16(isp116x, HCuPINTENB, 0);
irqstat = isp116x_read_reg16(isp116x, HCuPINT);
isp116x_write_reg16(isp116x, HCuPINT, irqstat);
DEBUG((">>>>>> irqstat %x <<<<<<", irqstat));
if (irqstat & HCuPINT_ATL)
{
DEBUG((">>>>>> HCuPINT_ATL <<<<<<"));
ret = 1;
}
if (irqstat & HCuPINT_OPR)
{
intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
DEBUG((">>>>>> HCuPINT_OPR %lx <<<<<<", intstat));
if (intstat & HCINT_UE)
{
ALERT(("unrecoverable error, controller disabled"));
/* FIXME: be optimistic, hope that bug won't repeat
* often. Make some non-interrupt context restart the
* controller. Count and limit the retries though;
* either hardware or software errors can go forever...
*/
isp116x_reset(isp116x);
ret = -1;
return -1;
}
if (intstat & HCINT_RHSC)
{
got_rhsc = 1;
ret = 1;
/* When root hub or any of its ports is going
to come out of suspend, it may take more
than 10ms for status bits to stabilize. */
mdelay(20);
}
if (intstat & HCINT_SO)
{
ALERT(("schedule overrun"));
ret = -1;
}
irqstat &= ~HCuPINT_OPR;
}
isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
return ret;
}
/* With one PTD we can transfer almost 1K in one go;
* HC does the splitting into endpoint digestible transactions
*/
static PTD ptd[1];
static inline long
max_transfer_len(struct usb_device *dev, unsigned long pipe)
{
long mpck = usb_maxpacket(dev, pipe);
/* One PTD can transfer 1023 bytes but try to always
* transfer multiples of endpoint buffer size
*/
return 1023 / mpck * mpck;
}
/* Do an USB transfer
*
* If we are in supervisor state, we poll for ikbd interrupts on the
* assumption that we were called from the timer interrupt (via etv_timer)
* and thus are running with interrupts disabled. This will happen when
* called by a USB mouse or keyboard driver.
*/
static long
isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
long dir, void *buffer, long len, long flags)
{
struct isp116x *isp116x = &isp116x_dev;
long type = usb_pipetype(pipe);
long epnum = usb_pipeendpoint(pipe);
long max = usb_maxpacket(dev, pipe);
long dir_out = usb_pipeout(pipe);
long speed_low = usb_pipeslow(pipe);
long i, done = 0, stat, timeout, cc;
/*
* For non-interrupt transfers, if the function is busy and we receive a NAK,
* we retry up to 500 frames (0.5s timeout).
* For interrupt transfers (e.g. mouse/keyboard), we expect to receive a NAK
* most of the time. So we don't retry, we just report an error and let the
* upper level driver retry the transfer at regular intervals.
*/
short retries = ((type==PIPE_INTERRUPT) || (flags&USB_BULK_FLAG_EARLY_TIMEOUT)) ? 0 : 500;
short set_extra_delay = 0;
short poll_interrupts = 0;
DEBUG(("------------------------------------------------"));
dump_msg(dev, pipe, buffer, len, "SUBMIT");
DEBUG(("------------------------------------------------"));
/*
* set flag if we need to poll for ikbd interrupts
*/
if (Super(1L))
poll_interrupts = 1;
if (poll_interrupts && ikbd_int_pending())
fake_ikbd_int();
dev->act_len = 0L; /* for safety, init bytes transferred */
if (len >= 1024)
{
ALERT(("Too big job"));
dev->status = USB_ST_CRC_ERR;
return -1;
}
if (isp116x->disabled)
{
ALERT(("EPIPE"));
dev->status = USB_ST_CRC_ERR;
return -1;
}
/* device pulled? Shortcut the action. */
if (devgone == dev)
{
ALERT(("ENODEV"));
dev->status = USB_ST_CRC_ERR;
return USB_ST_CRC_ERR;
}
if (!max)
{
ALERT(("pipesize for pipe %lx is zero", pipe));
dev->status = USB_ST_CRC_ERR;
return -1;
}
if (type == PIPE_ISOCHRONOUS)
{
ALERT(("isochronous transfers not supported"));
dev->status = USB_ST_CRC_ERR;
return -1;
}
/* Another job in progress */
if (!lock_usb(&job_in_progress))
{
DEBUG(("Another USB job in progress -- must not happen"));
dev->status = USB_ST_BUF_ERR;
return -1;
}
/* FIFO not empty? */
if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL)
{
DEBUG(("****** FIFO not empty! ******"));
dev->status = USB_ST_BUF_ERR;
unlock_usb(&job_in_progress);
return -1;
}
retry:
isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
/* Prepare the PTD data */
ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
/*
* Setting the B5_5 bit below limits interrupt transfers to one per frame.
* If this bit is NOT set, interrupt transfers by the ISP1160 violate the
* USB standard and some hardware will malfunction (information courtesy
* of Christian Zietz).
*/
ptd->len = PTD_LEN(len) | PTD_DIR(dir) | PTD_B5_5(type == PIPE_INTERRUPT);
ptd->faddr = PTD_FA(usb_pipedevice(pipe));
retry_same:
/* FIFO not empty? */