forked from jayluxferro/USBCDCEthernet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBCDCEthernet.cpp
executable file
·3055 lines (2553 loc) · 101 KB
/
USBCDCEthernet.cpp
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
/*
* Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
File: USBCDCEthernet.cpp
Description: This is a sample USB Communication Device Class (CDC) driver, Ethernet model.
Note that this sample has not been tested against any actual hardware since there
are very few CDC Ethernet devices currently in existence.
This sample requires Mac OS X 10.1 and later. If built on a version prior to
Mac OS X 10.2, a compiler warning "warning: ANSI C++ forbids data member `ip_opts'
with same name as enclosing class" will be issued. This warning can be ignored.
Copyright: © Copyright 1998-2002 Apple Computer, Inc. All rights reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Change History (most recent first):
<1> 07/30/02 New sample.
<2> 12/01/02 Fixed a couple of bugs and added an output buffer pool
*/
#include "USBCDCEthernet.h"
#include "DM9601.h"
#define MIN_BAUD (50 << 1)
static globals g; // Instantiate the globals
static struct MediumTable
{
UInt32 type;
UInt32 speed;
}
mediumTable[] =
{
{kIOMediumEthernetNone, 0},
{kIOMediumEthernetAuto, 0},
{kIOMediumEthernet10BaseT | kIOMediumOptionHalfDuplex, 10},
{kIOMediumEthernet10BaseT | kIOMediumOptionFullDuplex, 10},
{kIOMediumEthernet100BaseTX | kIOMediumOptionHalfDuplex, 100},
{kIOMediumEthernet100BaseTX | kIOMediumOptionFullDuplex, 100},
{kIOMediumEthernet1000BaseT | kIOMediumOptionHalfDuplex, 1000},
{kIOMediumEthernet1000BaseT | kIOMediumOptionFullDuplex, 1000},
};
#define numStats 13
UInt16 stats[13] = { kXMIT_OK_REQ,
kRCV_OK_REQ,
kXMIT_ERROR_REQ,
kRCV_ERROR_REQ,
kRCV_CRC_ERROR_REQ,
kRCV_ERROR_ALIGNMENT_REQ,
kXMIT_ONE_COLLISION_REQ,
kXMIT_MORE_COLLISIONS_REQ,
kXMIT_DEFERRED_REQ,
kXMIT_MAX_COLLISION_REQ,
kRCV_OVERRUN_REQ,
kXMIT_TIMES_CARRIER_LOST_REQ,
kXMIT_LATE_COLLISIONS_REQ
};
#define super IOEthernetController
OSDefineMetaClassAndStructors(com_apple_driver_dts_USBCDCEthernet, IOEthernetController);
#if USE_ELG
/****************************************************************************************************/
//
// Function: AllocateEventLog
//
// Inputs: size - amount of memory to allocate
//
// Outputs: None
//
// Desc: Allocates the event log buffer
//
/****************************************************************************************************/
static void AllocateEventLog(UInt32 size)
{
if (g.evLogBuf)
return;
g.evLogFlag = 0; // assume insufficient memory
g.evLogBuf = (UInt8*)IOMalloc(size);
if (!g.evLogBuf)
{
kprintf("com_apple_driver_dts_USBCDCEthernet evLog allocation failed ");
return;
}
bzero(g.evLogBuf, size);
g.evLogBufp = g.evLogBuf;
g.evLogBufe = g.evLogBufp + kEvLogSize - 0x20; // ??? overran buffer?
g.evLogFlag = 0xFEEDBEEF; // continuous wraparound
// g.evLogFlag = 'step'; // stop at each ELG
// g.evLogFlag = 0x0333; // any nonzero - don't wrap - stop logging at buffer end
IOLog("AllocateEventLog - &globals=%8x buffer=%8x", (unsigned int)&g, (unsigned int)g.evLogBuf);
return;
}/* end AllocateEventLog */
/****************************************************************************************************/
//
// Function: EvLog
//
// Inputs: a - anything, b - anything, ascii - 4 charater tag, str - any info string
//
// Outputs: None
//
// Desc: Writes the various inputs to the event log buffer
//
/****************************************************************************************************/
static void EvLog(UInt32 a, UInt32 b, UInt32 ascii, char* str)
{
register UInt32 *lp; // Long pointer
mach_timespec_t time;
if (g.evLogFlag == 0)
return;
IOGetTime(&time);
lp = (UInt32*)g.evLogBufp;
g.evLogBufp += 0x10;
if (g.evLogBufp >= g.evLogBufe) // handle buffer wrap around if any
{
g.evLogBufp = g.evLogBuf;
if (g.evLogFlag != 0xFEEDBEEF) // make 0xFEEDBEEF a symbolic ???
g.evLogFlag = 0; // stop tracing if wrap undesired
}
// compose interrupt level with 3 byte time stamp:
*lp++ = (g.intLevel << 24) | ((time.tv_nsec >> 10) & 0x003FFFFF); // ~ 1 microsec resolution
*lp++ = a;
*lp++ = b;
*lp = ascii;
if(g.evLogFlag == 'step')
{
static char code[ 5 ] = {0,0,0,0,0};
*(UInt32*)&code = ascii;
IOLog("%8x com_apple_driver_dts_USBCDCEthernet: %8x %8x %s\n", time.tv_nsec>>10, (unsigned int)a, (unsigned int)b, code);
}
return;
}/* end EvLog */
#endif // USE_ELG
#if LOG_DATA
/****************************************************************************************************/
//
// Function: Asciify
//
// Inputs: i - the nibble
//
// Outputs: return byte - ascii byte
//
// Desc: Converts to ascii.
//
/****************************************************************************************************/
static UInt8 Asciify(UInt8 i)
{
i &= 0xF;
if (i < 10)
return('0' + i);
else return(55 + i);
}/* end Asciify */
#define dumplen 32 // Set this to the number of bytes to dump and the rest should work out correct
#define buflen ((dumplen*2)+dumplen)+3
#define Asciistart (dumplen*2)+3
/****************************************************************************************************/
//
// Function: USBLogData
//
// Inputs: Dir - direction
// Count - number of bytes
// buf - the data
//
// Outputs:
//
// Desc: Puts the data in the log.
//
/****************************************************************************************************/
static void USBLogData(UInt8 Dir, UInt32 Count, char *buf)
{
UInt8 wlen, i, Aspnt, Hxpnt;
UInt8 wchr;
char LocBuf[buflen+1];
for (i=0; i<=buflen; i++)
{
LocBuf[i] = 0x20;
}
LocBuf[i] = 0x00;
if (Dir == kUSBIn)
{
IOLog("com_apple_driver_dts_USBCDCEthernet: USBLogData - Read Complete, size = %8x\n", (unsigned int)Count);
} else {
if (Dir == kUSBOut)
{
IOLog("com_apple_driver_dts_USBCDCEthernet: USBLogData - Write, size = %8x\n", (unsigned int)Count);
} else {
if (Dir == kUSBAnyDirn)
{
IOLog("com_apple_driver_dts_USBCDCEthernet: USBLogData - Other, size = %8x\n", (unsigned int)Count);
}
}
}
if (Count > dumplen)
{
wlen = dumplen;
} else {
wlen = Count;
}
if (wlen > 0)
{
Aspnt = Asciistart;
Hxpnt = 0;
for (i=1; i<=wlen; i++)
{
wchr = buf[i-1];
LocBuf[Hxpnt++] = Asciify(wchr >> 4);
LocBuf[Hxpnt++] = Asciify(wchr);
if ((wchr < 0x20) || (wchr > 0x7F)) // Non printable characters
{
LocBuf[Aspnt++] = 0x2E; // Replace with a period
} else {
LocBuf[Aspnt++] = wchr;
}
}
LocBuf[(wlen + Asciistart) + 1] = 0x00;
IOLog(LocBuf);
IOLog("\n");
IOSleep(Sleep_Time); // Try and keep the log from overflowing
} else {
IOLog("com_apple_driver_dts_USBCDCEthernet: USBLogData - No data, Count=0\n");
}
}/* end USBLogData */
#endif // LOG_DATA
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::commReadComplete
//
// Inputs: obj - me, param - parameter block(the Port), rc - return code, remaining - what's left
// (whose idea was that?)
//
// Outputs: None
//
// Desc: Interrupt pipe (Comm interface) read completion routine
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::commReadComplete(void *obj, void *param, IOReturn rc, UInt32 remaining)
{
com_apple_driver_dts_USBCDCEthernet *me = (com_apple_driver_dts_USBCDCEthernet*)obj;
IOReturn ior;
UInt8 notif, status;
ELG(rc, 0, 'cRC+', "com_apple_driver_dts_USBCDCEthernet::commReadComplete");
if (rc == kIOReturnSuccess) // If operation returned ok
{
ELG(0, remaining, 'cRC+', "com_apple_driver_dts_USBCDCEthernet::commReadComplete succeed");
status = me->fCommPipeBuffer[0];
if (status & 0x40)
{
me->fLinkStatus = 1;
me->setLinkStatus(0);
}
else
{
me->fLinkStatus = 0;
me->setLinkStatus(1);
}
notif = me->fCommPipeBuffer[1];
if (!(notif & kResponse_Available))
{
UInt8 control = 0;
control |= RCRDiscardLong | RCRDiscardCRC | RCRRXEnable;
me->Write1Register(RegRCR, control); // 0x31
control &= ~RCRRXEnable;
me->Write1Register(RegRCR, control); // 0x30
control |= RCRRXEnable;
me->Write1Register(RegRCR, control); // 0x31
}
}
else if (rc == kIOReturnAborted)
{
return;
}
// Queue the next read, only if not aborted
ior = me->fCommPipe->Read(me->fCommPipeMDP, &me->fCommCompletionInfo, NULL);
if (ior != kIOReturnSuccess)
{
ELG(0, ior, 'cRF-', "com_apple_driver_dts_USBCDCEthernet::commReadComplete - Failed to queue next read");
if (ior == kIOUSBPipeStalled)
{
me->fCommPipe->Reset();
ior = me->fCommPipe->Read(me->fCommPipeMDP, &me->fCommCompletionInfo, NULL);
if (ior != kIOReturnSuccess)
{
ELG(0, ior, 'cR--', "com_apple_driver_dts_USBCDCEthernet::commReadComplete - Failed, read dead");
me->fCommDead = true;
}
}
}
return ;
}/* end commReadComplete */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::dataReadComplete
//
// Inputs: obj - me
// param - unused
// rc - return code
// remaining - what's left
//
// Outputs: None
//
// Desc: BulkIn pipe (Data interface) read completion routine
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::dataReadComplete(void *obj, void *param, IOReturn rc, UInt32 remaining)
{
com_apple_driver_dts_USBCDCEthernet *me = (com_apple_driver_dts_USBCDCEthernet*)obj;
IOReturn ior;
ELG(rc, remaining, 'dRC-', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete");
if (rc == kIOReturnSuccess) // If operation returned ok
{
ELG(me->fMax_Block_Size, remaining, 'dRC+', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete - Moving the incoming bytes up the stack");
LogData(kUSBIn, (me->fMax_Block_Size - remaining), me->fPipeInBuffer);
// Move the incoming bytes up the stack
me->receivePacket(me->fPipeInBuffer, me->fMax_Block_Size - remaining);
} else {
ELG(0, rc, 'dRc-', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete - Read completion io err");
if (rc != kIOReturnAborted)
{
rc = me->clearPipeStall(me->fInPipe);
if (rc != kIOReturnSuccess)
{
ELG(0, rc, 'dR--', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete - clear stall failed (trying to continue)");
}
}
}
// Queue the next read, only if not aborted
if (rc != kIOReturnAborted)
{
ior = me->fInPipe->Read(me->fPipeInMDP, &me->fReadCompletionInfo, NULL);
if (ior != kIOReturnSuccess)
{
ELG(0, ior, 'dRe-', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete - Failed to queue read");
if (ior == kIOUSBPipeStalled)
{
me->fInPipe->Reset();
ior = me->fInPipe->Read(me->fPipeInMDP, &me->fReadCompletionInfo, NULL);
if (ior != kIOReturnSuccess)
{
ELG(0, ior, 'dR--', "com_apple_driver_dts_USBCDCEthernet::dataReadComplete - Failed, read dead");
me->fDataDead = true;
}
}
}
}
return;
}/* end dataReadComplete */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::dataWriteComplete
//
// Inputs: obj - me
// param - pool index
// rc - return code
// remaining - what's left
//
// Outputs: None
//
// Desc: BulkOut pipe (Data interface) write completion routine
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::dataWriteComplete(void *obj, void *param, IOReturn rc, UInt32 remaining)
{
com_apple_driver_dts_USBCDCEthernet *me = (com_apple_driver_dts_USBCDCEthernet *)obj;
mbuf_t m;
UInt32 pktLen = 0;
#if LDEBUG
UInt32 numbufs = 0;
#endif /* LDEBUG */
UInt32 poolIndx;
poolIndx = (uintptr_t)param;
if (rc == kIOReturnSuccess) // If operation returned ok
{
ELG(rc, poolIndx, 'dWC+', "com_apple_driver_dts_USBCDCEthernet::dataWriteComplete");
if (me->fPipeOutBuff[poolIndx].m != NULL) // Null means zero length write
{
m = me->fPipeOutBuff[poolIndx].m;
while (m)
{
pktLen += mbuf_len(m);
#if LDEBUG
numbufs++;
#endif /* LDEBUG */
m = mbuf_next(m);
}
me->freePacket(me->fPipeOutBuff[poolIndx].m); // Free the mbuf
me->fPipeOutBuff[poolIndx].m = NULL;
if ((pktLen % me->fOutPacketSize) == 0) // If it was a multiple of max packet size then we need to do a zero length write
{
ELG(rc, pktLen, 'dWCz', "com_apple_driver_dts_USBCDCEthernet::dataWriteComplete - writing zero length packet");
me->fPipeOutBuff[poolIndx].pipeOutMDP->setLength(0);
me->fWriteCompletionInfo.parameter = NULL;
me->fOutPipe->Write(me->fPipeOutBuff[poolIndx].pipeOutMDP, &me->fWriteCompletionInfo);
}
}
} else {
ELG(rc, poolIndx, 'dWe-', "com_apple_driver_dts_USBCDCEthernet::dataWriteComplete - IO err");
if (me->fPipeOutBuff[poolIndx].m != NULL)
{
me->freePacket(me->fPipeOutBuff[poolIndx].m); // Free the mbuf anyway
me->fPipeOutBuff[poolIndx].m = NULL;
}
if (rc != kIOReturnAborted)
{
rc = me->clearPipeStall(me->fOutPipe);
if (rc != kIOReturnSuccess)
{
ELG(0, rc, 'dW--', "com_apple_driver_dts_USBCDCEthernet::dataWriteComplete - clear stall failed (trying to continue)");
}
}
}
return;
}/* end dataWriteComplete */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::merWriteComplete
//
// Inputs: obj - me
// param - parameter block (may or may not be present depending on request)
// rc - return code
// remaining - what's left
//
// Outputs: None
//
// Desc: Management element request write completion routine
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::merWriteComplete(void *obj, void *param, IOReturn rc, UInt32 remaining)
{
IOUSBDevRequest *MER = (IOUSBDevRequest*)param;
UInt16 dataLen;
if (MER)
{
if (rc == kIOReturnSuccess)
{
ELG(MER->bRequest, remaining, 'mWC+', "com_apple_driver_dts_USBCDCEthernet::merWriteComplete");
} else {
ELG(MER->bRequest, rc, 'mWC-', "com_apple_driver_dts_USBCDCEthernet::merWriteComplete - io err");
}
dataLen = MER->wLength;
ELG(0, dataLen, 'mWC ', "com_apple_driver_dts_USBCDCEthernet::merWriteComplete - data length");
if ((dataLen != 0) && (MER->pData))
{
IOFree(MER->pData, dataLen);
}
IOFree(MER, sizeof(IOUSBDevRequest));
} else {
if (rc == kIOReturnSuccess)
{
ELG(0, remaining, 'mWr+', "com_apple_driver_dts_USBCDCEthernet::merWriteComplete (request unknown)");
} else {
ELG(0, rc, 'rWr-', "com_apple_driver_dts_USBCDCEthernet::merWriteComplete (request unknown) - io err");
}
}
return;
}/* end merWriteComplete */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::statsWriteComplete
//
// Inputs: obj - me
// param - parameter block
// rc - return code
// remaining - what's left
//
// Outputs: None
//
// Desc: Ethernet statistics request write completion routine
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::statsWriteComplete(void *obj, void *param, IOReturn rc, UInt32 remaining)
{
com_apple_driver_dts_USBCDCEthernet *me = (com_apple_driver_dts_USBCDCEthernet *)obj;
IOUSBDevRequest *STREQ = (IOUSBDevRequest*)param;
UInt16 currStat;
if (STREQ)
{
if (rc == kIOReturnSuccess)
{
ELG(STREQ->bRequest, remaining, 'sWC+', "com_apple_driver_dts_USBCDCEthernet::statsWriteComplete");
currStat = STREQ->wValue;
switch(currStat)
{
case kXMIT_OK_REQ:
me->fpNetStats->outputPackets = USBToHostLong(me->fStatValue);
break;
case kRCV_OK_REQ:
me->fpNetStats->inputPackets = USBToHostLong(me->fStatValue);
break;
case kXMIT_ERROR_REQ:
me->fpNetStats->outputErrors = USBToHostLong(me->fStatValue);
break;
case kRCV_ERROR_REQ:
me->fpNetStats->inputErrors = USBToHostLong(me->fStatValue);
break;
case kRCV_CRC_ERROR_REQ:
me->fpEtherStats->dot3StatsEntry.fcsErrors = USBToHostLong(me->fStatValue);
break;
case kRCV_ERROR_ALIGNMENT_REQ:
me->fpEtherStats->dot3StatsEntry.alignmentErrors = USBToHostLong(me->fStatValue);
break;
case kXMIT_ONE_COLLISION_REQ:
me->fpEtherStats->dot3StatsEntry.singleCollisionFrames = USBToHostLong(me->fStatValue);
break;
case kXMIT_MORE_COLLISIONS_REQ:
me->fpEtherStats->dot3StatsEntry.multipleCollisionFrames = USBToHostLong(me->fStatValue);
break;
case kXMIT_DEFERRED_REQ:
me->fpEtherStats->dot3StatsEntry.deferredTransmissions = USBToHostLong(me->fStatValue);
break;
case kXMIT_MAX_COLLISION_REQ:
me->fpNetStats->collisions = USBToHostLong(me->fStatValue);
break;
case kRCV_OVERRUN_REQ:
me->fpEtherStats->dot3StatsEntry.frameTooLongs = USBToHostLong(me->fStatValue);
break;
case kXMIT_TIMES_CARRIER_LOST_REQ:
me->fpEtherStats->dot3StatsEntry.carrierSenseErrors = USBToHostLong(me->fStatValue);
break;
case kXMIT_LATE_COLLISIONS_REQ:
me->fpEtherStats->dot3StatsEntry.lateCollisions = USBToHostLong(me->fStatValue);
break;
default:
ELG(currStat, rc, 'sWI-', "com_apple_driver_dts_USBCDCEthernet::statsWriteComplete - Invalid stats code");
break;
}
} else {
ELG(STREQ->bRequest, rc, 'sWC-', "com_apple_driver_dts_USBCDCEthernet::statsWriteComplete - io err");
}
IOFree(STREQ, sizeof(IOUSBDevRequest));
} else {
if (rc == kIOReturnSuccess)
{
ELG(0, remaining, 'sWr+', "com_apple_driver_dts_USBCDCEthernet::statsWriteComplete (request unknown)");
} else {
ELG(0, rc, 'sWr-', "com_apple_driver_dts_USBCDCEthernet::statsWriteComplete (request unknown) - io err");
}
}
me->fStatValue = 0;
me->fStatInProgress = false;
return;
}/* end statsWriteComplete */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::init
//
// Inputs: properties - data (keys and values) used to match
//
// Outputs: Return code - true (init successful), false (init failed)
//
// Desc: Initialize the driver.
//
/****************************************************************************************************/
bool com_apple_driver_dts_USBCDCEthernet::init(OSDictionary *properties)
{
UInt32 i;
g.evLogBufp = NULL;
#if USE_ELG
AllocateEventLog(kEvLogSize);
ELG(&g, g.evLogBufp, 'USBM', "com_apple_driver_dts_USBCDCEthernet::init - event logging set up.");
waitForService(resourceMatching("kdp"));
#endif /* USE_ELG */
ELG(0, 0, 'init', "com_apple_driver_dts_USBCDCEthernet::init");
if (super::init(properties) == false)
{
ELG(0, 0, 'in--', "com_apple_driver_dts_USBCDCEthernet::init - initialize super failed");
return false;
}
// Set some defaults
fMax_Block_Size = 0x1000;
fCurrStat = 0;
fStatInProgress = false;
fDataDead = false;
fCommDead = false;
fPacketFilter = kPACKET_TYPE_DIRECTED | kPACKET_TYPE_BROADCAST | kPACKET_TYPE_MULTICAST;
for (i=0; i<kOutBufPool; i++)
{
fPipeOutBuff[i].pipeOutMDP = NULL;
fPipeOutBuff[i].pipeOutBuffer = NULL;
fPipeOutBuff[i].m = NULL;
}
return true;
}/* end init*/
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::start
//
// Inputs: provider - my provider
//
// Outputs: Return code - true (it's me), false (sorry it probably was me, but I can't configure it)
//
// Desc: This is called once it has beed determined I'm probably the best
// driver for this device.
//
/****************************************************************************************************/
bool com_apple_driver_dts_USBCDCEthernet::start(IOService *provider)
{
UInt8 configs; // number of device configurations
ELG(this, provider, 'strt', "com_apple_driver_dts_USBCDCEthernet::start - this, provider.");
if(!super::start(provider))
{
ALERT(0, 0, 'SS--', "com_apple_driver_dts_USBCDCEthernet::start - start super failed");
return false;
}
// Get my USB device provider - the device
fpDevice = OSDynamicCast(IOUSBDevice, provider);
if(!fpDevice)
{
ALERT(0, 0, 'Dev-', "com_apple_driver_dts_USBCDCEthernet::start - provider invalid");
stop(provider);
return false;
}
// Let's see if we have any configurations to play with
configs = fpDevice->GetNumConfigurations();
if (configs < 1)
{
ALERT(0, 0, 'Cfg-', "com_apple_driver_dts_USBCDCEthernet::start - no configurations");
stop(provider);
return false;
}
// Now take control of the device and configure it
if (!fpDevice->open(this))
{
ALERT(0, 0, 'Opn-', "com_apple_driver_dts_USBCDCEthernet::start - unable to open device");
stop(provider);
return false;
}
if (!configureDevice(configs))
{
ALERT(0, 0, 'Nub-', "com_apple_driver_dts_USBCDCEthernet::start - failed");
fpDevice->close(this);
fpDevice = NULL;
stop(provider);
return false;
}
ELG(0, 0, 'Nub+', "com_apple_driver_dts_USBCDCEthernet::start - successful");
return true;
}/* end start */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::free
//
// Inputs: None
//
// Outputs: None
//
// Desc: Clean up and free the log
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::free()
{
ELG(0, 0, 'free', "com_apple_driver_dts_USBCDCEthernet::free");
#if USE_ELG
if (g.evLogBuf)
IOFree(g.evLogBuf, kEvLogSize);
#endif /* USE_ELG */
super::free();
return;
}/* end free */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::stop
//
// Inputs: provider - my provider
//
// Outputs: None
//
// Desc: Stops
//
/****************************************************************************************************/
void com_apple_driver_dts_USBCDCEthernet::stop(IOService *provider)
{
ELG(0, 0, 'stop', "com_apple_driver_dts_USBCDCEthernet::stop");
if (fNetworkInterface)
{
fNetworkInterface->release();
fNetworkInterface = NULL;
}
if (fCommInterface)
{
fCommInterface->close(this);
fCommInterface->release();
fCommInterface = NULL;
}
if (fDataInterface)
{
// disable RX
UInt8 control;
if (ReadRegister(RegRCR, sizeof(control), &control) == kIOReturnSuccess)
{
control &= ~RCRRXEnable;
Write1Register(RegRCR, control);
}
fDataInterface->close(this);
fDataInterface->release();
fDataInterface = NULL;
}
if (fpDevice)
{
fpDevice->close(this);
fpDevice = NULL;
}
if (fMediumDict)
{
fMediumDict->release();
fMediumDict = NULL;
}
super::stop(provider);
return;
}/* end stop */
/****************************************************************************************************/
//
// Method: com_apple_driver_dts_USBCDCEthernet::configureDevice
//
// Inputs: numConfigs - number of configurations present
//
// Outputs: return Code - true (device configured), false (device not configured)
//
// Desc: Finds the configurations and then the appropriate interfaces etc.
//
/****************************************************************************************************/
bool com_apple_driver_dts_USBCDCEthernet::configureDevice(UInt8 numConfigs)
{
IOUSBFindInterfaceRequest req; // device request
const IOUSBInterfaceDescriptor *altInterfaceDesc;
IOReturn ior = kIOReturnSuccess;
UInt16 numends = 0;
UInt16 alt;
bool goodCall;
ELG(0, numConfigs, 'cDev', "com_apple_driver_dts_USBCDCEthernet::configureDevice");
// Initialize and "configure" the device
if (!initDevice(numConfigs))
{
ELG(0, 0, 'cDi-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - initDevice failed");
return false;
}
// Get the Comm. Class interface
req.bInterfaceClass = kUSBCompositeClass;
req.bInterfaceSubClass = kUSBCompositeSubClass;
req.bInterfaceProtocol = 0;
req.bAlternateSetting = 0;
fCommInterface = fpDevice->FindNextInterface(NULL, &req);
if (!fCommInterface)
{
ELG(0, 0, 'FIC-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - Finding the first CDC interface failed");
return false;
}
#if 1
UInt8 registerValue = 0;
if (ReadRegister(RegNSR, sizeof(registerValue), ®isterValue) != kIOReturnSuccess)
return false;
#else
if (!getFunctionalDescriptors())
{
ELG(0, 0, 'cDi-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - getFunctionalDescriptors failed");
return false;
}
#endif
goodCall = fCommInterface->open(this);
if (!goodCall)
{
ELG(0, 0, 'epC-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - open comm interface failed.");
fCommInterface = NULL;
return false;
}
fCommInterfaceNumber = fCommInterface->GetInterfaceNumber();
// Now get the Data Class interface
req.bInterfaceClass = kUSBCompositeClass;
req.bInterfaceSubClass = kUSBCompositeSubClass;
req.bInterfaceProtocol = 0;
req.bAlternateSetting = 0;
fDataInterface = fpDevice->FindNextInterface(NULL, &req);
if (fDataInterface)
{
numends = fDataInterface->GetNumEndpoints();
if (numends > 1) // There must be (at least) two bulk endpoints
{
ELG(numends, fDataInterface, 'cDD+', "com_apple_driver_dts_USBCDCEthernet::configureDevice - Data Class interface found");
} else {
altInterfaceDesc = fDataInterface->FindNextAltInterface(NULL, &req);
if (!altInterfaceDesc)
{
ELG(0, 0, 'cDn-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - FindNextAltInterface failed");
}
while (altInterfaceDesc)
{
numends = altInterfaceDesc->bNumEndpoints;
if (numends > 1)
{
goodCall = fDataInterface->open(this);
if (goodCall)
{
alt = altInterfaceDesc->bAlternateSetting;
ELG(numends, alt, 'cD++', "com_apple_driver_dts_USBCDCEthernet::configureDevice - Data Class interface (alternate) found");
ior = fDataInterface->SetAlternateInterface(this, alt);
if (ior == kIOReturnSuccess)
{
ELG(0, 0, 'cDA+', "com_apple_driver_dts_USBCDCEthernet::configureDevice - Alternate set");
break;
} else {
ELG(0, 0, 'cDS-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - SetAlternateInterface failed");
numends = 0;
}
} else {
ELG(0, 0, 'cDD-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - open data interface failed.");
numends = 0;
}
} else {
ELG(0, altInterfaceDesc, 'cDe-', "com_apple_driver_dts_USBCDCEthernet::configureDevice - No endpoints this alternate");