usb: mausb_host: Add link layer implementation
PATCH v2 2/8 usb: mausb_host: Add link layer implementation
Implemented link layer using kernel sockets. Link layer manages network communication and provides interface for upper layers of MA-USB stack.
--- drivers/usb/mausb_host/Makefile | 1 + drivers/usb/mausb_host/ip_link.c | 346 +++++++++++++++++++++++++++++++ drivers/usb/mausb_host/ip_link.h | 83 ++++++++ 3 files changed, 430 insertions(+)
create mode 100644 drivers/usb/mausb_host/ip_link.c create mode 100644 drivers/usb/mausb_host/ip_link.h
(quoted diff) +0 -0
b/drivers/usb/mausb_host/Makefile
index ad7fec4d9ff9..4db9dbfc1ebb 100644
drivers/usb/mausb_host/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_HOST_MAUSB) += mausb_host.o mausb_host-y := mausb_host.o mausb_host-y += utils.o +mausb_host-y += ip_link.o ccflags-y += -I$(srctree)/$(src) ccflags-y += -g
(quoted diff) +0 -0
b/drivers/usb/mausb_host/ip_link.c
new file mode 100644
index 000000000000..2c036295412c
drivers/usb/mausb_host/ip_link.c new file +234 -0
234 lines not on this page · raw message
*recv_ctx)
+{
+ recv_ctx->buffer = NULL;
+ recv_ctx->left = 0;
+ recv_ctx->received = 0;
+}
+
+static inline void __mausb_ip_recv_ctx_free(struct mausb_ip_recv_ctx *recv_ctx)
+{
+ kfree(recv_ctx->buffer);
+ __mausb_ip_recv_ctx_clear(recv_ctx);
+}
+
+static int __mausb_ip_recv(struct mausb_ip_ctx *ip_ctx) +{
+ struct msghdr msghd;
+ struct kvec vec;
+ int status;
+ bool peek = true;
+ unsigned int optval = 1;
+ struct socket *client_socket = (struct socket *)ip_ctx->client_socket;
+
+ /* receive with timeout of 0.5s */
+ while (true) {
+ memset(&msghd, 0, sizeof(msghd));
+ if (peek) {
+ vec.iov_base = ip_ctx->recv_ctx.common_hdr;
+ vec.iov_len = sizeof(ip_ctx->recv_ctx.common_hdr);
+ msghd.msg_flags = MSG_PEEK;
+ } else {
+ vec.iov_base =
+ ip_ctx->recv_ctx.buffer +
+ ip_ctx->recv_ctx.received;
+ vec.iov_len = ip_ctx->recv_ctx.left;
+ msghd.msg_flags = MSG_WAITALL;
+ }
+
+ if (!ip_ctx->udp) {
+ status = kernel_setsockopt(client_socket, IPPROTO_TCP,
+ TCP_QUICKACK, + (char *)&optval, + sizeof(optval));
+ if (status != 0) {
+ mausb_pr_warn("Setting TCP_QUICKACK failed: %s:%d, status=%d",
+ ip_ctx->ip_addr, ip_ctx->port,
+ status);
+ }
+ }
+
+ status = kernel_recvmsg(client_socket, &msghd, &vec, 1,
+ vec.iov_len, (int)msghd.msg_flags);
+ if (status == -EAGAIN) {
+ return -EAGAIN;
+ } else if (status <= 0) {
+ mausb_pr_warn("kernel_recvmsg host %s:%d, status=%d",
+ ip_ctx->ip_addr, ip_ctx->port, status);
+
+ __mausb_ip_recv_ctx_free(&ip_ctx->recv_ctx);
+ ip_ctx->fn_callback(ip_ctx->ctx, ip_ctx->channel,
+ MAUSB_LINK_RECV, status, NULL);
+ return status;
+ }
+
+ mausb_pr_debug("kernel_recvmsg host %s:%d, status=%d",
+ ip_ctx->ip_addr, ip_ctx->port, status);
+
+ if (peek) {
+ if ((unsigned int)status <
+ sizeof(ip_ctx->recv_ctx.common_hdr))
+ return -EAGAIN;
+ /* length field of mausb_common_hdr */
+ ip_ctx->recv_ctx.left =
+ *(u16 *)(&ip_ctx->recv_ctx.common_hdr[2]);
+ ip_ctx->recv_ctx.received = 0;
+ ip_ctx->recv_ctx.buffer =
+ kzalloc(ip_ctx->recv_ctx.left, GFP_KERNEL);
+ peek = false;
+ if (!ip_ctx->recv_ctx.buffer) {
+ ip_ctx->fn_callback(ip_ctx->ctx,
+ ip_ctx->channel, + MAUSB_LINK_RECV, + -ENOMEM, NULL);
+ return -ENOMEM;
+ }
+ } else {
+ if (status < ip_ctx->recv_ctx.left) {
+ ip_ctx->recv_ctx.left -= (u16)status;
+ ip_ctx->recv_ctx.received += (u16)status;
+ } else {
+ ip_ctx->fn_callback(ip_ctx->ctx,
+ ip_ctx->channel, + MAUSB_LINK_RECV, status, + ip_ctx->recv_ctx.buffer);
+ __mausb_ip_recv_ctx_clear(&ip_ctx->recv_ctx);
+ peek = true;
+ }
+ }
+ }
+
+ return status;
+}
+
+static void __mausb_ip_recv_work(struct work_struct *work) +{
+ struct mausb_ip_ctx *ip_ctx = container_of(work, struct mausb_ip_ctx,
+ recv_work);
+ int status = __mausb_ip_recv(ip_ctx);
+
+ if (status <= 0 && status != -EAGAIN)
+ return;
+
+ queue_work(ip_ctx->recv_workq, &ip_ctx->recv_work);
+}
(quoted diff) +0 -0
b/drivers/usb/mausb_host/ip_link.h
new file mode 100644
index 000000000000..b5b9ec63d6a9
drivers/usb/mausb_host/ip_link.h new file +83 -0
83 lines not on this page · raw message
Signature · 1 line
2.17.1