blob: 08f2af253dcc9570e81df89b020a248f9324aa82 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#include "tusb_option.h"
28
29#if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_CDC)
30
31#include "device/usbd.h"
32#include "device/usbd_pvt.h"
33
34#include "cdc_device.h"
35
36//--------------------------------------------------------------------+
37// MACRO CONSTANT TYPEDEF
38//--------------------------------------------------------------------+
39enum
40{
41 BULK_PACKET_SIZE = (TUD_OPT_HIGH_SPEED ? 512 : 64)
42};
43
44typedef struct
45{
46 uint8_t itf_num;
47 uint8_t ep_notif;
48 uint8_t ep_in;
49 uint8_t ep_out;
50
51 // Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
52 uint8_t line_state;
53
54 /*------------- From this point, data is not cleared by bus reset -------------*/
55 char wanted_char;
56 cdc_line_coding_t line_coding;
57
58 // FIFO
59 tu_fifo_t rx_ff;
60 tu_fifo_t tx_ff;
61
62 uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
63 uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
64
65#if CFG_FIFO_MUTEX
66 osal_mutex_def_t rx_ff_mutex;
67 osal_mutex_def_t tx_ff_mutex;
68#endif
69
70 // Endpoint Transfer buffer
71 CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
72 CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE];
73
74}cdcd_interface_t;
75
76#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
77
78//--------------------------------------------------------------------+
79// INTERNAL OBJECT & FUNCTION DECLARATION
80//--------------------------------------------------------------------+
81CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
82
83static bool _prep_out_transaction (cdcd_interface_t* p_cdc)
84{
85 uint8_t const rhport = TUD_OPT_RHPORT;
86 uint16_t available = tu_fifo_remaining(&p_cdc->rx_ff);
87
88 // Prepare for incoming data but only allow what we can store in the ring buffer.
89 // TODO Actually we can still carry out the transfer, keeping count of received bytes
90 // and slowly move it to the FIFO when read().
91 // This pre-check reduces endpoint claiming
92 TU_VERIFY(available >= sizeof(p_cdc->epout_buf));
93
94 // claim endpoint
95 TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_out));
96
97 // fifo can be changed before endpoint is claimed
98 available = tu_fifo_remaining(&p_cdc->rx_ff);
99
100 if ( available >= sizeof(p_cdc->epout_buf) )
101 {
102 return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
103 }else
104 {
105 // Release endpoint since we don't make any transfer
106 usbd_edpt_release(rhport, p_cdc->ep_out);
107
108 return false;
109 }
110}
111
112//--------------------------------------------------------------------+
113// APPLICATION API
114//--------------------------------------------------------------------+
115bool tud_cdc_n_connected(uint8_t itf)
116{
117 // DTR (bit 0) active is considered as connected
118 return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);
119}
120
121uint8_t tud_cdc_n_get_line_state (uint8_t itf)
122{
123 return _cdcd_itf[itf].line_state;
124}
125
126void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
127{
128 (*coding) = _cdcd_itf[itf].line_coding;
129}
130
131void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
132{
133 _cdcd_itf[itf].wanted_char = wanted;
134}
135
136
137//--------------------------------------------------------------------+
138// READ API
139//--------------------------------------------------------------------+
140uint32_t tud_cdc_n_available(uint8_t itf)
141{
142 return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
143}
144
145uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
146{
147 cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
148 uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, bufsize);
149 _prep_out_transaction(p_cdc);
150 return num_read;
151}
152
153bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr)
154{
155 return tu_fifo_peek(&_cdcd_itf[itf].rx_ff, chr);
156}
157
158void tud_cdc_n_read_flush (uint8_t itf)
159{
160 cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
161 tu_fifo_clear(&p_cdc->rx_ff);
162 _prep_out_transaction(p_cdc);
163}
164
165//--------------------------------------------------------------------+
166// WRITE API
167//--------------------------------------------------------------------+
168uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
169{
170 cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
171 uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, bufsize);
172
173 // flush if queue more than packet size
174 if ( tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE )
175 {
176 tud_cdc_n_write_flush(itf);
177 }
178
179 return ret;
180}
181
182uint32_t tud_cdc_n_write_flush (uint8_t itf)
183{
184 cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
185
186 // Skip if usb is not ready yet
187 TU_VERIFY( tud_ready(), 0 );
188
189 // No data to send
190 if ( !tu_fifo_count(&p_cdc->tx_ff) ) return 0;
191
192 uint8_t const rhport = TUD_OPT_RHPORT;
193
194 // Claim the endpoint
195 TU_VERIFY( usbd_edpt_claim(rhport, p_cdc->ep_in), 0 );
196
197 // Pull data from FIFO
198 uint16_t const count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf));
199
200 if ( count )
201 {
202 TU_ASSERT( usbd_edpt_xfer(rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0 );
203 return count;
204 }else
205 {
206 // Release endpoint since we don't make any transfer
207 // Note: data is dropped if terminal is not connected
208 usbd_edpt_release(rhport, p_cdc->ep_in);
209 return 0;
210 }
211}
212
213uint32_t tud_cdc_n_write_available (uint8_t itf)
214{
215 return tu_fifo_remaining(&_cdcd_itf[itf].tx_ff);
216}
217
218bool tud_cdc_n_write_clear (uint8_t itf)
219{
220 return tu_fifo_clear(&_cdcd_itf[itf].tx_ff);
221}
222
223//--------------------------------------------------------------------+
224// USBD Driver API
225//--------------------------------------------------------------------+
226void cdcd_init(void)
227{
228 tu_memclr(_cdcd_itf, sizeof(_cdcd_itf));
229
230 for(uint8_t i=0; i<CFG_TUD_CDC; i++)
231 {
232 cdcd_interface_t* p_cdc = &_cdcd_itf[i];
233
234 p_cdc->wanted_char = (char) -1;
235
236 // default line coding is : stop bit = 1, parity = none, data bits = 8
237 p_cdc->line_coding.bit_rate = 115200;
238 p_cdc->line_coding.stop_bits = 0;
239 p_cdc->line_coding.parity = 0;
240 p_cdc->line_coding.data_bits = 8;
241
242 // Config RX fifo
243 tu_fifo_config(&p_cdc->rx_ff, p_cdc->rx_ff_buf, TU_ARRAY_SIZE(p_cdc->rx_ff_buf), 1, false);
244
245 // Config TX fifo as overwritable at initialization and will be changed to non-overwritable
246 // if terminal supports DTR bit. Without DTR we do not know if data is actually polled by terminal.
247 // In this way, the most current data is prioritized.
248 tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);
249
250#if CFG_FIFO_MUTEX
251 tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
252 tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
253#endif
254 }
255}
256
257void cdcd_reset(uint8_t rhport)
258{
259 (void) rhport;
260
261 for(uint8_t i=0; i<CFG_TUD_CDC; i++)
262 {
263 cdcd_interface_t* p_cdc = &_cdcd_itf[i];
264
265 tu_memclr(p_cdc, ITF_MEM_RESET_SIZE);
266 tu_fifo_clear(&p_cdc->rx_ff);
267 tu_fifo_clear(&p_cdc->tx_ff);
268 tu_fifo_set_overwritable(&p_cdc->tx_ff, true);
269 }
270}
271
272uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len)
273{
274 // Only support ACM subclass
275 TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
276 CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0);
277
278 // Find available interface
279 cdcd_interface_t * p_cdc = NULL;
280 for(uint8_t cdc_id=0; cdc_id<CFG_TUD_CDC; cdc_id++)
281 {
282 if ( _cdcd_itf[cdc_id].ep_in == 0 )
283 {
284 p_cdc = &_cdcd_itf[cdc_id];
285 break;
286 }
287 }
288 TU_ASSERT(p_cdc, 0);
289
290 //------------- Control Interface -------------//
291 p_cdc->itf_num = itf_desc->bInterfaceNumber;
292
293 uint16_t drv_len = sizeof(tusb_desc_interface_t);
294 uint8_t const * p_desc = tu_desc_next( itf_desc );
295
296 // Communication Functional Descriptors
297 while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
298 {
299 drv_len += tu_desc_len(p_desc);
300 p_desc = tu_desc_next(p_desc);
301 }
302
303 if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
304 {
305 // notification endpoint
306 tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
307
308 TU_ASSERT( usbd_edpt_open(rhport, desc_ep), 0 );
309 p_cdc->ep_notif = desc_ep->bEndpointAddress;
310
311 drv_len += tu_desc_len(p_desc);
312 p_desc = tu_desc_next(p_desc);
313 }
314
315 //------------- Data Interface (if any) -------------//
316 if ( (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) &&
317 (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
318 {
319 // next to endpoint descriptor
320 drv_len += tu_desc_len(p_desc);
321 p_desc = tu_desc_next(p_desc);
322
323 // Open endpoint pair
324 TU_ASSERT( usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in), 0 );
325
326 drv_len += 2*sizeof(tusb_desc_endpoint_t);
327 }
328
329 // Prepare for incoming data
330 _prep_out_transaction(p_cdc);
331
332 return drv_len;
333}
334
335// Invoked when a control transfer occurred on an interface of this class
336// Driver response accordingly to the request and the transfer stage (setup/data/ack)
337// return false to stall control endpoint (e.g unsupported request)
338bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
339{
340 // Handle class request only
341 TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
342
343 uint8_t itf = 0;
344 cdcd_interface_t* p_cdc = _cdcd_itf;
345
346 // Identify which interface to use
347 for ( ; ; itf++, p_cdc++)
348 {
349 if (itf >= TU_ARRAY_SIZE(_cdcd_itf)) return false;
350
351 if ( p_cdc->itf_num == request->wIndex ) break;
352 }
353
354 switch ( request->bRequest )
355 {
356 case CDC_REQUEST_SET_LINE_CODING:
357 if (stage == CONTROL_STAGE_SETUP)
358 {
359 TU_LOG2(" Set Line Coding\r\n");
360 tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
361 }
362 else if ( stage == CONTROL_STAGE_ACK)
363 {
364 if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
365 }
366 break;
367
368 case CDC_REQUEST_GET_LINE_CODING:
369 if (stage == CONTROL_STAGE_SETUP)
370 {
371 TU_LOG2(" Get Line Coding\r\n");
372 tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
373 }
374 break;
375
376 case CDC_REQUEST_SET_CONTROL_LINE_STATE:
377 if (stage == CONTROL_STAGE_SETUP)
378 {
379 tud_control_status(rhport, request);
380 }
381 else if (stage == CONTROL_STAGE_ACK)
382 {
383 // CDC PSTN v1.2 section 6.3.12
384 // Bit 0: Indicates if DTE is present or not.
385 // This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready)
386 // Bit 1: Carrier control for half-duplex modems.
387 // This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
388 bool const dtr = tu_bit_test(request->wValue, 0);
389 bool const rts = tu_bit_test(request->wValue, 1);
390
391 p_cdc->line_state = (uint8_t) request->wValue;
392
393 // Disable fifo overwriting if DTR bit is set
394 tu_fifo_set_overwritable(&p_cdc->tx_ff, !dtr);
395
396 TU_LOG2(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts);
397
398 // Invoke callback
399 if ( tud_cdc_line_state_cb ) tud_cdc_line_state_cb(itf, dtr, rts);
400 }
401 break;
402 case CDC_REQUEST_SEND_BREAK:
403 if (stage == CONTROL_STAGE_SETUP)
404 {
405 tud_control_status(rhport, request);
406 }
407 else if (stage == CONTROL_STAGE_ACK)
408 {
409 TU_LOG2(" Send Break\r\n");
410 if ( tud_cdc_send_break_cb ) tud_cdc_send_break_cb(itf, request->wValue);
411 }
412 break;
413
414 default: return false; // stall unsupported request
415 }
416
417 return true;
418}
419
420bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
421{
422 (void) result;
423
424 uint8_t itf;
425 cdcd_interface_t* p_cdc;
426
427 // Identify which interface to use
428 for (itf = 0; itf < CFG_TUD_CDC; itf++)
429 {
430 p_cdc = &_cdcd_itf[itf];
431 if ( ( ep_addr == p_cdc->ep_out ) || ( ep_addr == p_cdc->ep_in ) ) break;
432 }
433 TU_ASSERT(itf < CFG_TUD_CDC);
434
435 // Received new data
436 if ( ep_addr == p_cdc->ep_out )
437 {
438 tu_fifo_write_n(&p_cdc->rx_ff, &p_cdc->epout_buf, xferred_bytes);
439
440 // Check for wanted char and invoke callback if needed
441 if ( tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1) )
442 {
443 for ( uint32_t i = 0; i < xferred_bytes; i++ )
444 {
445 if ( (p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff) )
446 {
447 tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
448 }
449 }
450 }
451
452 // invoke receive callback (if there is still data)
453 if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
454
455 // prepare for OUT transaction
456 _prep_out_transaction(p_cdc);
457 }
458
459 // Data sent to host, we continue to fetch from tx fifo to send.
460 // Note: This will cause incorrect baudrate set in line coding.
461 // Though maybe the baudrate is not really important !!!
462 if ( ep_addr == p_cdc->ep_in )
463 {
464 // invoke transmit callback to possibly refill tx fifo
465 if ( tud_cdc_tx_complete_cb ) tud_cdc_tx_complete_cb(itf);
466
467 if ( 0 == tud_cdc_n_write_flush(itf) )
468 {
469 // If there is no data left, a ZLP should be sent if
470 // xferred_bytes is multiple of EP Packet size and not zero
471 if ( !tu_fifo_count(&p_cdc->tx_ff) && xferred_bytes && (0 == (xferred_bytes & (BULK_PACKET_SIZE-1))) )
472 {
473 if ( usbd_edpt_claim(rhport, p_cdc->ep_in) )
474 {
475 usbd_edpt_xfer(rhport, p_cdc->ep_in, NULL, 0);
476 }
477 }
478 }
479 }
480
481 // nothing to do with notif endpoint for now
482
483 return true;
484}
485
486#endif