Browse Source

projects/riscv_usb: Add custom vendor commands in DFU firmware

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
Sylvain Munaut 5 years ago
parent
commit
8b0bebfde2

+ 1 - 0
projects/riscv_usb/fw/Makefile

@@ -39,6 +39,7 @@ HEADERS_dfu=\
 SOURCES_dfu=\
 	fw_dfu.c \
 	usb_dfu.c \
+	usb_dfu_vendor.c \
 	usb_desc_dfu.c
 
 HEADERS_app=\

+ 20 - 3
projects/riscv_usb/fw/usb_dfu.c

@@ -31,6 +31,12 @@
 #include "usb_dfu_proto.h"
 
 
+#define DFU_VENDOR_PROTO
+#ifdef DFU_VENDOR_PROTO
+enum usb_fnd_resp dfu_vendor_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer);
+#endif
+
+
 #define DFU_POLL_MS		250
 
 
@@ -220,11 +226,22 @@ _dfu_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
 {
 	uint8_t state;
 
-	/* If this a class request for DFU interface ? */
-	if (USB_REQ_TYPE_RCPT(req) != (USB_REQ_TYPE_CLASS | USB_REQ_RCPT_INTF))
+	/* If this a class or vendor request for DFU interface ? */
+	if (req->wIndex != g_dfu.intf)
 		return USB_FND_CONTINUE;
 
-	if (req->wIndex != g_dfu.intf)
+#ifdef DFU_VENDOR_PROTO
+	if ((USB_REQ_TYPE(req) | USB_REQ_RCPT(req)) == (USB_REQ_TYPE_VENDOR | USB_REQ_RCPT_INTF)) {
+		/* Let vendor code use our large buffer */
+		xfer->data = g_dfu.buf;
+		xfer->len  = sizeof(g_dfu.buf);
+
+		/* Call vendor code */
+		return dfu_vendor_ctrl_req(req, xfer);
+	}
+#endif
+
+	if ((USB_REQ_TYPE(req) | USB_REQ_RCPT(req)) != (USB_REQ_TYPE_CLASS | USB_REQ_RCPT_INTF))
 		return USB_FND_CONTINUE;
 
 	/* Check if this request is allowed in this state */

+ 72 - 0
projects/riscv_usb/fw/usb_dfu_vendor.c

@@ -0,0 +1,72 @@
+/*
+ * usb_dfu_vendor.c
+ *
+ * Copyright (C) 2019 Sylvain Munaut
+ * All rights reserved.
+ *
+ * LGPL v3+, see LICENSE.lgpl3
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "usb.h"
+#include "spi.h"
+
+
+#define USB_RT_DFU_VENDOR_VERSION	((0 << 8) | 0xc1)
+#define USB_RT_DFU_VENDOR_SPI_EXEC	((1 << 8) | 0x41)
+#define USB_RT_DFU_VENDOR_SPI_RESULT	((2 << 8) | 0xc1)
+
+
+static bool
+_dfu_vendor_spi_exec_cb(struct usb_xfer *xfer)
+{
+	struct spi_xfer_chunk sx[1] = {
+		{ .data = xfer->data, .len = xfer->len, .read = true, .write = true, },
+	};
+	spi_xfer(SPI_CS_FLASH, sx, 1);
+	return true;
+}
+
+enum usb_fnd_resp
+dfu_vendor_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)
+{
+	switch (req->wRequestAndType)
+	{
+	case USB_RT_DFU_VENDOR_VERSION:
+		xfer->len  = 2;
+		xfer->data[0] = 0x01;
+		xfer->data[1] = 0x00;
+		break;
+
+	case USB_RT_DFU_VENDOR_SPI_EXEC:
+		xfer->cb_done = _dfu_vendor_spi_exec_cb;
+		break;
+
+	case USB_RT_DFU_VENDOR_SPI_RESULT:
+		/* Really nothing to do, data is already in the buffer, and we serve
+		 * whatever the host requested ... */
+		break;
+
+	default:
+		return USB_FND_ERROR;
+	}
+
+	return USB_FND_SUCCESS;
+}