acassis commented on a change in pull request #1233:
URL: https://github.com/apache/incubator-nuttx/pull/1233#discussion_r439875344



##########
File path: drivers/usbhost/usbhost_cdcmbim.c
##########
@@ -0,0 +1,2562 @@
+/****************************************************************************
+ * drivers/usbhost/usbhost_cdcmbim.c
+ *
+ * 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/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>
+
+#define CDCMBIM_NETBUF_SIZE 8192
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* put in cdc.h */
+#define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
+#define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
+#define USB_CDC_GET_NTB_PARAMETERS        0x80
+#define USB_CDC_SET_NTB_INPUT_SIZE        0x86
+#define USB_CDC_SET_NTB_FORMAT            0x83
+#define USB_CDC_SET_CRC_MODE              0x8a
+#define USB_CDC_SET_MAX_DATAGRAM_SIZE     0x88
+
+#define USB_CDC_NCM_NTB16_SUPPORTED       (1 << 0)
+#define USB_CDC_NCM_NTB32_SUPPORTED       (1 << 1)
+#define USB_CDC_NCM_NTB16_FORMAT          0x00
+#define USB_CDC_NCM_NTB32_FORMAT          0x01
+#define USB_CDC_NCM_DATAGRAM_FORMAT_CRC   0x30
+#define USB_CDC_NCM_DATAGRAM_FORMAT_NOCRC 0x31
+#define USB_CDC_NCM_CRC_NOT_APPENDED      0x00
+#define USB_CDC_NCM_CRC_APPENDED          0x01
+#define USB_CDC_NCM_NTH16_SIGNATURE       0x484D434E
+
+/* 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_CDCMBIM_NTDELAY
+#  define USBHOST_CDCMBIM_NTDELAY MSEC2TICK(CONFIG_USBHOST_CDCMBIM_NTDELAY)
+#else
+#  define USBHOST_CDCMBIM_NTDELAY MSEC2TICK(200)
+#endif
+
+#ifndef CONFIG_USBHOST_CDCMBIM_NPOLLWAITERS
+#  define CONFIG_USBHOST_CDCMBIM_NPOLLWAITERS 1
+#endif
+
+/* Driver support ***********************************************************/
+
+/* This format is used to construct the /dev/cdc-wdm[n] device driver path.
+ * It defined here so that it will be used consistently in all places.
+ */
+
+#define DEV_FORMAT          "/dev/cdc-wdm%d"
+#define DEV_NAMELEN         16
+
+/* 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_ALLFOUND    0x1f
+
+#define USBHOST_MAX_CREFS   0x7fff
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct usb_cdc_ncm_nth16_s
+{
+  uint8_t signature[4];
+  uint8_t length[2];
+  uint8_t sequence[2];
+  uint8_t block_length[2];
+  uint8_t ndp_index[2];
+};
+
+struct usb_cdc_ncm_dpe16_s
+{
+  uint8_t index[2];
+  uint8_t length[2];
+};
+
+struct usb_cdc_ncm_ndp16_s
+{
+  uint8_t signature[4];
+  uint8_t length[2];
+  uint8_t next_ndp_index[2];
+  struct usb_cdc_ncm_dpe16_s dpe16[0];
+};
+
+struct usb_cdc_ncm_ntb_params_s
+{
+  uint8_t length[2];
+  uint8_t ntb_formats_supported[2];
+  uint8_t ntb_in_max_size[4];
+  uint8_t ndp_in_divisor[2];
+  uint8_t ndp_in_payload_remainder[2];
+  uint8_t ndp_in_alignment[2];
+  uint8_t reserved[2];
+  uint8_t ntb_out_max_size[4];
+  uint8_t ndp_out_divisor[2];
+  uint8_t ndp_out_payload_remainder[2];
+  uint8_t ndp_out_alignment[2];
+  uint8_t ntb_out_max_datagrams[2];
+};
+
+struct usb_csifdesc_s
+{
+  uint8_t len;
+  uint8_t type;
+  uint8_t subtype;
+};
+
+struct usb_mbim_desc_s
+{
+  uint8_t len;
+  uint8_t type;
+  uint8_t subtype;
+  uint8_t mbim_version[2];
+  uint8_t max_ctrl_message[2];
+  uint8_t num_filters;
+  uint8_t max_filter_size;
+  uint8_t max_segment_size[2];
+  uint8_t network_capabilities;
+};
+
+/* This structure contains the internal, private state of the USB host class
+ * driver.
+ */
+
+struct usbhost_cdcmbim_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 */
+
+  uint8_t                 minor;        /* Minor number identifying the 
/dev/cdc-wdm[n] device */
+  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 */
+  sem_t                   exclsem;      /* Used to maintain mutual exclusive 
access */
+  struct work_s           ntwork;       /* Notification work */
+  struct work_s           comm_rxwork;  /* Communication interface RX 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                comm_rxlen;   /* Number of bytes in the RX buffer */
+  uint16_t                comm_rxmsgs;  /* Number of messages available to be 
read */
+  uint16_t                comm_rxpos;   /* Read position for input buffer */
+  uint16_t                maxctrlsize;  /* Maximum size of a ctrl request */
+  uint16_t                maxintsize;   /* Maximum size of interrupt IN packet 
*/
+  uint32_t                maxntbin;     /* Maximum size of NTB IN message */
+  uint32_t                maxntbout;    /* Maximum size of NTB OUT message */
+  FAR uint8_t            *ctrlreq;      /* Allocated ctrl request structure */
+  FAR uint8_t            *data_txbuf;   /* Allocated TX buffer for network 
datagrams */
+  FAR uint8_t            *data_rxbuf;   /* Allocated RX buffer for network 
datagrams */
+  FAR uint8_t            *comm_rxbuf;   /* Allocated RX buffer comm IN 
messages */
+  FAR uint8_t            *notification; /* Allocated RX buffer for async 
notifications */
+  FAR uint8_t            *rxnetbuf;     /* Allocated RX buffer for NTB frames 
*/
+  FAR uint8_t            *txnetbuf;     /* Allocated TX buffer for NTB frames 
*/
+  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 */
+  uint16_t                ntbseq;       /* NTB sequence number */
+
+  struct pollfd *fds[CONFIG_USBHOST_CDCMBIM_NPOLLWAITERS];
+
+  /* Network device members */
+
+  WDOG_ID                 txpoll;       /* TX poll timer */
+  bool                    bifup;        /* true:ifup false:ifdown */
+  struct net_driver_s     netdev;       /* Interface understood by the network 
*/
+  uint8_t                 txpktbuf[MAX_NETDEV_PKTSIZE];
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* Semaphores */
+
+static void usbhost_takesem(sem_t *sem);
+#define usbhost_givesem(s) nxsem_post(s);
+
+/* Polling support */
+
+static void usbhost_pollnotify(FAR struct usbhost_cdcmbim_s *priv);
+
+/* Memory allocation services */
+
+static inline FAR struct usbhost_cdcmbim_s *usbhost_allocclass(void);
+static inline void usbhost_freeclass(FAR struct usbhost_cdcmbim_s *usbclass);
+
+/* Device name management */
+
+static int usbhost_allocdevno(FAR struct usbhost_cdcmbim_s *priv);
+static void usbhost_freedevno(FAR struct usbhost_cdcmbim_s *priv);
+static inline void usbhost_mkdevname(FAR struct usbhost_cdcmbim_s *priv,
+                                     FAR char *devname);
+
+/* Worker thread actions */
+
+static void usbhost_notification_work(FAR void *arg);
+static void usbhost_notification_callback(FAR void *arg, ssize_t nbytes);
+static void usbhost_rxdata_work(FAR void *arg);
+static void usbhost_bulkin_work(FAR void *arg);
+
+static void usbhost_destroy(FAR void *arg);
+
+/* Helpers for usbhost_connect() */
+
+static int usbhost_cfgdesc(FAR struct usbhost_cdcmbim_s *priv,
+                           FAR const uint8_t *configdesc, int desclen);
+static inline int usbhost_devinit(FAR struct usbhost_cdcmbim_s *priv);
+
+/* (Little Endian) Data helpers */
+
+static inline uint16_t usbhost_getle16(const uint8_t *val);
+static inline void usbhost_putle16(uint8_t *dest, uint16_t val);
+static inline uint32_t usbhost_getle32(const uint8_t *val);
+static void usbhost_putle32(uint8_t *dest, uint32_t val);
+
+/* Buffer memory management */
+
+static int usbhost_alloc_buffers(FAR struct usbhost_cdcmbim_s *priv);
+static void usbhost_free_buffers(FAR struct usbhost_cdcmbim_s *priv);
+
+/* struct usbhost_registry_s methods */
+
+static struct usbhost_class_s
+              *usbhost_create(FAR struct usbhost_hubport_s *hport,
+                              FAR const struct usbhost_id_s *id);
+
+/* struct usbhost_class_s methods */
+
+static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
+                           FAR const uint8_t *configdesc, int desclen);
+static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass);
+
+/* Control interface driver methods */
+
+static ssize_t cdcwdm_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static ssize_t cdcwdm_write(FAR struct file *filep, FAR const char *buffer,
+                            size_t buflen);
+static int     cdcwdm_poll(FAR struct file *filep, FAR struct pollfd *fds,
+                           bool setup);
+
+/* NuttX network callback functions */
+
+static int cdcmbim_ifup(struct net_driver_s *dev);
+static int cdcmbim_ifdown(struct net_driver_s *dev);
+static int cdcmbim_txavail(struct net_driver_s *dev);
+
+/* Network support functions */
+
+static void cdcmbim_receive(struct usbhost_cdcmbim_s *priv, uint8_t *buf,
+                            size_t len);
+
+static int cdcmbim_txpoll(struct net_driver_s *dev);
+static void cdcmbim_txpoll_work(void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* This structure provides the registry entry ID information that will  be
+ * used to associate the USB class driver to a connected USB device.
+ */
+
+static const struct usbhost_id_s g_id =
+{
+  USB_CLASS_CDC,      /* base     */
+  CDC_SUBCLASS_MBIM,  /* subclass */
+  0,                  /* proto    */
+  0,                  /* vid      */
+  0                   /* pid      */
+};
+
+/* This is the USB host storage class's registry entry */
+
+static struct usbhost_registry_s g_cdcmbim =
+{
+  NULL,                   /* flink    */
+  usbhost_create,         /* create   */
+  1,                      /* nids     */
+  &g_id                   /* id[]     */
+};
+
+/* File operations for control channel */
+
+static const struct file_operations cdcwdm_fops =
+{
+  NULL,          /* open */
+  NULL,          /* close */
+  cdcwdm_read,   /* read */
+  cdcwdm_write,  /* write */
+  NULL,          /* seek */
+  NULL,          /* ioctl */
+  cdcwdm_poll    /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL         /* unlink */
+#endif
+};
+
+/* This is a bitmap that is used to allocate device names /dev/cdc-wdm[n]. */

Review comment:
       no, it has 78 chars




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to