File: usb_cdc_acm.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (344 lines) | stat: -rw-r--r-- 9,049 bytes parent folder | download | duplicates (3)
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
/*
 * 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-2024 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 "strfmt.h"
#include "usb_serial.h"
#include "usb_cdc_acm.h"
#include "usb_internal.h"
#include "bitfield.h"

struct UsbSerialDataStruct {
  UsbDevice *device;
  const UsbInterfaceDescriptor *interface;
  const UsbEndpointDescriptor *endpoint;

  USB_CDC_ACM_LineCoding lineCoding;
};

static int
usbGetParameters_CDC_ACM (UsbDevice *device, uint8_t request, uint16_t value, void *data, uint16_t size) {
  UsbSerialData *usd = usbGetSerialData(device);

  ssize_t result = usbControlRead(device,
    UsbControlRecipient_Interface, UsbControlType_Class,
    request, value, usd->interface->bInterfaceNumber,
    data, size, 1000
  );

  return result != -1;
}

static int
usbGetParameter_CDC_ACM (UsbDevice *device, uint8_t request, void *data, uint16_t size) {
  return usbGetParameters_CDC_ACM(device, request, 0, data, size);
}

static int
usbSetParameters_CDC_ACM (UsbDevice *device, uint8_t request, uint16_t value, const void *data, uint16_t size) {
  UsbSerialData *usd = usbGetSerialData(device);

  ssize_t result = usbControlWrite(device,
    UsbControlRecipient_Interface, UsbControlType_Class,
    request, value, usd->interface->bInterfaceNumber,
    data, size, 1000
  );

  return result != -1;
}

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

static int
usbSetControlLines_CDC_ACM (UsbDevice *device, uint16_t lines) {
  return usbSetParameter_CDC_ACM(device, USB_CDC_ACM_CTL_SetControlLineState, lines);
}

static void
usbLogLineCoding_CDC_ACM (const USB_CDC_ACM_LineCoding *lineCoding) {
  char log[0X80];

  STR_BEGIN(log, sizeof(log));
  STR_PRINTF("CDC ACM line coding:");

  { // baud (bits per second)
    uint32_t baud = getLittleEndian32(lineCoding->dwDTERate);
    STR_PRINTF(" Baud:%" PRIu32, baud);
  }

  { // number of data bits
    STR_PRINTF(" Data:%u", lineCoding->bDataBits);
  }

  { // number of stop bits
    const char *bits;

#define USB_CDC_ACM_STOP(value,name) \
case USB_CDC_ACM_STOP_##value: bits = #name; break;
    switch (lineCoding->bCharFormat) {
      USB_CDC_ACM_STOP(1  , 1  )
      USB_CDC_ACM_STOP(1_5, 1.5)
      USB_CDC_ACM_STOP(2  , 2  )
      default: bits = "?"; break;
    }
#undef USB_CDC_ACM_STOP

    STR_PRINTF(" Stop:%s", bits);
  }

  { // type of parity
    const char *parity;

#define USB_CDC_ACM_PARITY(value,name) \
case USB_CDC_ACM_PARITY_##value: parity = #name; break;
    switch (lineCoding->bParityType) {
      USB_CDC_ACM_PARITY(NONE , none )
      USB_CDC_ACM_PARITY(ODD  , odd  )
      USB_CDC_ACM_PARITY(EVEN , even )
      USB_CDC_ACM_PARITY(MARK , mark )
      USB_CDC_ACM_PARITY(SPACE, space)
      default: parity = "?"; break;
    }
#undef USB_CDC_ACM_PARITY

    STR_PRINTF(" Parity:%s", parity);
  }

  STR_END;
  logMessage(LOG_CATEGORY(SERIAL_IO), "%s", log);
}

static int
usbSetLineProperties_CDC_ACM (UsbDevice *device, unsigned int baud, unsigned int dataBits, SerialStopBits stopBits, SerialParity parity) {
  USB_CDC_ACM_LineCoding lineCoding;
  memset(&lineCoding, 0, sizeof(lineCoding));

  putLittleEndian32(&lineCoding.dwDTERate, baud);

  switch (dataBits) {
    case  5:
    case  6:
    case  7:
    case  8:
    case 16:
      lineCoding.bDataBits = dataBits;
      break;

    default:
      logUnsupportedDataBits(dataBits);
      errno = EINVAL;
      return 0;
  }

  switch (stopBits) {
    case SERIAL_STOP_1:
      lineCoding.bCharFormat = USB_CDC_ACM_STOP_1;
      break;

    case SERIAL_STOP_1_5:
      lineCoding.bCharFormat = USB_CDC_ACM_STOP_1_5;
      break;

    case SERIAL_STOP_2:
      lineCoding.bCharFormat = USB_CDC_ACM_STOP_2;
      break;

    default:
      logUnsupportedStopBits(stopBits);
      errno = EINVAL;
      return 0;
  }

  switch (parity) {
    case SERIAL_PARITY_NONE:
      lineCoding.bParityType = USB_CDC_ACM_PARITY_NONE;
      break;

    case SERIAL_PARITY_ODD:
      lineCoding.bParityType = USB_CDC_ACM_PARITY_ODD;
      break;

    case SERIAL_PARITY_EVEN:
      lineCoding.bParityType = USB_CDC_ACM_PARITY_EVEN;
      break;

    case SERIAL_PARITY_MARK:
      lineCoding.bParityType = USB_CDC_ACM_PARITY_MARK;
      break;

    case SERIAL_PARITY_SPACE:
      lineCoding.bParityType = USB_CDC_ACM_PARITY_SPACE;
      break;

    default:
      logUnsupportedParity(parity);
      errno = EINVAL;
      return 0;
  }

  {
    UsbSerialData *usd = usbGetSerialData(device);
    USB_CDC_ACM_LineCoding *oldCoding = &usd->lineCoding;

    if (memcmp(&lineCoding, oldCoding, sizeof(lineCoding)) != 0) {
      if (!usbSetParameters_CDC_ACM(device, USB_CDC_ACM_CTL_SetLineCoding, 0,
                                    &lineCoding, sizeof(lineCoding))) {
        return 0;
      }

      *oldCoding = lineCoding;
      usbLogLineCoding_CDC_ACM(&lineCoding);
    }
  }

  return 1;
}

static int
usbSetFlowControl_CDC_ACM (UsbDevice *device, SerialFlowControl flow) {
  if (flow) {
    logUnsupportedFlowControl(flow);
    errno = EINVAL;
    return 0;
  }

  return 1;
}

static const UsbInterfaceDescriptor *
usbFindCommunicationInterface (UsbDevice *device) {
  const UsbDescriptor *descriptor = NULL;

  while (usbNextDescriptor(device, &descriptor)) {
    if (descriptor->header.bDescriptorType == UsbDescriptorType_Interface) {
      if (descriptor->interface.bInterfaceClass == 0X02) {
        return &descriptor->interface;
      }
    }
  }

  logMessage(LOG_WARNING, "USB: communication interface descriptor not found");
  errno = ENOENT;
  return NULL;
}

static const UsbEndpointDescriptor *
usbFindInterruptInputEndpoint (UsbDevice *device, const UsbInterfaceDescriptor *interface) {
  const UsbDescriptor *descriptor = (const UsbDescriptor *)interface;

  while (usbNextDescriptor(device, &descriptor)) {
    if (descriptor->header.bDescriptorType == UsbDescriptorType_Interface) break;

    if (descriptor->header.bDescriptorType == UsbDescriptorType_Endpoint) {
      if (USB_ENDPOINT_DIRECTION(&descriptor->endpoint) == UsbEndpointDirection_Input) {
        if (USB_ENDPOINT_TRANSFER(&descriptor->endpoint) == UsbEndpointTransfer_Interrupt) {
          return &descriptor->endpoint;
        }
      }
    }
  }

  logMessage(LOG_WARNING, "USB: interrupt input endpoint descriptor not found");
  errno = ENOENT;
  return NULL;
}

static int
usbMakeData_CDC_ACM (UsbDevice *device, UsbSerialData **serialData) {
  UsbSerialData *usd;

  if ((usd = malloc(sizeof(*usd)))) {
    memset(usd, 0, sizeof(*usd));
    usd->device = device;

    if ((usd->interface = usbFindCommunicationInterface(device))) {
      unsigned char interfaceNumber = usd->interface->bInterfaceNumber;

      if (usbClaimInterface(device, interfaceNumber)) {
        if (usbSetAlternative(device, usd->interface->bInterfaceNumber, usd->interface->bAlternateSetting)) {
          if ((usd->endpoint = usbFindInterruptInputEndpoint(device, usd->interface))) {
            usbBeginInput(device, USB_ENDPOINT_NUMBER(usd->endpoint));
            *serialData = usd;
            return 1;
          }
        }

        usbReleaseInterface(device, interfaceNumber);
      }
    }

    free(usd);
  } else {
    logMallocError();
  }

  return 0;
}

static void
usbDestroyData_CDC_ACM (UsbSerialData *usd) {
  usbReleaseInterface(usd->device, usd->interface->bInterfaceNumber);
  free(usd);
}

static int
usbEnableAdapter_CDC_ACM (UsbDevice *device) {
  UsbSerialData *usd = usbGetSerialData(device);

  if (!usbSetControlLines_CDC_ACM(device, 0)) return 0;
  if (!usbSetControlLines_CDC_ACM(device, USB_CDC_ACM_LINE_DTR)) return 0;

  {
    USB_CDC_ACM_LineCoding *lineCoding = &usd->lineCoding;

    if (!usbGetParameter_CDC_ACM(device, USB_CDC_ACM_CTL_GetLineCoding,
                                  lineCoding, sizeof(*lineCoding))) {
      return 0;
    }

    usbLogLineCoding_CDC_ACM(lineCoding);
  }

  return 1;
}

static void
usbDisableAdapter_CDC_ACM (UsbDevice *device) {
  usbSetControlLines_CDC_ACM(device, 0);
}

const UsbSerialOperations usbSerialOperations_CDC_ACM = {
  .name = "CDC_ACM",

  .makeData = usbMakeData_CDC_ACM,
  .destroyData = usbDestroyData_CDC_ACM,

  .setLineProperties = usbSetLineProperties_CDC_ACM,
  .setFlowControl = usbSetFlowControl_CDC_ACM,

  .enableAdapter = usbEnableAdapter_CDC_ACM,
  .disableAdapter = usbDisableAdapter_CDC_ACM
};