summaryrefslogtreecommitdiffstats
path: root/drivers/usb/isp116x-hcd.c
blob: ac6703056c901d6be2e4d3be6fd03156a3bfb6ce (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
/*
 * ISP116x HCD (Host Controller Driver) for u-boot.
 *
 * Copyright (C) 2006-2007 Rodolfo Giometti <giometti@linux.it>
 * Copyright (C) 2006-2007 Eurotech S.p.A. <info@eurotech.it>
 *
 * 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.
 *
 * This program 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, wd@denx.de.
 *
 *    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 <pbl@cypress.com>
 *         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 <gj@denx.de>
 *
 *    [[GNU/GPL disclaimer]]
 *
 *    Note: Part of this code has been derived from linux
 */

#include <common.h>

#ifdef CONFIG_USB_ISP116X_HCD
#include <asm/io.h>
#include <usb.h>
#include <malloc.h>
#include <linux/list.h>

/*
 * ISP116x chips require certain delays between accesses to its
 * registers. The following timing options exist.
 *
 * 1. Configure your memory controller (the best)
 * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
 *
 * Value is in microseconds.
 */
#ifdef ISP116X_HCD_USE_UDELAY
#define UDELAY		1
#endif

/*
 * On some (slowly?) machines an extra delay after data packing into
 * controller's FIFOs is required, * otherwise you may get the following
 * error:
 *
 *   uboot> usb start
 *   (Re)start USB...
 *   USB:   scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
 *   isp116x: isp116x_submit_job: ****** FIFO not ready! ******
 *
 *         USB device not responding, giving up (status=4)
 *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
 *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
 *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
 *         3 USB Device(s) found
 *                scanning bus for storage devices... 0 Storage Device(s) found
 *
 * Value is in milliseconds.
 */
#ifdef ISP116X_HCD_USE_EXTRA_DELAY
#define EXTRA_DELAY	2
#endif

/*
 * Enable the following defines if you wish enable debugging messages.
 */
#undef DEBUG			/* enable debugging messages */
#undef TRACE			/* enable tracing code */
#undef VERBOSE			/* verbose debugging messages */

#include "isp116x.h"

#define DRIVER_VERSION	"08 Jan 2007"
static const char hcd_name[] = "isp116x-hcd";

struct isp116x isp116x_dev;
struct isp116x_platform_data isp116x_board;
static int got_rhsc;		/* root hub status change */
struct usb_device *devgone;	/* device which was disconnected */
static int rh_devnum;		/* address of Root Hub endpoint */

/* ------------------------------------------------------------------------- */

#define ALIGN(x,a)	(((x)+(a)-1UL)&~((a)-1UL))
#define min_t(type,x,y)	\
	({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })

/* ------------------------------------------------------------------------- */

static int isp116x_reset(struct isp116x *isp116x);

/* --- Debugging functions ------------------------------------------------- */

#define isp116x_show_reg(d, r) {				\
	if ((r) < 0x20) {					\
		DBG("%-12s[%02x]: %08x", #r,			\
			r, isp116x_read_reg32(d, r));		\
	} else {						\
		DBG("%-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)

static int 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,
		     int len, char *str)
{
#if defined(VERBOSE)
	int i;
#endif

	DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#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)
	if (len > 0 && buffer) {
		printf(__FILE__ ": data(%d):", len);
		for (i = 0; i < 16 && i < len; i++)
			printf(" %02x", ((__u8 *) buffer)[i]);
		printf("%s\n", i < len ? "..." : "");
	}
#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(struct ptd *ptd)
{
#if defined(VERBOSE)
	int k;
#endif

	DBG("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)
	printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
	for (k = 0; k < sizeof(struct ptd); ++k)
		printf("%02x ", ((u8 *) ptd)[k]);
	printf("\n");
#endif
}

static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
{
#if defined(VERBOSE)
	int k;

	if (type == 0 /* 0ut data */ ) {
		printf("isp116x: %s: out data: ", __FUNCTION__);
		for (k = 0; k < PTD_GET_LEN(ptd); ++k)
			printf("%02x ", ((u8 *) buf)[k]);
		printf("\n");
	}
	if (type == 1 /* 1n data */ ) {
		printf("isp116x: %s: in data: ", __FUNCTION__);
		for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
			printf("%02x ", ((u8 *) buf)[k]);
		printf("\n");
	}

	if (PTD_GET_LAST(ptd))
		DBG("--- 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

/* --- Virtual Root Hub ---------------------------------------------------- */

/* Device descriptor */
static __u8 root_hub_dev_des[] = {
	0x12,			/*  __u8  bLength; */
	0x01,			/*  __u8  bDescriptorType; Device */
	0x10,			/*  __u16 bcdUSB; v1.1 */
	0x01,
	0x09,			/*  __u8  bDeviceClass; HUB_CLASSCODE */
	0x00,			/*  __u8  bDeviceSubClass; */
	0x00,			/*  __u8  bDeviceProtocol; */
	0x08,			/*  __u8  bMaxPacketSize0; 8 Bytes */
	0x00,			/*  __u16 idVendor; */
	0x00,
	0x00,			/*  __u16 idProduct; */
	0x00,
	0x00,			/*  __u16 bcdDevice; */
	0x00,
	0x00,			/*  __u8  iManufacturer; */
	0x01,			/*  __u8  iProduct; */
	0x00,			/*  __u8  iSerialNumber; */
	0x01			/*  __u8  bNumConfigurations; */
};

/* Configuration descriptor */
static __u8 root_hub_config_des[] = {
	0x09,			/*  __u8  bLength; */
	0x02,			/*  __u8  bDescriptorType; Configuration */
	0x19,			/*  __u16 wTotalLength; */
	0x00,
	0x01,			/*  __u8  bNumInterfaces; */
	0x01,			/*  __u8  bConfigurationValue; */
	0x00,			/*  __u8  iConfiguration; */
	0x40,			/*  __u8  bmAttributes;
				   Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
	0x00,			/*  __u8  MaxPower; */

	/* interface */
	0x09,			/*  __u8  if_bLength; */
	0x04,			/*  __u8  if_bDescriptorType; Interface */
	0x00,			/*  __u8  if_bInterfaceNumber; */
	0x00,			/*  __u8  if_bAlternateSetting; */
	0x01,			/*  __u8  if_bNumEndpoints; */
	0x09,			/*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
	0x00,			/*  __u8  if_bInterfaceSubClass; */
	0x00,			/*  __u8  if_bInterfaceProtocol; */
	0x00,			/*  __u8  if_iInterface; */

	/* endpoint */
	0x07,			/*  __u8  ep_bLength; */
	0x05,			/*  __u8  ep_bDescriptorType; Endpoint */
	0x81,			/*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
	0x03,			/*  __u8  ep_bmAttributes; Interrupt */
	0x00,			/*  __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
	0x02,
	0xff			/*  __u8  ep_bInterval; 255 ms */
};

static unsigned char root_hub_str_index0[] = {
	0x04,			/*  __u8  bLength; */
	0x03,			/*  __u8  bDescriptorType; String-descriptor */
	0x09,			/*  __u8  lang ID */
	0x04,			/*  __u8  lang ID */
};

static unsigned char root_hub_str_index1[] = {
	0x22,			/*  __u8  bLength; */
	0x03,			/*  __u8  bDescriptorType; String-descriptor */
	'I',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'S',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'P',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'1',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'1',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'6',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'x',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	' ',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'R',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'o',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'o',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	't',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	' ',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'H',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'u',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
	'b',			/*  __u8  Unicode */
	0,			/*  __u8  Unicode */
};

/*
 * Hub class-specific descriptor is constructed dynamically
 */

/* --- Virtual root hub management functions ------------------------------- */

static int rh_check_port_status(struct isp116x *isp116x)
{
	u32 temp, ndp, i;
	int res;

	res = -1;
	temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
	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 --------------------------------------------- */

/* Write len bytes to fifo, pad till 32-bit boundary
 */
static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
{
	u8 *dp = (u8 *) buf;
	u16 *dp2 = (u16 *) buf;
	u16 w;
	int quot = len % 4;

	if ((unsigned long)dp2 & 1) {
		/* not aligned */
		for (; len > 1; len -= 2) {
			w = *dp++;
			w |= *dp++ << 8;
			isp116x_raw_write_data16(isp116x, w);
		}
		if (len)
			isp116x_write_data16(isp116x, (u16) * dp);
	} else {
		/* aligned */
		for (; len > 1; len -= 2)
			isp116x_raw_write_data16(isp116x, *dp2++);
		if (len)
			isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
	}
	if (quot == 1 || quot == 2)
		isp116x_raw_write_data16(isp116x, 0);
}

/* Read len bytes from fifo and then read till 32-bit boundary
 */
static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
{
	u8 *dp = (u8 *) buf;
	u16 *dp2 = (u16 *) buf;
	u16 w;
	int quot = len % 4;

	if ((unsigned long)dp2 & 1) {
		/* not aligned */
		for (; len > 1; len -= 2) {
			w = isp116x_raw_read_data16(isp116x);
			*dp++ = w & 0xff;
			*dp++ = (w >> 8) & 0xff;
		}
		if (len)
			*dp = 0xff & isp116x_read_data16(isp116x);
	} else {
		/* aligned */
		for (; len > 1; len -= 2)
			*dp2++ = isp116x_raw_read_data16(isp116x);
		if (len)
			*(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
	}
	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, struct ptd *ptd, int n, void *data,
		      int len)
{
	int buflen = n * sizeof(struct ptd) + len;
	int i, done;

	DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);

	isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
	isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
	isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);

	done = 0;
	for (i = 0; i < n; i++) {
		DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));

		dump_ptd(&ptd[i]);
		isp116x_write_data16(isp116x, ptd[i].count);
		isp116x_write_data16(isp116x, ptd[i].mps);
		isp116x_write_data16(isp116x, ptd[i].len);
		isp116x_write_data16(isp116x, ptd[i].faddr);

		dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
		write_ptddata_to_fifo(isp116x,
				      (__u8 *) data + done,
				      PTD_GET_LEN(&ptd[i]));

		done += PTD_GET_LEN(&ptd[i]);
	}
}

/* Read the processed PTD's and data from fifo ram back to URBs' buffers.
 * Fifo must be full and done */
static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
		       unsigned long pipe, struct ptd *ptd, int n, void *data,
		       int len)
{
	int buflen = n * sizeof(struct ptd) + len;
	int i, done, cc, ret;

	isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
	isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
	isp116x_write_addr(isp116x, HCATLPORT);

	ret = TD_CC_NOERROR;
	done = 0;
	for (i = 0; i < n; i++) {
		DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));

		ptd[i].count = isp116x_read_data16(isp116x);
		ptd[i].mps = isp116x_read_data16(isp116x);
		ptd[i].len = isp116x_read_data16(isp116x);
		ptd[i].faddr = isp116x_read_data16(isp116x);
		dump_ptd(&ptd[i]);

		read_ptddata_from_fifo(isp116x,
				       (__u8 *) data + done,
				       PTD_GET_LEN(&ptd[i]));
		dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);

		done += PTD_GET_LEN(&ptd[i]);

		cc = PTD_GET_CC(&ptd[i]);

		/* 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;
	}

	DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);

	return ret;
}

/* Interrupt handling
 */
static int isp116x_interrupt(struct isp116x *isp116x)
{
	u16 irqstat;
	u32 intstat;
	int ret = 0;

	isp116x_write_reg16(isp116x, HCuPINTENB, 0);
	irqstat = isp116x_read_reg16(isp116x, HCuPINT);
	isp116x_write_reg16(isp116x, HCuPINT, irqstat);
	DBG(">>>>>> irqstat %x <<<<<<", irqstat);

	if (irqstat & HCuPINT_ATL) {
		DBG(">>>>>> HCuPINT_ATL <<<<<<");
		udelay(500);
		ret = 1;
	}

	if (irqstat & HCuPINT_OPR) {
		intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
		isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
		DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);

		if (intstat & HCINT_UE) {
			ERR("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. */
			wait_ms(20);
		}

		if (intstat & HCINT_SO) {
			ERR("schedule overrun");
			ret = -1;
		}

		irqstat &= ~HCuPINT_OPR;
	}

	return ret;
}

/* With one PTD we can transfer almost 1K in one go;
 * HC does the splitting into endpoint digestible transactions
 */
struct ptd ptd[1];

static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
{
	unsigned 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
 */
static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
			      int dir, void *buffer, int len)
{
	struct isp116x *isp116x = &isp116x_dev;
	int type = usb_pipetype(pipe);
	int epnum = usb_pipeendpoint(pipe);
	int max = usb_maxpacket(dev, pipe);
	int dir_out = usb_pipeout(pipe);
	int speed_low = usb_pipeslow(pipe);
	int i, done = 0, stat, timeout, cc;

	/* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
	int retries = 500;

	DBG("------------------------------------------------");
	dump_msg(dev, pipe, buffer, len, "SUBMIT");
	DBG("------------------------------------------------");

	if (len >= 1024) {
		ERR("Too big job");
		dev->status = USB_ST_CRC_ERR;
		return -1;
	}

	if (isp116x->disabled) {
		ERR("EPIPE");
		dev->status = USB_ST_CRC_ERR;
		return -1;
	}

	/* device pulled? Shortcut the action. */
	if (devgone == dev) {
		ERR("ENODEV");
		dev->status = USB_ST_CRC_ERR;
		return USB_ST_CRC_ERR;
	}

	if (!max) {
		ERR("pipesize for pipe %lx is zero", pipe);
		dev->status = USB_ST_CRC_ERR;
		return -1;
	}

	if (type == PIPE_ISOCHRONOUS) {
		ERR("isochronous transfers not supported");
		dev->status = USB_ST_CRC_ERR;
		return -1;
	}

	/* FIFO not empty? */
	if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
		ERR("****** FIFO not empty! ******");
		dev->status = USB_ST_BUF_ERR;
		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;
	ptd->len = PTD_LEN(len) | PTD_DIR(dir);
	ptd->faddr = PTD_FA(usb_pipedevice(pipe));

retry_same:
	/* Pack data into FIFO ram */
	pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
#ifdef EXTRA_DELAY
	wait_ms(EXTRA_DELAY);
#endif

	/* Start the data transfer */

	/* Allow more time for a BULK device to react - some are slow */
	if (usb_pipetype(pipe) == PIPE_BULK)
		timeout = 5000;
	else
		timeout = 100;

	/* Wait for it to complete */
	for (;;) {
		/* Check whether the controller is done */
		stat = isp116x_interrupt(isp116x);

		if (stat < 0) {
			dev->status = USB_ST_CRC_ERR;
			break;
		}
		if (stat > 0)
			break;

		/* Check the timeout */
		if (--timeout)
			udelay(1);
		else {
			ERR("CTL:TIMEOUT ");
			stat = USB_ST_CRC_ERR;
			break;
		}
	}

	/* We got an Root Hub Status Change interrupt */
	if (got_rhsc) {
		isp116x_show_regs(isp116x);

		got_rhsc = 0;

		/* Abuse timeout */
		timeout = rh_check_port_status(isp116x);
		if (timeout >= 0) {
			/*
			 * FIXME! NOTE! AAAARGH!
			 * This is potentially dangerous because it assumes
			 * that only one device is ever plugged in!
			 */
			devgone = dev;
		}
	}

	/* Ok, now we can read transfer status */

	/* FIFO not ready? */
	if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
		ERR("****** FIFO not ready! ******");
		dev->status = USB_ST_BUF_ERR;
		return -1;
	}

	/* Unpack data from FIFO ram */
	cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);

	i = PTD_GET_COUNT(ptd);
	done += i;
	buffer += i;
	len -= i;

	/* There was some kind of real problem; Prepare the PTD again
	 * and retry from the failed transaction on
	 */
	if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
		if (retries >= 100) {
			retries -= 100;
			/* The chip will have toggled the toggle bit for the failed
			 * transaction too. We have to toggle it back.
			 */
			usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
			goto retry;
		}
	}
	/* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
	 * the transactions from the first on for the whole frame. It may be busy and we retry
	 * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
	 * PTD didn't make it because the function was busy or the frame ended before the PTD
	 * finished. We prepare the rest of the data and try again.
	 */
	else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
		if (retries) {
			--retries;
			if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
			usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
			goto retry;
		}
	}

	if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
		DBG("****** completition code error %x ******", cc);
		switch (cc) {
		case TD_CC_BITSTUFFING:
			dev->status = USB_ST_BIT_ERR;
			break;
		case TD_CC_STALL:
			dev->status = USB_ST_STALLED;
			break;
		case TD_BUFFEROVERRUN:
		case TD_BUFFERUNDERRUN:
			dev->status = USB_ST_BUF_ERR;
			break;
		default:
			dev->status = USB_ST_CRC_ERR;
		}
		return -cc;
	}
	else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));

	dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");

	dev->status = 0;
	return done;
}

/* Adapted from au1x00_usb_ohci.c
 */
static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
				 void *buffer, int transfer_len,
				 struct devrequest *cmd)
{
	struct isp116x *isp116x = &isp116x_dev;
	u32 tmp = 0;

	int leni = transfer_len;
	int len = 0;
	int stat = 0;
	u32 datab[4];
	u8 *data_buf = (u8 *) datab;
	u16 bmRType_bReq;
	u16 wValue;
	u16 wIndex;
	u16 wLength;

	if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
		INFO("Root-Hub submit IRQ: NOT implemented");
		return 0;
	}

	bmRType_bReq = cmd->requesttype | (cmd->request << 8);
	wValue = swap_16(cmd->value);
	wIndex = swap_16(cmd->index);
	wLength = swap_16(cmd->length);

	DBG("--- HUB ----------------------------------------");
	DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
	    bmRType_bReq, wValue, wIndex, wLength);
	dump_msg(dev, pipe, buffer, transfer_len, "RH");
	DBG("------------------------------------------------");

	switch (bmRType_bReq) {
	case RH_GET_STATUS:
		DBG("RH_GET_STATUS");

		*(__u16 *) data_buf = swap_16(1);
		len = 2;
		break;

	case RH_GET_STATUS | RH_INTERFACE:
		DBG("RH_GET_STATUS | RH_INTERFACE");

		*(__u16 *) data_buf = swap_16(0);
		len = 2;
		break;

	case RH_GET_STATUS | RH_ENDPOINT:
		DBG("RH_GET_STATUS | RH_ENDPOINT");

		*(__u16 *) data_buf = swap_16(0);
		len = 2;
		break;

	case RH_GET_STATUS | RH_CLASS:
		DBG("RH_GET_STATUS | RH_CLASS");

		tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);

		*(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
		len = 4;
		break;

	case RH_GET_STATUS | RH_OTHER | RH_CLASS:
		DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");

		tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
		*(__u32 *) data_buf = swap_32(tmp);
		isp116x_show_regs(isp116x);
		len = 4;
		break;

	case RH_CLEAR_FEATURE | RH_ENDPOINT:
		DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");

		switch (wValue) {
		case RH_ENDPOINT_STALL:
			DBG("C_HUB_ENDPOINT_STALL");
			len = 0;
			break;
		}
		break;

	case RH_CLEAR_FEATURE | RH_CLASS:
		DBG("RH_CLEAR_FEATURE | RH_CLASS");

		switch (wValue) {
		case RH_C_HUB_LOCAL_POWER:
			DBG("C_HUB_LOCAL_POWER");
			len = 0;
			break;

		case RH_C_HUB_OVER_CURRENT:
			DBG("C_HUB_OVER_CURRENT");
			isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
			len = 0;
			break;
		}
		break;

	case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
		DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");

		switch (wValue) {
		case RH_PORT_ENABLE:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_CCS);
			len = 0;
			break;

		case RH_PORT_SUSPEND:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_POCI);
			len = 0;
			break;

		case RH_PORT_POWER:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_LSDA);
			len = 0;
			break;

		case RH_C_PORT_CONNECTION:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_CSC);
			len = 0;
			break;

		case RH_C_PORT_ENABLE:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PESC);
			len = 0;
			break;

		case RH_C_PORT_SUSPEND:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PSSC);
			len = 0;
			break;

		case RH_C_PORT_OVER_CURRENT:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_POCI);
			len = 0;
			break;

		case RH_C_PORT_RESET:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PRSC);
			len = 0;
			break;

		default:
			ERR("invalid wValue");
			stat = USB_ST_STALLED;
		}

		isp116x_show_regs(isp116x);

		break;

	case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
		DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");

		switch (wValue) {
		case RH_PORT_SUSPEND:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PSS);
			len = 0;
			break;

		case RH_PORT_RESET:
			/* Spin until any current reset finishes */
			while (1) {
				tmp =
				    isp116x_read_reg32(isp116x,
						       HCRHPORT1 + wIndex - 1);
				if (!(tmp & RH_PS_PRS))
					break;
				wait_ms(1);
			}
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PRS);
			wait_ms(10);

			len = 0;
			break;

		case RH_PORT_POWER:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PPS);
			len = 0;
			break;

		case RH_PORT_ENABLE:
			isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
					    RH_PS_PES);
			len = 0;
			break;

		default:
			ERR("invalid wValue");
			stat = USB_ST_STALLED;
		}

		isp116x_show_regs(isp116x);

		break;

	case RH_SET_ADDRESS:
		DBG("RH_SET_ADDRESS");

		rh_devnum = wValue;
		len = 0;
		break;

	case RH_GET_DESCRIPTOR:
		DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);

		switch (wValue) {
		case (USB_DT_DEVICE << 8):	/* device descriptor */
			len = min_t(unsigned int,
				    leni, min_t(unsigned int,
						sizeof(root_hub_dev_des),
						wLength));
			data_buf = root_hub_dev_des;
			break;

		case (USB_DT_CONFIG << 8):	/* configuration descriptor */
			len = min_t(unsigned int,
				    leni, min_t(unsigned int,
						sizeof(root_hub_config_des),
						wLength));
			data_buf = root_hub_config_des;
			break;

		case ((USB_DT_STRING << 8) | 0x00):	/* string 0 descriptors */
			len = min_t(unsigned int,
				    leni, min_t(unsigned int,
						sizeof(root_hub_str_index0),
						wLength));
			data_buf = root_hub_str_index0;
			break;

		case ((USB_DT_STRING << 8) | 0x01):	/* string 1 descriptors */
			len = min_t(unsigned int,
				    leni, min_t(unsigned int,
						sizeof(root_hub_str_index1),
						wLength));
			data_buf = root_hub_str_index1;
			break;

		default:
			ERR("invalid wValue");
			stat = USB_ST_STALLED;
		}

		break;

	case RH_GET_DESCRIPTOR | RH_CLASS:
		DBG("RH_GET_DESCRIPTOR | RH_CLASS");

		tmp = isp116x_read_reg32(isp116x, HCRHDESCA);

		data_buf[0] = 0x09;	/* min length; */
		data_buf[1] = 0x29;
		data_buf[2] = tmp & RH_A_NDP;
		data_buf[3] = 0;
		if (tmp & RH_A_PSM)	/* per-port power switching? */
			data_buf[3] |= 0x01;
		if (tmp & RH_A_NOCP)	/* no overcurrent reporting? */
			data_buf[3] |= 0x10;
		else if (tmp & RH_A_OCPM)	/* per-port overcurrent rep? */
			data_buf[3] |= 0x08;

		/* Corresponds to data_buf[4-7] */
		datab[1] = 0;
		data_buf[5] = (tmp & RH_A_POTPGT) >> 24;

		tmp = isp116x_read_reg32(isp116x, HCRHDESCB);

		data_buf[7] = tmp & RH_B_DR;
		if (data_buf[2] < 7)
			data_buf[8] = 0xff;
		else {
			data_buf[0] += 2;
			data_buf[8] = (tmp & RH_B_DR) >> 8;
			data_buf[10] = data_buf[9] = 0xff;
		}

		len = min_t(unsigned int, leni,
			    min_t(unsigned int, data_buf[0], wLength));
		break;

	case RH_GET_CONFIGURATION:
		DBG("RH_GET_CONFIGURATION");

		*(__u8 *) data_buf = 0x01;
		len = 1;
		break;

	case RH_SET_CONFIGURATION:
		DBG("RH_SET_CONFIGURATION");

		isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
		len = 0;
		break;

	default:
		ERR("*** *** *** unsupported root hub command *** *** ***");
		stat = USB_ST_STALLED;
	}

	len = min_t(int, len, leni);
	if (buffer != data_buf)
		memcpy(buffer, data_buf, len);

	dev->act_len = len;
	dev->status = stat;
	DBG("dev act_len %d, status %d", dev->act_len, dev->status);

	dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");

	return stat;
}

/* --- Transfer functions -------------------------------------------------- */

int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		   int len, int interval)
{
	DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
	    dev, pipe, buffer, len, interval);

	return -1;
}

int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		       int len, struct devrequest *setup)
{
	int devnum = usb_pipedevice(pipe);
	int epnum = usb_pipeendpoint(pipe);
	int max = max_transfer_len(dev, pipe);
	int dir_in = usb_pipein(pipe);
	int done, ret;

	/* Control message is for the HUB? */
	if (devnum == rh_devnum)
		return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);

	/* Ok, no HUB message so send the message to the device */

	/* Setup phase */
	DBG("--- SETUP PHASE --------------------------------");
	usb_settoggle(dev, epnum, 1, 0);
	ret = isp116x_submit_job(dev, pipe,
				 PTD_DIR_SETUP,
				 setup, sizeof(struct devrequest));
	if (ret < 0) {
		DBG("control setup phase error (ret = %d", ret);
		return -1;
	}

	/* Data phase */
	DBG("--- DATA PHASE ---------------------------------");
	done = 0;
	usb_settoggle(dev, epnum, !dir_in, 1);
	while (done < len) {
		ret = isp116x_submit_job(dev, pipe,
					 dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
					 (__u8 *) buffer + done,
					 max > len - done ? len - done : max);
		if (ret < 0) {
			DBG("control data phase error (ret = %d)", ret);
			return -1;
		}
		done += ret;

		if (dir_in && ret < max)	/* short packet */
			break;
	}

	/* Status phase */
	DBG("--- STATUS PHASE -------------------------------");
	usb_settoggle(dev, epnum, !dir_in, 1);
	ret = isp116x_submit_job(dev, pipe,
				 !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
	if (ret < 0) {
		DBG("control status phase error (ret = %d", ret);
		return -1;
	}

	dev->act_len = done;

	dump_msg(dev, pipe, buffer, len, "DEV(ret)");

	return done;
}

int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
		    int len)
{
	int dir_out = usb_pipeout(pipe);
	int max = max_transfer_len(dev, pipe);
	int done, ret;

	DBG("--- BULK ---------------------------------------");
	DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
	    usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);

	done = 0;
	while (done < len) {
		ret = isp116x_submit_job(dev, pipe,
					 !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
					 (__u8 *) buffer + done,
					 max > len - done ? len - done : max);
		if (ret < 0) {
			DBG("error on bulk message (ret = %d)", ret);
			return -1;
		}

		done += ret;

		if (!dir_out && ret < max)	/* short packet */
			break;
	}

	dev->act_len = done;

	return 0;
}

/* --- Basic functions ----------------------------------------------------- */

static int isp116x_sw_reset(struct isp116x *isp116x)
{
	int retries = 15;
	int ret = 0;

	DBG("");

	isp116x->disabled = 1;

	isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
	isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
	while (--retries) {
		/* It usually resets within 1 ms */
		wait_ms(1);
		if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
			break;
	}
	if (!retries) {
		ERR("software reset timeout");
		ret = -1;
	}
	return ret;
}

static int isp116x_reset(struct isp116x *isp116x)
{
	unsigned long t;
	u16 clkrdy = 0;
	int ret, timeout = 15 /* ms */ ;

	DBG("");

	ret = isp116x_sw_reset(isp116x);
	if (ret)
		return ret;

	for (t = 0; t < timeout; t++) {
		clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
		if (clkrdy)
			break;
		wait_ms(1);
	}
	if (!clkrdy) {
		ERR("clock not ready after %dms", timeout);
		/* After sw_reset the clock won't report to be ready, if
		   H_WAKEUP pin is high. */
		ERR("please make sure that the H_WAKEUP pin is pulled low!");
		ret = -1;
	}
	return ret;
}

static void isp116x_stop(struct isp116x *isp116x)
{
	u32 val;

	DBG("");

	isp116x_write_reg16(isp116x, HCuPINTENB, 0);

	/* Switch off ports' power, some devices don't come up
	   after next 'start' without this */
	val = isp116x_read_reg32(isp116x, HCRHDESCA);
	val &= ~(RH_A_NPS | RH_A_PSM);
	isp116x_write_reg32(isp116x, HCRHDESCA, val);
	isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);

	isp116x_sw_reset(isp116x);
}

/*
 *  Configure the chip. The chip must be successfully reset by now.
 */
static int isp116x_start(struct isp116x *isp116x)
{
	struct isp116x_platform_data *board = isp116x->board;
	u32 val;

	DBG("");

	/* Clear interrupt status and disable all interrupt sources */
	isp116x_write_reg16(isp116x, HCuPINT, 0xff);
	isp116x_write_reg16(isp116x, HCuPINTENB, 0);

	isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
	isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);

	/* Hardware configuration */
	val = HCHWCFG_DBWIDTH(1);
	if (board->sel15Kres)
		val |= HCHWCFG_15KRSEL;
	/* Remote wakeup won't work without working clock */
	if (board->remote_wakeup_enable)
		val |= HCHWCFG_CLKNOTSTOP;
	if (board->oc_enable)
		val |= HCHWCFG_ANALOG_OC;
	isp116x_write_reg16(isp116x, HCHWCFG, val);

	/* --- Root hub configuration */
	val = (25 << 24) & RH_A_POTPGT;
	/* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
	   be always set. Yet, instead, we request individual port
	   power switching. */
	val |= RH_A_PSM;
	/* Report overcurrent per port */
	val |= RH_A_OCPM;
	isp116x_write_reg32(isp116x, HCRHDESCA, val);
	isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);

	val = RH_B_PPCM;
	isp116x_write_reg32(isp116x, HCRHDESCB, val);
	isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);

	val = 0;
	if (board->remote_wakeup_enable)
		val |= RH_HS_DRWE;
	isp116x_write_reg32(isp116x, HCRHSTATUS, val);
	isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);

	isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);

	/* Go operational */
	val = HCCONTROL_USB_OPER;
	if (board->remote_wakeup_enable)
		val |= HCCONTROL_RWE;
	isp116x_write_reg32(isp116x, HCCONTROL, val);

	/* Disable ports to avoid race in device enumeration */
	isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
	isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);

	isp116x_show_regs(isp116x);

	isp116x->disabled = 0;

	return 0;
}

/* --- Init functions ------------------------------------------------------ */

int isp116x_check_id(struct isp116x *isp116x)
{
	int val;

	val = isp116x_read_reg16(isp116x, HCCHIPID);
	if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
		ERR("invalid chip ID %04x", val);
		return -1;
	}

	return 0;
}

int usb_lowlevel_init(void)
{
	struct isp116x *isp116x = &isp116x_dev;

	DBG("");

	got_rhsc = rh_devnum = 0;

	/* Init device registers addr */
	isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
	isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;

	/* Setup specific board settings */
#ifdef ISP116X_HCD_SEL15kRES
	isp116x_board.sel15Kres = 1;
#endif
#ifdef ISP116X_HCD_OC_ENABLE
	isp116x_board.oc_enable = 1;
#endif
#ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
	isp116x_board.remote_wakeup_enable = 1;
#endif
	isp116x->board = &isp116x_board;

	/* Try to get ISP116x silicon chip ID */
	if (isp116x_check_id(isp116x) < 0)
		return -1;

	isp116x->disabled = 1;
	isp116x->sleeping = 0;

	isp116x_reset(isp116x);
	isp116x_start(isp116x);

	return 0;
}

int usb_lowlevel_stop(void)
{
	struct isp116x *isp116x = &isp116x_dev;

	DBG("");

	if (!isp116x->disabled)
		isp116x_stop(isp116x);

	return 0;
}

#endif				/* CONFIG_USB_ISP116X_HCD */