forked from berg/osx-pl2303
-
Notifications
You must be signed in to change notification settings - Fork 9
/
osx_pl2303.cpp
4380 lines (3515 loc) · 141 KB
/
osx_pl2303.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
/*
* osx_pl2303.cpp Prolific PL2303 USB to serial adaptor driver for OS X
*
* Copyright (c) 2006 BJA Electronics, Jeroen Arnoldus ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Driver is inspired by the following projects:
* - Linux kernel PL2303.c Copyright (C) 2001-2004 Greg Kroah-Hartman ([email protected])
* Copyright (C) 2003 IBM Corp.
* - Apple16x50Serial Copyright (c) 1997-2003 Apple Computer, Inc. All rights reserved.
* Copyright (c) 1994-1996 NeXT Software, Inc. All rights reserved.
* - AppleRS232Serial Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
* - AppleUSBIrda Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
*
*
*
* Tests:
* - Driver only tested with ATEN UC-RS232A, but should support other PL2303 based USB to RS232 converters
* - Handshake signals
*
* Todo:
* - Implementation Powermanagement
* - Fix USBF: Could not open device: Strange error message
*
*
*
* http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
*/
#define FIX_PARITY_PROCESSING 1
#include <IOKit/IOLib.h>
#include <IOKit/IOTypes.h>
#include <IOKit/IOMessage.h>
#include "osx_pl2303.h"
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/usb/IOUSBInterface.h>
#include <IOKit/usb/IOUSBLog.h>
#include <kern/clock.h>
extern "C" {
#include <pexpert/pexpert.h>
}
//To enable logging remove comments from #define DEBUG and #define DATALOG
//Use USB Prober to monitor the logs.
//#define DEBUG
//#define DATALOG
#ifdef DEBUG
#define DEBUG_IOLog(args...) USBLog (args)
#else
#define DEBUG_IOLog(args...)
#endif
#ifdef DATALOG
#define DATA_IOLog(args...) USBLog (args)
#else
#define DATA_IOLog(args...)
#endif
#define super IOSerialDriverSync
OSDefineMetaClassAndStructors(nl_bjaelectronics_driver_PL2303, IOSerialDriverSync)
/****************************************************************************************************/
//
// 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 */
bool nl_bjaelectronics_driver_PL2303::init(OSDictionary *dict)
{
bool res = super::init(dict);
DEBUG_IOLog(4,"%s(%p)::Initializing\n", getName(), this);
return res;
}
void nl_bjaelectronics_driver_PL2303::free(void)
{
DEBUG_IOLog(4,"%s(%p)::Freeing\n", getName(), this);
super::free();
}
IOService *nl_bjaelectronics_driver_PL2303::probe(IOService *provider, SInt32 *score)
{
IOUSBDevice *Provider;
DEBUG_IOLog(4,",%s(%p)::Probe\n", getName(), this);
Provider = OSDynamicCast(IOUSBDevice, provider);
if (!Provider) {
IOLog("%s(%p)::Probe Attached to non-IOUSBDevice provider! Failing probe()\n", getName(), this);
return NULL;
}
IOService *res = super::probe(provider, score);
DEBUG_IOLog(5,"%s(%p)::Probe successful\n", getName(), this);
return res;
}
bool nl_bjaelectronics_driver_PL2303::start(IOService *provider)
{
enum pl2303_type type = type_1;
OSNumber *release;
fTerminate = false; // Make sure we don't think we're being terminated
fPort = NULL;
fNub = NULL;
fpInterface = NULL;
fpinterruptPipeBuffer = NULL;
fPipeInBuffer = NULL;
fPipeOutBuffer = NULL;
fpDevice = NULL;
fpInPipe = NULL;
fpOutPipe = NULL;
fpInterruptPipe = NULL;
fUSBStarted = false; // set to true when start finishes up ok
fSessions = 0;
fReadActive = false;
fWriteActive = false;
DEBUG_IOLog(4,"%s(%p)::start PL2303 Driver\n", getName(), this);
if( !super::start( provider ) )
{
IOLog("%s(%p)::start - super failed\n", getName(), this);
goto Fail;
}
fpDevice = OSDynamicCast(IOUSBDevice, provider);
if(!fpDevice)
{
IOLog("%s(%p)::start - Provider isn't a USB device!!!\n", getName(), this);
goto Fail;
}
if (fpDevice->GetNumConfigurations() < 1)
{
IOLog("%s(%p)::start - no composite configurations\n", getName(), this);
goto Fail;
}
// make our nub (and fPort) now
if( !createNub() ) goto Fail;
// Now configure it (leaves device suspended)
if( !configureDevice( fpDevice->GetNumConfigurations() ) ) goto Fail;
// Finally create the bsd tty (serial stream) and leave it there until usb stop
if( !createSerialStream() ) goto Fail;
fWorkLoop = getWorkLoop();
if (!fWorkLoop)
{
IOLog("%s(%p)::start - getWorkLoop failed\n", getName(), this);
goto Fail;
}
fWorkLoop->retain();
fCommandGate = IOCommandGate::commandGate(this);
if (!fCommandGate)
{
IOLog("%s(%p)::start - create commandGate failed\n", getName(), this);
goto Fail;
}
if (fWorkLoop->addEventSource(fCommandGate) != kIOReturnSuccess)
{
IOLog("%s(%p)::start - addEventSource fCommandGate to WorkLoop failed\n", getName(), this);
goto Fail;
}
fCommandGate->enable();
release = (OSNumber *) fpDevice->getProperty(kUSBDeviceReleaseNumber);
DEBUG_IOLog(1,"%s(%p)::start - Get device version: %p \n", getName(), this, release->unsigned16BitValue() );
if (release->unsigned16BitValue()==PROLIFIC_REV_H) {
DEBUG_IOLog(1,"%s(%p)::start - Chip type: H \n" );
type = type_1; // was rev_H
} else if ( release->unsigned16BitValue()==PROLIFIC_REV_X ) {
DEBUG_IOLog(1,"%s(%p)::start - Chip type: X \n" );
type = rev_HX; // was rev_X
} else if ( release->unsigned16BitValue()==PROLIFIC_REV_HX_CHIP_D ) {
DEBUG_IOLog(1,"%s(%p)::start - Chip type: HX \n" );
type = rev_HX;
} else if ( release->unsigned16BitValue()==PROLIFIC_REV_1 ) {
DEBUG_IOLog(1,"%s(%p)::start - Chip type: 1 \n" );
type = type_1;
} else {
DEBUG_IOLog(1,"%s(%p)::start - Chip type: unkwown \n" );
type = unknown;
}
fPort->type = type;
fUSBStarted = true;
DEBUG_IOLog(3,"%s(%p)::start - Allocate resources \n", getName(), this);
return true;
Fail:
if (fNub)
{
destroyNub();
}
if (fCommandGate)
{
fCommandGate->release();
fCommandGate = NULL;
}
if (fWorkLoop)
{
fWorkLoop->release();
fWorkLoop = NULL;
}
DEBUG_IOLog(1,"%s(%p)::start - failed\n", getName(), this);
stop( provider );
return false;
}
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::stop
//
// Inputs: provider - my provider
//
// Outputs: None
//
// Desc: Stops
//
/****************************************************************************************************/
void nl_bjaelectronics_driver_PL2303::stop( IOService *provider )
{
fUSBStarted = false; // reset usb start/stop flag for CheckSerialState
CheckSerialState(); // turn serial off, release resources
DEBUG_IOLog(5,"%s(%p)::stop CheckSerialState succeed\n", getName(), this);
if (fCommandGate)
{
fCommandGate->release();
fCommandGate = NULL;
DEBUG_IOLog(5,"%s(%p)::stop Command gate destroyed\n", getName(), this);
}
if (fWorkLoop)
{
fWorkLoop->release();
fWorkLoop = NULL;
DEBUG_IOLog(5,"%s(%p)::stop workloop destroyed\n", getName(), this);
}
destroySerialStream(); // release the bsd tty
destroyNub(); // delete the nubs and fPort
if (fpInterface)
{
fpInterface->release(); // retain done in ConfigureDevice
fpInterface = NULL;
DEBUG_IOLog(5,"%s(%p)::stop fpInterface destroyed\n", getName(), this);
}
// release our power manager state - NOT IMPLEMENTED
// PMstop();
super::stop( provider );
return;
}/* end stop */
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::getWorkLoop
//
// Inputs:
//
// Outputs:
//
// Desc: create our own workloop if we don't have one already.
//
/****************************************************************************************************/
IOWorkLoop* nl_bjaelectronics_driver_PL2303::getWorkLoop() const
{
IOWorkLoop *w;
DEBUG_IOLog(4,"%s(%p)::getWorkLoop\n", getName(), this);
if (fWorkLoop) w = fWorkLoop;
else w = IOWorkLoop::workLoop();
return w;
}/* end getWorkLoop */
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::privateWatchState
//
// Inputs: port - the specified port, state - state watching for, mask - state mask (the specific bits)
//
// Outputs: IOReturn - kIOReturnSuccess, kIOReturnIOError or kIOReturnIPCError
//
// Desc: Wait for the at least one of the state bits defined in mask to be equal
// to the value defined in state. Check on entry then sleep until necessary.
// A return value of kIOReturnSuccess means that at least one of the port state
// bits specified by mask is equal to the value passed in by state. A return
// value of kIOReturnIOError indicates that the port went inactive. A return
// value of kIOReturnIPCError indicates sleep was interrupted by a signal.
//
/****************************************************************************************************/
IOReturn nl_bjaelectronics_driver_PL2303::privateWatchState( PortInfo_t *port, UInt32 *state, UInt32 mask )
{
unsigned watchState, foundStates;
bool autoActiveBit = false;
IOReturn rtn = kIOReturnSuccess;
DEBUG_IOLog(4,"%s(%p)::privateWatchState\n", getName(), this);
watchState = *state;
// hack to get around problem with carrier detection - Do we need this hack ??
if ( !(mask & (PD_S_ACQUIRED | PD_S_ACTIVE)) )
{
watchState &= ~PD_S_ACTIVE; // Check for low PD_S_ACTIVE
mask |= PD_S_ACTIVE; // Register interest in PD_S_ACTIVE bit
autoActiveBit = true;
}
for (;;)
{
// Check port state for any interesting bits with watchState value
// NB. the '^ ~' is a XNOR and tests for equality of bits.
DEBUG_IOLog(4,"%s(%p)::privateWatchState :watchState %p port->State %p mask %p\n", getName(), this, watchState,port->State,mask );
foundStates = (watchState ^ ~port->State) & mask;
DEBUG_IOLog(4,"%s(%p)::privateWatchState :foundStates %p \n", getName(), this, foundStates );
if ( foundStates )
{
*state = port->State;
if ( autoActiveBit && (foundStates & PD_S_ACTIVE) )
{
rtn = kIOReturnIOError;
} else {
rtn = kIOReturnSuccess;
}
break;
}
port->WatchStateMask |= mask;
retain(); // Just to make sure all threads are awake
fCommandGate->retain(); // before we're released
rtn = fCommandGate->commandSleep((void *)&port->State);
fCommandGate->retain();
if (rtn == THREAD_TIMED_OUT)
{
rtn = kIOReturnTimeout;
break;
} else {
if (rtn == THREAD_INTERRUPTED)
{
rtn = kIOReturnAborted;
break;
}
}
release();
}/* end for */
// As it is impossible to undo the masking used by this
// thread, we clear down the watch state mask and wakeup
// every sleeping thread to reinitialize the mask before exiting.
port->WatchStateMask = 0;
fCommandGate->commandWakeup((void *)&port->State);
DEBUG_IOLog(4,"%s(%p)::privateWatchState end\n", getName(), this);
return rtn;
}/* end privateWatchState */
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::allocateResources
//
// Inputs:
//
// Outputs: return code - true (allocate was successful), false (it failed)
//
// Desc: Finishes up the rest of the configuration and gets all the endpoints open
//
/****************************************************************************************************/
bool nl_bjaelectronics_driver_PL2303::allocateResources( void )
{
IOUSBFindEndpointRequest epReq; // endPoint request struct on stack
bool goodCall; // return flag fm Interface call
vm_size_t aBuffSize;
DEBUG_IOLog(4,"%s(%p)::allocateResources\n", getName(), this);
// Open all the end points
if (!fpInterface) {
IOLog("%s(%p)::allocateResources failed - no fpInterface.\n", getName(), this);
goto Fail;
}
goodCall = fpInterface->open( this ); // close done in releaseResources
if ( !goodCall ){
IOLog("%s(%p)::allocateResources - open data interface failed.\n", getName(), this);
fpInterface->release();
fpInterface = NULL;
return false;
}
fpInterfaceNumber = fpInterface->GetInterfaceNumber();
epReq.type = kUSBBulk;
epReq.direction = kUSBIn;
epReq.maxPacketSize = 0;
epReq.interval = 0;
fpInPipe = fpInterface->FindNextPipe( 0, &epReq );
if (!fpInPipe) {
IOLog("%s(%p)::allocateResources failed - no fpInPipe.\n", getName(), this);
goto Fail;
}
epReq.direction = kUSBOut;
fpOutPipe = fpInterface->FindNextPipe( 0, &epReq );
if (!fpOutPipe) {
IOLog("%s(%p)::allocateResources failed - no fpOutPipe.\n", getName(), this);
goto Fail;
}
epReq.type = kUSBInterrupt;
epReq.direction = kUSBIn;
fpInterruptPipe = fpInterface->FindNextPipe( 0, &epReq );
if (!fpInterruptPipe) {
IOLog("%s(%p)::allocateResources failed - no fpInterruptPipe.\n", getName(), this);
goto Fail;
}
// Allocate Memory Descriptor Pointer with memory for the interrupt-in pipe:
aBuffSize = INTERRUPT_BUFF_SIZE;
if ( (fpDevice->GetVendorID() == SIEMENS_VENDOR_ID ) && (fpDevice->GetProductID() == SIEMENS_PRODUCT_ID_X65) ) {
aBuffSize = 1;
DEBUG_IOLog( 3, "%s(%p)::allocateResources interrupt Buff size = 1\n", getName(), this);
}
fpinterruptPipeMDP = IOBufferMemoryDescriptor::withCapacity( aBuffSize, kIODirectionIn );
if (!fpinterruptPipeMDP) {
IOLog("%s(%p)::allocateResources failed - no fpinterruptPipeMDP.\n", getName(), this);
goto Fail;
}
fpinterruptPipeMDP->setLength( aBuffSize );
fpinterruptPipeBuffer = (UInt8*)fpinterruptPipeMDP->getBytesNoCopy();
// Allocate Memory Descriptor Pointer with memory for the data-in bulk pipe:
fpPipeInMDP = IOBufferMemoryDescriptor::withCapacity( USBLapPayLoad, kIODirectionIn );
if (!fpPipeInMDP) {
IOLog("%s(%p)::allocateResources failed - no fpPipeInMDP.\n", getName(), this);
goto Fail;
}
fpPipeInMDP->setLength( USBLapPayLoad );
fPipeInBuffer = (UInt8*)fpPipeInMDP->getBytesNoCopy();
// Allocate Memory Descriptor Pointer with memory for the data-out bulk pipe:
fpPipeOutMDP = IOBufferMemoryDescriptor::withCapacity( MAX_BLOCK_SIZE, kIODirectionOut );
if (!fpPipeOutMDP) {
IOLog("%s(%p)::allocateResources failed - no fpPipeOutMDP.\n", getName(), this);
goto Fail;
}
fpPipeOutMDP->setLength( MAX_BLOCK_SIZE );
fPipeOutBuffer = (UInt8*)fpPipeOutMDP->getBytesNoCopy();
// set up the completion info for all three pipes
if (!fPort) {
IOLog("%s(%p)::allocateResources failed - no fPort.\n", getName(), this);
goto Fail;
}
finterruptCompletionInfo.target = this;
finterruptCompletionInfo.action = interruptReadComplete;
finterruptCompletionInfo.parameter = fPort;
fReadCompletionInfo.target = this;
fReadCompletionInfo.action = dataReadComplete;
fReadCompletionInfo.parameter = fPort;
fWriteCompletionInfo.target = this;
fWriteCompletionInfo.action = dataWriteComplete;
fWriteCompletionInfo.parameter = fPort;
if( setSerialConfiguration() ){
IOLog("%s(%p)::allocateResources setSerialConfiguration failed\n", getName(), this);
goto Fail;
}
DEBUG_IOLog(5,"%s(%p)::allocateResources successful\n", getName(), this);
return true;
Fail:
return false;
} // allocateResources
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::releaseResources
//
// Inputs: port - the Port
//
// Outputs: None
//
// Desc: Frees up the pipe resources allocated in allocateResources
//
/****************************************************************************************************/
void nl_bjaelectronics_driver_PL2303::releaseResources( void )
{
DEBUG_IOLog(4,"nl_bjaelectronics_driver_PL2303::releaseResources\n");
if ( fpInterface ) {
fpInterface->close( this );
}
if ( fpPipeOutMDP ) {
fpPipeOutMDP->release();
fpPipeOutMDP = 0;
}
if ( fpPipeInMDP ) {
fpPipeInMDP->release();
fpPipeInMDP = 0;
}
if ( fpinterruptPipeMDP ) {
fpinterruptPipeMDP->release();
fpinterruptPipeMDP = 0;
}
return;
}/* end releaseResources */
//
// startSerial
//
// assumes createSerialStream is called once at usb start time
// calls allocateResources to open endpoints
//
bool nl_bjaelectronics_driver_PL2303::startSerial()
{
IOUSBDevRequest request;
char * buf;
IOReturn rtn;
DEBUG_IOLog(1,"%s(%p)::startSerial \n", getName(), this);
/* Ugly hack to make device clean */
DEBUG_IOLog(5,"%s(%p)::startSerial RESET DEVICE \n", getName(), this);
fUSBStarted = false;
DEBUG_IOLog(5,"%s(%p)::startSerial close device-1\n", getName(), this);
if(fpDevice) { fpDevice->close( fpDevice ); }
DEBUG_IOLog(5,"%s(%p)::startSerial reset device-1 \n", getName(), this);
if(fpDevice) { fpDevice->ResetDevice(); }
int i;
i = 0;
while (!fUSBStarted & (i < 10)) { IOSleep(10); i++; }
DEBUG_IOLog(5,"%s(%p)::startSerial close device-2 timout: %d \n", getName(), this, i);
if(fpDevice) { fpDevice->close( fpDevice ); }
DEBUG_IOLog(5,"%s(%p)::startSerial reset device-2 \n", getName(), this);
if(fpDevice) { fpDevice->ResetDevice(); }
/* **************************** */
if (!fNub) {
IOLog("%s(%p)::startSerial fNub not available\n", getName(), this);
goto Fail;
}
buf = (char *) IOMalloc(10);
if (!buf) {
IOLog("%s(%p)::startSerial could not alloc memory for buf\n", getName(), this);
goto Fail;
}
// make chip as sane as can be
#define FISH(a,b,c,d) \
request.bmRequestType = a; \
request.bRequest = b; \
request.wValue = c; \
request.wIndex = d; \
request.wLength = 1; \
request.pData = buf; \
rtn = fpDevice->DeviceRequest(&request); \
DEBUG_IOLog(5,"%s(%p)::startSerial FISH 0x%x:0x%x:0x%x:0x%x %d - %x\n", getName(), this,a,b,c,d,rtn,buf[0]);
#define SOUP(a,b,c,d) \
request.bmRequestType = a; \
request.bRequest = b; \
request.wValue = c; \
request.wIndex = d; \
request.wLength = 0; \
request.pData = NULL; \
rtn = fpDevice->DeviceRequest(&request); \
DEBUG_IOLog(5,"%s(%p)::startSerial SOUP 0x%x:0x%x:0x%x:0x%x %d\n", getName(), this,a,b,c,d,rtn);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
// FISH (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x81, 1);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
if (fPort->type == rev_HX) {
/* HX chip */
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
/* reset upstream data pipes */
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
} else {
SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
}
IOFree(buf, 10);
// open the pipe endpoints
if (!allocateResources() ) {
IOLog("%s(%p)::start Allocate resources failed\n", getName(), this);
goto Fail;
}
startPipes(); // start reading on the usb pipes
return true;
Fail:
return false;
}
void nl_bjaelectronics_driver_PL2303::stopSerial( bool resetDevice )
{
DEBUG_IOLog(1,"%s(%p)::stopSerial\n", getName(), this);
stopPipes(); // stop reading on the usb pipes
if (fpPipeOutMDP != NULL) // better test for releaseResources?
{
releaseResources( );
}
DEBUG_IOLog(1,"%s(%p)::stopSerial stopSerial succeed\n", getName(), this);
Fail:
return;
}
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::CheckSerialState
//
// Inputs: open session count (fSessions)
// usb start/stop (fStartStopUSB) -- replace with fTerminate?
//
// Outputs:
//
// Desc: Turns Serial on or off if appropriate
//
/****************************************************************************************************/
IOReturn nl_bjaelectronics_driver_PL2303::CheckSerialState( void )
{
Boolean newState = fUSBStarted & // usb must have started, and
// (fPowerState == kIrDAPowerOnState) & // powered on by the power manager, and
(fSessions > 0); // one of the clients too
DEBUG_IOLog(4,"%s(%p)::CheckSerialState\n", getName(), this);
if ( newState ){
fTerminate = false;
if ( !startSerial() )
{
fTerminate = true;
IOLog("%s(%p)::CheckSerialState - StartSerial failed\n", getName(), this);
} else {
DEBUG_IOLog(5,"%s(%p)::CheckSerialState - StartSerial successful\n", getName(), this);
}
} else if (!newState && !fTerminate) // Turn Serial off if needed
{
DEBUG_IOLog(5,"%s(%p)::CheckSerialState - StopSerial\n", getName(), this);
fTerminate = true; // Make it look like we've been terminated
stopSerial(true); // stop irda and stop pipe i/o
DEBUG_IOLog(5,"%s(%p)::CheckSerialState - StopSerial successful\n", getName(), this);
}
return kIOReturnSuccess;
// return ior;
}/* end CheckSerialState */
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::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 nl_bjaelectronics_driver_PL2303::configureDevice( UInt8 numConfigs )
{
IOUSBFindInterfaceRequest req; // device request Class on stack
const IOUSBConfigurationDescriptor *cd = NULL; // configuration descriptor
IOUSBInterfaceDescriptor *intf = NULL; // interface descriptor
IOReturn ior;
UInt8 cval;
UInt8 config = 0;
DEBUG_IOLog(4,"%s(%p)::configureDevice\n", getName(), this);
for (cval=0; cval<numConfigs; cval++)
{
cd = fpDevice->GetFullConfigurationDescriptor(cval);
if ( !cd )
{
IOLog("%s(%p)::configureDevice - Error getting the full configuration descriptor\n", getName(), this);
}
else {
// Find the first one - there may be more to go on in the future
req.bInterfaceClass = kIOUSBFindInterfaceDontCare;
req.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
req.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
req.bAlternateSetting = kIOUSBFindInterfaceDontCare;
ior = fpDevice->FindNextInterfaceDescriptor(cd, intf, &req, &intf);
if ( ior == kIOReturnSuccess )
{
if ( intf ){
config = cd->bConfigurationValue;
DEBUG_IOLog(5,"%s(%p)::configureDevice - Interface descriptor found\n", getName(), this);
break;
} else {
DEBUG_IOLog(5,"%s(%p)::configureDevice - That's weird the interface was null\n", getName(), this);
cd = NULL;
}
} else {
IOLog("%s(%p)::configureDevice - No CDC interface found this configuration\n", getName(), this);
cd = NULL;
}
}
}
if ( !cd )
{
goto Fail;
}
// Now lets do it for real
req.bInterfaceClass = kIOUSBFindInterfaceDontCare;
req.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
req.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
req.bAlternateSetting = kIOUSBFindInterfaceDontCare;
fpInterface = fpDevice->FindNextInterface( NULL, &req );
if ( !fpInterface )
{
DEBUG_IOLog(4,"%s(%p)::configureDevice - Find next interface failed open device and reallocate objects\n", getName(), this);
if (!fpDevice->open(fpDevice))
{
IOLog("%s(%p)::configureDevice - unable to open device for configuration \n", getName(), this);
goto Fail;
}
IOReturn rtn = fpDevice->SetConfiguration(fpDevice, fpDevice->GetFullConfigurationDescriptor(0)->bConfigurationValue, true);
if (rtn)
{
IOLog("%s(%p)::configureDevice - unable to set the configuration\n", getName(), this);
goto Fail;
}
fpInterface = fpDevice->FindNextInterface( NULL, &req );
if ( !fpInterface )
{
IOLog("%s(%p)::configureDevice - Find interface failed\n", getName(), this);
goto Fail;
} else {
DEBUG_IOLog(5,"%s(%p)::configureDevice Interface found\n", getName(), this);
}
} else {
DEBUG_IOLog(5,"%s(%p)::configureDevice Interface found\n", getName(), this);
}
fpInterface->retain(); // release done in stop()
return true;
Fail:
return false;
}/* end configureDevice */
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::createNub
//
// Inputs:
//
// Outputs: fNub and fPort
//
// Desc: allocates and inits, but doesn't publish the BSD info on the nub yet
// create serial stream finishes the job later.
//
/****************************************************************************************************/
bool nl_bjaelectronics_driver_PL2303::createNub(void)
{
DEBUG_IOLog(4,"%s(%p)::createNub\n", getName(), this);
if (fNub == NULL) {
fNub = new IORS232SerialStreamSync;
}
if( !fNub ) goto Fail;
if (fPort == NULL) {
fPort = (PortInfo_t*)IOMalloc( sizeof(PortInfo_t) );
}
if( !fPort ) goto Fail;
bzero(fPort, sizeof(PortInfo_t));
if( !fNub->init(0, fPort ) ) goto Fail;
if( !fNub->attach( this ) ) goto Fail;
return true;
Fail:
IOLog("%s(%p)::Createnub failed\n", getName(), this);
// could try and clean up here, but let's start by just not crashing.
return false;
}
void nl_bjaelectronics_driver_PL2303::destroyNub()
{
DEBUG_IOLog(4,"%s(%p)::destroyNub Try to destroy nub\n", getName(), this);
if (fPort != NULL) {
IOFree( fPort, sizeof(PortInfo_t) );
fPort = NULL;
DEBUG_IOLog(5,"%s(%p)::destroyNub fPort reset \n", getName(), this);
}
if (fNub) {
fNub->detach(this);
fNub->release(); // crash boom?
fNub = NULL;
DEBUG_IOLog(5,"%s(%p)::destroyNub Nub destroyed \n", getName(), this);
}
}
/****************************************************************************************************/
//
// Method: nl_bjaelectronics_driver_PL2303::createSuffix
//
// Inputs: None
//
// Outputs: return Code - true (suffix created), false (suffix not create), sufKey - the key
//
// Desc: Creates the suffix key. It attempts to use the serial number string from the device
// if it's reasonable i.e. less than 8 bytes ascii. Remember it's stored in unicode
// format. If it's not present or not reasonable it will generate the suffix based
// on the location property tag. At least this remains the same across boots if the
// device is plugged into the same physical location. In the latter case trailing
// zeros are removed.
//
/****************************************************************************************************/
bool nl_bjaelectronics_driver_PL2303::createSuffix( unsigned char *sufKey )
{
IOReturn rc;
UInt8 serBuf[10]; // arbitrary size > 8
OSNumber *location;
UInt32 locVal;
UInt8 *rlocVal;
UInt16 offs, i, sig = 0;
UInt8 indx;
bool keyOK = false;
DEBUG_IOLog(4,"%s(%p)::createSuffix\n", getName(), this);
indx = fpDevice->GetSerialNumberStringIndex();
DEBUG_IOLog(5,"%s(%p)::createSuffix the index of string descriptor describing the device's serial number: %p\n", getName(), this, indx );
if (indx != 0 )
{
// Generate suffix key based on the serial number string (if reasonable <= 8 and > 0)
rc = fpDevice->GetStringDescriptor(indx, (char *)&serBuf, sizeof(serBuf));
if ( !rc )
{
DEBUG_IOLog(5,"%s(%p)::createSuffix serial number: %s\n", getName(), this, serBuf );
int serBufLength = strlen((char *)&serBuf);
if ( (serBufLength > 0) && (serBufLength < 9) )
{
strncpy( (char *)sufKey, (const char *)&serBuf, serBufLength);
keyOK = true;
}
} else {
IOLog("%s(%p)::createSuffix error reading serial number string\n", getName(), this );
}
}