aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/zd1201.c
blob: b45c27d42fd83f98ac6a5f3479b0251f40443e7b (plain) (blame)
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
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
/*
 *	Driver for ZyDAS zd1201 based wireless USB devices.
 *
 *	Copyright (c) 2004, 2005 Jeroen Vreeken (pe1rxq@amsat.org)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	version 2 as published by the Free Software Foundation.
 *
 *	Parts of this driver have been derived from a wlan-ng version
 *	modified by ZyDAS. They also made documentation available, thanks!
 *	Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
 */

#include <linux/module.h>
#include <linux/usb.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/wireless.h>
#include <linux/ieee80211.h>
#include <net/iw_handler.h>
#include <linux/string.h>
#include <linux/if_arp.h>
#include <linux/firmware.h>
#include "zd1201.h"

static struct usb_device_id zd1201_table[] = {
	{USB_DEVICE(0x0586, 0x3400)}, /* Peabird Wireless USB Adapter */
	{USB_DEVICE(0x0ace, 0x1201)}, /* ZyDAS ZD1201 Wireless USB Adapter */
	{USB_DEVICE(0x050d, 0x6051)}, /* Belkin F5D6051 usb  adapter */
	{USB_DEVICE(0x0db0, 0x6823)}, /* MSI UB11B usb  adapter */
	{USB_DEVICE(0x1044, 0x8005)}, /* GIGABYTE GN-WLBZ201 usb adapter */
	{}
};

static int ap;	/* Are we an AP or a normal station? */

#define ZD1201_VERSION	"0.15"

MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
MODULE_DESCRIPTION("Driver for ZyDAS ZD1201 based USB Wireless adapters");
MODULE_VERSION(ZD1201_VERSION);
MODULE_LICENSE("GPL");
module_param(ap, int, 0);
MODULE_PARM_DESC(ap, "If non-zero Access Point firmware will be loaded");
MODULE_DEVICE_TABLE(usb, zd1201_table);


static int zd1201_fw_upload(struct usb_device *dev, int apfw)
{
	const struct firmware *fw_entry;
	const char *data;
	unsigned long len;
	int err;
	unsigned char ret;
	char *buf;
	char *fwfile;

	if (apfw)
		fwfile = "zd1201-ap.fw";
	else
		fwfile = "zd1201.fw";

	err = request_firmware(&fw_entry, fwfile, &dev->dev);
	if (err) {
		dev_err(&dev->dev, "Failed to load %s firmware file!\n", fwfile);
		dev_err(&dev->dev, "Make sure the hotplug firmware loader is installed.\n");
		dev_err(&dev->dev, "Goto http://linux-lc100020.sourceforge.net for more info.\n");
		return err;
	}

	data = fw_entry->data;
        len = fw_entry->size;

	buf = kmalloc(1024, GFP_ATOMIC);
	if (!buf)
		goto exit;
	
	while (len > 0) {
		int translen = (len > 1024) ? 1024 : len;
		memcpy(buf, data, translen);

		err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0,
		    USB_DIR_OUT | 0x40, 0, 0, buf, translen,
		    ZD1201_FW_TIMEOUT);
		if (err < 0)
			goto exit;

		len -= translen;
		data += translen;
	}
                                        
	err = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x2,
	    USB_DIR_OUT | 0x40, 0, 0, NULL, 0, ZD1201_FW_TIMEOUT);
	if (err < 0)
		goto exit;

	err = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x4,
	    USB_DIR_IN | 0x40, 0,0, &ret, sizeof(ret), ZD1201_FW_TIMEOUT);
	if (err < 0)
		goto exit;

	if (ret & 0x80) {
		err = -EIO;
		goto exit;
	}

	err = 0;
exit:
	kfree(buf);
	release_firmware(fw_entry);
	return err;
}

static void zd1201_usbfree(struct urb *urb)
{
	struct zd1201 *zd = urb->context;

	switch(urb->status) {
		case -EILSEQ:
		case -ENODEV:
		case -ETIME:
		case -ENOENT:
		case -EPIPE:
		case -EOVERFLOW:
		case -ESHUTDOWN:
			dev_warn(&zd->usb->dev, "%s: urb failed: %d\n", 
			    zd->dev->name, urb->status);
	}

	kfree(urb->transfer_buffer);
	usb_free_urb(urb);
	return;
}

/* cmdreq message: 
	u32 type
	u16 cmd
	u16 parm0
	u16 parm1
	u16 parm2
	u8  pad[4]

	total: 4 + 2 + 2 + 2 + 2 + 4 = 16
*/
static int zd1201_docmd(struct zd1201 *zd, int cmd, int parm0,
			int parm1, int parm2)
{
	unsigned char *command;
	int ret;
	struct urb *urb;

	command = kmalloc(16, GFP_ATOMIC);
	if (!command)
		return -ENOMEM;

	*((__le32*)command) = cpu_to_le32(ZD1201_USB_CMDREQ);
	*((__le16*)&command[4]) = cpu_to_le16(cmd);
	*((__le16*)&command[6]) = cpu_to_le16(parm0);
	*((__le16*)&command[8]) = cpu_to_le16(parm1);
	*((__le16*)&command[10])= cpu_to_le16(parm2);

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb) {
		kfree(command);
		return -ENOMEM;
	}
	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
			  command, 16, zd1201_usbfree, zd);
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret) {
		kfree(command);
		usb_free_urb(urb);
	}

	return ret;
}

/* Callback after sending out a packet */
static void zd1201_usbtx(struct urb *urb)
{
	struct zd1201 *zd = urb->context;
	netif_wake_queue(zd->dev);
	return;
}

/* Incoming data */
static void zd1201_usbrx(struct urb *urb)
{
	struct zd1201 *zd = urb->context;
	int free = 0;
	unsigned char *data = urb->transfer_buffer;
	struct sk_buff *skb;
	unsigned char type;

	if (!zd)
		return;

	switch(urb->status) {
		case -EILSEQ:
		case -ENODEV:
		case -ETIME:
		case -ENOENT:
		case -EPIPE:
		case -EOVERFLOW:
		case -ESHUTDOWN:
			dev_warn(&zd->usb->dev, "%s: rx urb failed: %d\n",
			    zd->dev->name, urb->status);
			free = 1;
			goto exit;
	}
	
	if (urb->status != 0 || urb->actual_length == 0)
		goto resubmit;

	type = data[0];
	if (type == ZD1201_PACKET_EVENTSTAT || type == ZD1201_PACKET_RESOURCE) {
		memcpy(zd->rxdata, data, urb->actual_length);
		zd->rxlen = urb->actual_length;
		zd->rxdatas = 1;
		wake_up(&zd->rxdataq);
	}
	/* Info frame */
	if (type == ZD1201_PACKET_INQUIRE) {
		int i = 0;
		unsigned short infotype, framelen, copylen;
		framelen = le16_to_cpu(*(__le16*)&data[4]);
		infotype = le16_to_cpu(*(__le16*)&data[6]);

		if (infotype == ZD1201_INF_LINKSTATUS) {
			short linkstatus;

			linkstatus = le16_to_cpu(*(__le16*)&data[8]);
			switch(linkstatus) {
				case 1:
					netif_carrier_on(zd->dev);
					break;
				case 2:
					netif_carrier_off(zd->dev);
					break;
				case 3:
					netif_carrier_off(zd->dev);
					break;
				case 4:
					netif_carrier_on(zd->dev);
					break;
				default:
					netif_carrier_off(zd->dev);
			}
			goto resubmit;
		}
		if (infotype == ZD1201_INF_ASSOCSTATUS) {
			short status = le16_to_cpu(*(__le16*)(data+8));
			int event;
			union iwreq_data wrqu;

			switch (status) {
				case ZD1201_ASSOCSTATUS_STAASSOC:
				case ZD1201_ASSOCSTATUS_REASSOC:
					event = IWEVREGISTERED;
					break;
				case ZD1201_ASSOCSTATUS_DISASSOC:
				case ZD1201_ASSOCSTATUS_ASSOCFAIL:
				case ZD1201_ASSOCSTATUS_AUTHFAIL:
				default:
					event = IWEVEXPIRED;
			}
			memcpy(wrqu.addr.sa_data, data+10, ETH_ALEN);
			wrqu.addr.sa_family = ARPHRD_ETHER;

			/* Send event to user space */
			wireless_send_event(zd->dev, event, &wrqu, NULL);

			goto resubmit;
		}
		if (infotype == ZD1201_INF_AUTHREQ) {
			union iwreq_data wrqu;

			memcpy(wrqu.addr.sa_data, data+8, ETH_ALEN);
			wrqu.addr.sa_family = ARPHRD_ETHER;
			/* There isn't a event that trully fits this request.
			   We assume that userspace will be smart enough to
			   see a new station being expired and sends back a
			   authstation ioctl to authorize it. */
			wireless_send_event(zd->dev, IWEVEXPIRED, &wrqu, NULL);
			goto resubmit;
		}
		/* Other infotypes are handled outside this handler */
		zd->rxlen = 0;
		while (i < urb->actual_length) {
			copylen = le16_to_cpu(*(__le16*)&data[i+2]);
			/* Sanity check, sometimes we get junk */
			if (copylen+zd->rxlen > sizeof(zd->rxdata))
				break;
			memcpy(zd->rxdata+zd->rxlen, data+i+4, copylen);
			zd->rxlen += copylen;
			i += 64;
		}
		if (i >= urb->actual_length) {
			zd->rxdatas = 1;
			wake_up(&zd->rxdataq);
		}
		goto  resubmit;
	}
	/* Actual data */
	if (data[urb->actual_length-1] == ZD1201_PACKET_RXDATA) {
		int datalen = urb->actual_length-1;
		unsigned short len, fc, seq;
		struct hlist_node *node;

		len = ntohs(*(__be16 *)&data[datalen-2]);
		if (len>datalen)
			len=datalen;
		fc = le16_to_cpu(*(__le16 *)&data[datalen-16]);
		seq = le16_to_cpu(*(__le16 *)&data[datalen-24]);

		if (zd->monitor) {
			if (datalen < 24)
				goto resubmit;
			if (!(skb = dev_alloc_skb(datalen+24)))
				goto resubmit;
			
			memcpy(skb_put(skb, 2), &data[datalen-16], 2);
			memcpy(skb_put(skb, 2), &data[datalen-2], 2);
			memcpy(skb_put(skb, 6), &data[datalen-14], 6);
			memcpy(skb_put(skb, 6), &data[datalen-22], 6);
			memcpy(skb_put(skb, 6), &data[datalen-8], 6);
			memcpy(skb_put(skb, 2), &data[datalen-24], 2);
			memcpy(skb_put(skb, len), data, len);
			skb->protocol = eth_type_trans(skb, zd->dev);
			zd->stats.rx_packets++;
			zd->stats.rx_bytes += skb->len;
			netif_rx(skb);
			goto resubmit;
		}
			
		if ((seq & IEEE80211_SCTL_FRAG) ||
		    (fc & IEEE80211_FCTL_MOREFRAGS)) {
			struct zd1201_frag *frag = NULL;
			char *ptr;

			if (datalen<14)
				goto resubmit;
			if ((seq & IEEE80211_SCTL_FRAG) == 0) {
				frag = kmalloc(sizeof(*frag), GFP_ATOMIC);
				if (!frag)
					goto resubmit;
				skb = dev_alloc_skb(IEEE80211_MAX_DATA_LEN +14+2);
				if (!skb) {
					kfree(frag);
					goto resubmit;
				}
				frag->skb = skb;
				frag->seq = seq & IEEE80211_SCTL_SEQ;
				skb_reserve(skb, 2);
				memcpy(skb_put(skb, 12), &data[datalen-14], 12);
				memcpy(skb_put(skb, 2), &data[6], 2);
				memcpy(skb_put(skb, len), data+8, len);
				hlist_add_head(&frag->fnode, &zd->fraglist);
				goto resubmit;
			}
			hlist_for_each_entry(frag, node, &zd->fraglist, fnode)
				if (frag->seq == (seq&IEEE80211_SCTL_SEQ))
					break;
			if (!frag)
				goto resubmit;
			skb = frag->skb;
			ptr = skb_put(skb, len);
			if (ptr)
				memcpy(ptr, data+8, len);
			if (fc & IEEE80211_FCTL_MOREFRAGS)
				goto resubmit;
			hlist_del_init(&frag->fnode);
			kfree(frag);
		} else {
			if (datalen<14)
				goto resubmit;
			skb = dev_alloc_skb(len + 14 + 2);
			if (!skb)
				goto resubmit;
			skb_reserve(skb, 2);
			memcpy(skb_put(skb, 12), &data[datalen-14], 12);
			memcpy(skb_put(skb, 2), &data[6], 2);
			memcpy(skb_put(skb, len), data+8, len);
		}
		skb->protocol = eth_type_trans(skb, zd->dev);
		zd->stats.rx_packets++;
		zd->stats.rx_bytes += skb->len;
		netif_rx(skb);
	}
resubmit:
	memset(data, 0, ZD1201_RXSIZE);

	urb->status = 0;
	urb->dev = zd->usb;
	if(usb_submit_urb(urb, GFP_ATOMIC))
		free = 1;

exit:
	if (free) {
		zd->rxlen = 0;
		zd->rxdatas = 1;
		wake_up(&zd->rxdataq);
		kfree(urb->transfer_buffer);
	}
	return;
}

static int zd1201_getconfig(struct zd1201 *zd, int rid, void *riddata,
	unsigned int riddatalen)
{
	int err;
	int i = 0;
	int code;
	int rid_fid;
	int length;
	unsigned char *pdata;

	zd->rxdatas = 0;
	err = zd1201_docmd(zd, ZD1201_CMDCODE_ACCESS, rid, 0, 0);
	if (err)
		return err;

	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
	if (!zd->rxlen)
		return -EIO;

	code = le16_to_cpu(*(__le16*)(&zd->rxdata[4]));
	rid_fid = le16_to_cpu(*(__le16*)(&zd->rxdata[6]));
	length = le16_to_cpu(*(__le16*)(&zd->rxdata[8]));
	if (length > zd->rxlen)
		length = zd->rxlen-6;

	/* If access bit is not on, then error */
	if ((code & ZD1201_ACCESSBIT) != ZD1201_ACCESSBIT || rid_fid != rid )
		return -EINVAL;

	/* Not enough buffer for allocating data */
	if (riddatalen != (length - 4)) {
		dev_dbg(&zd->usb->dev, "riddatalen mismatches, expected=%u, (packet=%u) length=%u, rid=0x%04X, rid_fid=0x%04X\n",
		    riddatalen, zd->rxlen, length, rid, rid_fid);
		return -ENODATA;
	}

	zd->rxdatas = 0;
	/* Issue SetRxRid commnd */			
	err = zd1201_docmd(zd, ZD1201_CMDCODE_SETRXRID, rid, 0, length);
	if (err)
		return err;

	/* Receive RID record from resource packets */
	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
	if (!zd->rxlen)
		return -EIO;

	if (zd->rxdata[zd->rxlen - 1] != ZD1201_PACKET_RESOURCE) {
		dev_dbg(&zd->usb->dev, "Packet type mismatch: 0x%x not 0x3\n",
		    zd->rxdata[zd->rxlen-1]);
		return -EINVAL;
	}

	/* Set the data pointer and received data length */
	pdata = zd->rxdata;
	length = zd->rxlen;

	do {
		int actual_length;

		actual_length = (length > 64) ? 64 : length;

		if (pdata[0] != 0x3) {
			dev_dbg(&zd->usb->dev, "Rx Resource packet type error: %02X\n",
			    pdata[0]);
			return -EINVAL;
		}

		if (actual_length != 64) {
			/* Trim the last packet type byte */
			actual_length--;
		}

		/* Skip the 4 bytes header (RID length and RID) */
		if (i == 0) {
			pdata += 8;
			actual_length -= 8;
		} else {
			pdata += 4;
			actual_length -= 4;
		}
		
		memcpy(riddata, pdata, actual_length);
		riddata += actual_length;
		pdata += actual_length;
		length -= 64;
		i++;
	} while (length > 0);

	return 0;
}

/*
 *	resreq:
 *		byte	type
 *		byte	sequence
 *		u16	reserved
 *		byte	data[12]
 *	total: 16
 */
static int zd1201_setconfig(struct zd1201 *zd, int rid, void *buf, int len, int wait)
{
	int err;
	unsigned char *request;
	int reqlen;
	char seq=0;
	struct urb *urb;
	gfp_t gfp_mask = wait ? GFP_NOIO : GFP_ATOMIC;

	len += 4;			/* first 4 are for header */

	zd->rxdatas = 0;
	zd->rxlen = 0;
	for (seq=0; len > 0; seq++) {
		request = kmalloc(16, gfp_mask);
		if (!request)
			return -ENOMEM;
		urb = usb_alloc_urb(0, gfp_mask);
		if (!urb) {
			kfree(request);
			return -ENOMEM;
		}
		memset(request, 0, 16);
		reqlen = len>12 ? 12 : len;
		request[0] = ZD1201_USB_RESREQ;
		request[1] = seq;
		request[2] = 0;
		request[3] = 0;
		if (request[1] == 0) {
			/* add header */
			*(__le16*)&request[4] = cpu_to_le16((len-2+1)/2);
			*(__le16*)&request[6] = cpu_to_le16(rid);
			memcpy(request+8, buf, reqlen-4);
			buf += reqlen-4;
		} else {
			memcpy(request+4, buf, reqlen);
			buf += reqlen;
		}

		len -= reqlen;

		usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb,
		    zd->endp_out2), request, 16, zd1201_usbfree, zd);
		err = usb_submit_urb(urb, gfp_mask);
		if (err)
			goto err;
	}

	request = kmalloc(16, gfp_mask);
	if (!request)
		return -ENOMEM;
	urb = usb_alloc_urb(0, gfp_mask);
	if (!urb) {
		kfree(request);
		return -ENOMEM;
	}
	*((__le32*)request) = cpu_to_le32(ZD1201_USB_CMDREQ);
	*((__le16*)&request[4]) = 
	    cpu_to_le16(ZD1201_CMDCODE_ACCESS|ZD1201_ACCESSBIT);
	*((__le16*)&request[6]) = cpu_to_le16(rid);
	*((__le16*)&request[8]) = cpu_to_le16(0);
	*((__le16*)&request[10]) = cpu_to_le16(0);
	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out2),
	     request, 16, zd1201_usbfree, zd);
	err = usb_submit_urb(urb, gfp_mask);
	if (err)
		goto err;
	
	if (wait) {
		wait_event_interruptible(zd->rxdataq, zd->rxdatas);
		if (!zd->rxlen || le16_to_cpu(*(__le16*)&zd->rxdata[6]) != rid) {
			dev_dbg(&zd->usb->dev, "wrong or no RID received\n");
		}
	}

	return 0;
err:
	kfree(request);
	usb_free_urb(urb);
	return err;
}

static inline int zd1201_getconfig16(struct zd1201 *zd, int rid, short *val)
{
	int err;
	__le16 zdval;

	err = zd1201_getconfig(zd, rid, &zdval, sizeof(__le16));
	if (err)
		return err;
	*val = le16_to_cpu(zdval);
	return 0;
}

static inline int zd1201_setconfig16(struct zd1201 *zd, int rid, short val)
{
	__le16 zdval = cpu_to_le16(val);
	return (zd1201_setconfig(zd, rid, &zdval, sizeof(__le16), 1));
}

static int zd1201_drvr_start(struct zd1201 *zd)
{
	int err, i;
	short max;
	__le16 zdmax;
	unsigned char *buffer;

	buffer = kzalloc(ZD1201_RXSIZE, GFP_KERNEL);
	if (!buffer)
		return -ENOMEM;

	usb_fill_bulk_urb(zd->rx_urb, zd->usb, 
	    usb_rcvbulkpipe(zd->usb, zd->endp_in), buffer, ZD1201_RXSIZE,
	    zd1201_usbrx, zd);

	err = usb_submit_urb(zd->rx_urb, GFP_KERNEL);
	if (err)
		goto err_buffer;

	err = zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
	if (err)
		goto err_urb;

	err = zd1201_getconfig(zd, ZD1201_RID_CNFMAXTXBUFFERNUMBER, &zdmax,
	    sizeof(__le16));
	if (err)
		goto err_urb;

	max = le16_to_cpu(zdmax);
	for (i=0; i<max; i++) {
		err = zd1201_docmd(zd, ZD1201_CMDCODE_ALLOC, 1514, 0, 0);
		if (err)
			goto err_urb;
	}

	return 0;

err_urb:
	usb_kill_urb(zd->rx_urb);
	return err;
err_buffer:
	kfree(buffer);
	return err;
}

/*	Magic alert: The firmware doesn't seem to like the MAC state being
 *	toggled in promisc (aka monitor) mode.
 *	(It works a number of times, but will halt eventually)
 *	So we turn it of before disabling and on after enabling if needed.
 */
static int zd1201_enable(struct zd1201 *zd)
{
	int err;

	if (zd->mac_enabled)
		return 0;

	err = zd1201_docmd(zd, ZD1201_CMDCODE_ENABLE, 0, 0, 0);
	if (!err)
		zd->mac_enabled = 1;

	if (zd->monitor)
		err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 1);

	return err;
}

static int zd1201_disable(struct zd1201 *zd)
{
	int err;

	if (!zd->mac_enabled)
		return 0;
	if (zd->monitor) {
		err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
		if (err)
			return err;
	}

	err = zd1201_docmd(zd, ZD1201_CMDCODE_DISABLE, 0, 0, 0);
	if (!err)
		zd->mac_enabled = 0;
	return err;
}

static int zd1201_mac_reset(struct zd1201 *zd)
{
	if (!zd->mac_enabled)
		return 0;
	zd1201_disable(zd);
	return zd1201_enable(zd);
}

static int zd1201_join(struct zd1201 *zd, char *essid, int essidlen)
{
	int err, val;
	char buf[IW_ESSID_MAX_SIZE+2];

	err = zd1201_disable(zd);
	if (err)
		return err;

	val = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
	val |= ZD1201_CNFAUTHENTICATION_SHAREDKEY;
	err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, val);
	if (err)
		return err;

	*(__le16 *)buf = cpu_to_le16(essidlen);
	memcpy(buf+2, essid, essidlen);
	if (!zd->ap) {	/* Normal station */
		err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
		    IW_ESSID_MAX_SIZE+2, 1);
		if (err)
			return err;
	} else {	/* AP */
		err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNSSID, buf,
		    IW_ESSID_MAX_SIZE+2, 1);
		if (err)
			return err;
	}

	err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR, 
	    zd->dev->dev_addr, zd->dev->addr_len, 1);
	if (err)
		return err;

	err = zd1201_enable(zd);
	if (err)
		return err;

	msleep(100);
	return 0;
}

static int zd1201_net_open(struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);

	/* Start MAC with wildcard if no essid set */
	if (!zd->mac_enabled)
		zd1201_join(zd, zd->essid, zd->essidlen);
	netif_start_queue(dev);

	return 0;
}

static int zd1201_net_stop(struct net_device *dev)
{
	netif_stop_queue(dev);
	return 0;
}

/*
	RFC 1042 encapsulates Ethernet frames in 802.11 frames
	by prefixing them with 0xaa, 0xaa, 0x03) followed by a SNAP OID of 0
	(0x00, 0x00, 0x00). Zd requires an additional padding, copy
	of ethernet addresses, length of the standard RFC 1042 packet
	and a command byte (which is nul for tx).
	
	tx frame (from Wlan NG):
	RFC 1042:
		llc		0xAA 0xAA 0x03 (802.2 LLC)
		snap		0x00 0x00 0x00 (Ethernet encapsulated)
		type		2 bytes, Ethernet type field
		payload		(minus eth header)
	Zydas specific:
		padding		1B if (skb->len+8+1)%64==0
		Eth MAC addr	12 bytes, Ethernet MAC addresses
		length		2 bytes, RFC 1042 packet length 
				(llc+snap+type+payload)
		zd		1 null byte, zd1201 packet type
 */
static int zd1201_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);
	unsigned char *txbuf = zd->txdata;
	int txbuflen, pad = 0, err;
	struct urb *urb = zd->tx_urb;

	if (!zd->mac_enabled || zd->monitor) {
		zd->stats.tx_dropped++;
		kfree_skb(skb);
		return 0;
	}
	netif_stop_queue(dev);

	txbuflen = skb->len + 8 + 1;
	if (txbuflen%64 == 0) {
		pad = 1;
		txbuflen++;
	}
	txbuf[0] = 0xAA;
	txbuf[1] = 0xAA;
	txbuf[2] = 0x03;
	txbuf[3] = 0x00;	/* rfc1042 */
	txbuf[4] = 0x00;
	txbuf[5] = 0x00;

	skb_copy_from_linear_data_offset(skb, 12, txbuf + 6, skb->len - 12);
	if (pad)
		txbuf[skb->len-12+6]=0;
	skb_copy_from_linear_data(skb, txbuf + skb->len - 12 + 6 + pad, 12);
	*(__be16*)&txbuf[skb->len+6+pad] = htons(skb->len-12+6);
	txbuf[txbuflen-1] = 0;

	usb_fill_bulk_urb(urb, zd->usb, usb_sndbulkpipe(zd->usb, zd->endp_out),
	    txbuf, txbuflen, zd1201_usbtx, zd);

	err = usb_submit_urb(zd->tx_urb, GFP_ATOMIC);
	if (err) {
		zd->stats.tx_errors++;
		netif_start_queue(dev);
		return err;
	}
	zd->stats.tx_packets++;
	zd->stats.tx_bytes += skb->len;
	dev->trans_start = jiffies;
	kfree_skb(skb);

	return 0;
}

static void zd1201_tx_timeout(struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);

	if (!zd)
		return;
	dev_warn(&zd->usb->dev, "%s: TX timeout, shooting down urb\n",
	    dev->name);
	usb_unlink_urb(zd->tx_urb);
	zd->stats.tx_errors++;
	/* Restart the timeout to quiet the watchdog: */
	dev->trans_start = jiffies;
}

static int zd1201_set_mac_address(struct net_device *dev, void *p)
{
	struct sockaddr *addr = p;
	struct zd1201 *zd = netdev_priv(dev);
	int err;

	if (!zd)
		return -ENODEV;

	err = zd1201_setconfig(zd, ZD1201_RID_CNFOWNMACADDR, 
	    addr->sa_data, dev->addr_len, 1);
	if (err)
		return err;
	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

	return zd1201_mac_reset(zd);
}

static struct net_device_stats *zd1201_get_stats(struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);

	return &zd->stats;
}

static struct iw_statistics *zd1201_get_wireless_stats(struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);

	return &zd->iwstats;
}

static void zd1201_set_multicast(struct net_device *dev)
{
	struct zd1201 *zd = netdev_priv(dev);
	struct dev_mc_list *mc = dev->mc_list;
	unsigned char reqbuf[ETH_ALEN*ZD1201_MAXMULTI];
	int i;

	if (dev->mc_count > ZD1201_MAXMULTI)
		return;

	for (i=0; i<dev->mc_count; i++) {
		memcpy(reqbuf+i*ETH_ALEN, mc->dmi_addr, ETH_ALEN);
		mc = mc->next;
	}
	zd1201_setconfig(zd, ZD1201_RID_CNFGROUPADDRESS, reqbuf,
	    dev->mc_count*ETH_ALEN, 0);
	
}

static int zd1201_config_commit(struct net_device *dev, 
    struct iw_request_info *info, struct iw_point *data, char *essid)
{
	struct zd1201 *zd = netdev_priv(dev);

	return zd1201_mac_reset(zd);
}

static int zd1201_get_name(struct net_device *dev,
    struct iw_request_info *info, char *name, char *extra)
{
	strcpy(name, "IEEE 802.11b");
	return 0;
}

static int zd1201_set_freq(struct net_device *dev,
    struct iw_request_info *info, struct iw_freq *freq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short channel = 0;
	int err;

	if (freq->e == 0)
		channel = freq->m;
	else {
		if (freq->m >= 2482)
			channel = 14;
		if (freq->m >= 2407)
			channel = (freq->m-2407)/5;
	}

	err = zd1201_setconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, channel);
	if (err)
		return err;

	zd1201_mac_reset(zd);

	return 0;
}

static int zd1201_get_freq(struct net_device *dev,
    struct iw_request_info *info, struct iw_freq *freq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short channel;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFOWNCHANNEL, &channel);
	if (err)
		return err;
	freq->e = 0;
	freq->m = channel;

	return 0;
}

static int zd1201_set_mode(struct net_device *dev,
    struct iw_request_info *info, __u32 *mode, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short porttype, monitor = 0;
	unsigned char buffer[IW_ESSID_MAX_SIZE+2];
	int err;

	if (zd->ap) {
		if (*mode != IW_MODE_MASTER)
			return -EINVAL;
		return 0;
	}

	err = zd1201_setconfig16(zd, ZD1201_RID_PROMISCUOUSMODE, 0);
	if (err)
		return err;
	zd->dev->type = ARPHRD_ETHER;
	switch(*mode) {
		case IW_MODE_MONITOR:
			monitor = 1;
			zd->dev->type = ARPHRD_IEEE80211;
			/* Make sure we are no longer associated with by
			   setting an 'impossible' essid.
			   (otherwise we mess up firmware)
			 */
			zd1201_join(zd, "\0-*#\0", 5);
			/* Put port in pIBSS */
		case 8: /* No pseudo-IBSS in wireless extensions (yet) */
			porttype = ZD1201_PORTTYPE_PSEUDOIBSS;
			break;
		case IW_MODE_ADHOC:
			porttype = ZD1201_PORTTYPE_IBSS;
			break;
		case IW_MODE_INFRA:
			porttype = ZD1201_PORTTYPE_BSS;
			break;
		default:
			return -EINVAL;
	}

	err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
	if (err)
		return err;
	if (zd->monitor && !monitor) {
			zd1201_disable(zd);
			*(__le16 *)buffer = cpu_to_le16(zd->essidlen);
			memcpy(buffer+2, zd->essid, zd->essidlen);
			err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID,
			    buffer, IW_ESSID_MAX_SIZE+2, 1);
			if (err)
				return err;
	}
	zd->monitor = monitor;
	/* If monitor mode is set we don't actually turn it on here since it
	 * is done during mac reset anyway (see zd1201_mac_enable).
	 */
	zd1201_mac_reset(zd);

	return 0;
}

static int zd1201_get_mode(struct net_device *dev,
    struct iw_request_info *info, __u32 *mode, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short porttype;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPORTTYPE, &porttype);
	if (err)
		return err;
	switch(porttype) {
		case ZD1201_PORTTYPE_IBSS:
			*mode = IW_MODE_ADHOC;
			break;
		case ZD1201_PORTTYPE_BSS:
			*mode = IW_MODE_INFRA;
			break;
		case ZD1201_PORTTYPE_WDS:
			*mode = IW_MODE_REPEAT;
			break;
		case ZD1201_PORTTYPE_PSEUDOIBSS:
			*mode = 8;/* No Pseudo-IBSS... */
			break;
		case ZD1201_PORTTYPE_AP:
			*mode = IW_MODE_MASTER;
			break;
		default:
			dev_dbg(&zd->usb->dev, "Unknown porttype: %d\n",
			    porttype);
			*mode = IW_MODE_AUTO;
	}
	if (zd->monitor)
		*mode = IW_MODE_MONITOR;

	return 0;
}

static int zd1201_get_range(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *wrq, char *extra)
{
	struct iw_range *range = (struct iw_range *)extra;

	wrq->length = sizeof(struct iw_range);
	memset(range, 0, sizeof(struct iw_range));
	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = WIRELESS_EXT;

	range->max_qual.qual = 128;
	range->max_qual.level = 128;
	range->max_qual.noise = 128;
	range->max_qual.updated = 7;

	range->encoding_size[0] = 5;
	range->encoding_size[1] = 13;
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = ZD1201_NUMKEYS;

	range->num_bitrates = 4;
	range->bitrate[0] = 1000000;
	range->bitrate[1] = 2000000;
	range->bitrate[2] = 5500000;
	range->bitrate[3] = 11000000;

	range->min_rts = 0;
	range->min_frag = ZD1201_FRAGMIN;
	range->max_rts = ZD1201_RTSMAX;
	range->min_frag = ZD1201_FRAGMAX;

	return 0;
}

/*	Little bit of magic here: we only get the quality if we poll
 *	for it, and we never get an actual request to trigger such
 *	a poll. Therefore we 'assume' that the user will soon ask for
 *	the stats after asking the bssid.
 */
static int zd1201_get_wap(struct net_device *dev,
    struct iw_request_info *info, struct sockaddr *ap_addr, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	unsigned char buffer[6];

	if (!zd1201_getconfig(zd, ZD1201_RID_COMMSQUALITY, buffer, 6)) {
		/* Unfortunately the quality and noise reported is useless.
		   they seem to be accumulators that increase until you
		   read them, unless we poll on a fixed interval we can't
		   use them
		 */
		/*zd->iwstats.qual.qual = le16_to_cpu(((__le16 *)buffer)[0]);*/
		zd->iwstats.qual.level = le16_to_cpu(((__le16 *)buffer)[1]);
		/*zd->iwstats.qual.noise = le16_to_cpu(((__le16 *)buffer)[2]);*/
		zd->iwstats.qual.updated = 2;
	}

	return zd1201_getconfig(zd, ZD1201_RID_CURRENTBSSID, ap_addr->sa_data, 6);
}

static int zd1201_set_scan(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *srq, char *extra)
{
	/* We do everything in get_scan */
	return 0;
}

static int zd1201_get_scan(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *srq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	int err, i, j, enabled_save;
	struct iw_event iwe;
	char *cev = extra;
	char *end_buf = extra + IW_SCAN_MAX_DATA;

	/* No scanning in AP mode */
	if (zd->ap)
		return -EOPNOTSUPP;

	/* Scan doesn't seem to work if disabled */
	enabled_save = zd->mac_enabled;
	zd1201_enable(zd);

	zd->rxdatas = 0;
	err = zd1201_docmd(zd, ZD1201_CMDCODE_INQUIRE, 
	     ZD1201_INQ_SCANRESULTS, 0, 0);
	if (err)
		return err;

	wait_event_interruptible(zd->rxdataq, zd->rxdatas);
	if (!zd->rxlen)
		return -EIO;

	if (le16_to_cpu(*(__le16*)&zd->rxdata[2]) != ZD1201_INQ_SCANRESULTS)
		return -EIO;

	for(i=8; i<zd->rxlen; i+=62) {
		iwe.cmd = SIOCGIWAP;
		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
		memcpy(iwe.u.ap_addr.sa_data, zd->rxdata+i+6, 6);
		cev = iwe_stream_add_event(info, cev, end_buf,
					   &iwe, IW_EV_ADDR_LEN);

		iwe.cmd = SIOCGIWESSID;
		iwe.u.data.length = zd->rxdata[i+16];
		iwe.u.data.flags = 1;
		cev = iwe_stream_add_point(info, cev, end_buf,
					   &iwe, zd->rxdata+i+18);

		iwe.cmd = SIOCGIWMODE;
		if (zd->rxdata[i+14]&0x01)
			iwe.u.mode = IW_MODE_MASTER;
		else
			iwe.u.mode = IW_MODE_ADHOC;
		cev = iwe_stream_add_event(info, cev, end_buf,
					   &iwe, IW_EV_UINT_LEN);
		
		iwe.cmd = SIOCGIWFREQ;
		iwe.u.freq.m = zd->rxdata[i+0];
		iwe.u.freq.e = 0;
		cev = iwe_stream_add_event(info, cev, end_buf,
					   &iwe, IW_EV_FREQ_LEN);
		
		iwe.cmd = SIOCGIWRATE;
		iwe.u.bitrate.fixed = 0;
		iwe.u.bitrate.disabled = 0;
		for (j=0; j<10; j++) if (zd->rxdata[i+50+j]) {
			iwe.u.bitrate.value = (zd->rxdata[i+50+j]&0x7f)*500000;
			cev = iwe_stream_add_event(info, cev, end_buf,
						   &iwe, IW_EV_PARAM_LEN);
		}
		
		iwe.cmd = SIOCGIWENCODE;
		iwe.u.data.length = 0;
		if (zd->rxdata[i+14]&0x10)
			iwe.u.data.flags = IW_ENCODE_ENABLED;
		else
			iwe.u.data.flags = IW_ENCODE_DISABLED;
		cev = iwe_stream_add_point(info, cev, end_buf, &iwe, NULL);
		
		iwe.cmd = IWEVQUAL;
		iwe.u.qual.qual = zd->rxdata[i+4];
		iwe.u.qual.noise= zd->rxdata[i+2]/10-100;
		iwe.u.qual.level = (256+zd->rxdata[i+4]*100)/255-100;
		iwe.u.qual.updated = 7;
		cev = iwe_stream_add_event(info, cev, end_buf,
					   &iwe, IW_EV_QUAL_LEN);
	}

	if (!enabled_save)
		zd1201_disable(zd);

	srq->length = cev - extra;
	srq->flags = 0;

	return 0;
}

static int zd1201_set_essid(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *data, char *essid)
{
	struct zd1201 *zd = netdev_priv(dev);

	if (data->length > IW_ESSID_MAX_SIZE)
		return -EINVAL;
	if (data->length < 1)
		data->length = 1;
	zd->essidlen = data->length;
	memset(zd->essid, 0, IW_ESSID_MAX_SIZE+1);
	memcpy(zd->essid, essid, data->length);
	return zd1201_join(zd, zd->essid, zd->essidlen);
}

static int zd1201_get_essid(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *data, char *essid)
{
	struct zd1201 *zd = netdev_priv(dev);

	memcpy(essid, zd->essid, zd->essidlen);
	data->flags = 1;
	data->length = zd->essidlen;

	return 0;
}

static int zd1201_get_nick(struct net_device *dev, struct iw_request_info *info,
    struct iw_point *data, char *nick)
{
	strcpy(nick, "zd1201");
	data->flags = 1;
	data->length = strlen(nick);
	return 0;
}

static int zd1201_set_rate(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short rate;
	int err;

	switch (rrq->value) {
		case 1000000:
			rate = ZD1201_RATEB1;
			break;
		case 2000000:
			rate = ZD1201_RATEB2;
			break;
		case 5500000:
			rate = ZD1201_RATEB5;
			break;
		case 11000000:
		default:
			rate = ZD1201_RATEB11;
			break;
	}
	if (!rrq->fixed) { /* Also enable all lower bitrates */
		rate |= rate-1;
	}

	err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL, rate);
	if (err)
		return err;

	return zd1201_mac_reset(zd);
}

static int zd1201_get_rate(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short rate;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CURRENTTXRATE, &rate);
	if (err)
		return err;

	switch(rate) {
		case 1:
			rrq->value = 1000000;
			break;
		case 2:
			rrq->value = 2000000;
			break;
		case 5:
			rrq->value = 5500000;
			break;
		case 11:
			rrq->value = 11000000;
			break;
		default:
			rrq->value = 0;
	}
	rrq->fixed = 0;
	rrq->disabled = 0;

	return 0;
}

static int zd1201_set_rts(struct net_device *dev, struct iw_request_info *info,
    struct iw_param *rts, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	int err;
	short val = rts->value;

	if (rts->disabled || !rts->fixed)
		val = ZD1201_RTSMAX;
	if (val > ZD1201_RTSMAX)
		return -EINVAL;
	if (val < 0)
		return -EINVAL;

	err = zd1201_setconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, val);
	if (err)
		return err;
	return zd1201_mac_reset(zd);
}

static int zd1201_get_rts(struct net_device *dev, struct iw_request_info *info,
    struct iw_param *rts, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short rtst;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFRTSTHRESHOLD, &rtst);
	if (err)
		return err;
	rts->value = rtst;
	rts->disabled = (rts->value == ZD1201_RTSMAX);
	rts->fixed = 1;

	return 0;
}

static int zd1201_set_frag(struct net_device *dev, struct iw_request_info *info,
    struct iw_param *frag, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	int err;
	short val = frag->value;

	if (frag->disabled || !frag->fixed)
		val = ZD1201_FRAGMAX;
	if (val > ZD1201_FRAGMAX)
		return -EINVAL;
	if (val < ZD1201_FRAGMIN)
		return -EINVAL;
	if (val & 1)
		return -EINVAL;
	err = zd1201_setconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, val);
	if (err)
		return err;
	return zd1201_mac_reset(zd);
}

static int zd1201_get_frag(struct net_device *dev, struct iw_request_info *info,
    struct iw_param *frag, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short fragt;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFFRAGTHRESHOLD, &fragt);
	if (err)
		return err;
	frag->value = fragt;
	frag->disabled = (frag->value == ZD1201_FRAGMAX);
	frag->fixed = 1;

	return 0;
}

static int zd1201_set_retry(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	return 0;
}

static int zd1201_get_retry(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	return 0;
}

static int zd1201_set_encode(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *erq, char *key)
{
	struct zd1201 *zd = netdev_priv(dev);
	short i;
	int err, rid;

	if (erq->length > ZD1201_MAXKEYLEN)
		return -EINVAL;

	i = (erq->flags & IW_ENCODE_INDEX)-1;
	if (i == -1) {
		err = zd1201_getconfig16(zd,ZD1201_RID_CNFDEFAULTKEYID,&i);
		if (err)
			return err;
	} else {
		err = zd1201_setconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, i);
		if (err)
			return err;
	}

	if (i < 0 || i >= ZD1201_NUMKEYS)
		return -EINVAL;

	rid = ZD1201_RID_CNFDEFAULTKEY0 + i;
	err = zd1201_setconfig(zd, rid, key, erq->length, 1);
	if (err)
		return err;
	zd->encode_keylen[i] = erq->length;
	memcpy(zd->encode_keys[i], key, erq->length);

	i=0;
	if (!(erq->flags & IW_ENCODE_DISABLED & IW_ENCODE_MODE)) {
		i |= 0x01;
		zd->encode_enabled = 1;
	} else
		zd->encode_enabled = 0;
	if (erq->flags & IW_ENCODE_RESTRICTED & IW_ENCODE_MODE) {
		i |= 0x02;
		zd->encode_restricted = 1;
	} else
		zd->encode_restricted = 0;
	err = zd1201_setconfig16(zd, ZD1201_RID_CNFWEBFLAGS, i);
	if (err)
		return err;

	if (zd->encode_enabled)
		i = ZD1201_CNFAUTHENTICATION_SHAREDKEY;
	else
		i = ZD1201_CNFAUTHENTICATION_OPENSYSTEM;
	err = zd1201_setconfig16(zd, ZD1201_RID_CNFAUTHENTICATION, i);
	if (err)
		return err;

	return zd1201_mac_reset(zd);
}

static int zd1201_get_encode(struct net_device *dev,
    struct iw_request_info *info, struct iw_point *erq, char *key)
{
	struct zd1201 *zd = netdev_priv(dev);
	short i;
	int err;

	if (zd->encode_enabled)
		erq->flags = IW_ENCODE_ENABLED;
	else
		erq->flags = IW_ENCODE_DISABLED;
	if (zd->encode_restricted)
		erq->flags |= IW_ENCODE_RESTRICTED;
	else
		erq->flags |= IW_ENCODE_OPEN;

	i = (erq->flags & IW_ENCODE_INDEX) -1;
	if (i == -1) {
		err = zd1201_getconfig16(zd, ZD1201_RID_CNFDEFAULTKEYID, &i);
		if (err)
			return err;
	}
	if (i<0 || i>= ZD1201_NUMKEYS)
		return -EINVAL;

	erq->flags |= i+1;

	erq->length = zd->encode_keylen[i];
	memcpy(key, zd->encode_keys[i], erq->length);

	return 0;
}

static int zd1201_set_power(struct net_device *dev, 
    struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short enabled, duration, level;
	int err;

	enabled = vwrq->disabled ? 0 : 1;
	if (enabled) {
		if (vwrq->flags & IW_POWER_PERIOD) {
			duration = vwrq->value;
			err = zd1201_setconfig16(zd, 
			    ZD1201_RID_CNFMAXSLEEPDURATION, duration);
			if (err)
				return err;
			goto out;
		}
		if (vwrq->flags & IW_POWER_TIMEOUT) {
			err = zd1201_getconfig16(zd, 
			    ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
			if (err)
				return err;
			level = vwrq->value * 4 / duration;
			if (level > 4)
				level = 4;
			if (level < 0)
				level = 0;
			err = zd1201_setconfig16(zd, ZD1201_RID_CNFPMEPS,
			    level);
			if (err)
				return err;
			goto out;
		}
		return -EINVAL;
	}
out:
	return zd1201_setconfig16(zd, ZD1201_RID_CNFPMENABLED, enabled);
}

static int zd1201_get_power(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short enabled, level, duration;
	int err;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMENABLED, &enabled);
	if (err)
		return err;
	err = zd1201_getconfig16(zd, ZD1201_RID_CNFPMEPS, &level);
	if (err)
		return err;
	err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXSLEEPDURATION, &duration);
	if (err)
		return err;
	vwrq->disabled = enabled ? 0 : 1;
	if (vwrq->flags & IW_POWER_TYPE) {
		if (vwrq->flags & IW_POWER_PERIOD) {
			vwrq->value = duration;
			vwrq->flags = IW_POWER_PERIOD;
		} else {
			vwrq->value = duration * level / 4;
			vwrq->flags = IW_POWER_TIMEOUT;
		}
	}
	if (vwrq->flags & IW_POWER_MODE) {
		if (enabled && level)
			vwrq->flags = IW_POWER_UNICAST_R;
		else
			vwrq->flags = IW_POWER_ALL_R;
	}

	return 0;
}


static const iw_handler zd1201_iw_handler[] =
{
	(iw_handler) zd1201_config_commit,	/* SIOCSIWCOMMIT */
	(iw_handler) zd1201_get_name,    	/* SIOCGIWNAME */
	(iw_handler) NULL,			/* SIOCSIWNWID */
	(iw_handler) NULL,			/* SIOCGIWNWID */
	(iw_handler) zd1201_set_freq,		/* SIOCSIWFREQ */
	(iw_handler) zd1201_get_freq,		/* SIOCGIWFREQ */
	(iw_handler) zd1201_set_mode,		/* SIOCSIWMODE */
	(iw_handler) zd1201_get_mode,		/* SIOCGIWMODE */
	(iw_handler) NULL,                  	/* SIOCSIWSENS */
	(iw_handler) NULL,           		/* SIOCGIWSENS */
	(iw_handler) NULL,			/* SIOCSIWRANGE */
	(iw_handler) zd1201_get_range,           /* SIOCGIWRANGE */
	(iw_handler) NULL,			/* SIOCSIWPRIV */
	(iw_handler) NULL,			/* SIOCGIWPRIV */
	(iw_handler) NULL,			/* SIOCSIWSTATS */
	(iw_handler) NULL,			/* SIOCGIWSTATS */
	(iw_handler) NULL,			/* SIOCSIWSPY */
	(iw_handler) NULL,			/* SIOCGIWSPY */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL/*zd1201_set_wap*/,		/* SIOCSIWAP */
	(iw_handler) zd1201_get_wap,		/* SIOCGIWAP */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,       		/* SIOCGIWAPLIST */
	(iw_handler) zd1201_set_scan,		/* SIOCSIWSCAN */
	(iw_handler) zd1201_get_scan,		/* SIOCGIWSCAN */
	(iw_handler) zd1201_set_essid,		/* SIOCSIWESSID */
	(iw_handler) zd1201_get_essid,		/* SIOCGIWESSID */
	(iw_handler) NULL,         		/* SIOCSIWNICKN */
	(iw_handler) zd1201_get_nick, 		/* SIOCGIWNICKN */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) NULL,			/* -- hole -- */
	(iw_handler) zd1201_set_rate,		/* SIOCSIWRATE */
	(iw_handler) zd1201_get_rate,		/* SIOCGIWRATE */
	(iw_handler) zd1201_set_rts,		/* SIOCSIWRTS */
	(iw_handler) zd1201_get_rts,		/* SIOCGIWRTS */
	(iw_handler) zd1201_set_frag,		/* SIOCSIWFRAG */
	(iw_handler) zd1201_get_frag,		/* SIOCGIWFRAG */
	(iw_handler) NULL,         		/* SIOCSIWTXPOW */
	(iw_handler) NULL,          		/* SIOCGIWTXPOW */
	(iw_handler) zd1201_set_retry,		/* SIOCSIWRETRY */
	(iw_handler) zd1201_get_retry,		/* SIOCGIWRETRY */
	(iw_handler) zd1201_set_encode,		/* SIOCSIWENCODE */
	(iw_handler) zd1201_get_encode,		/* SIOCGIWENCODE */
	(iw_handler) zd1201_set_power,		/* SIOCSIWPOWER */
	(iw_handler) zd1201_get_power,		/* SIOCGIWPOWER */
};

static int zd1201_set_hostauth(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);

	if (!zd->ap)
		return -EOPNOTSUPP;

	return zd1201_setconfig16(zd, ZD1201_RID_CNFHOSTAUTH, rrq->value);
}

static int zd1201_get_hostauth(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short hostauth;
	int err;

	if (!zd->ap)
		return -EOPNOTSUPP;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFHOSTAUTH, &hostauth);
	if (err)
		return err;
	rrq->value = hostauth;
	rrq->fixed = 1;

	return 0;
}

static int zd1201_auth_sta(struct net_device *dev,
    struct iw_request_info *info, struct sockaddr *sta, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	unsigned char buffer[10];

	if (!zd->ap)
		return -EOPNOTSUPP;

	memcpy(buffer, sta->sa_data, ETH_ALEN);
	*(short*)(buffer+6) = 0;	/* 0==success, 1==failure */
	*(short*)(buffer+8) = 0;

	return zd1201_setconfig(zd, ZD1201_RID_AUTHENTICATESTA, buffer, 10, 1);
}

static int zd1201_set_maxassoc(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	int err;

	if (!zd->ap)
		return -EOPNOTSUPP;

	err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, rrq->value);
	if (err)
		return err;
	return 0;
}

static int zd1201_get_maxassoc(struct net_device *dev,
    struct iw_request_info *info, struct iw_param *rrq, char *extra)
{
	struct zd1201 *zd = netdev_priv(dev);
	short maxassoc;
	int err;

	if (!zd->ap)
		return -EOPNOTSUPP;

	err = zd1201_getconfig16(zd, ZD1201_RID_CNFMAXASSOCSTATIONS, &maxassoc);
	if (err)
		return err;
	rrq->value = maxassoc;
	rrq->fixed = 1;

	return 0;
}

static const iw_handler zd1201_private_handler[] = {
	(iw_handler) zd1201_set_hostauth,	/* ZD1201SIWHOSTAUTH */
	(iw_handler) zd1201_get_hostauth,	/* ZD1201GIWHOSTAUTH */
	(iw_handler) zd1201_auth_sta,		/* ZD1201SIWAUTHSTA */
	(iw_handler) NULL,			/* nothing to get */
	(iw_handler) zd1201_set_maxassoc,	/* ZD1201SIMAXASSOC */
	(iw_handler) zd1201_get_maxassoc,	/* ZD1201GIMAXASSOC */
};

static const struct iw_priv_args zd1201_private_args[] = {
	{ ZD1201SIWHOSTAUTH, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	    IW_PRIV_TYPE_NONE, "sethostauth" },
	{ ZD1201GIWHOSTAUTH, IW_PRIV_TYPE_NONE,
	    IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "gethostauth" },
	{ ZD1201SIWAUTHSTA, IW_PRIV_TYPE_ADDR | IW_PRIV_SIZE_FIXED | 1,
	    IW_PRIV_TYPE_NONE, "authstation" },
	{ ZD1201SIWMAXASSOC, IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
	    IW_PRIV_TYPE_NONE, "setmaxassoc" },
	{ ZD1201GIWMAXASSOC, IW_PRIV_TYPE_NONE,
	    IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "getmaxassoc" },
};

static const struct iw_handler_def zd1201_iw_handlers = {
	.num_standard 		= ARRAY_SIZE(zd1201_iw_handler),
	.num_private 		= ARRAY_SIZE(zd1201_private_handler),
	.num_private_args 	= ARRAY_SIZE(zd1201_private_args),
	.standard 		= (iw_handler *)zd1201_iw_handler,
	.private 		= (iw_handler *)zd1201_private_handler,
	.private_args 		= (struct iw_priv_args *) zd1201_private_args,
	.get_wireless_stats	= zd1201_get_wireless_stats,
};

static int zd1201_probe(struct usb_interface *interface,
			const struct usb_device_id *id)
{
	struct zd1201 *zd;
	struct net_device *dev;
	struct usb_device *usb;
	int err;
	short porttype;
	char buf[IW_ESSID_MAX_SIZE+2];

	usb = interface_to_usbdev(interface);

	dev = alloc_etherdev(sizeof(*zd));
	if (!dev)
		return -ENOMEM;
	zd = netdev_priv(dev);
	zd->dev = dev;

	zd->ap = ap;
	zd->usb = usb;
	zd->removed = 0;
	init_waitqueue_head(&zd->rxdataq);
	INIT_HLIST_HEAD(&zd->fraglist);
	
	err = zd1201_fw_upload(usb, zd->ap);
	if (err) {
		dev_err(&usb->dev, "zd1201 firmware upload failed: %d\n", err);
		goto err_zd;
	}
	
	zd->endp_in = 1;
	zd->endp_out = 1;
	zd->endp_out2 = 2;
	zd->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	zd->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!zd->rx_urb || !zd->tx_urb)
		goto err_zd;

	mdelay(100);
	err = zd1201_drvr_start(zd);
	if (err)
		goto err_zd;

	err = zd1201_setconfig16(zd, ZD1201_RID_CNFMAXDATALEN, 2312);
	if (err)
		goto err_start;

	err = zd1201_setconfig16(zd, ZD1201_RID_TXRATECNTL,
	    ZD1201_RATEB1 | ZD1201_RATEB2 | ZD1201_RATEB5 | ZD1201_RATEB11);
	if (err)
		goto err_start;

	dev->open = zd1201_net_open;
	dev->stop = zd1201_net_stop;
	dev->get_stats = zd1201_get_stats;
	dev->wireless_handlers =
	    (struct iw_handler_def *)&zd1201_iw_handlers;
	dev->hard_start_xmit = zd1201_hard_start_xmit;
	dev->watchdog_timeo = ZD1201_TX_TIMEOUT;
	dev->tx_timeout = zd1201_tx_timeout;
	dev->set_multicast_list = zd1201_set_multicast;
	dev->set_mac_address = zd1201_set_mac_address;
	strcpy(dev->name, "wlan%d");

	err = zd1201_getconfig(zd, ZD1201_RID_CNFOWNMACADDR, 
	    dev->dev_addr, dev->addr_len);
	if (err)
		goto err_start;

	/* Set wildcard essid to match zd->essid */
	*(__le16 *)buf = cpu_to_le16(0);
	err = zd1201_setconfig(zd, ZD1201_RID_CNFDESIREDSSID, buf,
	    IW_ESSID_MAX_SIZE+2, 1);
	if (err)
		goto err_start;

	if (zd->ap)
		porttype = ZD1201_PORTTYPE_AP;
	else
		porttype = ZD1201_PORTTYPE_BSS;
	err = zd1201_setconfig16(zd, ZD1201_RID_CNFPORTTYPE, porttype);
	if (err)
		goto err_start;

	SET_NETDEV_DEV(dev, &usb->dev);

	err = register_netdev(dev);
	if (err)
		goto err_start;
	dev_info(&usb->dev, "%s: ZD1201 USB Wireless interface\n",
	    dev->name);

	usb_set_intfdata(interface, zd);
	zd1201_enable(zd);	/* zd1201 likes to startup enabled, */
	zd1201_disable(zd);	/* interfering with all the wifis in range */
	return 0;

err_start:
	/* Leave the device in reset state */
	zd1201_docmd(zd, ZD1201_CMDCODE_INIT, 0, 0, 0);
err_zd:
	usb_free_urb(zd->tx_urb);
	usb_free_urb(zd->rx_urb);
	free_netdev(dev);
	return err;
}

static void zd1201_disconnect(struct usb_interface *interface)
{
	struct zd1201 *zd=(struct zd1201 *)usb_get_intfdata(interface);
	struct hlist_node *node, *node2;
	struct zd1201_frag *frag;

	if (!zd)
		return;
	usb_set_intfdata(interface, NULL);

	hlist_for_each_entry_safe(frag, node, node2, &zd->fraglist, fnode) {
		hlist_del_init(&frag->fnode);
		kfree_skb(frag->skb);
		kfree(frag);
	}

	if (zd->tx_urb) {
		usb_kill_urb(zd->tx_urb);
		usb_free_urb(zd->tx_urb);
	}
	if (zd->rx_urb) {
		usb_kill_urb(zd->rx_urb);
		usb_free_urb(zd->rx_urb);
	}

	if (zd->dev) {
		unregister_netdev(zd->dev);
		free_netdev(zd->dev);
	}
}

#ifdef CONFIG_PM

static int zd1201_suspend(struct usb_interface *interface,
			   pm_message_t message)
{
	struct zd1201 *zd = usb_get_intfdata(interface);

	netif_device_detach(zd->dev);

	zd->was_enabled = zd->mac_enabled;

	if (zd->was_enabled)
		return zd1201_disable(zd);
	else
		return 0;
}

static int zd1201_resume(struct usb_interface *interface)
{
	struct zd1201 *zd = usb_get_intfdata(interface);

	if (!zd || !zd->dev)
		return -ENODEV;

	netif_device_attach(zd->dev);

	if (zd->was_enabled)
		return zd1201_enable(zd);
	else
		return 0;
}

#else

#define zd1201_suspend NULL
#define zd1201_resume  NULL

#endif

static struct usb_driver zd1201_usb = {
	.name = "zd1201",
	.probe = zd1201_probe,
	.disconnect = zd1201_disconnect,
	.id_table = zd1201_table,
	.suspend = zd1201_suspend,
	.resume = zd1201_resume,
};

static int __init zd1201_init(void)
{
	return usb_register(&zd1201_usb);
}

static void __exit zd1201_cleanup(void)
{
	usb_deregister(&zd1201_usb);
}

module_init(zd1201_init);
module_exit(zd1201_cleanup);
olding GPIO informaton * for detecting VBUS * @port: gpio port number * @intr: gpio interrupt number * @irq_work_fall Structure for WorkQueue * @irq_work_rise Structure for WorkQueue */ struct pch_vbus_gpio_data { int port; int intr; struct work_struct irq_work_fall; struct work_struct irq_work_rise; }; /** * struct pch_udc_dev - Structure holding complete information * of the PCH USB device * @gadget: gadget driver data * @driver: reference to gadget driver bound * @pdev: reference to the PCI device * @ep: array of endpoints * @lock: protects all state * @active: enabled the PCI device * @stall: stall requested * @prot_stall: protcol stall requested * @irq_registered: irq registered with system * @mem_region: device memory mapped * @registered: driver regsitered with system * @suspended: driver in suspended state * @connected: gadget driver associated * @vbus_session: required vbus_session state * @set_cfg_not_acked: pending acknowledgement 4 setup * @waiting_zlp_ack: pending acknowledgement 4 ZLP * @data_requests: DMA pool for data requests * @stp_requests: DMA pool for setup requests * @dma_addr: DMA pool for received * @ep0out_buf: Buffer for DMA * @setup_data: Received setup data * @phys_addr: of device memory * @base_addr: for mapped device memory * @irq: IRQ line for the device * @cfg_data: current cfg, intf, and alt in use * @vbus_gpio: GPIO informaton for detecting VBUS */ struct pch_udc_dev { struct usb_gadget gadget; struct usb_gadget_driver *driver; struct pci_dev *pdev; struct pch_udc_ep ep[PCH_UDC_EP_NUM]; spinlock_t lock; /* protects all state */ unsigned active:1, stall:1, prot_stall:1, irq_registered:1, mem_region:1, suspended:1, connected:1, vbus_session:1, set_cfg_not_acked:1, waiting_zlp_ack:1; struct pci_pool *data_requests; struct pci_pool *stp_requests; dma_addr_t dma_addr; void *ep0out_buf; struct usb_ctrlrequest setup_data; unsigned long phys_addr; void __iomem *base_addr; unsigned irq; struct pch_udc_cfg_data cfg_data; struct pch_vbus_gpio_data vbus_gpio; }; #define to_pch_udc(g) (container_of((g), struct pch_udc_dev, gadget)) #define PCH_UDC_PCI_BAR 1 #define PCI_DEVICE_ID_INTEL_EG20T_UDC 0x8808 #define PCI_VENDOR_ID_ROHM 0x10DB #define PCI_DEVICE_ID_ML7213_IOH_UDC 0x801D #define PCI_DEVICE_ID_ML7831_IOH_UDC 0x8808 static const char ep0_string[] = "ep0in"; static DEFINE_SPINLOCK(udc_stall_spinlock); /* stall spin lock */ static bool speed_fs; module_param_named(speed_fs, speed_fs, bool, S_IRUGO); MODULE_PARM_DESC(speed_fs, "true for Full speed operation"); /** * struct pch_udc_request - Structure holding a PCH USB device request packet * @req: embedded ep request * @td_data_phys: phys. address * @td_data: first dma desc. of chain * @td_data_last: last dma desc. of chain * @queue: associated queue * @dma_going: DMA in progress for request * @dma_mapped: DMA memory mapped for request * @dma_done: DMA completed for request * @chain_len: chain length * @buf: Buffer memory for align adjustment * @dma: DMA memory for align adjustment */ struct pch_udc_request { struct usb_request req; dma_addr_t td_data_phys; struct pch_udc_data_dma_desc *td_data; struct pch_udc_data_dma_desc *td_data_last; struct list_head queue; unsigned dma_going:1, dma_mapped:1, dma_done:1; unsigned chain_len; void *buf; dma_addr_t dma; }; static inline u32 pch_udc_readl(struct pch_udc_dev *dev, unsigned long reg) { return ioread32(dev->base_addr + reg); } static inline void pch_udc_writel(struct pch_udc_dev *dev, unsigned long val, unsigned long reg) { iowrite32(val, dev->base_addr + reg); } static inline void pch_udc_bit_set(struct pch_udc_dev *dev, unsigned long reg, unsigned long bitmask) { pch_udc_writel(dev, pch_udc_readl(dev, reg) | bitmask, reg); } static inline void pch_udc_bit_clr(struct pch_udc_dev *dev, unsigned long reg, unsigned long bitmask) { pch_udc_writel(dev, pch_udc_readl(dev, reg) & ~(bitmask), reg); } static inline u32 pch_udc_ep_readl(struct pch_udc_ep *ep, unsigned long reg) { return ioread32(ep->dev->base_addr + ep->offset_addr + reg); } static inline void pch_udc_ep_writel(struct pch_udc_ep *ep, unsigned long val, unsigned long reg) { iowrite32(val, ep->dev->base_addr + ep->offset_addr + reg); } static inline void pch_udc_ep_bit_set(struct pch_udc_ep *ep, unsigned long reg, unsigned long bitmask) { pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) | bitmask, reg); } static inline void pch_udc_ep_bit_clr(struct pch_udc_ep *ep, unsigned long reg, unsigned long bitmask) { pch_udc_ep_writel(ep, pch_udc_ep_readl(ep, reg) & ~(bitmask), reg); } /** * pch_udc_csr_busy() - Wait till idle. * @dev: Reference to pch_udc_dev structure */ static void pch_udc_csr_busy(struct pch_udc_dev *dev) { unsigned int count = 200; /* Wait till idle */ while ((pch_udc_readl(dev, UDC_CSR_BUSY_ADDR) & UDC_CSR_BUSY) && --count) cpu_relax(); if (!count) dev_err(&dev->pdev->dev, "%s: wait error\n", __func__); } /** * pch_udc_write_csr() - Write the command and status registers. * @dev: Reference to pch_udc_dev structure * @val: value to be written to CSR register * @addr: address of CSR register */ static void pch_udc_write_csr(struct pch_udc_dev *dev, unsigned long val, unsigned int ep) { unsigned long reg = PCH_UDC_CSR(ep); pch_udc_csr_busy(dev); /* Wait till idle */ pch_udc_writel(dev, val, reg); pch_udc_csr_busy(dev); /* Wait till idle */ } /** * pch_udc_read_csr() - Read the command and status registers. * @dev: Reference to pch_udc_dev structure * @addr: address of CSR register * * Return codes: content of CSR register */ static u32 pch_udc_read_csr(struct pch_udc_dev *dev, unsigned int ep) { unsigned long reg = PCH_UDC_CSR(ep); pch_udc_csr_busy(dev); /* Wait till idle */ pch_udc_readl(dev, reg); /* Dummy read */ pch_udc_csr_busy(dev); /* Wait till idle */ return pch_udc_readl(dev, reg); } /** * pch_udc_rmt_wakeup() - Initiate for remote wakeup * @dev: Reference to pch_udc_dev structure */ static inline void pch_udc_rmt_wakeup(struct pch_udc_dev *dev) { pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); mdelay(1); pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); } /** * pch_udc_get_frame() - Get the current frame from device status register * @dev: Reference to pch_udc_dev structure * Retern current frame */ static inline int pch_udc_get_frame(struct pch_udc_dev *dev) { u32 frame = pch_udc_readl(dev, UDC_DEVSTS_ADDR); return (frame & UDC_DEVSTS_TS_MASK) >> UDC_DEVSTS_TS_SHIFT; } /** * pch_udc_clear_selfpowered() - Clear the self power control * @dev: Reference to pch_udc_regs structure */ static inline void pch_udc_clear_selfpowered(struct pch_udc_dev *dev) { pch_udc_bit_clr(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP); } /** * pch_udc_set_selfpowered() - Set the self power control * @dev: Reference to pch_udc_regs structure */ static inline void pch_udc_set_selfpowered(struct pch_udc_dev *dev) { pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_SP); } /** * pch_udc_set_disconnect() - Set the disconnect status. * @dev: Reference to pch_udc_regs structure */ static inline void pch_udc_set_disconnect(struct pch_udc_dev *dev) { pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD); } /** * pch_udc_clear_disconnect() - Clear the disconnect status. * @dev: Reference to pch_udc_regs structure */ static void pch_udc_clear_disconnect(struct pch_udc_dev *dev) { /* Clear the disconnect */ pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD); mdelay(1); /* Resume USB signalling */ pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); } /** * pch_udc_reconnect() - This API initializes usb device controller, * and clear the disconnect status. * @dev: Reference to pch_udc_regs structure */ static void pch_udc_init(struct pch_udc_dev *dev); static void pch_udc_reconnect(struct pch_udc_dev *dev) { pch_udc_init(dev); /* enable device interrupts */ /* pch_udc_enable_interrupts() */ pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_UR | UDC_DEVINT_ENUM); /* Clear the disconnect */ pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_SD); mdelay(1); /* Resume USB signalling */ pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RES); } /** * pch_udc_vbus_session() - set or clearr the disconnect status. * @dev: Reference to pch_udc_regs structure * @is_active: Parameter specifying the action * 0: indicating VBUS power is ending * !0: indicating VBUS power is starting */ static inline void pch_udc_vbus_session(struct pch_udc_dev *dev, int is_active) { if (is_active) { pch_udc_reconnect(dev); dev->vbus_session = 1; } else { if (dev->driver && dev->driver->disconnect) { spin_unlock(&dev->lock); dev->driver->disconnect(&dev->gadget); spin_lock(&dev->lock); } pch_udc_set_disconnect(dev); dev->vbus_session = 0; } } /** * pch_udc_ep_set_stall() - Set the stall of endpoint * @ep: Reference to structure of type pch_udc_ep_regs */ static void pch_udc_ep_set_stall(struct pch_udc_ep *ep) { if (ep->in) { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F); pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S); } else { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S); } } /** * pch_udc_ep_clear_stall() - Clear the stall of endpoint * @ep: Reference to structure of type pch_udc_ep_regs */ static inline void pch_udc_ep_clear_stall(struct pch_udc_ep *ep) { /* Clear the stall */ pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_S); /* Clear NAK by writing CNAK */ pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK); } /** * pch_udc_ep_set_trfr_type() - Set the transfer type of endpoint * @ep: Reference to structure of type pch_udc_ep_regs * @type: Type of endpoint */ static inline void pch_udc_ep_set_trfr_type(struct pch_udc_ep *ep, u8 type) { pch_udc_ep_writel(ep, ((type << UDC_EPCTL_ET_SHIFT) & UDC_EPCTL_ET_MASK), UDC_EPCTL_ADDR); } /** * pch_udc_ep_set_bufsz() - Set the maximum packet size for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs * @buf_size: The buffer word size */ static void pch_udc_ep_set_bufsz(struct pch_udc_ep *ep, u32 buf_size, u32 ep_in) { u32 data; if (ep_in) { data = pch_udc_ep_readl(ep, UDC_BUFIN_FRAMENUM_ADDR); data = (data & 0xffff0000) | (buf_size & 0xffff); pch_udc_ep_writel(ep, data, UDC_BUFIN_FRAMENUM_ADDR); } else { data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR); data = (buf_size << 16) | (data & 0xffff); pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR); } } /** * pch_udc_ep_set_maxpkt() - Set the Max packet size for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs * @pkt_size: The packet byte size */ static void pch_udc_ep_set_maxpkt(struct pch_udc_ep *ep, u32 pkt_size) { u32 data = pch_udc_ep_readl(ep, UDC_BUFOUT_MAXPKT_ADDR); data = (data & 0xffff0000) | (pkt_size & 0xffff); pch_udc_ep_writel(ep, data, UDC_BUFOUT_MAXPKT_ADDR); } /** * pch_udc_ep_set_subptr() - Set the Setup buffer pointer for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs * @addr: Address of the register */ static inline void pch_udc_ep_set_subptr(struct pch_udc_ep *ep, u32 addr) { pch_udc_ep_writel(ep, addr, UDC_SUBPTR_ADDR); } /** * pch_udc_ep_set_ddptr() - Set the Data descriptor pointer for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs * @addr: Address of the register */ static inline void pch_udc_ep_set_ddptr(struct pch_udc_ep *ep, u32 addr) { pch_udc_ep_writel(ep, addr, UDC_DESPTR_ADDR); } /** * pch_udc_ep_set_pd() - Set the poll demand bit for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs */ static inline void pch_udc_ep_set_pd(struct pch_udc_ep *ep) { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_P); } /** * pch_udc_ep_set_rrdy() - Set the receive ready bit for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs */ static inline void pch_udc_ep_set_rrdy(struct pch_udc_ep *ep) { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY); } /** * pch_udc_ep_clear_rrdy() - Clear the receive ready bit for the endpoint * @ep: Reference to structure of type pch_udc_ep_regs */ static inline void pch_udc_ep_clear_rrdy(struct pch_udc_ep *ep) { pch_udc_ep_bit_clr(ep, UDC_EPCTL_ADDR, UDC_EPCTL_RRDY); } /** * pch_udc_set_dma() - Set the 'TDE' or RDE bit of device control * register depending on the direction specified * @dev: Reference to structure of type pch_udc_regs * @dir: whether Tx or Rx * DMA_DIR_RX: Receive * DMA_DIR_TX: Transmit */ static inline void pch_udc_set_dma(struct pch_udc_dev *dev, int dir) { if (dir == DMA_DIR_RX) pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE); else if (dir == DMA_DIR_TX) pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE); } /** * pch_udc_clear_dma() - Clear the 'TDE' or RDE bit of device control * register depending on the direction specified * @dev: Reference to structure of type pch_udc_regs * @dir: Whether Tx or Rx * DMA_DIR_RX: Receive * DMA_DIR_TX: Transmit */ static inline void pch_udc_clear_dma(struct pch_udc_dev *dev, int dir) { if (dir == DMA_DIR_RX) pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_RDE); else if (dir == DMA_DIR_TX) pch_udc_bit_clr(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_TDE); } /** * pch_udc_set_csr_done() - Set the device control register * CSR done field (bit 13) * @dev: reference to structure of type pch_udc_regs */ static inline void pch_udc_set_csr_done(struct pch_udc_dev *dev) { pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, UDC_DEVCTL_CSR_DONE); } /** * pch_udc_disable_interrupts() - Disables the specified interrupts * @dev: Reference to structure of type pch_udc_regs * @mask: Mask to disable interrupts */ static inline void pch_udc_disable_interrupts(struct pch_udc_dev *dev, u32 mask) { pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, mask); } /** * pch_udc_enable_interrupts() - Enable the specified interrupts * @dev: Reference to structure of type pch_udc_regs * @mask: Mask to enable interrupts */ static inline void pch_udc_enable_interrupts(struct pch_udc_dev *dev, u32 mask) { pch_udc_bit_clr(dev, UDC_DEVIRQMSK_ADDR, mask); } /** * pch_udc_disable_ep_interrupts() - Disable endpoint interrupts * @dev: Reference to structure of type pch_udc_regs * @mask: Mask to disable interrupts */ static inline void pch_udc_disable_ep_interrupts(struct pch_udc_dev *dev, u32 mask) { pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, mask); } /** * pch_udc_enable_ep_interrupts() - Enable endpoint interrupts * @dev: Reference to structure of type pch_udc_regs * @mask: Mask to enable interrupts */ static inline void pch_udc_enable_ep_interrupts(struct pch_udc_dev *dev, u32 mask) { pch_udc_bit_clr(dev, UDC_EPIRQMSK_ADDR, mask); } /** * pch_udc_read_device_interrupts() - Read the device interrupts * @dev: Reference to structure of type pch_udc_regs * Retern The device interrupts */ static inline u32 pch_udc_read_device_interrupts(struct pch_udc_dev *dev) { return pch_udc_readl(dev, UDC_DEVIRQSTS_ADDR); } /** * pch_udc_write_device_interrupts() - Write device interrupts * @dev: Reference to structure of type pch_udc_regs * @val: The value to be written to interrupt register */ static inline void pch_udc_write_device_interrupts(struct pch_udc_dev *dev, u32 val) { pch_udc_writel(dev, val, UDC_DEVIRQSTS_ADDR); } /** * pch_udc_read_ep_interrupts() - Read the endpoint interrupts * @dev: Reference to structure of type pch_udc_regs * Retern The endpoint interrupt */ static inline u32 pch_udc_read_ep_interrupts(struct pch_udc_dev *dev) { return pch_udc_readl(dev, UDC_EPIRQSTS_ADDR); } /** * pch_udc_write_ep_interrupts() - Clear endpoint interupts * @dev: Reference to structure of type pch_udc_regs * @val: The value to be written to interrupt register */ static inline void pch_udc_write_ep_interrupts(struct pch_udc_dev *dev, u32 val) { pch_udc_writel(dev, val, UDC_EPIRQSTS_ADDR); } /** * pch_udc_read_device_status() - Read the device status * @dev: Reference to structure of type pch_udc_regs * Retern The device status */ static inline u32 pch_udc_read_device_status(struct pch_udc_dev *dev) { return pch_udc_readl(dev, UDC_DEVSTS_ADDR); } /** * pch_udc_read_ep_control() - Read the endpoint control * @ep: Reference to structure of type pch_udc_ep_regs * Retern The endpoint control register value */ static inline u32 pch_udc_read_ep_control(struct pch_udc_ep *ep) { return pch_udc_ep_readl(ep, UDC_EPCTL_ADDR); } /** * pch_udc_clear_ep_control() - Clear the endpoint control register * @ep: Reference to structure of type pch_udc_ep_regs * Retern The endpoint control register value */ static inline void pch_udc_clear_ep_control(struct pch_udc_ep *ep) { return pch_udc_ep_writel(ep, 0, UDC_EPCTL_ADDR); } /** * pch_udc_read_ep_status() - Read the endpoint status * @ep: Reference to structure of type pch_udc_ep_regs * Retern The endpoint status */ static inline u32 pch_udc_read_ep_status(struct pch_udc_ep *ep) { return pch_udc_ep_readl(ep, UDC_EPSTS_ADDR); } /** * pch_udc_clear_ep_status() - Clear the endpoint status * @ep: Reference to structure of type pch_udc_ep_regs * @stat: Endpoint status */ static inline void pch_udc_clear_ep_status(struct pch_udc_ep *ep, u32 stat) { return pch_udc_ep_writel(ep, stat, UDC_EPSTS_ADDR); } /** * pch_udc_ep_set_nak() - Set the bit 7 (SNAK field) * of the endpoint control register * @ep: Reference to structure of type pch_udc_ep_regs */ static inline void pch_udc_ep_set_nak(struct pch_udc_ep *ep) { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_SNAK); } /** * pch_udc_ep_clear_nak() - Set the bit 8 (CNAK field) * of the endpoint control register * @ep: reference to structure of type pch_udc_ep_regs */ static void pch_udc_ep_clear_nak(struct pch_udc_ep *ep) { unsigned int loopcnt = 0; struct pch_udc_dev *dev = ep->dev; if (!(pch_udc_ep_readl(ep, UDC_EPCTL_ADDR) & UDC_EPCTL_NAK)) return; if (!ep->in) { loopcnt = 10000; while (!(pch_udc_read_ep_status(ep) & UDC_EPSTS_MRXFIFO_EMP) && --loopcnt) udelay(5); if (!loopcnt) dev_err(&dev->pdev->dev, "%s: RxFIFO not Empty\n", __func__); } loopcnt = 10000; while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_NAK) && --loopcnt) { pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_CNAK); udelay(5); } if (!loopcnt) dev_err(&dev->pdev->dev, "%s: Clear NAK not set for ep%d%s\n", __func__, ep->num, (ep->in ? "in" : "out")); } /** * pch_udc_ep_fifo_flush() - Flush the endpoint fifo * @ep: reference to structure of type pch_udc_ep_regs * @dir: direction of endpoint * 0: endpoint is OUT * !0: endpoint is IN */ static void pch_udc_ep_fifo_flush(struct pch_udc_ep *ep, int dir) { if (dir) { /* IN ep */ pch_udc_ep_bit_set(ep, UDC_EPCTL_ADDR, UDC_EPCTL_F); return; } } /** * pch_udc_ep_enable() - This api enables endpoint * @regs: Reference to structure pch_udc_ep_regs * @desc: endpoint descriptor */ static void pch_udc_ep_enable(struct pch_udc_ep *ep, struct pch_udc_cfg_data *cfg, const struct usb_endpoint_descriptor *desc) { u32 val = 0; u32 buff_size = 0; pch_udc_ep_set_trfr_type(ep, desc->bmAttributes); if (ep->in) buff_size = UDC_EPIN_BUFF_SIZE; else buff_size = UDC_EPOUT_BUFF_SIZE; pch_udc_ep_set_bufsz(ep, buff_size, ep->in); pch_udc_ep_set_maxpkt(ep, usb_endpoint_maxp(desc)); pch_udc_ep_set_nak(ep); pch_udc_ep_fifo_flush(ep, ep->in); /* Configure the endpoint */ val = ep->num << UDC_CSR_NE_NUM_SHIFT | ep->in << UDC_CSR_NE_DIR_SHIFT | ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) << UDC_CSR_NE_TYPE_SHIFT) | (cfg->cur_cfg << UDC_CSR_NE_CFG_SHIFT) | (cfg->cur_intf << UDC_CSR_NE_INTF_SHIFT) | (cfg->cur_alt << UDC_CSR_NE_ALT_SHIFT) | usb_endpoint_maxp(desc) << UDC_CSR_NE_MAX_PKT_SHIFT; if (ep->in) pch_udc_write_csr(ep->dev, val, UDC_EPIN_IDX(ep->num)); else pch_udc_write_csr(ep->dev, val, UDC_EPOUT_IDX(ep->num)); } /** * pch_udc_ep_disable() - This api disables endpoint * @regs: Reference to structure pch_udc_ep_regs */ static void pch_udc_ep_disable(struct pch_udc_ep *ep) { if (ep->in) { /* flush the fifo */ pch_udc_ep_writel(ep, UDC_EPCTL_F, UDC_EPCTL_ADDR); /* set NAK */ pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR); pch_udc_ep_bit_set(ep, UDC_EPSTS_ADDR, UDC_EPSTS_IN); } else { /* set NAK */ pch_udc_ep_writel(ep, UDC_EPCTL_SNAK, UDC_EPCTL_ADDR); } /* reset desc pointer */ pch_udc_ep_writel(ep, 0, UDC_DESPTR_ADDR); } /** * pch_udc_wait_ep_stall() - Wait EP stall. * @dev: Reference to pch_udc_dev structure */ static void pch_udc_wait_ep_stall(struct pch_udc_ep *ep) { unsigned int count = 10000; /* Wait till idle */ while ((pch_udc_read_ep_control(ep) & UDC_EPCTL_S) && --count) udelay(5); if (!count) dev_err(&ep->dev->pdev->dev, "%s: wait error\n", __func__); } /** * pch_udc_init() - This API initializes usb device controller * @dev: Rreference to pch_udc_regs structure */ static void pch_udc_init(struct pch_udc_dev *dev) { if (NULL == dev) { pr_err("%s: Invalid address\n", __func__); return; } /* Soft Reset and Reset PHY */ pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR); pch_udc_writel(dev, UDC_SRST | UDC_PSRST, UDC_SRST_ADDR); mdelay(1); pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR); pch_udc_writel(dev, 0x00, UDC_SRST_ADDR); mdelay(1); /* mask and clear all device interrupts */ pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK); pch_udc_bit_set(dev, UDC_DEVIRQSTS_ADDR, UDC_DEVINT_MSK); /* mask and clear all ep interrupts */ pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL); pch_udc_bit_set(dev, UDC_EPIRQSTS_ADDR, UDC_EPINT_MSK_DISABLE_ALL); /* enable dynamic CSR programmingi, self powered and device speed */ if (speed_fs) pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG | UDC_DEVCFG_SP | UDC_DEVCFG_SPD_FS); else /* defaul high speed */ pch_udc_bit_set(dev, UDC_DEVCFG_ADDR, UDC_DEVCFG_CSR_PRG | UDC_DEVCFG_SP | UDC_DEVCFG_SPD_HS); pch_udc_bit_set(dev, UDC_DEVCTL_ADDR, (PCH_UDC_THLEN << UDC_DEVCTL_THLEN_SHIFT) | (PCH_UDC_BRLEN << UDC_DEVCTL_BRLEN_SHIFT) | UDC_DEVCTL_MODE | UDC_DEVCTL_BREN | UDC_DEVCTL_THE); } /** * pch_udc_exit() - This API exit usb device controller * @dev: Reference to pch_udc_regs structure */ static void pch_udc_exit(struct pch_udc_dev *dev) { /* mask all device interrupts */ pch_udc_bit_set(dev, UDC_DEVIRQMSK_ADDR, UDC_DEVINT_MSK); /* mask all ep interrupts */ pch_udc_bit_set(dev, UDC_EPIRQMSK_ADDR, UDC_EPINT_MSK_DISABLE_ALL); /* put device in disconnected state */ pch_udc_set_disconnect(dev); } /** * pch_udc_pcd_get_frame() - This API is invoked to get the current frame number * @gadget: Reference to the gadget driver * * Return codes: * 0: Success * -EINVAL: If the gadget passed is NULL */ static int pch_udc_pcd_get_frame(struct usb_gadget *gadget) { struct pch_udc_dev *dev; if (!gadget) return -EINVAL; dev = container_of(gadget, struct pch_udc_dev, gadget); return pch_udc_get_frame(dev); } /** * pch_udc_pcd_wakeup() - This API is invoked to initiate a remote wakeup * @gadget: Reference to the gadget driver * * Return codes: * 0: Success * -EINVAL: If the gadget passed is NULL */ static int pch_udc_pcd_wakeup(struct usb_gadget *gadget) { struct pch_udc_dev *dev; unsigned long flags; if (!gadget) return -EINVAL; dev = container_of(gadget, struct pch_udc_dev, gadget); spin_lock_irqsave(&dev->lock, flags); pch_udc_rmt_wakeup(dev); spin_unlock_irqrestore(&dev->lock, flags); return 0; } /** * pch_udc_pcd_selfpowered() - This API is invoked to specify whether the device * is self powered or not * @gadget: Reference to the gadget driver * @value: Specifies self powered or not * * Return codes: * 0: Success * -EINVAL: If the gadget passed is NULL */ static int pch_udc_pcd_selfpowered(struct usb_gadget *gadget, int value) { struct pch_udc_dev *dev; if (!gadget) return -EINVAL; dev = container_of(gadget, struct pch_udc_dev, gadget); if (value) pch_udc_set_selfpowered(dev); else pch_udc_clear_selfpowered(dev); return 0; } /** * pch_udc_pcd_pullup() - This API is invoked to make the device * visible/invisible to the host * @gadget: Reference to the gadget driver * @is_on: Specifies whether the pull up is made active or inactive * * Return codes: * 0: Success * -EINVAL: If the gadget passed is NULL */ static int pch_udc_pcd_pullup(struct usb_gadget *gadget, int is_on) { struct pch_udc_dev *dev; if (!gadget) return -EINVAL; dev = container_of(gadget, struct pch_udc_dev, gadget); if (is_on) { pch_udc_reconnect(dev); } else { if (dev->driver && dev->driver->disconnect) { spin_unlock(&dev->lock); dev->driver->disconnect(&dev->gadget); spin_lock(&dev->lock); } pch_udc_set_disconnect(dev); } return 0; } /** * pch_udc_pcd_vbus_session() - This API is used by a driver for an external * transceiver (or GPIO) that * detects a VBUS power session starting/ending * @gadget: Reference to the gadget driver * @is_active: specifies whether the session is starting or ending * * Return codes: * 0: Success * -EINVAL: If the gadget passed is NULL */ static int pch_udc_pcd_vbus_session(struct usb_gadget *gadget, int is_active) { struct pch_udc_dev *dev; if (!gadget) return -EINVAL; dev = container_of(gadget, struct pch_udc_dev, gadget); pch_udc_vbus_session(dev, is_active); return 0; } /** * pch_udc_pcd_vbus_draw() - This API is used by gadget drivers during * SET_CONFIGURATION calls to * specify how much power the device can consume * @gadget: Reference to the gadget driver * @mA: specifies the current limit in 2mA unit * * Return codes: * -EINVAL: If the gadget passed is NULL * -EOPNOTSUPP: */ static int pch_udc_pcd_vbus_draw(struct usb_gadget *gadget, unsigned int mA) { return -EOPNOTSUPP; } static int pch_udc_start(struct usb_gadget *g, struct usb_gadget_driver *driver); static int pch_udc_stop(struct usb_gadget *g, struct usb_gadget_driver *driver); static const struct usb_gadget_ops pch_udc_ops = { .get_frame = pch_udc_pcd_get_frame, .wakeup = pch_udc_pcd_wakeup, .set_selfpowered = pch_udc_pcd_selfpowered, .pullup = pch_udc_pcd_pullup, .vbus_session = pch_udc_pcd_vbus_session, .vbus_draw = pch_udc_pcd_vbus_draw, .udc_start = pch_udc_start, .udc_stop = pch_udc_stop, }; /** * pch_vbus_gpio_get_value() - This API gets value of GPIO port as VBUS status. * @dev: Reference to the driver structure * * Return value: * 1: VBUS is high * 0: VBUS is low * -1: It is not enable to detect VBUS using GPIO */ static int pch_vbus_gpio_get_value(struct pch_udc_dev *dev) { int vbus = 0; if (dev->vbus_gpio.port) vbus = gpio_get_value(dev->vbus_gpio.port) ? 1 : 0; else vbus = -1; return vbus; } /** * pch_vbus_gpio_work_fall() - This API keeps watch on VBUS becoming Low. * If VBUS is Low, disconnect is processed * @irq_work: Structure for WorkQueue * */ static void pch_vbus_gpio_work_fall(struct work_struct *irq_work) { struct pch_vbus_gpio_data *vbus_gpio = container_of(irq_work, struct pch_vbus_gpio_data, irq_work_fall); struct pch_udc_dev *dev = container_of(vbus_gpio, struct pch_udc_dev, vbus_gpio); int vbus_saved = -1; int vbus; int count; if (!dev->vbus_gpio.port) return; for (count = 0; count < (PCH_VBUS_PERIOD / PCH_VBUS_INTERVAL); count++) { vbus = pch_vbus_gpio_get_value(dev); if ((vbus_saved == vbus) && (vbus == 0)) { dev_dbg(&dev->pdev->dev, "VBUS fell"); if (dev->driver && dev->driver->disconnect) { dev->driver->disconnect( &dev->gadget); } if (dev->vbus_gpio.intr) pch_udc_init(dev); else pch_udc_reconnect(dev); return; } vbus_saved = vbus; mdelay(PCH_VBUS_INTERVAL); } } /** * pch_vbus_gpio_work_rise() - This API checks VBUS is High. * If VBUS is High, connect is processed * @irq_work: Structure for WorkQueue * */ static void pch_vbus_gpio_work_rise(struct work_struct *irq_work) { struct pch_vbus_gpio_data *vbus_gpio = container_of(irq_work, struct pch_vbus_gpio_data, irq_work_rise); struct pch_udc_dev *dev = container_of(vbus_gpio, struct pch_udc_dev, vbus_gpio); int vbus; if (!dev->vbus_gpio.port) return; mdelay(PCH_VBUS_INTERVAL); vbus = pch_vbus_gpio_get_value(dev); if (vbus == 1) { dev_dbg(&dev->pdev->dev, "VBUS rose"); pch_udc_reconnect(dev); return; } } /** * pch_vbus_gpio_irq() - IRQ handler for GPIO intrerrupt for changing VBUS * @irq: Interrupt request number * @dev: Reference to the device structure * * Return codes: * 0: Success * -EINVAL: GPIO port is invalid or can't be initialized. */ static irqreturn_t pch_vbus_gpio_irq(int irq, void *data) { struct pch_udc_dev *dev = (struct pch_udc_dev *)data; if (!dev->vbus_gpio.port || !dev->vbus_gpio.intr) return IRQ_NONE; if (pch_vbus_gpio_get_value(dev)) schedule_work(&dev->vbus_gpio.irq_work_rise); else schedule_work(&dev->vbus_gpio.irq_work_fall); return IRQ_HANDLED; } /** * pch_vbus_gpio_init() - This API initializes GPIO port detecting VBUS. * @dev: Reference to the driver structure * @vbus_gpio Number of GPIO port to detect gpio * * Return codes: * 0: Success * -EINVAL: GPIO port is invalid or can't be initialized. */ static int pch_vbus_gpio_init(struct pch_udc_dev *dev, int vbus_gpio_port) { int err; int irq_num = 0; dev->vbus_gpio.port = 0; dev->vbus_gpio.intr = 0; if (vbus_gpio_port <= -1) return -EINVAL; err = gpio_is_valid(vbus_gpio_port); if (!err) { pr_err("%s: gpio port %d is invalid\n", __func__, vbus_gpio_port); return -EINVAL; } err = gpio_request(vbus_gpio_port, "pch_vbus"); if (err) { pr_err("%s: can't request gpio port %d, err: %d\n", __func__, vbus_gpio_port, err); return -EINVAL; } dev->vbus_gpio.port = vbus_gpio_port; gpio_direction_input(vbus_gpio_port); INIT_WORK(&dev->vbus_gpio.irq_work_fall, pch_vbus_gpio_work_fall); irq_num = gpio_to_irq(vbus_gpio_port); if (irq_num > 0) { irq_set_irq_type(irq_num, IRQ_TYPE_EDGE_BOTH); err = request_irq(irq_num, pch_vbus_gpio_irq, 0, "vbus_detect", dev); if (!err) { dev->vbus_gpio.intr = irq_num; INIT_WORK(&dev->vbus_gpio.irq_work_rise, pch_vbus_gpio_work_rise); } else { pr_err("%s: can't request irq %d, err: %d\n", __func__, irq_num, err); } } return 0; } /** * pch_vbus_gpio_free() - This API frees resources of GPIO port * @dev: Reference to the driver structure */ static void pch_vbus_gpio_free(struct pch_udc_dev *dev) { if (dev->vbus_gpio.intr) free_irq(dev->vbus_gpio.intr, dev); if (dev->vbus_gpio.port) gpio_free(dev->vbus_gpio.port); } /** * complete_req() - This API is invoked from the driver when processing * of a request is complete * @ep: Reference to the endpoint structure * @req: Reference to the request structure * @status: Indicates the success/failure of completion */ static void complete_req(struct pch_udc_ep *ep, struct pch_udc_request *req, int status) __releases(&dev->lock) __acquires(&dev->lock) { struct pch_udc_dev *dev; unsigned halted = ep->halted; list_del_init(&req->queue); /* set new status if pending */ if (req->req.status == -EINPROGRESS) req->req.status = status; else status = req->req.status; dev = ep->dev; if (req->dma_mapped) { if (req->dma == DMA_ADDR_INVALID) { if (ep->in) dma_unmap_single(&dev->pdev->dev, req->req.dma, req->req.length, DMA_TO_DEVICE); else dma_unmap_single(&dev->pdev->dev, req->req.dma, req->req.length, DMA_FROM_DEVICE); req->req.dma = DMA_ADDR_INVALID; } else { if (ep->in) dma_unmap_single(&dev->pdev->dev, req->dma, req->req.length, DMA_TO_DEVICE); else { dma_unmap_single(&dev->pdev->dev, req->dma, req->req.length, DMA_FROM_DEVICE); memcpy(req->req.buf, req->buf, req->req.length); } kfree(req->buf); req->dma = DMA_ADDR_INVALID; } req->dma_mapped = 0; } ep->halted = 1; spin_unlock(&dev->lock); if (!ep->in) pch_udc_ep_clear_rrdy(ep); req->req.complete(&ep->ep, &req->req); spin_lock(&dev->lock); ep->halted = halted; } /** * empty_req_queue() - This API empties the request queue of an endpoint * @ep: Reference to the endpoint structure */ static void empty_req_queue(struct pch_udc_ep *ep) { struct pch_udc_request *req; ep->halted = 1; while (!list_empty(&ep->queue)) { req = list_entry(ep->queue.next, struct pch_udc_request, queue); complete_req(ep, req, -ESHUTDOWN); /* Remove from list */ } } /** * pch_udc_free_dma_chain() - This function frees the DMA chain created * for the request * @dev Reference to the driver structure * @req Reference to the request to be freed * * Return codes: * 0: Success */ static void pch_udc_free_dma_chain(struct pch_udc_dev *dev, struct pch_udc_request *req) { struct pch_udc_data_dma_desc *td = req->td_data; unsigned i = req->chain_len; dma_addr_t addr2; dma_addr_t addr = (dma_addr_t)td->next; td->next = 0x00; for (; i > 1; --i) { /* do not free first desc., will be done by free for request */ td = phys_to_virt(addr); addr2 = (dma_addr_t)td->next; pci_pool_free(dev->data_requests, td, addr); td->next = 0x00; addr = addr2; } req->chain_len = 1; } /** * pch_udc_create_dma_chain() - This function creates or reinitializes * a DMA chain * @ep: Reference to the endpoint structure * @req: Reference to the request * @buf_len: The buffer length * @gfp_flags: Flags to be used while mapping the data buffer * * Return codes: * 0: success, * -ENOMEM: pci_pool_alloc invocation fails */ static int pch_udc_create_dma_chain(struct pch_udc_ep *ep, struct pch_udc_request *req, unsigned long buf_len, gfp_t gfp_flags) { struct pch_udc_data_dma_desc *td = req->td_data, *last; unsigned long bytes = req->req.length, i = 0; dma_addr_t dma_addr; unsigned len = 1; if (req->chain_len > 1) pch_udc_free_dma_chain(ep->dev, req); if (req->dma == DMA_ADDR_INVALID) td->dataptr = req->req.dma; else td->dataptr = req->dma; td->status = PCH_UDC_BS_HST_BSY; for (; ; bytes -= buf_len, ++len) { td->status = PCH_UDC_BS_HST_BSY | min(buf_len, bytes); if (bytes <= buf_len) break; last = td; td = pci_pool_alloc(ep->dev->data_requests, gfp_flags, &dma_addr); if (!td) goto nomem; i += buf_len; td->dataptr = req->td_data->dataptr + i; last->next = dma_addr; } req->td_data_last = td; td->status |= PCH_UDC_DMA_LAST; td->next = req->td_data_phys; req->chain_len = len; return 0; nomem: if (len > 1) { req->chain_len = len; pch_udc_free_dma_chain(ep->dev, req); } req->chain_len = 1; return -ENOMEM; } /** * prepare_dma() - This function creates and initializes the DMA chain * for the request * @ep: Reference to the endpoint structure * @req: Reference to the request * @gfp: Flag to be used while mapping the data buffer * * Return codes: * 0: Success * Other 0: linux error number on failure */ static int prepare_dma(struct pch_udc_ep *ep, struct pch_udc_request *req, gfp_t gfp) { int retval; /* Allocate and create a DMA chain */ retval = pch_udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp); if (retval) { pr_err("%s: could not create DMA chain:%d\n", __func__, retval); return retval; } if (ep->in) req->td_data->status = (req->td_data->status & ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY; return 0; } /** * process_zlp() - This function process zero length packets * from the gadget driver * @ep: Reference to the endpoint structure * @req: Reference to the request */ static void process_zlp(struct pch_udc_ep *ep, struct pch_udc_request *req) { struct pch_udc_dev *dev = ep->dev; /* IN zlp's are handled by hardware */ complete_req(ep, req, 0); /* if set_config or set_intf is waiting for ack by zlp * then set CSR_DONE */ if (dev->set_cfg_not_acked) { pch_udc_set_csr_done(dev); dev->set_cfg_not_acked = 0; } /* setup command is ACK'ed now by zlp */ if (!dev->stall && dev->waiting_zlp_ack) { pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX])); dev->waiting_zlp_ack = 0; } } /** * pch_udc_start_rxrequest() - This function starts the receive requirement. * @ep: Reference to the endpoint structure * @req: Reference to the request structure */ static void pch_udc_start_rxrequest(struct pch_udc_ep *ep, struct pch_udc_request *req) { struct pch_udc_data_dma_desc *td_data; pch_udc_clear_dma(ep->dev, DMA_DIR_RX); td_data = req->td_data; /* Set the status bits for all descriptors */ while (1) { td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY; if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST) break; td_data = phys_to_virt(td_data->next); } /* Write the descriptor pointer */ pch_udc_ep_set_ddptr(ep, req->td_data_phys); req->dma_going = 1; pch_udc_enable_ep_interrupts(ep->dev, UDC_EPINT_OUT_EP0 << ep->num); pch_udc_set_dma(ep->dev, DMA_DIR_RX); pch_udc_ep_clear_nak(ep); pch_udc_ep_set_rrdy(ep); } /** * pch_udc_pcd_ep_enable() - This API enables the endpoint. It is called * from gadget driver * @usbep: Reference to the USB endpoint structure * @desc: Reference to the USB endpoint descriptor structure * * Return codes: * 0: Success * -EINVAL: * -ESHUTDOWN: */ static int pch_udc_pcd_ep_enable(struct usb_ep *usbep, const struct usb_endpoint_descriptor *desc) { struct pch_udc_ep *ep; struct pch_udc_dev *dev; unsigned long iflags; if (!usbep || (usbep->name == ep0_string) || !desc || (desc->bDescriptorType != USB_DT_ENDPOINT) || !desc->wMaxPacketSize) return -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN)) return -ESHUTDOWN; spin_lock_irqsave(&dev->lock, iflags); ep->ep.desc = desc; ep->halted = 0; pch_udc_ep_enable(ep, &ep->dev->cfg_data, desc); ep->ep.maxpacket = usb_endpoint_maxp(desc); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); spin_unlock_irqrestore(&dev->lock, iflags); return 0; } /** * pch_udc_pcd_ep_disable() - This API disables endpoint and is called * from gadget driver * @usbep Reference to the USB endpoint structure * * Return codes: * 0: Success * -EINVAL: */ static int pch_udc_pcd_ep_disable(struct usb_ep *usbep) { struct pch_udc_ep *ep; struct pch_udc_dev *dev; unsigned long iflags; if (!usbep) return -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if ((usbep->name == ep0_string) || !ep->ep.desc) return -EINVAL; spin_lock_irqsave(&ep->dev->lock, iflags); empty_req_queue(ep); ep->halted = 1; pch_udc_ep_disable(ep); pch_udc_disable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); ep->ep.desc = NULL; INIT_LIST_HEAD(&ep->queue); spin_unlock_irqrestore(&ep->dev->lock, iflags); return 0; } /** * pch_udc_alloc_request() - This function allocates request structure. * It is called by gadget driver * @usbep: Reference to the USB endpoint structure * @gfp: Flag to be used while allocating memory * * Return codes: * NULL: Failure * Allocated address: Success */ static struct usb_request *pch_udc_alloc_request(struct usb_ep *usbep, gfp_t gfp) { struct pch_udc_request *req; struct pch_udc_ep *ep; struct pch_udc_data_dma_desc *dma_desc; struct pch_udc_dev *dev; if (!usbep) return NULL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; req = kzalloc(sizeof *req, gfp); if (!req) return NULL; req->req.dma = DMA_ADDR_INVALID; req->dma = DMA_ADDR_INVALID; INIT_LIST_HEAD(&req->queue); if (!ep->dev->dma_addr) return &req->req; /* ep0 in requests are allocated from data pool here */ dma_desc = pci_pool_alloc(ep->dev->data_requests, gfp, &req->td_data_phys); if (NULL == dma_desc) { kfree(req); return NULL; } /* prevent from using desc. - set HOST BUSY */ dma_desc->status |= PCH_UDC_BS_HST_BSY; dma_desc->dataptr = __constant_cpu_to_le32(DMA_ADDR_INVALID); req->td_data = dma_desc; req->td_data_last = dma_desc; req->chain_len = 1; return &req->req; } /** * pch_udc_free_request() - This function frees request structure. * It is called by gadget driver * @usbep: Reference to the USB endpoint structure * @usbreq: Reference to the USB request */ static void pch_udc_free_request(struct usb_ep *usbep, struct usb_request *usbreq) { struct pch_udc_ep *ep; struct pch_udc_request *req; struct pch_udc_dev *dev; if (!usbep || !usbreq) return; ep = container_of(usbep, struct pch_udc_ep, ep); req = container_of(usbreq, struct pch_udc_request, req); dev = ep->dev; if (!list_empty(&req->queue)) dev_err(&dev->pdev->dev, "%s: %s req=0x%p queue not empty\n", __func__, usbep->name, req); if (req->td_data != NULL) { if (req->chain_len > 1) pch_udc_free_dma_chain(ep->dev, req); pci_pool_free(ep->dev->data_requests, req->td_data, req->td_data_phys); } kfree(req); } /** * pch_udc_pcd_queue() - This function queues a request packet. It is called * by gadget driver * @usbep: Reference to the USB endpoint structure * @usbreq: Reference to the USB request * @gfp: Flag to be used while mapping the data buffer * * Return codes: * 0: Success * linux error number: Failure */ static int pch_udc_pcd_queue(struct usb_ep *usbep, struct usb_request *usbreq, gfp_t gfp) { int retval = 0; struct pch_udc_ep *ep; struct pch_udc_dev *dev; struct pch_udc_request *req; unsigned long iflags; if (!usbep || !usbreq || !usbreq->complete || !usbreq->buf) return -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if (!ep->ep.desc && ep->num) return -EINVAL; req = container_of(usbreq, struct pch_udc_request, req); if (!list_empty(&req->queue)) return -EINVAL; if (!dev->driver || (dev->gadget.speed == USB_SPEED_UNKNOWN)) return -ESHUTDOWN; spin_lock_irqsave(&dev->lock, iflags); /* map the buffer for dma */ if (usbreq->length && ((usbreq->dma == DMA_ADDR_INVALID) || !usbreq->dma)) { if (!((unsigned long)(usbreq->buf) & 0x03)) { if (ep->in) usbreq->dma = dma_map_single(&dev->pdev->dev, usbreq->buf, usbreq->length, DMA_TO_DEVICE); else usbreq->dma = dma_map_single(&dev->pdev->dev, usbreq->buf, usbreq->length, DMA_FROM_DEVICE); } else { req->buf = kzalloc(usbreq->length, GFP_ATOMIC); if (!req->buf) { retval = -ENOMEM; goto probe_end; } if (ep->in) { memcpy(req->buf, usbreq->buf, usbreq->length); req->dma = dma_map_single(&dev->pdev->dev, req->buf, usbreq->length, DMA_TO_DEVICE); } else req->dma = dma_map_single(&dev->pdev->dev, req->buf, usbreq->length, DMA_FROM_DEVICE); } req->dma_mapped = 1; } if (usbreq->length > 0) { retval = prepare_dma(ep, req, GFP_ATOMIC); if (retval) goto probe_end; } usbreq->actual = 0; usbreq->status = -EINPROGRESS; req->dma_done = 0; if (list_empty(&ep->queue) && !ep->halted) { /* no pending transfer, so start this req */ if (!usbreq->length) { process_zlp(ep, req); retval = 0; goto probe_end; } if (!ep->in) { pch_udc_start_rxrequest(ep, req); } else { /* * For IN trfr the descriptors will be programmed and * P bit will be set when * we get an IN token */ pch_udc_wait_ep_stall(ep); pch_udc_ep_clear_nak(ep); pch_udc_enable_ep_interrupts(ep->dev, (1 << ep->num)); } } /* Now add this request to the ep's pending requests */ if (req != NULL) list_add_tail(&req->queue, &ep->queue); probe_end: spin_unlock_irqrestore(&dev->lock, iflags); return retval; } /** * pch_udc_pcd_dequeue() - This function de-queues a request packet. * It is called by gadget driver * @usbep: Reference to the USB endpoint structure * @usbreq: Reference to the USB request * * Return codes: * 0: Success * linux error number: Failure */ static int pch_udc_pcd_dequeue(struct usb_ep *usbep, struct usb_request *usbreq) { struct pch_udc_ep *ep; struct pch_udc_request *req; struct pch_udc_dev *dev; unsigned long flags; int ret = -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if (!usbep || !usbreq || (!ep->ep.desc && ep->num)) return ret; req = container_of(usbreq, struct pch_udc_request, req); spin_lock_irqsave(&ep->dev->lock, flags); /* make sure it's still queued on this endpoint */ list_for_each_entry(req, &ep->queue, queue) { if (&req->req == usbreq) { pch_udc_ep_set_nak(ep); if (!list_empty(&req->queue)) complete_req(ep, req, -ECONNRESET); ret = 0; break; } } spin_unlock_irqrestore(&ep->dev->lock, flags); return ret; } /** * pch_udc_pcd_set_halt() - This function Sets or clear the endpoint halt * feature * @usbep: Reference to the USB endpoint structure * @halt: Specifies whether to set or clear the feature * * Return codes: * 0: Success * linux error number: Failure */ static int pch_udc_pcd_set_halt(struct usb_ep *usbep, int halt) { struct pch_udc_ep *ep; struct pch_udc_dev *dev; unsigned long iflags; int ret; if (!usbep) return -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if (!ep->ep.desc && !ep->num) return -EINVAL; if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN)) return -ESHUTDOWN; spin_lock_irqsave(&udc_stall_spinlock, iflags); if (list_empty(&ep->queue)) { if (halt) { if (ep->num == PCH_UDC_EP0) ep->dev->stall = 1; pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } else { pch_udc_ep_clear_stall(ep); } ret = 0; } else { ret = -EAGAIN; } spin_unlock_irqrestore(&udc_stall_spinlock, iflags); return ret; } /** * pch_udc_pcd_set_wedge() - This function Sets or clear the endpoint * halt feature * @usbep: Reference to the USB endpoint structure * @halt: Specifies whether to set or clear the feature * * Return codes: * 0: Success * linux error number: Failure */ static int pch_udc_pcd_set_wedge(struct usb_ep *usbep) { struct pch_udc_ep *ep; struct pch_udc_dev *dev; unsigned long iflags; int ret; if (!usbep) return -EINVAL; ep = container_of(usbep, struct pch_udc_ep, ep); dev = ep->dev; if (!ep->ep.desc && !ep->num) return -EINVAL; if (!ep->dev->driver || (ep->dev->gadget.speed == USB_SPEED_UNKNOWN)) return -ESHUTDOWN; spin_lock_irqsave(&udc_stall_spinlock, iflags); if (!list_empty(&ep->queue)) { ret = -EAGAIN; } else { if (ep->num == PCH_UDC_EP0) ep->dev->stall = 1; pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); ep->dev->prot_stall = 1; ret = 0; } spin_unlock_irqrestore(&udc_stall_spinlock, iflags); return ret; } /** * pch_udc_pcd_fifo_flush() - This function Flush the FIFO of specified endpoint * @usbep: Reference to the USB endpoint structure */ static void pch_udc_pcd_fifo_flush(struct usb_ep *usbep) { struct pch_udc_ep *ep; if (!usbep) return; ep = container_of(usbep, struct pch_udc_ep, ep); if (ep->ep.desc || !ep->num) pch_udc_ep_fifo_flush(ep, ep->in); } static const struct usb_ep_ops pch_udc_ep_ops = { .enable = pch_udc_pcd_ep_enable, .disable = pch_udc_pcd_ep_disable, .alloc_request = pch_udc_alloc_request, .free_request = pch_udc_free_request, .queue = pch_udc_pcd_queue, .dequeue = pch_udc_pcd_dequeue, .set_halt = pch_udc_pcd_set_halt, .set_wedge = pch_udc_pcd_set_wedge, .fifo_status = NULL, .fifo_flush = pch_udc_pcd_fifo_flush, }; /** * pch_udc_init_setup_buff() - This function initializes the SETUP buffer * @td_stp: Reference to the SETP buffer structure */ static void pch_udc_init_setup_buff(struct pch_udc_stp_dma_desc *td_stp) { static u32 pky_marker; if (!td_stp) return; td_stp->reserved = ++pky_marker; memset(&td_stp->request, 0xFF, sizeof td_stp->request); td_stp->status = PCH_UDC_BS_HST_RDY; } /** * pch_udc_start_next_txrequest() - This function starts * the next transmission requirement * @ep: Reference to the endpoint structure */ static void pch_udc_start_next_txrequest(struct pch_udc_ep *ep) { struct pch_udc_request *req; struct pch_udc_data_dma_desc *td_data; if (pch_udc_read_ep_control(ep) & UDC_EPCTL_P) return; if (list_empty(&ep->queue)) return; /* next request */ req = list_entry(ep->queue.next, struct pch_udc_request, queue); if (req->dma_going) return; if (!req->td_data) return; pch_udc_wait_ep_stall(ep); req->dma_going = 1; pch_udc_ep_set_ddptr(ep, 0); td_data = req->td_data; while (1) { td_data->status = (td_data->status & ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY; if ((td_data->status & PCH_UDC_DMA_LAST) == PCH_UDC_DMA_LAST) break; td_data = phys_to_virt(td_data->next); } pch_udc_ep_set_ddptr(ep, req->td_data_phys); pch_udc_set_dma(ep->dev, DMA_DIR_TX); pch_udc_ep_set_pd(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); pch_udc_ep_clear_nak(ep); } /** * pch_udc_complete_transfer() - This function completes a transfer * @ep: Reference to the endpoint structure */ static void pch_udc_complete_transfer(struct pch_udc_ep *ep) { struct pch_udc_request *req; struct pch_udc_dev *dev = ep->dev; if (list_empty(&ep->queue)) return; req = list_entry(ep->queue.next, struct pch_udc_request, queue); if ((req->td_data_last->status & PCH_UDC_BUFF_STS) != PCH_UDC_BS_DMA_DONE) return; if ((req->td_data_last->status & PCH_UDC_RXTX_STS) != PCH_UDC_RTS_SUCC) { dev_err(&dev->pdev->dev, "Invalid RXTX status (0x%08x) " "epstatus=0x%08x\n", (req->td_data_last->status & PCH_UDC_RXTX_STS), (int)(ep->epsts)); return; } req->req.actual = req->req.length; req->td_data_last->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST; req->td_data->status = PCH_UDC_BS_HST_BSY | PCH_UDC_DMA_LAST; complete_req(ep, req, 0); req->dma_going = 0; if (!list_empty(&ep->queue)) { pch_udc_wait_ep_stall(ep); pch_udc_ep_clear_nak(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } else { pch_udc_disable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } } /** * pch_udc_complete_receiver() - This function completes a receiver * @ep: Reference to the endpoint structure */ static void pch_udc_complete_receiver(struct pch_udc_ep *ep) { struct pch_udc_request *req; struct pch_udc_dev *dev = ep->dev; unsigned int count; struct pch_udc_data_dma_desc *td; dma_addr_t addr; if (list_empty(&ep->queue)) return; /* next request */ req = list_entry(ep->queue.next, struct pch_udc_request, queue); pch_udc_clear_dma(ep->dev, DMA_DIR_RX); pch_udc_ep_set_ddptr(ep, 0); if ((req->td_data_last->status & PCH_UDC_BUFF_STS) == PCH_UDC_BS_DMA_DONE) td = req->td_data_last; else td = req->td_data; while (1) { if ((td->status & PCH_UDC_RXTX_STS) != PCH_UDC_RTS_SUCC) { dev_err(&dev->pdev->dev, "Invalid RXTX status=0x%08x " "epstatus=0x%08x\n", (req->td_data->status & PCH_UDC_RXTX_STS), (int)(ep->epsts)); return; } if ((td->status & PCH_UDC_BUFF_STS) == PCH_UDC_BS_DMA_DONE) if (td->status & PCH_UDC_DMA_LAST) { count = td->status & PCH_UDC_RXTX_BYTES; break; } if (td == req->td_data_last) { dev_err(&dev->pdev->dev, "Not complete RX descriptor"); return; } addr = (dma_addr_t)td->next; td = phys_to_virt(addr); } /* on 64k packets the RXBYTES field is zero */ if (!count && (req->req.length == UDC_DMA_MAXPACKET)) count = UDC_DMA_MAXPACKET; req->td_data->status |= PCH_UDC_DMA_LAST; td->status |= PCH_UDC_BS_HST_BSY; req->dma_going = 0; req->req.actual = count; complete_req(ep, req, 0); /* If there is a new/failed requests try that now */ if (!list_empty(&ep->queue)) { req = list_entry(ep->queue.next, struct pch_udc_request, queue); pch_udc_start_rxrequest(ep, req); } } /** * pch_udc_svc_data_in() - This function process endpoint interrupts * for IN endpoints * @dev: Reference to the device structure * @ep_num: Endpoint that generated the interrupt */ static void pch_udc_svc_data_in(struct pch_udc_dev *dev, int ep_num) { u32 epsts; struct pch_udc_ep *ep; ep = &dev->ep[UDC_EPIN_IDX(ep_num)]; epsts = ep->epsts; ep->epsts = 0; if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE | UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY | UDC_EPSTS_RSS | UDC_EPSTS_XFERDONE))) return; if ((epsts & UDC_EPSTS_BNA)) return; if (epsts & UDC_EPSTS_HE) return; if (epsts & UDC_EPSTS_RSS) { pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } if (epsts & UDC_EPSTS_RCS) { if (!dev->prot_stall) { pch_udc_ep_clear_stall(ep); } else { pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } } if (epsts & UDC_EPSTS_TDC) pch_udc_complete_transfer(ep); /* On IN interrupt, provide data if we have any */ if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_RSS) && !(epsts & UDC_EPSTS_TDC) && !(epsts & UDC_EPSTS_TXEMPTY)) pch_udc_start_next_txrequest(ep); } /** * pch_udc_svc_data_out() - Handles interrupts from OUT endpoint * @dev: Reference to the device structure * @ep_num: Endpoint that generated the interrupt */ static void pch_udc_svc_data_out(struct pch_udc_dev *dev, int ep_num) { u32 epsts; struct pch_udc_ep *ep; struct pch_udc_request *req = NULL; ep = &dev->ep[UDC_EPOUT_IDX(ep_num)]; epsts = ep->epsts; ep->epsts = 0; if ((epsts & UDC_EPSTS_BNA) && (!list_empty(&ep->queue))) { /* next request */ req = list_entry(ep->queue.next, struct pch_udc_request, queue); if ((req->td_data_last->status & PCH_UDC_BUFF_STS) != PCH_UDC_BS_DMA_DONE) { if (!req->dma_going) pch_udc_start_rxrequest(ep, req); return; } } if (epsts & UDC_EPSTS_HE) return; if (epsts & UDC_EPSTS_RSS) { pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } if (epsts & UDC_EPSTS_RCS) { if (!dev->prot_stall) { pch_udc_ep_clear_stall(ep); } else { pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } } if (((epsts & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) == UDC_EPSTS_OUT_DATA) { if (ep->dev->prot_stall == 1) { pch_udc_ep_set_stall(ep); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); } else { pch_udc_complete_receiver(ep); } } if (list_empty(&ep->queue)) pch_udc_set_dma(dev, DMA_DIR_RX); } /** * pch_udc_svc_control_in() - Handle Control IN endpoint interrupts * @dev: Reference to the device structure */ static void pch_udc_svc_control_in(struct pch_udc_dev *dev) { u32 epsts; struct pch_udc_ep *ep; struct pch_udc_ep *ep_out; ep = &dev->ep[UDC_EP0IN_IDX]; ep_out = &dev->ep[UDC_EP0OUT_IDX]; epsts = ep->epsts; ep->epsts = 0; if (!(epsts & (UDC_EPSTS_IN | UDC_EPSTS_BNA | UDC_EPSTS_HE | UDC_EPSTS_TDC | UDC_EPSTS_RCS | UDC_EPSTS_TXEMPTY | UDC_EPSTS_XFERDONE))) return; if ((epsts & UDC_EPSTS_BNA)) return; if (epsts & UDC_EPSTS_HE) return; if ((epsts & UDC_EPSTS_TDC) && (!dev->stall)) { pch_udc_complete_transfer(ep); pch_udc_clear_dma(dev, DMA_DIR_RX); ep_out->td_data->status = (ep_out->td_data->status & ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY; pch_udc_ep_clear_nak(ep_out); pch_udc_set_dma(dev, DMA_DIR_RX); pch_udc_ep_set_rrdy(ep_out); } /* On IN interrupt, provide data if we have any */ if ((epsts & UDC_EPSTS_IN) && !(epsts & UDC_EPSTS_TDC) && !(epsts & UDC_EPSTS_TXEMPTY)) pch_udc_start_next_txrequest(ep); } /** * pch_udc_svc_control_out() - Routine that handle Control * OUT endpoint interrupts * @dev: Reference to the device structure */ static void pch_udc_svc_control_out(struct pch_udc_dev *dev) __releases(&dev->lock) __acquires(&dev->lock) { u32 stat; int setup_supported; struct pch_udc_ep *ep; ep = &dev->ep[UDC_EP0OUT_IDX]; stat = ep->epsts; ep->epsts = 0; /* If setup data */ if (((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) == UDC_EPSTS_OUT_SETUP) { dev->stall = 0; dev->ep[UDC_EP0IN_IDX].halted = 0; dev->ep[UDC_EP0OUT_IDX].halted = 0; dev->setup_data = ep->td_stp->request; pch_udc_init_setup_buff(ep->td_stp); pch_udc_clear_dma(dev, DMA_DIR_RX); pch_udc_ep_fifo_flush(&(dev->ep[UDC_EP0IN_IDX]), dev->ep[UDC_EP0IN_IDX].in); if ((dev->setup_data.bRequestType & USB_DIR_IN)) dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep; else /* OUT */ dev->gadget.ep0 = &ep->ep; spin_unlock(&dev->lock); /* If Mass storage Reset */ if ((dev->setup_data.bRequestType == 0x21) && (dev->setup_data.bRequest == 0xFF)) dev->prot_stall = 0; /* call gadget with setup data received */ setup_supported = dev->driver->setup(&dev->gadget, &dev->setup_data); spin_lock(&dev->lock); if (dev->setup_data.bRequestType & USB_DIR_IN) { ep->td_data->status = (ep->td_data->status & ~PCH_UDC_BUFF_STS) | PCH_UDC_BS_HST_RDY; pch_udc_ep_set_ddptr(ep, ep->td_data_phys); } /* ep0 in returns data on IN phase */ if (setup_supported >= 0 && setup_supported < UDC_EP0IN_MAX_PKT_SIZE) { pch_udc_ep_clear_nak(&(dev->ep[UDC_EP0IN_IDX])); /* Gadget would have queued a request when * we called the setup */ if (!(dev->setup_data.bRequestType & USB_DIR_IN)) { pch_udc_set_dma(dev, DMA_DIR_RX); pch_udc_ep_clear_nak(ep); } } else if (setup_supported < 0) { /* if unsupported request, then stall */ pch_udc_ep_set_stall(&(dev->ep[UDC_EP0IN_IDX])); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); dev->stall = 0; pch_udc_set_dma(dev, DMA_DIR_RX); } else { dev->waiting_zlp_ack = 1; } } else if ((((stat & UDC_EPSTS_OUT_MASK) >> UDC_EPSTS_OUT_SHIFT) == UDC_EPSTS_OUT_DATA) && !dev->stall) { pch_udc_clear_dma(dev, DMA_DIR_RX); pch_udc_ep_set_ddptr(ep, 0); if (!list_empty(&ep->queue)) { ep->epsts = stat; pch_udc_svc_data_out(dev, PCH_UDC_EP0); } pch_udc_set_dma(dev, DMA_DIR_RX); } pch_udc_ep_set_rrdy(ep); } /** * pch_udc_postsvc_epinters() - This function enables end point interrupts * and clears NAK status * @dev: Reference to the device structure * @ep_num: End point number */ static void pch_udc_postsvc_epinters(struct pch_udc_dev *dev, int ep_num) { struct pch_udc_ep *ep; struct pch_udc_request *req; ep = &dev->ep[UDC_EPIN_IDX(ep_num)]; if (!list_empty(&ep->queue)) { req = list_entry(ep->queue.next, struct pch_udc_request, queue); pch_udc_enable_ep_interrupts(ep->dev, PCH_UDC_EPINT(ep->in, ep->num)); pch_udc_ep_clear_nak(ep); } } /** * pch_udc_read_all_epstatus() - This function read all endpoint status * @dev: Reference to the device structure * @ep_intr: Status of endpoint interrupt */ static void pch_udc_read_all_epstatus(struct pch_udc_dev *dev, u32 ep_intr) { int i; struct pch_udc_ep *ep; for (i = 0; i < PCH_UDC_USED_EP_NUM; i++) { /* IN */ if (ep_intr & (0x1 << i)) { ep = &dev->ep[UDC_EPIN_IDX(i)]; ep->epsts = pch_udc_read_ep_status(ep); pch_udc_clear_ep_status(ep, ep->epsts); } /* OUT */ if (ep_intr & (0x10000 << i)) { ep = &dev->ep[UDC_EPOUT_IDX(i)]; ep->epsts = pch_udc_read_ep_status(ep); pch_udc_clear_ep_status(ep, ep->epsts); } } } /** * pch_udc_activate_control_ep() - This function enables the control endpoints * for traffic after a reset * @dev: Reference to the device structure */ static void pch_udc_activate_control_ep(struct pch_udc_dev *dev) { struct pch_udc_ep *ep; u32 val; /* Setup the IN endpoint */ ep = &dev->ep[UDC_EP0IN_IDX]; pch_udc_clear_ep_control(ep); pch_udc_ep_fifo_flush(ep, ep->in); pch_udc_ep_set_bufsz(ep, UDC_EP0IN_BUFF_SIZE, ep->in); pch_udc_ep_set_maxpkt(ep, UDC_EP0IN_MAX_PKT_SIZE); /* Initialize the IN EP Descriptor */ ep->td_data = NULL; ep->td_stp = NULL; ep->td_data_phys = 0; ep->td_stp_phys = 0; /* Setup the OUT endpoint */ ep = &dev->ep[UDC_EP0OUT_IDX]; pch_udc_clear_ep_control(ep); pch_udc_ep_fifo_flush(ep, ep->in); pch_udc_ep_set_bufsz(ep, UDC_EP0OUT_BUFF_SIZE, ep->in); pch_udc_ep_set_maxpkt(ep, UDC_EP0OUT_MAX_PKT_SIZE); val = UDC_EP0OUT_MAX_PKT_SIZE << UDC_CSR_NE_MAX_PKT_SHIFT; pch_udc_write_csr(ep->dev, val, UDC_EP0OUT_IDX); /* Initialize the SETUP buffer */ pch_udc_init_setup_buff(ep->td_stp); /* Write the pointer address of dma descriptor */ pch_udc_ep_set_subptr(ep, ep->td_stp_phys); /* Write the pointer address of Setup descriptor */ pch_udc_ep_set_ddptr(ep, ep->td_data_phys); /* Initialize the dma descriptor */ ep->td_data->status = PCH_UDC_DMA_LAST; ep->td_data->dataptr = dev->dma_addr; ep->td_data->next = ep->td_data_phys; pch_udc_ep_clear_nak(ep); } /** * pch_udc_svc_ur_interrupt() - This function handles a USB reset interrupt * @dev: Reference to driver structure */ static void pch_udc_svc_ur_interrupt(struct pch_udc_dev *dev) { struct pch_udc_ep *ep; int i; pch_udc_clear_dma(dev, DMA_DIR_TX); pch_udc_clear_dma(dev, DMA_DIR_RX); /* Mask all endpoint interrupts */ pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL); /* clear all endpoint interrupts */ pch_udc_write_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL); for (i = 0; i < PCH_UDC_EP_NUM; i++) { ep = &dev->ep[i]; pch_udc_clear_ep_status(ep, UDC_EPSTS_ALL_CLR_MASK); pch_udc_clear_ep_control(ep); pch_udc_ep_set_ddptr(ep, 0); pch_udc_write_csr(ep->dev, 0x00, i); } dev->stall = 0; dev->prot_stall = 0; dev->waiting_zlp_ack = 0; dev->set_cfg_not_acked = 0; /* disable ep to empty req queue. Skip the control EP's */ for (i = 0; i < (PCH_UDC_USED_EP_NUM*2); i++) { ep = &dev->ep[i]; pch_udc_ep_set_nak(ep); pch_udc_ep_fifo_flush(ep, ep->in); /* Complete request queue */ empty_req_queue(ep); } if (dev->driver && dev->driver->disconnect) { spin_unlock(&dev->lock); dev->driver->disconnect(&dev->gadget); spin_lock(&dev->lock); } } /** * pch_udc_svc_enum_interrupt() - This function handles a USB speed enumeration * done interrupt * @dev: Reference to driver structure */ static void pch_udc_svc_enum_interrupt(struct pch_udc_dev *dev) { u32 dev_stat, dev_speed; u32 speed = USB_SPEED_FULL; dev_stat = pch_udc_read_device_status(dev); dev_speed = (dev_stat & UDC_DEVSTS_ENUM_SPEED_MASK) >> UDC_DEVSTS_ENUM_SPEED_SHIFT; switch (dev_speed) { case UDC_DEVSTS_ENUM_SPEED_HIGH: speed = USB_SPEED_HIGH; break; case UDC_DEVSTS_ENUM_SPEED_FULL: speed = USB_SPEED_FULL; break; case UDC_DEVSTS_ENUM_SPEED_LOW: speed = USB_SPEED_LOW; break; default: BUG(); } dev->gadget.speed = speed; pch_udc_activate_control_ep(dev); pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 | UDC_EPINT_OUT_EP0); pch_udc_set_dma(dev, DMA_DIR_TX); pch_udc_set_dma(dev, DMA_DIR_RX); pch_udc_ep_set_rrdy(&(dev->ep[UDC_EP0OUT_IDX])); /* enable device interrupts */ pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US | UDC_DEVINT_ES | UDC_DEVINT_ENUM | UDC_DEVINT_SI | UDC_DEVINT_SC); } /** * pch_udc_svc_intf_interrupt() - This function handles a set interface * interrupt * @dev: Reference to driver structure */ static void pch_udc_svc_intf_interrupt(struct pch_udc_dev *dev) { u32 reg, dev_stat = 0; int i, ret; dev_stat = pch_udc_read_device_status(dev); dev->cfg_data.cur_intf = (dev_stat & UDC_DEVSTS_INTF_MASK) >> UDC_DEVSTS_INTF_SHIFT; dev->cfg_data.cur_alt = (dev_stat & UDC_DEVSTS_ALT_MASK) >> UDC_DEVSTS_ALT_SHIFT; dev->set_cfg_not_acked = 1; /* Construct the usb request for gadget driver and inform it */ memset(&dev->setup_data, 0 , sizeof dev->setup_data); dev->setup_data.bRequest = USB_REQ_SET_INTERFACE; dev->setup_data.bRequestType = USB_RECIP_INTERFACE; dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_alt); dev->setup_data.wIndex = cpu_to_le16(dev->cfg_data.cur_intf); /* programm the Endpoint Cfg registers */ /* Only one end point cfg register */ reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX); reg = (reg & ~UDC_CSR_NE_INTF_MASK) | (dev->cfg_data.cur_intf << UDC_CSR_NE_INTF_SHIFT); reg = (reg & ~UDC_CSR_NE_ALT_MASK) | (dev->cfg_data.cur_alt << UDC_CSR_NE_ALT_SHIFT); pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX); for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) { /* clear stall bits */ pch_udc_ep_clear_stall(&(dev->ep[i])); dev->ep[i].halted = 0; } dev->stall = 0; spin_unlock(&dev->lock); ret = dev->driver->setup(&dev->gadget, &dev->setup_data); spin_lock(&dev->lock); } /** * pch_udc_svc_cfg_interrupt() - This function handles a set configuration * interrupt * @dev: Reference to driver structure */ static void pch_udc_svc_cfg_interrupt(struct pch_udc_dev *dev) { int i, ret; u32 reg, dev_stat = 0; dev_stat = pch_udc_read_device_status(dev); dev->set_cfg_not_acked = 1; dev->cfg_data.cur_cfg = (dev_stat & UDC_DEVSTS_CFG_MASK) >> UDC_DEVSTS_CFG_SHIFT; /* make usb request for gadget driver */ memset(&dev->setup_data, 0 , sizeof dev->setup_data); dev->setup_data.bRequest = USB_REQ_SET_CONFIGURATION; dev->setup_data.wValue = cpu_to_le16(dev->cfg_data.cur_cfg); /* program the NE registers */ /* Only one end point cfg register */ reg = pch_udc_read_csr(dev, UDC_EP0OUT_IDX); reg = (reg & ~UDC_CSR_NE_CFG_MASK) | (dev->cfg_data.cur_cfg << UDC_CSR_NE_CFG_SHIFT); pch_udc_write_csr(dev, reg, UDC_EP0OUT_IDX); for (i = 0; i < PCH_UDC_USED_EP_NUM * 2; i++) { /* clear stall bits */ pch_udc_ep_clear_stall(&(dev->ep[i])); dev->ep[i].halted = 0; } dev->stall = 0; /* call gadget zero with setup data received */ spin_unlock(&dev->lock); ret = dev->driver->setup(&dev->gadget, &dev->setup_data); spin_lock(&dev->lock); } /** * pch_udc_dev_isr() - This function services device interrupts * by invoking appropriate routines. * @dev: Reference to the device structure * @dev_intr: The Device interrupt status. */ static void pch_udc_dev_isr(struct pch_udc_dev *dev, u32 dev_intr) { int vbus; /* USB Reset Interrupt */ if (dev_intr & UDC_DEVINT_UR) { pch_udc_svc_ur_interrupt(dev); dev_dbg(&dev->pdev->dev, "USB_RESET\n"); } /* Enumeration Done Interrupt */ if (dev_intr & UDC_DEVINT_ENUM) { pch_udc_svc_enum_interrupt(dev); dev_dbg(&dev->pdev->dev, "USB_ENUM\n"); } /* Set Interface Interrupt */ if (dev_intr & UDC_DEVINT_SI) pch_udc_svc_intf_interrupt(dev); /* Set Config Interrupt */ if (dev_intr & UDC_DEVINT_SC) pch_udc_svc_cfg_interrupt(dev); /* USB Suspend interrupt */ if (dev_intr & UDC_DEVINT_US) { if (dev->driver && dev->driver->suspend) { spin_unlock(&dev->lock); dev->driver->suspend(&dev->gadget); spin_lock(&dev->lock); } vbus = pch_vbus_gpio_get_value(dev); if ((dev->vbus_session == 0) && (vbus != 1)) { if (dev->driver && dev->driver->disconnect) { spin_unlock(&dev->lock); dev->driver->disconnect(&dev->gadget); spin_lock(&dev->lock); } pch_udc_reconnect(dev); } else if ((dev->vbus_session == 0) && (vbus == 1) && !dev->vbus_gpio.intr) schedule_work(&dev->vbus_gpio.irq_work_fall); dev_dbg(&dev->pdev->dev, "USB_SUSPEND\n"); } /* Clear the SOF interrupt, if enabled */ if (dev_intr & UDC_DEVINT_SOF) dev_dbg(&dev->pdev->dev, "SOF\n"); /* ES interrupt, IDLE > 3ms on the USB */ if (dev_intr & UDC_DEVINT_ES) dev_dbg(&dev->pdev->dev, "ES\n"); /* RWKP interrupt */ if (dev_intr & UDC_DEVINT_RWKP) dev_dbg(&dev->pdev->dev, "RWKP\n"); } /** * pch_udc_isr() - This function handles interrupts from the PCH USB Device * @irq: Interrupt request number * @dev: Reference to the device structure */ static irqreturn_t pch_udc_isr(int irq, void *pdev) { struct pch_udc_dev *dev = (struct pch_udc_dev *) pdev; u32 dev_intr, ep_intr; int i; dev_intr = pch_udc_read_device_interrupts(dev); ep_intr = pch_udc_read_ep_interrupts(dev); /* For a hot plug, this find that the controller is hung up. */ if (dev_intr == ep_intr) if (dev_intr == pch_udc_readl(dev, UDC_DEVCFG_ADDR)) { dev_dbg(&dev->pdev->dev, "UDC: Hung up\n"); /* The controller is reset */ pch_udc_writel(dev, UDC_SRST, UDC_SRST_ADDR); return IRQ_HANDLED; } if (dev_intr) /* Clear device interrupts */ pch_udc_write_device_interrupts(dev, dev_intr); if (ep_intr) /* Clear ep interrupts */ pch_udc_write_ep_interrupts(dev, ep_intr); if (!dev_intr && !ep_intr) return IRQ_NONE; spin_lock(&dev->lock); if (dev_intr) pch_udc_dev_isr(dev, dev_intr); if (ep_intr) { pch_udc_read_all_epstatus(dev, ep_intr); /* Process Control In interrupts, if present */ if (ep_intr & UDC_EPINT_IN_EP0) { pch_udc_svc_control_in(dev); pch_udc_postsvc_epinters(dev, 0); } /* Process Control Out interrupts, if present */ if (ep_intr & UDC_EPINT_OUT_EP0) pch_udc_svc_control_out(dev); /* Process data in end point interrupts */ for (i = 1; i < PCH_UDC_USED_EP_NUM; i++) { if (ep_intr & (1 << i)) { pch_udc_svc_data_in(dev, i); pch_udc_postsvc_epinters(dev, i); } } /* Process data out end point interrupts */ for (i = UDC_EPINT_OUT_SHIFT + 1; i < (UDC_EPINT_OUT_SHIFT + PCH_UDC_USED_EP_NUM); i++) if (ep_intr & (1 << i)) pch_udc_svc_data_out(dev, i - UDC_EPINT_OUT_SHIFT); } spin_unlock(&dev->lock); return IRQ_HANDLED; } /** * pch_udc_setup_ep0() - This function enables control endpoint for traffic * @dev: Reference to the device structure */ static void pch_udc_setup_ep0(struct pch_udc_dev *dev) { /* enable ep0 interrupts */ pch_udc_enable_ep_interrupts(dev, UDC_EPINT_IN_EP0 | UDC_EPINT_OUT_EP0); /* enable device interrupts */ pch_udc_enable_interrupts(dev, UDC_DEVINT_UR | UDC_DEVINT_US | UDC_DEVINT_ES | UDC_DEVINT_ENUM | UDC_DEVINT_SI | UDC_DEVINT_SC); } /** * gadget_release() - Free the gadget driver private data * @pdev reference to struct pci_dev */ static void gadget_release(struct device *pdev) { struct pch_udc_dev *dev = dev_get_drvdata(pdev); kfree(dev); } /** * pch_udc_pcd_reinit() - This API initializes the endpoint structures * @dev: Reference to the driver structure */ static void pch_udc_pcd_reinit(struct pch_udc_dev *dev) { const char *const ep_string[] = { ep0_string, "ep0out", "ep1in", "ep1out", "ep2in", "ep2out", "ep3in", "ep3out", "ep4in", "ep4out", "ep5in", "ep5out", "ep6in", "ep6out", "ep7in", "ep7out", "ep8in", "ep8out", "ep9in", "ep9out", "ep10in", "ep10out", "ep11in", "ep11out", "ep12in", "ep12out", "ep13in", "ep13out", "ep14in", "ep14out", "ep15in", "ep15out", }; int i; dev->gadget.speed = USB_SPEED_UNKNOWN; INIT_LIST_HEAD(&dev->gadget.ep_list); /* Initialize the endpoints structures */ memset(dev->ep, 0, sizeof dev->ep); for (i = 0; i < PCH_UDC_EP_NUM; i++) { struct pch_udc_ep *ep = &dev->ep[i]; ep->dev = dev; ep->halted = 1; ep->num = i / 2; ep->in = ~i & 1; ep->ep.name = ep_string[i]; ep->ep.ops = &pch_udc_ep_ops; if (ep->in) ep->offset_addr = ep->num * UDC_EP_REG_SHIFT; else ep->offset_addr = (UDC_EPINT_OUT_SHIFT + ep->num) * UDC_EP_REG_SHIFT; /* need to set ep->ep.maxpacket and set Default Configuration?*/ usb_ep_set_maxpacket_limit(&ep->ep, UDC_BULK_MAX_PKT_SIZE); list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list); INIT_LIST_HEAD(&ep->queue); } usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0IN_IDX].ep, UDC_EP0IN_MAX_PKT_SIZE); usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0OUT_IDX].ep, UDC_EP0OUT_MAX_PKT_SIZE); /* remove ep0 in and out from the list. They have own pointer */ list_del_init(&dev->ep[UDC_EP0IN_IDX].ep.ep_list); list_del_init(&dev->ep[UDC_EP0OUT_IDX].ep.ep_list); dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IDX].ep; INIT_LIST_HEAD(&dev->gadget.ep0->ep_list); } /** * pch_udc_pcd_init() - This API initializes the driver structure * @dev: Reference to the driver structure * * Return codes: * 0: Success */ static int pch_udc_pcd_init(struct pch_udc_dev *dev) { pch_udc_init(dev); pch_udc_pcd_reinit(dev); pch_vbus_gpio_init(dev, vbus_gpio_port); return 0; } /** * init_dma_pools() - create dma pools during initialization * @pdev: reference to struct pci_dev */ static int init_dma_pools(struct pch_udc_dev *dev) { struct pch_udc_stp_dma_desc *td_stp; struct pch_udc_data_dma_desc *td_data; /* DMA setup */ dev->data_requests = pci_pool_create("data_requests", dev->pdev, sizeof(struct pch_udc_data_dma_desc), 0, 0); if (!dev->data_requests) { dev_err(&dev->pdev->dev, "%s: can't get request data pool\n", __func__); return -ENOMEM; } /* dma desc for setup data */ dev->stp_requests = pci_pool_create("setup requests", dev->pdev, sizeof(struct pch_udc_stp_dma_desc), 0, 0); if (!dev->stp_requests) { dev_err(&dev->pdev->dev, "%s: can't get setup request pool\n", __func__); return -ENOMEM; } /* setup */ td_stp = pci_pool_alloc(dev->stp_requests, GFP_KERNEL, &dev->ep[UDC_EP0OUT_IDX].td_stp_phys); if (!td_stp) { dev_err(&dev->pdev->dev, "%s: can't allocate setup dma descriptor\n", __func__); return -ENOMEM; } dev->ep[UDC_EP0OUT_IDX].td_stp = td_stp; /* data: 0 packets !? */ td_data = pci_pool_alloc(dev->data_requests, GFP_KERNEL, &dev->ep[UDC_EP0OUT_IDX].td_data_phys); if (!td_data) { dev_err(&dev->pdev->dev, "%s: can't allocate data dma descriptor\n", __func__); return -ENOMEM; } dev->ep[UDC_EP0OUT_IDX].td_data = td_data; dev->ep[UDC_EP0IN_IDX].td_stp = NULL; dev->ep[UDC_EP0IN_IDX].td_stp_phys = 0; dev->ep[UDC_EP0IN_IDX].td_data = NULL; dev->ep[UDC_EP0IN_IDX].td_data_phys = 0; dev->ep0out_buf = kzalloc(UDC_EP0OUT_BUFF_SIZE * 4, GFP_KERNEL); if (!dev->ep0out_buf) return -ENOMEM; dev->dma_addr = dma_map_single(&dev->pdev->dev, dev->ep0out_buf, UDC_EP0OUT_BUFF_SIZE * 4, DMA_FROM_DEVICE); return 0; } static int pch_udc_start(struct usb_gadget *g, struct usb_gadget_driver *driver) { struct pch_udc_dev *dev = to_pch_udc(g); driver->driver.bus = NULL; dev->driver = driver; /* get ready for ep0 traffic */ pch_udc_setup_ep0(dev); /* clear SD */ if ((pch_vbus_gpio_get_value(dev) != 0) || !dev->vbus_gpio.intr) pch_udc_clear_disconnect(dev); dev->connected = 1; return 0; } static int pch_udc_stop(struct usb_gadget *g, struct usb_gadget_driver *driver) { struct pch_udc_dev *dev = to_pch_udc(g); pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK); /* Assures that there are no pending requests with this driver */ dev->driver = NULL; dev->connected = 0; /* set SD */ pch_udc_set_disconnect(dev); return 0; } static void pch_udc_shutdown(struct pci_dev *pdev) { struct pch_udc_dev *dev = pci_get_drvdata(pdev); pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK); pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL); /* disable the pullup so the host will think we're gone */ pch_udc_set_disconnect(dev); } static void pch_udc_remove(struct pci_dev *pdev) { struct pch_udc_dev *dev = pci_get_drvdata(pdev); usb_del_gadget_udc(&dev->gadget); /* gadget driver must not be registered */ if (dev->driver) dev_err(&pdev->dev, "%s: gadget driver still bound!!!\n", __func__); /* dma pool cleanup */ if (dev->data_requests) pci_pool_destroy(dev->data_requests); if (dev->stp_requests) { /* cleanup DMA desc's for ep0in */ if (dev->ep[UDC_EP0OUT_IDX].td_stp) { pci_pool_free(dev->stp_requests, dev->ep[UDC_EP0OUT_IDX].td_stp, dev->ep[UDC_EP0OUT_IDX].td_stp_phys); } if (dev->ep[UDC_EP0OUT_IDX].td_data) { pci_pool_free(dev->stp_requests, dev->ep[UDC_EP0OUT_IDX].td_data, dev->ep[UDC_EP0OUT_IDX].td_data_phys); } pci_pool_destroy(dev->stp_requests); } if (dev->dma_addr) dma_unmap_single(&dev->pdev->dev, dev->dma_addr, UDC_EP0OUT_BUFF_SIZE * 4, DMA_FROM_DEVICE); kfree(dev->ep0out_buf); pch_vbus_gpio_free(dev); pch_udc_exit(dev); if (dev->irq_registered) free_irq(pdev->irq, dev); if (dev->base_addr) iounmap(dev->base_addr); if (dev->mem_region) release_mem_region(dev->phys_addr, pci_resource_len(pdev, PCH_UDC_PCI_BAR)); if (dev->active) pci_disable_device(pdev); kfree(dev); } #ifdef CONFIG_PM static int pch_udc_suspend(struct pci_dev *pdev, pm_message_t state) { struct pch_udc_dev *dev = pci_get_drvdata(pdev); pch_udc_disable_interrupts(dev, UDC_DEVINT_MSK); pch_udc_disable_ep_interrupts(dev, UDC_EPINT_MSK_DISABLE_ALL); pci_disable_device(pdev); pci_enable_wake(pdev, PCI_D3hot, 0); if (pci_save_state(pdev)) { dev_err(&pdev->dev, "%s: could not save PCI config state\n", __func__); return -ENOMEM; } pci_set_power_state(pdev, pci_choose_state(pdev, state)); return 0; } static int pch_udc_resume(struct pci_dev *pdev) { int ret; pci_set_power_state(pdev, PCI_D0); pci_restore_state(pdev); ret = pci_enable_device(pdev); if (ret) { dev_err(&pdev->dev, "%s: pci_enable_device failed\n", __func__); return ret; } pci_enable_wake(pdev, PCI_D3hot, 0); return 0; } #else #define pch_udc_suspend NULL #define pch_udc_resume NULL #endif /* CONFIG_PM */ static int pch_udc_probe(struct pci_dev *pdev, const struct pci_device_id *id) { unsigned long resource; unsigned long len; int retval; struct pch_udc_dev *dev; /* init */ dev = kzalloc(sizeof *dev, GFP_KERNEL); if (!dev) { pr_err("%s: no memory for device structure\n", __func__); return -ENOMEM; } /* pci setup */ if (pci_enable_device(pdev) < 0) { kfree(dev); pr_err("%s: pci_enable_device failed\n", __func__); return -ENODEV; } dev->active = 1; pci_set_drvdata(pdev, dev); /* PCI resource allocation */ resource = pci_resource_start(pdev, 1); len = pci_resource_len(pdev, 1); if (!request_mem_region(resource, len, KBUILD_MODNAME)) { dev_err(&pdev->dev, "%s: pci device used already\n", __func__); retval = -EBUSY; goto finished; } dev->phys_addr = resource; dev->mem_region = 1; dev->base_addr = ioremap_nocache(resource, len); if (!dev->base_addr) { pr_err("%s: device memory cannot be mapped\n", __func__); retval = -ENOMEM; goto finished; } if (!pdev->irq) { dev_err(&pdev->dev, "%s: irq not set\n", __func__); retval = -ENODEV; goto finished; } /* initialize the hardware */ if (pch_udc_pcd_init(dev)) { retval = -ENODEV; goto finished; } if (request_irq(pdev->irq, pch_udc_isr, IRQF_SHARED, KBUILD_MODNAME, dev)) { dev_err(&pdev->dev, "%s: request_irq(%d) fail\n", __func__, pdev->irq); retval = -ENODEV; goto finished; } dev->irq = pdev->irq; dev->irq_registered = 1; pci_set_master(pdev); pci_try_set_mwi(pdev); /* device struct setup */ spin_lock_init(&dev->lock); dev->pdev = pdev; dev->gadget.ops = &pch_udc_ops; retval = init_dma_pools(dev); if (retval) goto finished; dev->gadget.name = KBUILD_MODNAME; dev->gadget.max_speed = USB_SPEED_HIGH; /* Put the device in disconnected state till a driver is bound */ pch_udc_set_disconnect(dev); retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget, gadget_release); if (retval) goto finished; return 0; finished: pch_udc_remove(pdev); return retval; } static const struct pci_device_id pch_udc_pcidev_id[] = { { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_EG20T_UDC), .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe, .class_mask = 0xffffffff, }, { PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7213_IOH_UDC), .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe, .class_mask = 0xffffffff, }, { PCI_DEVICE(PCI_VENDOR_ID_ROHM, PCI_DEVICE_ID_ML7831_IOH_UDC), .class = (PCI_CLASS_SERIAL_USB << 8) | 0xfe, .class_mask = 0xffffffff, }, { 0 }, }; MODULE_DEVICE_TABLE(pci, pch_udc_pcidev_id); static struct pci_driver pch_udc_driver = { .name = KBUILD_MODNAME, .id_table = pch_udc_pcidev_id, .probe = pch_udc_probe, .remove = pch_udc_remove, .suspend = pch_udc_suspend, .resume = pch_udc_resume, .shutdown = pch_udc_shutdown, }; module_pci_driver(pch_udc_driver); MODULE_DESCRIPTION("Intel EG20T USB Device Controller"); MODULE_AUTHOR("LAPIS Semiconductor, <tomoya-linux@dsn.lapis-semi.com>"); MODULE_LICENSE("GPL");