File: usb_cp2101.c

package info (click to toggle)
brltty 6.9-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 44,828 kB
  • sloc: ansic: 154,246; java: 13,514; sh: 9,934; xml: 5,672; tcl: 2,679; makefile: 2,346; awk: 713; lisp: 366; python: 321; ml: 301
file content (406 lines) | stat: -rw-r--r-- 12,998 bytes parent folder | download | duplicates (4)
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
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2025 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <string.h>
#include <errno.h>

#include "log.h"
#include "usb_serial.h"
#include "usb_cp2101.h"
#include "bitfield.h"

static size_t
usbGetProperty_CP2101 (UsbDevice *device, uint8_t request, void *data, size_t length) {
  ssize_t result;

  logMessage(LOG_CATEGORY(SERIAL_IO), "getting CP2101 property: %02X", request);
  result = usbControlRead(device, UsbControlRecipient_Interface, UsbControlType_Vendor,
                          request, 0, 0, data, length, 1000);
  if (result == -1) return 0;

  if (result < length) {
    unsigned char *bytes = data;
    memset(&bytes[result], 0, length-result);
  }

  logBytes(LOG_CATEGORY(SERIAL_IO), "CP2101 property input", data, result);
  return result;
}

static int
usbSetComplexProperty_CP2101 (
  UsbDevice *device,
  uint8_t request, uint16_t value,
  const void *data, size_t length
) {
  logMessage(LOG_CATEGORY(SERIAL_IO), "setting CP2101 property: %02X %04X", request, value);
  if (length) logBytes(LOG_CATEGORY(SERIAL_IO), "CP2101 property output", data, length);

  return usbControlWrite(device, UsbControlRecipient_Interface, UsbControlType_Vendor,
                         request, value, 0, data, length, 1000) != -1;
}

static int
usbSetSimpleProperty_CP2101 (UsbDevice *device, uint8_t request, uint16_t value) {
  return usbSetComplexProperty_CP2101(device, request, value, NULL, 0);
}

static int
usbVerifyBaudRate_CP2101 (UsbDevice *device, USB_CP2101_BaudRate expected) {
  USB_CP2101_BaudRate actual;
  size_t result;

  logMessage(LOG_CATEGORY(SERIAL_IO), "verifying CP2101 baud rate");
  result = usbGetProperty_CP2101(device, USB_CP2101_CTL_GetBaudRate,
                                 &actual, sizeof(actual));

  if (!result) {
    logMessage(LOG_WARNING, "unable to get CP2101 baud rate: %s", strerror(errno));
  } else if (result != sizeof(actual)) {
    logMessage(LOG_WARNING, "unexpected CP2101 baud rate size: %d", (int)result);
  } else if (getLittleEndian32(actual) != expected) {
    logMessage(LOG_WARNING,
               "unexpected CP2101 baud rate value:"
               " Expected:%"PRIu32 " Actual:%"PRIu32,
               expected, getLittleEndian32(actual));
  } else {
    return 1;
  }

  return 0;
}

static int
usbVerifyBaudDivisor_CP2101 (UsbDevice *device, USB_CP2101_BaudDivisor expected) {
  USB_CP2101_BaudDivisor actual;
  size_t result;

  logMessage(LOG_CATEGORY(SERIAL_IO), "verifying CP2101 baud divisor");
  result = usbGetProperty_CP2101(device, USB_CP2101_CTL_GetBaudDivisor,
                                 &actual, sizeof(actual));

  if (!result) {
    if (errno == EPIPE) return 1;
    logMessage(LOG_WARNING, "unable to get CP2101 baud divisor: %s", strerror(errno));
  } else if (result != sizeof(actual)) {
    logMessage(LOG_WARNING, "unexpected CP2101 baud divisor size: %d", (int)result);
  } else if (getLittleEndian16(actual) != expected) {
    logMessage(LOG_WARNING,
               "unexpected CP2101 baud divisor value: Expected:%u Actual:%u",
               expected, getLittleEndian16(actual));
  } else {
    return 1;
  }

  return 0;
}

static int
usbVerifyBaud_CP2101 (UsbDevice *device, USB_CP2101_BaudRate rate, USB_CP2101_BaudDivisor divisor) {
  if (!usbVerifyBaudRate_CP2101(device, rate)) return 0;
  if (!usbVerifyBaudDivisor_CP2101(device, divisor)) return 0;
  return 1;
}

static int
usbSetBaud_CP2101 (UsbDevice *device, unsigned int baud) {
  USB_CP2101_BaudDivisor divisor = USB_CP2101_BAUD_BASE / baud;

  if ((baud * divisor) != USB_CP2101_BAUD_BASE) {
    logUnsupportedBaud(baud);
    errno = EINVAL;
    return 0;
  }

  {
    USB_CP2101_BaudRate rate;

    logMessage(LOG_CATEGORY(SERIAL_IO), "setting CP2101 baud rate: %u", baud);
    putLittleEndian32(&rate, baud);

    if (!usbSetComplexProperty_CP2101(device, USB_CP2101_CTL_SetBaudRate, 0,
                                      &rate, sizeof(rate))) {
      logMessage(LOG_WARNING, "unable to set CP2101 baud rate: %s", strerror(errno));
    } else if (usbVerifyBaud_CP2101(device, baud, divisor)) {
      return 1;
    }
  }

  {
    logMessage(LOG_CATEGORY(SERIAL_IO), "setting CP2101 baud divisor: %u", divisor);

    if (!usbSetSimpleProperty_CP2101(device, USB_CP2101_CTL_SetBaudDivisor, divisor)) {
      logMessage(LOG_WARNING, "unable to set CP2101 baud divisor: %s", strerror(errno));
    } else if (usbVerifyBaud_CP2101(device, baud, divisor)) {
      return 1;
    }
  }

  return 0;
}

static int
usbSetModemState_CP2101 (UsbDevice *device, int state, int shift, const char *name) {
  if ((state < 0) || (state > 1)) {
    logMessage(LOG_WARNING, "unsupported CP2101 %s state: %d", name, state);
    errno = EINVAL;
    return 0;
  }

  logMessage(LOG_CATEGORY(SERIAL_IO),
             "setting CP2101 %s state: %s",
             name, (state? "high": "low"));

  return usbSetSimpleProperty_CP2101(device, USB_CP2101_CTL_SetModemHandShaking, ((1 << (shift + 8)) | (state << shift)));
}

static int
usbSetDtrState_CP2101 (UsbDevice *device, int state) {
  return usbSetModemState_CP2101(device, state, 0, "DTR");
}

static int
usbSetRtsState_CP2101 (UsbDevice *device, int state) {
  return usbSetModemState_CP2101(device, state, 1, "RTS");
}

static int
usbVerifyFlowControl_CP2101 (UsbDevice *device, const USB_CP2101_FlowControl *expected, size_t size) {
  USB_CP2101_FlowControl actual;
  size_t result;

  logMessage(LOG_CATEGORY(SERIAL_IO), "verifying CP2101 flow control");
  result = usbGetProperty_CP2101(device, USB_CP2101_CTL_GetFlowControl,
                                 &actual, sizeof(actual));

  if (!result) {
    logMessage(LOG_WARNING, "unable to get CP2101 flow control: %s", strerror(errno));
  } else if (result != size) {
    logMessage(LOG_WARNING, "unexpected CP2101 flow control size: %d", (int)result);
  } else if (memcmp(&actual, expected, size) != 0) {
    logMessage(LOG_WARNING, "unexpected CP2101 flow control data");
    logBytes(LOG_WARNING, "expected flow control", expected, size);
    logBytes(LOG_WARNING, "actual flow control", &actual, size);
  } else {
    return 1;
  }

  return 0;
}

static int
usbSetFlowControl_CP2101 (UsbDevice *device, SerialFlowControl flow) {
  USB_CP2101_FlowControl oldSettings;
  USB_CP2101_FlowControl newSettings;
  size_t size;

  logMessage(LOG_CATEGORY(SERIAL_IO), "getting CP2101 flow control");
  size = usbGetProperty_CP2101(device, USB_CP2101_CTL_GetFlowControl,
                               &oldSettings, sizeof(oldSettings));

  if (!size) {
    logMessage(LOG_WARNING, "unable to get CP2101 flow control");
    return 0;
  }

  newSettings = oldSettings;
  newSettings.handshakeOptions = getLittleEndian32(newSettings.handshakeOptions);
  newSettings.dataFlowOptions = getLittleEndian32(newSettings.dataFlowOptions);

  newSettings.handshakeOptions &= ~USB_CP2101_FLOW_HSO_DTR_MASK;
  newSettings.handshakeOptions |= USB_CP2101_FLOW_HSO_DTR_ACTIVE;

  if (flow & SERIAL_FLOW_OUTPUT_CTS) {
    flow &= ~SERIAL_FLOW_OUTPUT_CTS;
    newSettings.handshakeOptions |= USB_CP2101_FLOW_HSO_CTS_INTERPRET;
  } else {
    newSettings.handshakeOptions &= ~USB_CP2101_FLOW_HSO_CTS_INTERPRET;
  }

  if (flow & SERIAL_FLOW_OUTPUT_RTS) {
    flow &= ~SERIAL_FLOW_OUTPUT_RTS;
    newSettings.dataFlowOptions &= ~USB_CP2101_FLOW_DFO_RTS_MASK;
    newSettings.dataFlowOptions |= USB_CP2101_FLOW_DFO_RTS_XMT_ACTIVE;
  } else {
    newSettings.dataFlowOptions &= ~USB_CP2101_FLOW_DFO_RTS_MASK;
    newSettings.dataFlowOptions |= USB_CP2101_FLOW_DFO_RTS_ACTIVE;
  }

  if (flow & SERIAL_FLOW_OUTPUT_XON) {
    flow &= ~SERIAL_FLOW_OUTPUT_XON;
    newSettings.dataFlowOptions |= USB_CP2101_FLOW_DFO_AUTO_TRANSMIT;
  } else {
    newSettings.dataFlowOptions &= ~USB_CP2101_FLOW_DFO_AUTO_TRANSMIT;
  }

  if (flow & SERIAL_FLOW_INPUT_XON) {
    flow &= ~SERIAL_FLOW_INPUT_XON;
    newSettings.dataFlowOptions |= USB_CP2101_FLOW_DFO_AUTO_RECEIVE;
  } else {
    newSettings.dataFlowOptions &= ~USB_CP2101_FLOW_DFO_AUTO_RECEIVE;
  }

  putLittleEndian32(&newSettings.handshakeOptions, newSettings.handshakeOptions);
  putLittleEndian32(&newSettings.dataFlowOptions, newSettings.dataFlowOptions);

  if (flow) {
    logUnsupportedFlowControl(flow);
    errno = EINVAL;
    return 0;
  }

  if (memcmp(&newSettings, &oldSettings, size) == 0) {
    logMessage(LOG_CATEGORY(SERIAL_IO), "CP2101 flow control unchanged");
  }

  logMessage(LOG_CATEGORY(SERIAL_IO), "setting CP2101 flow control");

  if (!usbSetComplexProperty_CP2101(device, USB_CP2101_CTL_SetFlowControl, 0,
                                    &newSettings, size)) {
    logMessage(LOG_WARNING, "unable to set CP2101 flow control: %s", strerror(errno));
  } else if (usbVerifyFlowControl_CP2101(device, &newSettings, size)) {
    return 1;
  }

  return 0;
}

static int
usbVerifyLineControl_CP2101 (UsbDevice *device, USB_CP2101_LineControl expected) {
  USB_CP2101_LineControl actual;
  size_t result;

  logMessage(LOG_CATEGORY(SERIAL_IO), "verifying CP2101 line control");
  result = usbGetProperty_CP2101(device, USB_CP2101_CTL_GetLineControl,
                                 &actual, sizeof(actual));

  if (!result) {
    logMessage(LOG_WARNING, "unable to get CP2101 line control: %s", strerror(errno));
  } else if (result != sizeof(actual)) {
    logMessage(LOG_WARNING, "unexpected CP2101 line control size: %d", (int)result);
  } else if (getLittleEndian16(actual) != expected) {
    logMessage(LOG_WARNING,
               "unexpected CP2101 line control value: Expected:0X%04X Actual:0X%04X",
               expected, getLittleEndian16(actual));
  } else {
    return 1;
  }

  return 0;
}

static int
usbSetDataFormat_CP2101 (UsbDevice *device, unsigned int dataBits, SerialStopBits stopBits, SerialParity parity) {
  int ok = 1;
  USB_CP2101_LineControl lineControl = 0;

  {
    USB_CP2101_LineControl value;

    if ((dataBits >= USB_CP2101_DATA_MINIMUM) &&
        (dataBits <= USB_CP2101_DATA_MAXIMUM)) {
      value = dataBits;
    } else {
      logUnsupportedDataBits(dataBits);
      ok = 0;
      value = 8;
    }

    lineControl |= value << USB_CP2101_DATA_SHIFT;
  }

  {
    USB_CP2101_LineControl value;

    switch (parity) {
      case SERIAL_PARITY_NONE:  value = USB_CP2101_PARITY_NONE;  break;
      case SERIAL_PARITY_ODD:   value = USB_CP2101_PARITY_ODD;   break;
      case SERIAL_PARITY_EVEN:  value = USB_CP2101_PARITY_EVEN;  break;
      case SERIAL_PARITY_MARK:  value = USB_CP2101_PARITY_MARK;  break;
      case SERIAL_PARITY_SPACE: value = USB_CP2101_PARITY_SPACE; break;

      default:
        logUnsupportedParity(parity);
        ok = 0;
        value = USB_CP2101_PARITY_NONE;
        break;
    }

    lineControl |= value << USB_CP2101_PARITY_SHIFT;
  }

  {
    USB_CP2101_LineControl value;

    switch (stopBits) {
      case SERIAL_STOP_1:   value = USB_CP2101_STOP_1;   break;
      case SERIAL_STOP_1_5: value = USB_CP2101_STOP_1_5; break;
      case SERIAL_STOP_2:   value = USB_CP2101_STOP_2;   break;

      default:
        logUnsupportedStopBits(stopBits);
        ok = 0;
        value = USB_CP2101_STOP_1;
        break;
    }

    lineControl |= value << USB_CP2101_STOP_SHIFT;
  }

  if (ok) {
    logMessage(LOG_CATEGORY(SERIAL_IO), "setting CP2101 line control: 0X%04X", lineControl);

    if (!usbSetSimpleProperty_CP2101(device, USB_CP2101_CTL_SetLineControl, lineControl)) {
      logMessage(LOG_WARNING, "unable to set CP2101 line control: %0X04X", lineControl);
    } else if (usbVerifyLineControl_CP2101(device, lineControl)) {
      return 1;
    }
  }

  errno = EINVAL;
  return 0;
}

static int
usbSetInterfaceState_CP2101 (UsbDevice *device, int state) {
  logMessage(LOG_CATEGORY(SERIAL_IO),
             "setting CP2101 interface state: %s",
             (state? "enabled": "disabled"));

  return usbSetSimpleProperty_CP2101(device, USB_CP2101_CTL_EnableInterface, state);
}

static int
usbEnableAdapter_CP2101 (UsbDevice *device) {
  if (!usbSetInterfaceState_CP2101(device, 0)) return 0;
  if (!usbSetInterfaceState_CP2101(device, 1)) return 0;

  return 1;
}

const UsbSerialOperations usbSerialOperations_CP2101 = {
  .name = "CP2101",     
  .setBaud = usbSetBaud_CP2101,
  .setDataFormat = usbSetDataFormat_CP2101,
  .setFlowControl = usbSetFlowControl_CP2101,
  .setDtrState = usbSetDtrState_CP2101,
  .setRtsState = usbSetRtsState_CP2101,
  .enableAdapter = usbEnableAdapter_CP2101
};