[cdcconsole] Initial console over usb cdc serial.

This commit is contained in:
Erik Gilling
2018-01-16 13:37:08 -08:00
committed by Erik Gilling
parent 019d262d87
commit 448f7a7aba
3 changed files with 129 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 2018 The Fuchsia Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <lib/cdcconsole/cdcconsole.h>
#include <compiler.h>
#include <err.h>
#include <lib/cbuf.h>
#include <string.h>
#if !CONSOLE_HAS_INPUT_BUFFER
#error "CONSOLE_HAS_INPUT_BUFFER needs to be configured for cdcconsole."
#endif
/*
* This implementation is pretty simple right now. Possible future work:
* * Make cdcconsole_print() asynchronous.
* * Buffer output when offline so that you can see boot messages.
* * Buffer writes with a timeout allowing the driver to coalesce them into
* less packets.
*/
static status_t cdcconsole_rx_cb(ep_t endpoint, usbc_transfer_t *t);
static void cdcconsole_print(print_callback_t *cb, const char *str, size_t len) {
cdcconsole_t *con = cb->context;
if (con->online) {
cdcserial_write(&con->cdc_chan, len, (uint8_t *)str);
}
}
static void cdcconsole_queue_read(cdcconsole_t *con) {
cdcserial_read_async(&con->cdc_chan, &con->rx_transfer, cdcconsole_rx_cb,
sizeof(con->rx_buf), con->rx_buf);
}
static status_t cdcconsole_rx_cb(ep_t endpoint, usbc_transfer_t *t) {
cdcconsole_t *con = containerof(t, cdcconsole_t, rx_transfer);
cbuf_write(&console_input_cbuf, t->buf, t->bufpos, false);
cdcconsole_queue_read(con);
return NO_ERROR;
}
static void cdcconsole_online(cdcserial_channel_t *chan, bool online) {
cdcconsole_t *con = containerof(chan, cdcconsole_t, cdc_chan);
con->online = online;
if (online) {
cdcconsole_queue_read(con);
}
}
void cdcconsole_init(cdcconsole_t *con, int data_ep_addr, int ctrl_ep_addr) {
memset(con, 0x0, sizeof(*con));
con->cdc_chan.online_cb = cdcconsole_online;
cdcserial_create_channel(&con->cdc_chan, data_ep_addr, ctrl_ep_addr);
con->print_cb.print = cdcconsole_print;
con->print_cb.context = con;
register_print_callback(&con->print_cb);
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018 The Fuchsia Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <dev/usb/class/cdcserial.h>
#include <lib/io.h>
typedef struct {
bool online;
cdcserial_channel_t cdc_chan;
print_callback_t print_cb;
usbc_transfer_t rx_transfer;
uint8_t rx_buf[64];
} cdcconsole_t;
void cdcconsole_init(cdcconsole_t *con, int data_ep_addr, int ctrl_ep_addr);

8
lib/cdcconsole/rules.mk Normal file
View File

@@ -0,0 +1,8 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
MODULE_SRCS += \
$(LOCAL_DIR)/cdcconsole.c
include make/module.mk