xiaoxiang781216 commented on code in PR #18815: URL: https://github.com/apache/nuttx/pull/18815#discussion_r3159179532
########## drivers/usbhost/usbhost_cdcecm.c: ########## @@ -0,0 +1,2160 @@ +/**************************************************************************** + * drivers/usbhost/usbhost_cdcecm.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <semaphore.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <poll.h> +#include <fcntl.h> + +#include <arpa/inet.h> + +#include <nuttx/arch.h> +#include <nuttx/irq.h> +#include <nuttx/kmalloc.h> +#include <nuttx/kthread.h> +#include <nuttx/mutex.h> +#include <nuttx/fs/fs.h> +#include <nuttx/wqueue.h> +#include <nuttx/signal.h> +#include <nuttx/net/netdev.h> + +#include <nuttx/usb/cdc.h> +#include <nuttx/usb/usb.h> +#include <nuttx/usb/usbhost.h> + +#ifdef CONFIG_NET_PKT + #include <nuttx/net/pkt.h> +#endif + +#ifdef CONFIG_USBHOST_CDCECM + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Default max segment size if not specified by device */ + +#define CDCECM_DEFAULT_MAXSEG 1514 + +/* ECM control requests */ + +#define ECM_SET_PACKET_FILTER_REQUEST 0x43 + +/* ECM packet filter bits */ + +#define ECM_PACKET_TYPE_PROMISCUOUS (1 << 0) +#define ECM_PACKET_TYPE_ALL_MULTICAST (1 << 1) +#define ECM_PACKET_TYPE_DIRECTED (1 << 2) +#define ECM_PACKET_TYPE_BROADCAST (1 << 3) +#define ECM_PACKET_TYPE_MULTICAST (1 << 4) + +/* Default packet filter: unicast + broadcast */ + +#define ECM_DEFAULT_PACKET_FILTER (ECM_PACKET_TYPE_DIRECTED | \ + ECM_PACKET_TYPE_BROADCAST) + +/* Configuration ************************************************************/ + +#ifndef CONFIG_SCHED_WORKQUEUE +# warning "Worker thread support is required (CONFIG_SCHED_WORKQUEUE)" +#endif + +#ifndef CONFIG_USBHOST_ASYNCH +# warning Asynchronous transfer support is required (CONFIG_USBHOST_ASYNCH) +#endif + +#ifdef CONFIG_USBHOST_CDCECM_NTDELAY +# define USBHOST_CDCECM_NTDELAY MSEC2TICK(CONFIG_USBHOST_CDCECM_NTDELAY) +#else +# define USBHOST_CDCECM_NTDELAY MSEC2TICK(200) +#endif + +#ifndef CONFIG_USBHOST_CDCECM_RXBUFSIZE + #define CONFIG_USBHOST_CDCECM_RXBUFSIZE 2048 +#endif + +#ifndef CONFIG_USBHOST_CDCECM_TXBUFSIZE + #define CONFIG_USBHOST_CDCECM_TXBUFSIZE 2048 +#endif + +/* Used in usbhost_cfgdesc() */ + +#define USBHOST_CTRLIFFOUND 0x01 +#define USBHOST_DATAIFFOUND 0x02 +#define USBHOST_INTRIFFOUND 0x04 +#define USBHOST_BINFOUND 0x08 +#define USBHOST_BOUTFOUND 0x10 +#define USBHOST_ECMFOUND 0x20 +#define USBHOST_ALLFOUND 0x3f + +#define USBHOST_MAX_CREFS 0x7fff + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct usb_csifdesc_s +{ + uint8_t len; + uint8_t type; + uint8_t subtype; +}; + +/* This structure contains the internal, private state of the USB host class + * driver. + */ + +struct usbhost_cdcecm_s +{ + /* This is the externally visible portion of the state */ + + struct usbhost_class_s usbclass; + + /* The remainder of the fields are provided to the class driver */ + + volatile bool disconnected; /* TRUE: Device has been disconnected */ + uint16_t ctrlif; /* Control interface number */ + uint16_t dataif; /* Data interface number */ + int16_t crefs; /* Reference count on the driver instance */ + mutex_t lock; /* Used to maintain mutual exclusive access */ + struct work_s ntwork; /* Notification work */ + struct work_s bulk_rxwork; + struct work_s txpollwork; + struct work_s destroywork; + int16_t nnbytes; /* Number of bytes received in notification */ + int16_t bulkinbytes; + uint16_t maxintsize; /* Maximum size of interrupt IN packet */ + FAR uint8_t *ctrlreq; /* Allocated ctrl request structure */ + FAR uint8_t *notification; /* Allocated RX buffer for async notifications */ + FAR uint8_t *bulkinbuf; /* Allocated RX buffer for bulk IN */ + FAR uint8_t *bulkoutbuf; /* Allocated TX buffer for bulk OUT */ + usbhost_ep_t intin; /* Interrupt endpoint */ + usbhost_ep_t bulkin; /* Bulk IN endpoint */ + usbhost_ep_t bulkout; /* Bulk OUT endpoint */ + uint16_t bulkmxpacket; /* Max packet size for Bulk OUT endpoint */ + + /* Device info from ECM descriptor */ + + uint16_t maxsegment; /* Max Ethernet frame size */ + uint8_t macstridx; /* MAC address string index */ + uint8_t macaddr[6]; /* Device MAC address */ + + /* Network device members */ + + bool registered; /* true if the device was registered */ + bool bifup; /* true:ifup false:ifdown */ + struct net_driver_s netdev; /* Interface understood by the network */ Review Comment: here is an simple introduction doc: https://nuttx.apache.org/docs/latest/components/net/netdriver.html. and code: https://github.com/apache/nuttx/blob/master/drivers/net/netdev_upperhalf.c the idea is simple keeping the common code in upperhalf driver, so the lower half can focus on the hardware. https://github.com/apache/nuttx/blob/master/drivers/net/e1000.c is an example. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
