-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathosx_ch341_dev.cpp
333 lines (268 loc) · 10.5 KB
/
osx_ch341_dev.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
/*
* osx_ch341_dev.cpp Winchiphead CH341 USB to serial adaptor driver for OS X
*
* Copyright 2015-2016, YuXiang Zhang <[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:
* - osx-pl2303 Copyright (c) 2013 NoZAP B.V., Jeroen Arnoldus ([email protected], http://www.nozap.me http://www.nozap.nl )
* Copyright (c) 2006 BJA Electronics, Jeroen Arnoldus ([email protected])
* - ch34x.c Copyright (C) WCH 2002-2013 Web: http://www.winchiphead.com
* - Linux kernel ch341.c Copyright 2007, Frank A Kingswood <[email protected]>
* Copyright 2007, Werner Cornelius <[email protected]>
* Copyright 2009, Boris Hajduk <[email protected]>
*/
#include <IOKit/IOLib.h>
#include <IOKit/IOTypes.h>
#include <IOKit/IOMessage.h>
#include "osx_ch341.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>
}
IOReturn osx_wch_driver_ch341::ch341_control_out(UInt8 req, UInt16 value, UInt16 index)
{
IOReturn rtn;
IOUSBDevRequest request;
request.bmRequestType = VENDOR_WRITE_REQUEST_TYPE;
request.bRequest = req;
request.wValue = value;
request.wIndex = index;
request.wLength = 0;
request.pData = NULL;
rtn = fpDevice->DeviceRequest(&request);
DEBUG_IOLog(5,"%s(%p)::ch341_control_out 0x%x:0x%x:0x%x %d\n", getName(), this,req,value,index,rtn);
return rtn;
}
IOReturn osx_wch_driver_ch341::ch341_control_in(UInt8 req, UInt16 value, UInt16 index, void *buf, UInt16 len, UInt32 *lenDone)
{
IOReturn rtn;
IOUSBDevRequest request;
request.bmRequestType = VENDOR_READ_REQUEST_TYPE;
request.bRequest = req;
request.wValue = value;
request.wIndex = index;
request.wLength = len;
request.pData = buf;
rtn = fpDevice->DeviceRequest(&request);
DEBUG_IOLog(5,"%s(%p)::ch341_control_in 0x%x:0x%x:0x%x %d got:%d\n", getName(), this,req,value,index,rtn,request.wLenDone);
if(lenDone)
*lenDone = request.wLenDone;
return rtn;
}
//
// startSerial
//
// assumes createSerialStream is called once at usb start time
// calls allocateResources to open endpoints
//
bool osx_wch_driver_ch341::startSerial()
{
const unsigned int startSerial_buf_size = 8;
uint8_t * buf;
DEBUG_IOLog(4,"%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;
for (i=0; !fUSBStarted && (i < 10);i++)
IOSleep(10);
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 = (uint8_t *) IOMalloc(startSerial_buf_size);
if (!buf) {
IOLog("%s(%p)::startSerial could not alloc memory for buf\n", getName(), this);
goto Fail;
}
ch341_control_in(0x5f, 0, 0,buf,startSerial_buf_size);
DEBUG_IOLog(5, "Got two bytes 0x%x 0x%x\n",(uint32_t)buf[0],(uint32_t)buf[1]); /* expect two bytes 0x27 0x00 */
ch341_control_out (0xa1, 0, 0);
ch341_set_baudrate(DEFAULT_BAUD_RATE);
ch341_control_in(0x95, 0x2518, 0,buf,startSerial_buf_size);
DEBUG_IOLog(5, "Got two bytes 0x%x 0x%x\n",(uint32_t)buf[0],(uint32_t)buf[1]); /* expect two bytes 0x56 0x00 */
ch341_control_out (0x9a, 0x2518, 0x0050);
ch341_get_status(fPort); /* expect 0xff 0xee */
ch341_control_out (0xa1, 0x501f, 0xd90a);
ch341_set_baudrate(DEFAULT_BAUD_RATE);
setControlLines(fPort);
ch341_get_status(fPort); /* expect 0x9f 0xee */
IOFree(buf, startSerial_buf_size);
// 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;
}
/****************************************************************************************************/
//
// Function: SetBreak
//
// Inputs: Channel - The port
// break - true(send break), false(clear break)
//
// Outputs:
//
// Desc: Set and clear line break.
//
/****************************************************************************************************/
IOReturn osx_wch_driver_ch341::setBreak( bool data){
IOReturn rtn;
const uint16_t ch341_break_reg =
CH341_REG_BREAK1 | ((uint16_t) CH341_REG_BREAK2 << 8);
UInt16 reg_contents;
UInt8 *break_reg;
int break_state = data;
break_reg = (UInt8*)IOMalloc(2);
if (!break_reg){
return kIOReturnNoMemory;
}
rtn = ch341_control_in(CH341_REQ_READ_REG,
ch341_break_reg, 0, break_reg, 2);
if (rtn != kIOReturnSuccess) {
DEBUG_IOLog(3,"%s(%p)::setBreak Failed - ch341_control_in return: %d \n", getName(), this, rtn);
goto out;
}
DEBUG_IOLog(4,"%s(%p)::setBreak - initial ch341 break register contents - reg1: 0x%x, reg2: 0x%x \n",
getName(), this, break_reg[0], break_reg[1]);
if (break_state != 0) {
DEBUG_IOLog(4,"%s(%p)::setBreak - Enter break state requested\n", getName(), this);
break_reg[0] &= ~CH341_NBREAK_BITS_REG1;
break_reg[1] &= ~CH341_NBREAK_BITS_REG2;
} else {
DEBUG_IOLog(4,"%s(%p)::setBreak - Leave break state requested\n", getName(), this);
break_reg[0] |= CH341_NBREAK_BITS_REG1;
break_reg[1] |= CH341_NBREAK_BITS_REG2;
}
DEBUG_IOLog(4,"%s(%p)::setBreak - New ch341 break register contents - reg1: 0x%x, reg2: 0x%x \n",
getName(), this, break_reg[0], break_reg[1]);
reg_contents = break_reg[0] | (UInt16(break_reg[1]) << 8);
rtn = ch341_control_out(CH341_REQ_WRITE_REG,
ch341_break_reg, reg_contents);
if (rtn != kIOReturnSuccess) {
DEBUG_IOLog(3,"%s(%p)::setBreak Failed - ch341_control_out return: %d \n", getName(), this, rtn);
goto out;
}
out:
IOFree(break_reg, 2);
DEBUG_IOLog(5,"%s(%p)::setBreak - return: %d \n", getName(), this, rtn);
return rtn;
}
IOReturn osx_wch_driver_ch341::ch341_set_baudrate(UInt32 baud_rate)
{
short a, b;
IOReturn r;
UInt32 factor;
short divisor;
DEBUG_IOLog(4,"%s(%p)::ch341_set_baudrate %d\n", getName(), this, baud_rate);
if (!baud_rate)
return kIOReturnInvalid;
factor = (CH341_BAUDBASE_FACTOR / baud_rate);
divisor = CH341_BAUDBASE_DIVMAX;
while ((factor > 0xfff0) && divisor) {
factor >>= 3;
divisor--;
}
if (factor > 0xfff0)
return kIOReturnInvalid;
factor = 0x10000 - factor;
a = (factor & 0xff00) | divisor;
b = factor & 0xff;
r = ch341_control_out(0x9a, 0x1312, a);
if (!r)
r = ch341_control_out(0x9a, 0x0f2c, b);
return r;
}
IOReturn osx_wch_driver_ch341::ch341_set_termios(UInt32 baud_rate,UInt8 parity,UInt8 charLength,UInt8 stopbits)
{
UInt16 value = 0, index = 0;
UInt8 reg_value = 0;
IOReturn r;
UInt32 factor;
short divisor;
DEBUG_IOLog(4,"%s(%p)::ch341_set_termios %d %d %d %d\n", getName(), this, baud_rate, (UInt32)parity, (UInt32)charLength, (UInt32)stopbits);
reg_value |= charLength-5;
if(stopbits == 2) //cflag & CSTOPB
reg_value |= 0x04;
if(parity){ //cflag & PARENB
if(parity & 1) //cflag & PARODD
reg_value |= (0x08 | 0x00);
else
reg_value |= (0x08 | 0x10);
}
if (!baud_rate) //avoid dividing zero
return kIOReturnBadArgument;
factor = (CH341_BAUDBASE_FACTOR / baud_rate);
divisor = CH341_BAUDBASE_DIVMAX;
while ((factor > 0xfff0) && divisor) {
factor >>= 3;
divisor--;
}
if (factor > 0xfff0)
return kIOReturnBadArgument;
factor = 0x10000 - factor;
//enable SFR_UART RX and TX
reg_value |= 0xc0;
//enable SFR_UART Control register and timer
value |= 0x9c;
value |= (UInt16)reg_value << 8;
index |= 0x80 | (divisor & 0xff);
index |= factor & 0xff00;
r = ch341_control_out(0xa1, value, index);
return r;
}
IOReturn osx_wch_driver_ch341::ch341_get_status(PortInfo_t *port)
{
UInt32 done;
uint8_t *buf = (uint8_t *) IOMalloc(8);
if (!buf) {
IOLog("%s(%p)::ch341_get_status could not alloc memory for buf\n", getName(), this);
return kIOReturnNoMemory;
}
IOReturn rtn = ch341_control_in(0x95, 0x0706, 0, buf, 8, &done);
if(done == 2){
DEBUG_IOLog(4,"%s(%p)::ch341_get_status two bytes: 0x%x 0x%x\n", getName(), this, (uint32_t)buf[0], (uint32_t)buf[1]);
port->lineState = (~(*buf)) & CH341_BITS_MODEM_STAT;
}else{
DEBUG_IOLog(3,"%s(%p)::ch341_get_status got %d bytes, ignored\n", getName(), this, done);
}
IOFree(buf, 8);
return rtn;
}
IOReturn osx_wch_driver_ch341::ch341_set_handshake(UInt8 control)
{
DEBUG_IOLog(4,"%s(%p)::ch341_set_handshake 0x%x\n", getName(), this, (UInt32)control);
return ch341_control_out(0xa4, ~control, 0);
}