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
|
// SPDX-License-Identifier: GPL-2.0+
/*
* USB CDC serial (ACM) function driver
*
* Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
* Copyright (C) 2008 by David Brownell
* Copyright (C) 2008 by Nokia Corporation
* Copyright (C) 2009 by Samsung Electronics
* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org>
*/
#include <circbuf.h>
#include <console.h>
#include <errno.h>
#include <g_dnl.h>
#include <malloc.h>
#include <memalign.h>
#include <stdio_dev.h>
#include <version.h>
#include <watchdog.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <linux/usb/composite.h>
#include <linux/usb/cdc.h>
#define REQ_SIZE_MAX 512
struct f_acm {
int ctrl_id;
int data_id;
struct usb_ep *ep_in;
struct usb_ep *ep_out;
struct usb_ep *ep_notify;
struct usb_request *req_in;
struct usb_request *req_out;
bool connected;
bool tx_on;
circbuf_t rx_buf;
circbuf_t tx_buf;
struct usb_function usb_function;
struct usb_cdc_line_coding line_coding;
u16 handshake_bits;
#define ACM_CTRL_RTS BIT(1) /* unused with full duplex */
#define ACM_CTRL_DTR BIT(0) /* host is ready for data r/w */
struct udevice *udc;
};
static struct f_acm *default_acm_function;
static inline struct f_acm *func_to_acm(struct usb_function *f)
{
return container_of(f, struct f_acm, usb_function);
}
static inline struct f_acm *stdio_to_acm(struct stdio_dev *s)
{
/* stdio dev is cloned on registration, do not use container_of */
return s->priv;
}
static struct usb_interface_assoc_descriptor
acm_iad_descriptor = {
.bLength = sizeof(acm_iad_descriptor),
.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
.bFirstInterface = 0,
.bInterfaceCount = 2, // control + data
.bFunctionClass = USB_CLASS_COMM,
.bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
.bFunctionProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
};
static struct usb_interface_descriptor acm_control_intf_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 1,
.bInterfaceClass = USB_CLASS_COMM,
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
.bInterfaceProtocol = USB_CDC_ACM_PROTO_AT_V25TER,
};
static struct usb_interface_descriptor acm_data_intf_desc = {
.bLength = sizeof(acm_data_intf_desc),
.bDescriptorType = USB_DT_INTERFACE,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_CDC_DATA,
};
static struct usb_cdc_header_desc acm_header_desc = {
.bLength = sizeof(acm_header_desc),
.bDescriptorType = USB_DT_CS_INTERFACE,
.bDescriptorSubType = USB_CDC_HEADER_TYPE,
.bcdCDC = __constant_cpu_to_le16(0x0110),
};
static struct usb_cdc_call_mgmt_descriptor acm_call_mgmt_desc = {
.bLength = sizeof(acm_call_mgmt_desc),
.bDescriptorType = USB_DT_CS_INTERFACE,
.bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
.bmCapabilities = 0,
.bDataInterface = 0x01,
};
static struct usb_cdc_acm_descriptor acm_desc = {
.bLength = sizeof(acm_desc),
.bDescriptorType = USB_DT_CS_INTERFACE,
.bDescriptorSubType = USB_CDC_ACM_TYPE,
.bmCapabilities = USB_CDC_CAP_LINE,
};
static struct usb_cdc_union_desc acm_union_desc = {
.bLength = sizeof(acm_union_desc),
.bDescriptorType = USB_DT_CS_INTERFACE,
.bDescriptorSubType = USB_CDC_UNION_TYPE,
.bMasterInterface0 = 0x00,
.bSlaveInterface0 = 0x01,
};
static struct usb_endpoint_descriptor acm_fs_notify_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = 3 | USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = __constant_cpu_to_le16(64),
.bInterval = 32,
};
static struct usb_endpoint_descriptor acm_fs_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_endpoint_descriptor acm_fs_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
};
static struct usb_descriptor_header *acm_fs_function[] = {
(struct usb_descriptor_header *)&acm_iad_descriptor,
(struct usb_descriptor_header *)&acm_control_intf_desc,
(struct usb_descriptor_header *)&acm_header_desc,
(struct usb_descriptor_header *)&acm_call_mgmt_desc,
(struct usb_descriptor_header *)&acm_desc,
(struct usb_descriptor_header *)&acm_union_desc,
(struct usb_descriptor_header *)&acm_fs_notify_desc,
(struct usb_descriptor_header *)&acm_data_intf_desc,
(struct usb_descriptor_header *)&acm_fs_in_desc,
(struct usb_descriptor_header *)&acm_fs_out_desc,
NULL,
};
static struct usb_endpoint_descriptor acm_hs_notify_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_INT,
.wMaxPacketSize = __constant_cpu_to_le16(64),
.bInterval = 11,
};
static struct usb_endpoint_descriptor acm_hs_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_endpoint_descriptor acm_hs_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = __constant_cpu_to_le16(512),
};
static struct usb_descriptor_header *acm_hs_function[] = {
(struct usb_descriptor_header *)&acm_iad_descriptor,
(struct usb_descriptor_header *)&acm_control_intf_desc,
(struct usb_descriptor_header *)&acm_header_desc,
(struct usb_descriptor_header *)&acm_call_mgmt_desc,
(struct usb_descriptor_header *)&acm_desc,
(struct usb_descriptor_header *)&acm_union_desc,
(struct usb_descriptor_header *)&acm_hs_notify_desc,
(struct usb_descriptor_header *)&acm_data_intf_desc,
(struct usb_descriptor_header *)&acm_hs_in_desc,
(struct usb_descriptor_header *)&acm_hs_out_desc,
NULL,
};
static inline struct usb_endpoint_descriptor *
ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
struct usb_endpoint_descriptor *fs)
{
if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
return hs;
return fs;
}
static int acm_bind(struct usb_configuration *c, struct usb_function *f)
{
struct usb_gadget *gadget = c->cdev->gadget;
struct f_acm *f_acm = func_to_acm(f);
struct usb_ep *ep;
int id;
id = usb_interface_id(c, f);
if (id < 0)
return id;
acm_iad_descriptor.bFirstInterface = id;
acm_control_intf_desc.bInterfaceNumber = id;
acm_union_desc.bMasterInterface0 = id;
f_acm->ctrl_id = id;
id = usb_interface_id(c, f);
if (id < 0)
return id;
acm_data_intf_desc.bInterfaceNumber = id;
acm_union_desc.bSlaveInterface0 = id;
acm_call_mgmt_desc.bDataInterface = id;
f_acm->data_id = id;
/* allocate instance-specific endpoints */
ep = usb_ep_autoconfig(gadget, &acm_fs_in_desc);
if (!ep)
return -ENODEV;
f_acm->ep_in = ep;
ep = usb_ep_autoconfig(gadget, &acm_fs_out_desc);
if (!ep)
return -ENODEV;
f_acm->ep_out = ep;
ep = usb_ep_autoconfig(gadget, &acm_fs_notify_desc);
if (!ep)
return -ENODEV;
f_acm->ep_notify = ep;
if (gadget_is_dualspeed(gadget)) {
/* Assume endpoint addresses are the same for both speeds */
acm_hs_in_desc.bEndpointAddress = acm_fs_in_desc.bEndpointAddress;
acm_hs_out_desc.bEndpointAddress = acm_fs_out_desc.bEndpointAddress;
acm_hs_notify_desc.bEndpointAddress = acm_fs_notify_desc.bEndpointAddress;
}
return 0;
}
static void acm_unbind(struct usb_configuration *c, struct usb_function *f)
{
struct f_acm *f_acm = func_to_acm(f);
if (default_acm_function == f_acm)
default_acm_function = NULL;
buf_free(&f_acm->rx_buf);
buf_free(&f_acm->tx_buf);
free(f_acm);
}
static void acm_notify_complete(struct usb_ep *ep, struct usb_request *req)
{
/* nothing to do */
}
static void acm_tx_complete(struct usb_ep *ep, struct usb_request *req)
{
struct f_acm *f_acm = req->context;
f_acm->tx_on = true;
}
static void acm_rx_complete(struct usb_ep *ep, struct usb_request *req)
{
struct f_acm *f_acm = req->context;
buf_push(&f_acm->rx_buf, req->buf, req->actual);
/* Queue RX req again */
req->actual = 0;
usb_ep_queue(ep, req, 0);
}
static struct usb_request *acm_start_ep(struct usb_ep *ep, void *complete_cb,
void *context)
{
struct usb_request *req;
req = usb_ep_alloc_request(ep, 0);
if (!req)
return NULL;
req->length = REQ_SIZE_MAX;
req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, REQ_SIZE_MAX);
if (!req->buf) {
usb_ep_free_request(ep, req);
return NULL;
}
memset(req->buf, 0, req->length);
req->complete = complete_cb;
req->context = context;
return req;
}
static int acm_start_data(struct f_acm *f_acm, struct usb_gadget *gadget)
{
const struct usb_endpoint_descriptor *d;
int ret;
/* EP IN */
d = ep_desc(gadget, &acm_hs_in_desc, &acm_fs_in_desc);
ret = usb_ep_enable(f_acm->ep_in, d);
if (ret)
return ret;
f_acm->req_in = acm_start_ep(f_acm->ep_in, acm_tx_complete, f_acm);
/* EP OUT */
d = ep_desc(gadget, &acm_hs_out_desc, &acm_fs_out_desc);
ret = usb_ep_enable(f_acm->ep_out, d);
if (ret)
return ret;
f_acm->req_out = acm_start_ep(f_acm->ep_out, acm_rx_complete, f_acm);
/* Start OUT transfer (EP OUT) */
ret = usb_ep_queue(f_acm->ep_out, f_acm->req_out, 0);
if (ret)
return ret;
return 0;
}
static int acm_start_ctrl(struct f_acm *f_acm, struct usb_gadget *gadget)
{
const struct usb_endpoint_descriptor *d;
usb_ep_disable(f_acm->ep_notify);
d = ep_desc(gadget, &acm_hs_notify_desc, &acm_fs_notify_desc);
usb_ep_enable(f_acm->ep_notify, d);
acm_start_ep(f_acm->ep_notify, acm_notify_complete, f_acm);
return 0;
}
static int acm_set_alt(struct usb_function *f, unsigned int intf, unsigned int alt)
{
struct usb_gadget *gadget = f->config->cdev->gadget;
struct f_acm *f_acm = func_to_acm(f);
if (intf == f_acm->ctrl_id) {
return acm_start_ctrl(f_acm, gadget);
} else if (intf == f_acm->data_id) {
acm_start_data(f_acm, gadget);
f_acm->connected = true;
f_acm->tx_on = true;
return 0;
}
return -EINVAL;
}
static int acm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
{
struct usb_gadget *gadget = f->config->cdev->gadget;
struct usb_request *req = f->config->cdev->req;
u16 w_index = le16_to_cpu(ctrl->wIndex);
u16 w_value = le16_to_cpu(ctrl->wValue);
u16 w_length = le16_to_cpu(ctrl->wLength);
struct f_acm *f_acm = func_to_acm(f);
int value = -1;
switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
| USB_CDC_REQ_SET_LINE_CODING:
/* SET_LINE_CODING */
if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id)
goto invalid;
value = w_length;
memcpy(&f_acm->line_coding, req->buf, value);
break;
case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
| USB_CDC_REQ_GET_LINE_CODING:
/* GET_LINE_CODING */
if (w_length != sizeof(f_acm->line_coding) || w_index != f_acm->ctrl_id)
goto invalid;
value = w_length;
memcpy(req->buf, &f_acm->line_coding, value);
break;
case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
| USB_CDC_REQ_SET_CONTROL_LINE_STATE:
/* SET_CONTROL_LINE_STATE */
if (w_index != f_acm->ctrl_id)
goto invalid;
value = 0;
f_acm->handshake_bits = w_value;
break;
default:
invalid:
printf("invalid control req%02x.%02x v%04x i%04x l%d\n",
ctrl->bRequestType, ctrl->bRequest, w_value, w_index,
w_length);
}
/* respond with data transfer or status phase? */
if (value >= 0) {
req->zero = 0;
req->length = value;
usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
}
return 0;
}
static void acm_disable(struct usb_function *f)
{
struct f_acm *f_acm = func_to_acm(f);
usb_ep_disable(f_acm->ep_out);
usb_ep_disable(f_acm->ep_in);
usb_ep_disable(f_acm->ep_notify);
if (f_acm->req_out) {
free(f_acm->req_out->buf);
usb_ep_free_request(f_acm->ep_out, f_acm->req_out);
f_acm->req_out = NULL;
}
if (f_acm->req_in) {
free(f_acm->req_in->buf);
usb_ep_free_request(f_acm->ep_in, f_acm->req_in);
f_acm->req_in = NULL;
}
}
/* static strings, in UTF-8 */
static struct usb_string acm_string_defs[] = {
[0].s = "CDC Abstract Control Model (ACM)",
[1].s = "CDC ACM Data",
[2].s = "CDC Serial",
{ } /* end of list */
};
static struct usb_gadget_strings acm_string_table = {
.language = 0x0409, /* en-us */
.strings = acm_string_defs,
};
static struct usb_gadget_strings *acm_strings[] = {
&acm_string_table,
NULL,
};
static void __acm_tx(struct f_acm *f_acm)
{
int len, ret;
do {
dm_usb_gadget_handle_interrupts(f_acm->udc);
if (!(f_acm->handshake_bits & ACM_CTRL_DTR))
break;
if (!f_acm->tx_on)
continue;
len = buf_pop(&f_acm->tx_buf, f_acm->req_in->buf, REQ_SIZE_MAX);
if (!len)
break;
f_acm->req_in->length = len;
ret = usb_ep_queue(f_acm->ep_in, f_acm->req_in, 0);
if (ret)
break;
f_acm->tx_on = false;
/* Do not reset the watchdog, if TX is stuck there is probably
* a real issue.
*/
} while (1);
}
static bool acm_connected(struct stdio_dev *dev)
{
struct f_acm *f_acm = stdio_to_acm(dev);
/* give a chance to process udc irq */
dm_usb_gadget_handle_interrupts(f_acm->udc);
return f_acm->connected;
}
static int acm_add(struct usb_configuration *c)
{
struct f_acm *f_acm;
int status;
f_acm = calloc(1, sizeof(*f_acm));
if (!f_acm)
return -ENOMEM;
f_acm->usb_function.name = "f_acm";
f_acm->usb_function.bind = acm_bind;
f_acm->usb_function.unbind = acm_unbind;
f_acm->usb_function.set_alt = acm_set_alt;
f_acm->usb_function.disable = acm_disable;
f_acm->usb_function.strings = acm_strings;
f_acm->usb_function.descriptors = acm_fs_function;
f_acm->usb_function.hs_descriptors = acm_hs_function;
f_acm->usb_function.setup = acm_setup;
status = udc_device_get_by_index(0, &f_acm->udc);
if (status)
return status;
status = usb_add_function(c, &f_acm->usb_function);
if (status) {
free(f_acm);
return status;
}
buf_init(&f_acm->rx_buf, 2048);
buf_init(&f_acm->tx_buf, 2048);
if (!default_acm_function)
default_acm_function = f_acm;
return status;
}
DECLARE_GADGET_BIND_CALLBACK(usb_serial_acm, acm_add);
/* STDIO */
static int acm_stdio_tstc(struct stdio_dev *dev)
{
struct f_acm *f_acm = stdio_to_acm(dev);
dm_usb_gadget_handle_interrupts(f_acm->udc);
return (f_acm->rx_buf.size > 0);
}
static int acm_stdio_getc(struct stdio_dev *dev)
{
struct f_acm *f_acm = stdio_to_acm(dev);
char c;
/* Wait for a character to arrive. */
while (!acm_stdio_tstc(dev))
schedule();
buf_pop(&f_acm->rx_buf, &c, 1);
return c;
}
static void acm_stdio_putc(struct stdio_dev *dev, const char c)
{
struct f_acm *f_acm = stdio_to_acm(dev);
if (c == '\n')
buf_push(&f_acm->tx_buf, "\r", 1);
buf_push(&f_acm->tx_buf, &c, 1);
if (!f_acm->connected)
return;
__acm_tx(f_acm);
}
static void acm_stdio_puts(struct stdio_dev *dev, const char *str)
{
struct f_acm *f_acm = stdio_to_acm(dev);
while (*str) {
if (*str == '\n')
buf_push(&f_acm->tx_buf, "\r", 1);
buf_push(&f_acm->tx_buf, str++, 1);
}
if (!f_acm->connected)
return;
__acm_tx(f_acm);
}
static int acm_stdio_start(struct stdio_dev *dev)
{
struct udevice *udc;
int ret;
if (dev->priv) { /* function already exist */
return 0;
}
ret = udc_device_get_by_index(0, &udc);
if (ret) {
pr_err("USB init failed: %d\n", ret);
return ret;
}
g_dnl_clear_detach();
ret = g_dnl_register("usb_serial_acm");
if (ret)
return ret;
if (default_acm_function)
dev->priv = default_acm_function;
else
return -ENODEV;
while (!acm_connected(dev)) {
if (ctrlc())
return -ECANCELED;
schedule();
}
return 0;
}
static int acm_stdio_stop(struct stdio_dev *dev)
{
g_dnl_unregister();
g_dnl_clear_detach();
return 0;
}
int drv_usbacm_init(void)
{
struct stdio_dev stdio;
strcpy(stdio.name, "usbacm");
stdio.flags = DEV_FLAGS_INPUT | DEV_FLAGS_OUTPUT;
stdio.tstc = acm_stdio_tstc;
stdio.getc = acm_stdio_getc;
stdio.putc = acm_stdio_putc;
stdio.puts = acm_stdio_puts;
stdio.start = acm_stdio_start;
stdio.stop = acm_stdio_stop;
stdio.priv = NULL;
stdio.ext = 0;
return stdio_register(&stdio);
}
|