Skip to content

Instantly share code, notes, and snippets.

@kuldeepdhaka
Last active August 1, 2016 13:59
Show Gist options
  • Save kuldeepdhaka/8bf5368aab8b0749de426c6b77f0c991 to your computer and use it in GitHub Desktop.
Save kuldeepdhaka/8bf5368aab8b0749de426c6b77f0c991 to your computer and use it in GitHub Desktop.
diff --git a/kernel/drivers/devusb_cdc_ecm.c b/kernel/drivers/devusb_cdc_ecm.c
index 3f274ff..d193175 100644
--- a/kernel/drivers/devusb_cdc_ecm.c
+++ b/kernel/drivers/devusb_cdc_ecm.c
@@ -34,22 +34,6 @@ struct pico_dev_usbeth {
};
static struct pico_dev_usbeth *pico_usbeth = NULL;
-static const struct usb_device_descriptor cdc_ecm_dev = {
- .bLength = USB_DT_DEVICE_SIZE,
- .bDescriptorType = USB_DT_DEVICE,
- .bcdUSB = 0x0200,
- .bDeviceClass = USB_CLASS_CDC,
- .bDeviceSubClass = 0,
- .bDeviceProtocol = 0,
- .bMaxPacketSize0 = 64,
- .idVendor = 0x0483,
- .idProduct = 0x5740,
- .bcdDevice = 0x0200,
- .iManufacturer = 1,
- .iProduct = 2,
- .iSerialNumber = 3,
- .bNumConfigurations = 1,
-};
static const struct usb_endpoint_descriptor comm_endp[] = {{
.bLength = USB_DT_ENDPOINT_SIZE,
@@ -160,42 +144,72 @@ static const struct usb_config_descriptor cdc_ecm_config = {
.interface = ifaces,
};
+static const struct usb_device_descriptor cdc_ecm_dev = {
+ .bLength = USB_DT_DEVICE_SIZE,
+ .bDescriptorType = USB_DT_DEVICE,
+ .bcdUSB = 0x0200,
+ .bDeviceClass = USB_CLASS_CDC,
+ .bDeviceSubClass = 0,
+ .bDeviceProtocol = 0,
+ .bMaxPacketSize0 = 64,
+ .idVendor = 0x0483,
+ .idProduct = 0x5740,
+ .bcdDevice = 0x0200,
+ .iManufacturer = 1,
+ .iProduct = 2,
+ .iSerialNumber = 3,
+ .bNumConfigurations = 1,
+
+ .config = &cdc_ecm_config
+};
+
static const char usb_string_manuf[] = "Insane adding machines";
static const char usb_string_name[] = "Frosted Eth gadget";
static const char usb_serialn[] = "01";
static const char usb_macaddr[] = "005af341b4c9";
-static const char *usb_strings[4] = {
+static const char *usb_strings_ascii[4] = {
usb_string_manuf, usb_string_name, usb_serialn, usb_macaddr
};
+static int usb_strings(usbd_device *_usbd_dev,
+ struct usbd_get_string_arg *arg)
+{
+ (void)_usbd_dev;
+ return usbd_handle_string_ascii(arg, usb_strings_ascii, 4);
+}
+
static const uint8_t mac_addr[6] = { 0, 0x5a, 0xf3, 0x41, 0xb4, 0xca };
-static int cdcecm_control_request(usbd_device *usbd_dev,
- struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
- void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
+static enum usbd_control_result cdcecm_control_request(
+ usbd_device *_usbd_dev, struct usbd_control_arg *arg)
{
- (void)complete;
- (void)buf;
- (void)usbd_dev;
+ (void)_usbd_dev;
+
+ const uint8_t bmReqMask = USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT;
+ const uint8_t bmReqVal = USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE;
+
+ if ((arg->setup.bmRequestType & bmReqMask) != bmReqVal) {
+ return USBD_REQ_NEXT;
+ }
switch (req->bRequest) {
case USB_CDC_REQ_SET_ETHERNET_MULTICAST_FILTER:
case USB_CDC_REQ_SET_ETHERNET_PACKET_FILTER:
case USB_CDC_REQ_SET_ETHERNET_PM_PATTERN_FILTER:
- return 1;
+ return USBD_REQ_HANDLED;
case USB_CDC_REQ_SET_CONTROL_LINE_STATE: {
- return 1;
+ return USBD_REQ_HANDLED;
}
case USB_CDC_REQ_SET_LINE_CODING:
if (*len < sizeof(struct usb_cdc_line_coding)) {
- return 0;
+ return USBD_REQ_STALL;
}
- return 1;
+ return USBD_REQ_HANDLED;
}
- return 0;
+ return USBD_REQ_STALL;
}
@@ -209,15 +223,10 @@ static int cdcecm_control_request(usbd_device *usbd_dev,
*
*
*/
-static void cdcecm_control_callback(usbd_device *usbd_dev, uint16_t wValue);
-
-static struct devusb_config devusb_cdc_ecm = {
- .conf_desc = &cdc_ecm_config,
- .dev_desc = &cdc_ecm_dev,
- .strings = usb_strings,
- .n_strings = 4,
- .callback = cdcecm_control_callback
-};
+static void cdcecm_set_config(usbd_device *usbd_dev,
+ const struct usb_config_descriptor *cfg);
+
+static usbd_device *usbd_dev;
struct usbeth_rx_buffer {
uint16_t size;
@@ -264,7 +273,7 @@ static void cdcecm_data_tx_complete_cb(usbd_device *usbd_dev, uint8_t ep)
}
-static void pico_usbeth_rx(void *arg)
+static void pico_usbeth_rx(void *arg)
{
struct usbeth_rx_buffer *cur_rxbuf = (struct usbeth_rx_buffer *)arg;
if (cur_rxbuf->status == RXBUF_INCOMING) {
@@ -319,20 +328,15 @@ static void cdcecm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
}
}
-static void cdcecm_control_callback(usbd_device *usbd_dev, uint16_t wValue)
+static void cdcecm_set_config(usbd_device *usbd_dev,
+ const struct usb_config_descriptor *cfg)
{
- (void)wValue;
+ (void)cfg;
usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64,
cdcecm_data_rx_cb);
usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 64, cdcecm_data_tx_complete_cb);
usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
-
- usbd_register_control_callback(
- usbd_dev,
- USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
- USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
- cdcecm_control_request);
}
@@ -349,11 +353,11 @@ static int pico_usbeth_send(struct pico_device *dev, void *buf, int len)
}
if (len <= 64)
- return usbd_ep_write_packet(devusb_cdc_ecm.usbd_dev, 0x81, buf, len);
+ return usbd_ep_write_packet(usbd_dev, 0x81, buf, len);
tx_frame.base = buf;
tx_frame.size = len;
- tx_frame.off = usbd_ep_write_packet(devusb_cdc_ecm.usbd_dev, 0x81, buf, 64);
+ tx_frame.off = usbd_ep_write_packet(usbd_dev, 0x81, buf, 64);
pico_usbeth->tx_busy++;
return 0;
}
@@ -409,7 +413,12 @@ int usb_ethernet_init(void)
/* Set default gateway */
pico_ipv4_route_add(zero, zero, default_gw, 1, NULL);
pico_usbeth->tx_busy = 0;
- if (usbdev_start(&devusb_cdc_ecm) < 0)
+ if (usbdev_start(&usbd_dev, cdc_ecm_dev) < 0)
return -EBUSY;
+
+ usbd_register_set_config_callback(usbd_dev, cdcecm_set_config);
+ usbd_register_control_callback(usbd_dev, cdcecm_control_request);
+ usbd_register_get_string_callback(usbd_dev, usb_strings);
+
return 0;
}
diff --git a/kernel/drivers/stm32_usb.c b/kernel/drivers/stm32_usb.c
index 4c337ee..475f038 100644
--- a/kernel/drivers/stm32_usb.c
+++ b/kernel/drivers/stm32_usb.c
@@ -17,7 +17,7 @@
* Authors:
*
*/
-
+
#include "frosted.h"
#include "device.h"
#include <unicore-mx/cm3/nvic.h>
@@ -31,29 +31,27 @@ static struct module mod_usb = {
.name = "usb-otg-guest",
};
+#ifdef CONFIG_DEVUSB
+uint8_t buffer[128] __attribute__(aligned(16));
+#endif
-
-#define MAX_USBS 1
-
-static struct devusb_config *dev_usb = NULL;
+static struct usbd_device *usbd_dev = NULL;
void otg_fs_isr(void)
{
- usbd_poll(dev_usb->usbd_dev);
+ usbd_poll(usbd_dev);
}
-int usbdev_start(struct devusb_config *d)
+int usbdev_start(usbd_device **_usbd_dev,
+ const struct usb_device_descriptor *dev_desc)
{
- if (dev_usb)
+ if (usbd_dev)
return -EBUSY;
rcc_periph_clock_enable(RCC_OTGFS);
- dev_usb = d;
- dev_usb->usbd_dev = usbd_init(&otgfs_usb_driver, dev_usb->dev_desc, dev_usb->conf_desc,
- dev_usb->strings, 4,
- dev_usb->buffer, 128);
- usbd_register_set_config_callback(dev_usb->usbd_dev, dev_usb->callback);
+ usbd_dev = usbd_init(USBD_STM32_OTG_FS, dev_desc, buffer, sizeof(buffer));
+ *_usbd_dev = usbd_dev;
nvic_enable_irq(NVIC_OTG_FS_IRQ);
}
diff --git a/kernel/drivers/usb.h b/kernel/drivers/usb.h
index aa4bdb8..ea0c136 100644
--- a/kernel/drivers/usb.h
+++ b/kernel/drivers/usb.h
@@ -15,26 +15,13 @@ struct usb_config {
struct gpio_config pio_vbus, pio_dm, pio_dp;
};
-
-#define DEVUSB_BUFLEN 128
-#define DEVUSB_STRINGS 128
-struct devusb_config {
- usbd_device *usbd_dev;
- const struct usb_device_descriptor *dev_desc;
- const struct usb_config_descriptor *conf_desc;
- const struct usb_endpoint_descriptor * data_endp;
- const char ** strings;
- int n_strings;
- uint8_t buffer[DEVUSB_BUFLEN];
- void (*callback)(usbd_device *usbd_dev, uint16_t wValue);
-};
-
#ifdef CONFIG_DEVUSB
- int usb_init(struct usb_config *conf);
+ int usb_init(usbd_device **usbd_dev,
+ const struct usb_device_descriptor *dev_desc);
int usbdev_start(struct devusb_config *conf);
#else
# define usb_init(x) ((-ENOENT))
-# define usbdev_start(x) ((-ENOENT))
+# define usbdev_start(x,y) ((-ENOENT))
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment