2015-05-06 20:49:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbhost/usbhost_cdcacm.c
|
|
|
|
*
|
2020-03-30 22:20:53 +02:00
|
|
|
* 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.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-02-27 07:52:21 +01:00
|
|
|
#include <stdint.h>
|
2015-05-06 20:49:00 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2015-05-06 20:49:00 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
#include <nuttx/clock.h>
|
2019-01-27 20:41:44 +01:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2015-05-06 20:49:00 +02:00
|
|
|
#include <nuttx/serial/serial.h>
|
|
|
|
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbhost.h>
|
|
|
|
#include <nuttx/usb/cdc.h>
|
|
|
|
#include <nuttx/usb/cdcacm.h>
|
|
|
|
#include <nuttx/usb/usbhost_devaddr.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_CDCACM
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST
|
|
|
|
# warning USB host support not enabled (CONFIG_USBHOST)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_BULK_DISABLE
|
|
|
|
# warning USB bulk endpoint support is disabled (CONFIG_USBHOST_BULK_DISABLE)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_INT_DISABLE
|
|
|
|
# warning USB interrupt endpoint support is disabled (CONFIG_USBHOST_INT_DISABLE)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SCHED_WORKQUEUE)
|
|
|
|
# warning Worker thread support is required (CONFIG_SCHED_WORKQUEUE)
|
|
|
|
#else
|
|
|
|
# ifndef CONFIG_SCHED_HPWORK
|
|
|
|
# warning High priority work thread support is required (CONFIG_SCHED_HPWORK)
|
|
|
|
# endif
|
|
|
|
# ifndef CONFIG_SCHED_LPWORK
|
|
|
|
# warning Low priority work thread support is required (CONFIG_SCHED_LPWORK)
|
|
|
|
# endif
|
|
|
|
# if CONFIG_SCHED_LPNTHREADS < 2
|
|
|
|
# warning Multiple low priority work threads recommended for performance (CONFIG_SCHED_LPNTHREADS > 1)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_ASYNCH
|
|
|
|
# warning Asynchronous transfer support is required (CONFIG_USBHOST_ASYNCH)
|
|
|
|
#endif
|
|
|
|
|
2015-05-11 23:10:41 +02:00
|
|
|
#ifndef CONFIG_SERIAL_REMOVABLE
|
|
|
|
# warning Removable serial device support is required (CONFIG_SERIAL_REMOVABLE)
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
#ifdef CONFIG_USBHOST_CDCACM_NTDELAY
|
|
|
|
# define USBHOST_CDCACM_NTDELAY MSEC2TICK(CONFIG_USBHOST_CDCACM_NTDELAY)
|
|
|
|
#else
|
|
|
|
# define USBHOST_CDCACM_NTDELAY MSEC2TICK(200)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_CDCACM_RXDELAY
|
|
|
|
# define USBHOST_CDCACM_RXDELAY MSEC2TICK(CONFIG_USBHOST_CDCACM_RXDELAY)
|
|
|
|
#else
|
|
|
|
# define USBHOST_CDCACM_RXDELAY MSEC2TICK(200)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_CDCACM_TXDELAY
|
|
|
|
# define USBHOST_CDCACM_TXDELAY MSEC2TICK(CONFIG_USBHOST_CDCACM_TXDELAY)
|
|
|
|
#else
|
|
|
|
# define USBHOST_CDCACM_TXDELAY MSEC2TICK(200)
|
|
|
|
#endif
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
/* Supported protocol */
|
|
|
|
|
|
|
|
#define HAVE_CLASS_REQUESTS 1
|
|
|
|
#define HAVE_INTIN_ENDPOINT 1
|
|
|
|
#define HAVE_CTRL_INTERFACE 1
|
|
|
|
|
|
|
|
#if defined(CONFIG_USBHOST_CDCACM_REDUCED)
|
|
|
|
# undef CONFIG_USBHOST_CDCACM_BULKONLY
|
|
|
|
# undef CONFIG_USBHOST_CDCACM_COMPLIANT
|
|
|
|
# undef HAVE_INTIN_ENDPOINT
|
|
|
|
#elif defined(CONFIG_USBHOST_CDCACM_BULKONLY)
|
|
|
|
# undef CONFIG_USBHOST_CDCACM_COMPLIANT
|
|
|
|
# undef HAVE_CLASS_REQUESTS
|
|
|
|
# undef HAVE_INTIN_ENDPOINT
|
|
|
|
# undef HAVE_CTRL_INTERFACE
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/* If the create() method is called by the USB host device driver from an
|
|
|
|
* interrupt handler, then it will be unable to call kmm_malloc() in order to
|
|
|
|
* allocate a new class instance. If the create() method is called from the
|
|
|
|
* interrupt level, then class instances must be pre-allocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_NPREALLOC
|
|
|
|
# define CONFIG_USBHOST_CDCACM_NPREALLOC 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 32
|
|
|
|
# error Currently limited to 32 devices /dev/ttyACM[n]
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_RXBUFSIZE
|
|
|
|
# define CONFIG_USBHOST_CDCACM_RXBUFSIZE 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_TXBUFSIZE
|
|
|
|
# define CONFIG_USBHOST_CDCACM_TXBUFSIZE 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Initial line coding */
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_BAUD
|
|
|
|
# define CONFIG_USBHOST_CDCACM_BAUD 115200
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_PARITY
|
|
|
|
# define CONFIG_USBHOST_CDCACM_PARITY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_BITS
|
|
|
|
# define CONFIG_USBHOST_CDCACM_BITS 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBHOST_CDCACM_2STOP
|
|
|
|
# define CONFIG_USBHOST_CDCACM_2STOP 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Driver support ***********************************************************/
|
2019-02-11 14:08:33 +01:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/* This format is used to construct the /dev/sd[n] device driver path. It
|
|
|
|
* defined here so that it will be used consistently in all places.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
#define DEV_FORMAT "/dev/ttyACM%d"
|
|
|
|
#define DEV_NAMELEN 16
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
#define MAX_NOTIFICATION 32
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Used in usbhost_connect() */
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
#define USBHOST_DATAIF_FOUND 0x01 /* Data interface found */
|
|
|
|
#define USBHOST_BULKIN_FOUND 0x02 /* Bulk IN interface found */
|
|
|
|
#define USBHOST_BULKOUT_FOUND 0x04 /* Bulk OUT interface found */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#if defined(CONFIG_USBHOST_CDCACM_BULKONLY)
|
2015-05-07 19:16:03 +02:00
|
|
|
# define USBHOST_MINFOUND 0x07 /* Minimum things needed */
|
|
|
|
# define USBHOST_ALLFOUND 0x07 /* All configuration things */
|
2015-05-10 16:16:58 +02:00
|
|
|
#elif defined(CONFIG_USBHOST_CDCACM_REDUCED)
|
|
|
|
# define USBHOST_CTRLIF_FOUND 0x08 /* Control interface found */
|
|
|
|
|
|
|
|
# define USBHOST_MINFOUND 0x07 /* Minimum things needed */
|
|
|
|
# define USBHOST_HAVE_CTRLIF 0x08 /* Needed for control interface */
|
|
|
|
# define USBHOST_ALLFOUND 0x0f /* All configuration things */
|
2015-05-07 19:16:03 +02:00
|
|
|
#else
|
|
|
|
# define USBHOST_CTRLIF_FOUND 0x08 /* Control interface found */
|
|
|
|
# define USBHOST_INTIN_FOUND 0x10 /* Interrupt IN interface found */
|
|
|
|
|
|
|
|
# define USBHOST_MINFOUND 0x07 /* Minimum things needed */
|
|
|
|
# define USBHOST_HAVE_CTRLIF 0x18 /* Needed for control interface */
|
|
|
|
# define USBHOST_ALLFOUND 0x1f /* All configuration things */
|
|
|
|
#endif
|
2015-05-07 18:02:51 +02:00
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
#define USBHOST_MAX_CREFS INT16_MAX /* Max cref count before signed overflow */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure contains the internal, private state of the USB host
|
|
|
|
* CDC/ACM class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct usbhost_cdcacm_s
|
|
|
|
{
|
|
|
|
/* This is the externally visible portion of the state. The usbclass must
|
|
|
|
* the first element of the structure. It is then cast compatible with
|
|
|
|
* struct usbhost_cdcacm_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct usbhost_class_s usbclass;
|
|
|
|
|
|
|
|
/* This is the standard of the lower-half serial interface. It is not
|
|
|
|
* the first element of the structure, but includes a pointer back to the
|
|
|
|
* the beginning of this structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct uart_dev_s uartdev;
|
|
|
|
|
|
|
|
/* The remainder of the fields are provide to the CDC/ACM class */
|
|
|
|
|
|
|
|
volatile bool disconnected; /* TRUE: Device has been disconnected */
|
|
|
|
bool stop2; /* True: 2 stop bits (for line coding) */
|
|
|
|
bool dsr; /* State of transmission carrier */
|
|
|
|
bool dcd; /* State of receiver carrier detection */
|
|
|
|
bool txena; /* True: TX "interrupts" enabled */
|
|
|
|
bool rxena; /* True: RX "interrupts" enabled */
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
bool iflow; /* True: Input flow control (RTS) enabled */
|
2016-02-24 18:54:02 +01:00
|
|
|
bool rts; /* True: Input flow control is in effect */
|
2015-05-06 20:49:00 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_OFLOWCONTROL
|
|
|
|
bool oflow; /* True: Output flow control (CTS) enabled */
|
|
|
|
#endif
|
|
|
|
uint8_t minor; /* Minor number identifying the /dev/ttyACM[n] device */
|
2015-05-07 18:02:51 +02:00
|
|
|
uint8_t dataif; /* Data interface number */
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-07 18:02:51 +02:00
|
|
|
uint8_t ctrlif; /* Control interface number */
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
uint8_t nbits; /* Number of bits (for line encoding) */
|
|
|
|
uint8_t parity; /* Parity (for line encoding) */
|
|
|
|
uint16_t pktsize; /* Allocated size of transfer buffers */
|
|
|
|
uint16_t nrxbytes; /* Number of bytes in the RX packet buffer */
|
|
|
|
uint16_t rxndx; /* Index to the next byte in the RX packet buffer */
|
|
|
|
int16_t crefs; /* Reference count on the driver instance */
|
|
|
|
int16_t nbytes; /* The number of bytes actually transferred */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* Used to maintain mutual exclusive access */
|
2015-05-06 20:49:00 +02:00
|
|
|
struct work_s ntwork; /* For asynchronous notification work */
|
|
|
|
struct work_s rxwork; /* For RX packet work */
|
|
|
|
struct work_s txwork; /* For TX packet work */
|
|
|
|
FAR uint8_t *ctrlreq; /* Allocated ctrl request structure */
|
|
|
|
FAR uint8_t *linecode; /* The allocated buffer for line encoding */
|
|
|
|
FAR uint8_t *notification; /* The allocated buffer for async notifications */
|
|
|
|
FAR uint8_t *inbuf; /* Allocated RX buffer for the Bulk IN endpoint */
|
|
|
|
FAR uint8_t *outbuf; /* Allocated TX buffer for the Bulk OUT endpoint */
|
|
|
|
uint32_t baud; /* Current baud for line coding */
|
|
|
|
usbhost_ep_t bulkin; /* Bulk IN endpoint */
|
|
|
|
usbhost_ep_t bulkout; /* Bulk OUT endpoint */
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
usbhost_ep_t intin; /* Interrupt IN endpoint (optional) */
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* This is the serial data buffer */
|
|
|
|
|
|
|
|
char rxbuffer[CONFIG_USBHOST_CDCACM_RXBUFSIZE];
|
|
|
|
char txbuffer[CONFIG_USBHOST_CDCACM_TXBUFSIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is how struct usbhost_cdcacm_s looks to the free list logic */
|
|
|
|
|
|
|
|
struct usbhost_freestate_s
|
|
|
|
{
|
|
|
|
FAR struct usbhost_freestate_s *flink;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Memory allocation services */
|
|
|
|
|
|
|
|
static FAR struct usbhost_cdcacm_s *usbhost_allocclass(void);
|
|
|
|
static void usbhost_freeclass(FAR struct usbhost_cdcacm_s *usbclass);
|
|
|
|
|
|
|
|
/* Device name management */
|
|
|
|
|
|
|
|
static int usbhost_devno_alloc(FAR struct usbhost_cdcacm_s *priv);
|
|
|
|
static void usbhost_devno_free(FAR struct usbhost_cdcacm_s *priv);
|
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_cdcacm_s *priv,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR char *devname);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* CDC/ACM request helpers */
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-06 20:49:00 +02:00
|
|
|
static int usbhost_linecoding_send(FAR struct usbhost_cdcacm_s *priv);
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
static void usbhost_notification_work(FAR void *arg);
|
|
|
|
static void usbhost_notification_callback(FAR void *arg, ssize_t nbytes);
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-10 16:16:58 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* UART buffer data transfer */
|
|
|
|
|
|
|
|
static void usbhost_txdata_work(FAR void *arg);
|
|
|
|
static void usbhost_rxdata_work(FAR void *arg);
|
|
|
|
|
|
|
|
/* Worker thread actions */
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg);
|
|
|
|
|
|
|
|
/* Helpers for usbhost_connect() */
|
|
|
|
|
|
|
|
static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
|
|
|
|
FAR const uint8_t *configdesc, int desclen);
|
|
|
|
|
|
|
|
/* (Little Endian) Data helpers */
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
static inline uint16_t usbhost_getle16(FAR const uint8_t *val);
|
|
|
|
static inline void usbhost_putle16(FAR uint8_t *dest, uint16_t val);
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2019-02-11 14:08:33 +01:00
|
|
|
static void usbhost_putle32(FAR uint8_t *dest, uint32_t val);
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Transfer descriptor memory management */
|
|
|
|
|
|
|
|
static int usbhost_alloc_buffers(FAR struct usbhost_cdcacm_s *priv);
|
|
|
|
static void usbhost_free_buffers(FAR struct usbhost_cdcacm_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);
|
|
|
|
|
|
|
|
/* Serial driver lower-half interfaces */
|
|
|
|
|
|
|
|
static int usbhost_setup(FAR struct uart_dev_s *uartdev);
|
|
|
|
static void usbhost_shutdown(FAR struct uart_dev_s *uartdev);
|
|
|
|
static int usbhost_attach(FAR struct uart_dev_s *uartdev);
|
|
|
|
static void usbhost_detach(FAR struct uart_dev_s *uartdev);
|
2020-03-30 22:20:53 +02:00
|
|
|
static int usbhost_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg);
|
2015-05-06 20:49:00 +02:00
|
|
|
static void usbhost_rxint(FAR struct uart_dev_s *uartdev, bool enable);
|
|
|
|
static bool usbhost_rxavailable(FAR struct uart_dev_s *uartdev);
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
static bool usbhost_rxflowcontrol(FAR struct uart_dev_s *uartdev,
|
|
|
|
unsigned int nbuffered, bool upper);
|
|
|
|
#endif
|
|
|
|
static void usbhost_txint(FAR struct uart_dev_s *uartdev, bool enable);
|
|
|
|
static bool usbhost_txready(FAR struct uart_dev_s *uartdev);
|
|
|
|
static bool usbhost_txempty(FAR struct uart_dev_s *uartdev);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure provides the registry entry ID information that will be
|
|
|
|
* used to associate the USB host CDC/ACM class to a connected USB
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
|
2020-01-09 11:58:07 +01:00
|
|
|
static const struct usbhost_id_s g_id[4] =
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2016-11-30 19:20:23 +01:00
|
|
|
{
|
|
|
|
USB_CLASS_CDC, /* base */
|
|
|
|
CDC_SUBCLASS_NONE, /* subclass */
|
|
|
|
CDC_PROTO_NONE, /* proto */
|
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
|
|
|
},
|
|
|
|
{
|
|
|
|
USB_CLASS_CDC, /* base */
|
|
|
|
CDC_SUBCLASS_ACM, /* subclass */
|
|
|
|
CDC_PROTO_ATM, /* proto */
|
|
|
|
0, /* vid */
|
|
|
|
0 /* pid */
|
2020-01-09 11:58:07 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
USB_CLASS_VENDOR_SPEC, /* base */
|
|
|
|
CDC_SUBCLASS_NONE, /* subclass */
|
|
|
|
CDC_PROTO_NONE, /* proto */
|
|
|
|
0x2c7c, /* vid */
|
|
|
|
0x0125 /* pid */
|
|
|
|
},
|
|
|
|
{
|
|
|
|
USB_CLASS_VENDOR_SPEC, /* base */
|
|
|
|
CDC_SUBCLASS_ACM, /* subclass */
|
|
|
|
CDC_PROTO_NONE, /* proto */
|
|
|
|
0x2c7c, /* vid */
|
|
|
|
0x0125 /* pid */
|
2016-11-30 19:20:23 +01:00
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the USB host CDC/ACM class's registry entry */
|
|
|
|
|
|
|
|
static struct usbhost_registry_s g_cdcacm =
|
|
|
|
{
|
|
|
|
NULL, /* flink */
|
|
|
|
usbhost_create, /* create */
|
2020-01-09 11:58:07 +01:00
|
|
|
4, /* nids */
|
2016-11-30 19:20:23 +01:00
|
|
|
&g_id[0] /* id[] */
|
2015-05-06 20:49:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Serial driver lower half interface */
|
|
|
|
|
|
|
|
static const struct uart_ops_s g_uart_ops =
|
|
|
|
{
|
|
|
|
usbhost_setup, /* setup */
|
|
|
|
usbhost_shutdown, /* shutdown */
|
|
|
|
usbhost_attach, /* attach */
|
|
|
|
usbhost_detach, /* detach */
|
|
|
|
usbhost_ioctl, /* ioctl */
|
2023-06-03 14:19:54 +02:00
|
|
|
NULL, /* receive */
|
2015-05-06 20:49:00 +02:00
|
|
|
usbhost_rxint, /* rxinit */
|
|
|
|
usbhost_rxavailable, /* rxavailable */
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
usbhost_rxflowcontrol, /* rxflowcontrol */
|
2023-06-03 14:19:54 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
|
|
|
NULL, /* dmasend */
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_RXDMA
|
|
|
|
NULL, /* dmareceive */
|
|
|
|
NULL, /* dmarxfree */
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_TXDMA
|
|
|
|
NULL, /* dmatxavail */
|
2015-05-06 20:49:00 +02:00
|
|
|
#endif
|
|
|
|
NULL, /* send */
|
|
|
|
usbhost_txint, /* txinit */
|
|
|
|
usbhost_txready, /* txready */
|
|
|
|
usbhost_txempty /* txempty */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is an array of pre-allocated USB host CDC/ACM class instances */
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 0
|
|
|
|
static struct usbhost_cdcacm_s g_prealloc[CONFIG_USBHOST_CDCACM_NPREALLOC];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This is a list of free, pre-allocated USB host CDC/ACM class instances */
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 0
|
|
|
|
static FAR struct usbhost_freestate_s *g_freelist;
|
|
|
|
#endif
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* This is a bitmap that is used to allocate device
|
|
|
|
* minor numbers /dev/ttyACM[n].
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
static uint32_t g_devinuse;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_allocclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is really part of the logic that implements the create() method
|
|
|
|
* of struct usbhost_registry_s. This function allocates memory for one
|
|
|
|
* new class instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, this function will return a non-NULL instance of struct
|
|
|
|
* usbhost_class_s. NULL is returned on failure; this function will
|
|
|
|
* will fail only if there are insufficient resources to create another
|
|
|
|
* USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 0
|
|
|
|
static FAR struct usbhost_cdcacm_s *usbhost_allocclass(void)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_freestate_s *entry;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* We may be executing from an interrupt handler so we need to take one of
|
|
|
|
* our pre-allocated class instances from the free list.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
entry = g_freelist;
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
g_freelist = entry->flink;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Allocated: %p\n", entry);
|
2015-05-06 20:49:00 +02:00
|
|
|
return (FAR struct usbhost_cdcacm_s *)entry;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static FAR struct usbhost_cdcacm_s *usbhost_allocclass(void)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
|
|
|
|
/* We are not executing from an interrupt handler so we can just call
|
|
|
|
* kmm_malloc() to get memory for the class instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(!up_interrupt_context());
|
2019-02-11 14:08:33 +01:00
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)
|
|
|
|
kmm_malloc(sizeof(struct usbhost_cdcacm_s));
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Allocated: %p\n", priv);
|
2015-05-06 20:49:00 +02:00
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_freeclass
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a class instance previously allocated by usbhost_allocclass().
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* usbclass - A reference to the class instance to be freed.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 0
|
|
|
|
static void usbhost_freeclass(FAR struct usbhost_cdcacm_s *usbclass)
|
|
|
|
{
|
2019-02-11 14:08:33 +01:00
|
|
|
FAR struct usbhost_freestate_s *entry =
|
|
|
|
(FAR struct usbhost_freestate_s *)usbclass;
|
2015-05-06 20:49:00 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
DEBUGASSERT(entry != NULL);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", entry);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Just put the pre-allocated class structure back on the freelist */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
entry->flink = g_freelist;
|
|
|
|
g_freelist = entry;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void usbhost_freeclass(FAR struct usbhost_cdcacm_s *usbclass)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(usbclass != NULL);
|
|
|
|
|
2020-04-07 16:31:47 +02:00
|
|
|
/* Free the class instance (calling kmm_free() in case we are executing
|
2015-05-06 20:49:00 +02:00
|
|
|
* from an interrupt handler.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Freeing: %p\n", usbclass);
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(usbclass);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_devno_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a unique /dev/ttyACM[n] minor number in the range 0-31.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_devno_alloc(FAR struct usbhost_cdcacm_s *priv)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int devno;
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
for (devno = 0; devno < 32; devno++)
|
|
|
|
{
|
|
|
|
uint32_t bitno = 1 << devno;
|
|
|
|
if ((g_devinuse & bitno) == 0)
|
|
|
|
{
|
|
|
|
g_devinuse |= bitno;
|
|
|
|
priv->minor = devno;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_devno_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a /dev/ttyACM[n] minor number so that it can be used.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_devno_free(FAR struct usbhost_cdcacm_s *priv)
|
|
|
|
{
|
|
|
|
int devno = priv->minor;
|
|
|
|
|
|
|
|
if (devno >= 0 && devno < 32)
|
|
|
|
{
|
2016-02-14 14:32:58 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
g_devinuse &= ~(1 << devno);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_mkdevname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Format a /dev/ttyACM[n] device name given a minor number.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void usbhost_mkdevname(FAR struct usbhost_cdcacm_s *priv,
|
|
|
|
FAR char *devname)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
snprintf(devname, DEV_NAMELEN, DEV_FORMAT, priv->minor);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_linecoding_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Format and send the on EP0.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-06 20:49:00 +02:00
|
|
|
static int usbhost_linecoding_send(FAR struct usbhost_cdcacm_s *priv)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct cdc_linecoding_s *linecode;
|
|
|
|
FAR struct usb_ctrlreq_s *ctrlreq;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
DEBUGASSERT(hport);
|
|
|
|
|
|
|
|
/* Initialize the line coding structure */
|
|
|
|
|
|
|
|
linecode = (FAR struct cdc_linecoding_s *)priv->linecode;
|
|
|
|
usbhost_putle32(linecode->baud, priv->baud);
|
|
|
|
linecode->stop = (priv->stop2) ? 2 : 0;
|
|
|
|
linecode->parity = priv->parity;
|
|
|
|
linecode->nbits = priv->nbits;
|
|
|
|
|
|
|
|
/* Initialize the control request */
|
|
|
|
|
|
|
|
ctrlreq = (FAR struct usb_ctrlreq_s *)priv->ctrlreq;
|
2019-02-11 14:08:33 +01:00
|
|
|
ctrlreq->type = USB_DIR_OUT | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_RECIPIENT_INTERFACE;
|
2015-05-06 20:49:00 +02:00
|
|
|
ctrlreq->req = ACM_SET_LINE_CODING;
|
|
|
|
|
|
|
|
usbhost_putle16(ctrlreq->value, 0);
|
2015-05-08 18:58:41 +02:00
|
|
|
usbhost_putle16(ctrlreq->index, priv->ctrlif);
|
2015-05-06 20:49:00 +02:00
|
|
|
usbhost_putle16(ctrlreq->len, SIZEOF_CDC_LINECODING);
|
|
|
|
|
|
|
|
/* And send the request */
|
|
|
|
|
|
|
|
ret = DRVR_CTRLOUT(hport->drvr, hport->ep0, ctrlreq, priv->linecode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_CTRLOUT failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_notification_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle receipt of an asynchronous notification from the CDC/ACM device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - The argument provided with the asynchronous I/O was setup
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Probably called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
static void usbhost_notification_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct cdc_notification_s *inmsg;
|
|
|
|
uint16_t value;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)arg;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
DEBUGASSERT(hport);
|
|
|
|
|
|
|
|
/* Are we still connected? */
|
|
|
|
|
|
|
|
if (!priv->disconnected && priv->intin)
|
|
|
|
{
|
|
|
|
/* Yes.. Was an interrupt IN message received correctly? */
|
|
|
|
|
|
|
|
if (priv->nbytes >= 0)
|
|
|
|
{
|
|
|
|
/* Yes.. decode the notification */
|
|
|
|
|
|
|
|
inmsg = (FAR struct cdc_notification_s *)priv->notification;
|
|
|
|
value = usbhost_getle16(inmsg->value);
|
|
|
|
index = usbhost_getle16(inmsg->index);
|
|
|
|
len = usbhost_getle16(inmsg->len);
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
uinfo("type: %02x notification: %02x value: %04x index: %04x "
|
|
|
|
"len: %04x\n",
|
|
|
|
inmsg->type, inmsg->notification, value, index, len);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* We care only about the SerialState notification */
|
|
|
|
|
|
|
|
if ((inmsg->type == (USB_REQ_DIR_IN | USB_REQ_TYPE_CLASS |
|
|
|
|
USB_REQ_RECIPIENT_INTERFACE)) &&
|
|
|
|
(inmsg->notification == ACM_SERIAL_STATE) &&
|
|
|
|
(value == 0) &&
|
2015-05-08 18:58:41 +02:00
|
|
|
(index == priv->ctrlif) &&
|
2015-05-06 20:49:00 +02:00
|
|
|
(len == 2))
|
|
|
|
{
|
|
|
|
uint16_t state = usbhost_getle16(inmsg->data);
|
|
|
|
|
|
|
|
/* And we care only about the state of the DCD and DSR in the
|
|
|
|
* the notification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->dcd = ((state & CDC_UART_RXCARRIER) != 0);
|
|
|
|
priv->dsr = ((state & CDC_UART_TXCARRIER) != 0);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("ACM_SERIAL_STATE: DCD=%d DSR=%d\n",
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->dcd, priv->dsr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup to receive the next line status change event */
|
|
|
|
|
|
|
|
ret = DRVR_ASYNCH(hport->drvr, priv->intin,
|
|
|
|
(FAR uint8_t *)priv->notification,
|
|
|
|
MAX_NOTIFICATION, usbhost_notification_callback,
|
2015-05-09 16:23:11 +02:00
|
|
|
priv);
|
2015-05-06 20:49:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_ASYNCH failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_notification_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle receipt of line status from the CDC/ACM device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - The argument provided with the asynchronous I/O was setup
|
|
|
|
* nbytes - The number of bytes actually transferred (or a negated errno
|
|
|
|
* value;
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Probably called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
static void usbhost_notification_callback(FAR void *arg, ssize_t nbytes)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv = (FAR struct usbhost_cdcacm_s *)arg;
|
|
|
|
uint32_t delay = 0;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Are we still connected? */
|
|
|
|
|
|
|
|
if (!priv->disconnected)
|
|
|
|
{
|
|
|
|
/* Check for a failure. On higher end host controllers, the
|
|
|
|
* asynchronous transfer will pend until data is available (OHCI and
|
|
|
|
* EHCI). On lower end host controllers (like STM32 and EFM32), the
|
|
|
|
* transfer will fail immediately when the device NAKs the first
|
|
|
|
* attempted interrupt IN transfer (with nbytes == -EAGAIN). In that
|
|
|
|
* case (or in the case of other errors), we must fall back to
|
|
|
|
* polling.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
DEBUGASSERT(nbytes >= INT16_MIN && nbytes <= INT16_MAX);
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->nbytes = (int16_t)nbytes;
|
|
|
|
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* This debug output is good to know, but really a nuisance for
|
|
|
|
* those configurations where we have to fall back to polling.
|
|
|
|
* FIX: Don't output the message is the result is -EAGAIN.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if defined(CONFIG_DEBUG_USB) && !defined(CONFIG_DEBUG_INFO)
|
2015-05-06 20:49:00 +02:00
|
|
|
if (nbytes != -EAGAIN)
|
|
|
|
#endif
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Transfer failed: %d\n", nbytes);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't know the nature of the failure, but we need to do all
|
|
|
|
* that we can do to avoid a CPU hog error loop.
|
|
|
|
*
|
|
|
|
* Use the low-priority work queue and delay polling for the next
|
|
|
|
* event. We want to use as little CPU bandwidth as possible in
|
|
|
|
* this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
delay = USBHOST_CDCACM_NTDELAY;
|
|
|
|
}
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
/* Make sure that the work structure available. There is a remote
|
|
|
|
* chance that this may collide with a device disconnection event.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (work_available(&priv->ntwork))
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
work_queue(HPWORK, &priv->ntwork,
|
2022-04-17 17:05:40 +02:00
|
|
|
usbhost_notification_work,
|
2020-01-02 17:49:34 +01:00
|
|
|
priv, delay);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
/****************************************************************************
|
2015-05-06 20:49:00 +02:00
|
|
|
* UART buffer data transfer
|
2019-02-11 14:08:33 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_txdata_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send more OUT data to the attached CDC/ACM device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the CDC/ACM class private data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_txdata_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct uart_dev_s *uartdev;
|
|
|
|
FAR struct uart_buffer_s *txbuf;
|
|
|
|
ssize_t nwritten;
|
|
|
|
int txndx;
|
|
|
|
int txtail;
|
2015-05-14 18:22:08 +02:00
|
|
|
int ret;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)arg;
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
DEBUGASSERT(hport);
|
|
|
|
|
|
|
|
uartdev = &priv->uartdev;
|
|
|
|
txbuf = &uartdev->xmit;
|
|
|
|
|
|
|
|
/* Do nothing if TX transmission is disabled */
|
|
|
|
|
|
|
|
if (!priv->txena)
|
|
|
|
{
|
|
|
|
/* Terminate the work now *without* rescheduling */
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-11 20:47:37 +02:00
|
|
|
/* Loop until The UART TX buffer is empty (or we become disconnected) */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
txtail = txbuf->tail;
|
|
|
|
txndx = 0;
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
while (txtail != txbuf->head && priv->txena && !priv->disconnected)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Copy data from the UART TX buffer until either 1) the UART TX
|
2015-05-14 18:22:08 +02:00
|
|
|
* buffer has been emptied, or 2) the Bulk OUT buffer is full.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
txndx = 0;
|
|
|
|
while (txtail != txbuf->head && txndx < priv->pktsize)
|
|
|
|
{
|
|
|
|
/* Copy the next byte */
|
|
|
|
|
|
|
|
priv->outbuf[txndx] = txbuf->buffer[txtail];
|
|
|
|
|
|
|
|
/* Increment counters and indices */
|
|
|
|
|
|
|
|
txndx++;
|
2017-03-07 13:58:32 +01:00
|
|
|
if (++txtail >= txbuf->size)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
txtail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Save the updated tail pointer so that it cannot be sent again */
|
|
|
|
|
|
|
|
txbuf->tail = txtail;
|
|
|
|
|
|
|
|
/* Bytes were removed from the TX buffer. Inform any waiters that
|
|
|
|
* there is space available in the TX buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uart_datasent(uartdev);
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Send the filled TX buffer to the CDC/ACM device */
|
|
|
|
|
|
|
|
nwritten = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
|
|
|
priv->outbuf, txndx);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
/* The most likely reason for a failure is that CDC/ACM device
|
2015-05-14 18:22:08 +02:00
|
|
|
* NAK'ed our packet OR that the device has been disconnected.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
2015-05-14 18:22:08 +02:00
|
|
|
* Just break out of the loop, rescheduling the work (unless
|
|
|
|
* the device is disconnected).
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
uerr("ERROR: DRVR_TRANSFER for packet failed: %d\n",
|
|
|
|
(int)nwritten);
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
/* We get here because: 1) the UART TX buffer is empty and there is
|
|
|
|
* nothing more to send, 2) the CDC/ACM device was not ready to accept our
|
|
|
|
* data, or the device is no longer available.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
* If the last packet sent was and even multiple of the packet size, then
|
|
|
|
* we need to send a zero length packet (ZLP).
|
|
|
|
*/
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
if (txndx == priv->pktsize && !priv->disconnected)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Send the ZLP to the CDC/ACM device */
|
|
|
|
|
|
|
|
nwritten = DRVR_TRANSFER(hport->drvr, priv->bulkout,
|
|
|
|
priv->outbuf, 0);
|
|
|
|
if (nwritten < 0)
|
|
|
|
{
|
|
|
|
/* The most likely reason for a failure is that CDC/ACM device
|
|
|
|
* NAK'ed our packet.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_TRANSFER for ZLP failed: %d\n", (int)nwritten);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Check again if TX reception is enabled and that the device is still
|
|
|
|
* connected. These states could have changed since we started the
|
|
|
|
* transfer.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
if (priv->txena && !priv->disconnected)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Schedule TX data work to occur after a delay. */
|
|
|
|
|
|
|
|
ret = work_queue(LPWORK, &priv->txwork, usbhost_txdata_work, priv,
|
|
|
|
USBHOST_CDCACM_TXDELAY);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
UNUSED(ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_rxdata_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get more IN data from the attached CDC/ACM device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the CDC/ACM class private data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_rxdata_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct uart_dev_s *uartdev;
|
|
|
|
FAR struct uart_buffer_s *rxbuf;
|
|
|
|
ssize_t nread;
|
|
|
|
int nxfrd;
|
|
|
|
int nexthead;
|
|
|
|
int rxndx;
|
|
|
|
int ret;
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)arg;
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
hport = priv->usbclass.hport;
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(hport);
|
|
|
|
|
|
|
|
uartdev = &priv->uartdev;
|
|
|
|
rxbuf = &uartdev->recv;
|
|
|
|
|
|
|
|
/* Get the index in the RX packet buffer where we will take the first
|
|
|
|
* byte of data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rxndx = priv->rxndx;
|
|
|
|
nxfrd = 0;
|
|
|
|
|
|
|
|
/* Get the index to the value of the UART RX buffer head AFTER the
|
|
|
|
* first character has been stored. We need to know this in order
|
|
|
|
* to test if the UART RX buffer is full.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nexthead = rxbuf->head + 1;
|
|
|
|
if (nexthead >= rxbuf->size)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until either:
|
|
|
|
*
|
|
|
|
* 1. The UART RX buffer is full
|
|
|
|
* 2. There is no more data available from the CDC/ACM device
|
2015-05-14 18:22:08 +02:00
|
|
|
* 3. RX rec
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
while (priv->rxena && priv->rts && !priv->disconnected)
|
|
|
|
#else
|
|
|
|
while (priv->rxena && !priv->disconnected)
|
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2020-04-09 14:46:24 +02:00
|
|
|
/* Stop now if there is no room for another
|
|
|
|
* character in the RX buffer.
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
if (nexthead == rxbuf->tail)
|
|
|
|
{
|
|
|
|
/* Break out of the loop, rescheduling the work */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we have any buffer RX data to transfer? */
|
|
|
|
|
|
|
|
if (priv->nrxbytes < 1)
|
|
|
|
{
|
|
|
|
/* No.. Read more data from the CDC/ACM device */
|
|
|
|
|
|
|
|
nread = DRVR_TRANSFER(hport->drvr, priv->bulkin,
|
|
|
|
priv->inbuf, priv->pktsize);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
2015-05-14 18:22:08 +02:00
|
|
|
/* The most likely reason for a failure is that the has no
|
|
|
|
* data available now and NAK'ed the IN token OR that the
|
|
|
|
* transfer was cancelled because the device was disconnected.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
2015-05-14 18:22:08 +02:00
|
|
|
* Just break out of the loop, rescheduling the work (if the
|
|
|
|
* device was not disconnected.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
uerr("ERROR: DRVR_TRANSFER for packet failed: %d\n",
|
|
|
|
(int)nread);
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
/* Save the number of bytes read. This might be zero if
|
|
|
|
* a Zero Length Packet (ZLP) is received. The ZLP is
|
|
|
|
* part of the bulk transfer protocol, but otherwise of
|
|
|
|
* no interest to us.
|
|
|
|
*/
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->nrxbytes = (uint16_t)nread;
|
|
|
|
rxndx = 0;
|
|
|
|
|
|
|
|
/* Ignore ZLPs */
|
|
|
|
|
|
|
|
if (nread == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer one byte from the RX packet buffer into UART RX buffer */
|
|
|
|
|
|
|
|
rxbuf->buffer[rxbuf->head] = priv->inbuf[rxndx];
|
|
|
|
nxfrd++;
|
|
|
|
|
|
|
|
/* Save the updated indices */
|
|
|
|
|
|
|
|
rxbuf->head = nexthead;
|
|
|
|
priv->rxndx = rxndx;
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
/* Update the head point for for the next pass through the loop
|
|
|
|
* handling. If nexthead incremented to rxbuf->tail, then the
|
|
|
|
* RX buffer will and we will exit the loop at the top.
|
|
|
|
*/
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
if (++nexthead >= rxbuf->size)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
|
|
|
|
/* Increment the index in the USB IN packet buffer. If the
|
|
|
|
* index becomes equal to the number of bytes in the buffer, then
|
2015-05-07 19:27:51 +02:00
|
|
|
* we have consumed all of the RX data.
|
2015-05-07 19:16:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (++rxndx >= priv->nrxbytes)
|
|
|
|
{
|
2015-05-07 19:27:51 +02:00
|
|
|
/* In that case set the number of bytes in the buffer to zero.
|
|
|
|
* This will force re-reading on the next time through the loop.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 19:16:03 +02:00
|
|
|
priv->nrxbytes = 0;
|
2015-05-14 18:22:08 +02:00
|
|
|
priv->rxndx = 0;
|
2015-05-07 19:27:51 +02:00
|
|
|
|
|
|
|
/* Inform any waiters there there is new incoming data available. */
|
|
|
|
|
|
|
|
uart_datareceived(uartdev);
|
|
|
|
nxfrd = 0;
|
2015-05-07 19:16:03 +02:00
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
/* We break out to here: 1) the UART RX buffer is full, 2) the CDC/ACM
|
|
|
|
* device is not ready to provide us with more serial data, or 3) the
|
|
|
|
* device has been disconnected.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
2015-05-14 18:22:08 +02:00
|
|
|
* Check if the device is still available: RX enabled, no RX flow
|
|
|
|
* control in effect, and that the device is not disconnected. These
|
|
|
|
* states could have changed since we started the transfer.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
2019-02-11 14:08:33 +01:00
|
|
|
if (priv->rxena && priv->rts && work_available(&priv->rxwork) &&
|
|
|
|
!priv->disconnected)
|
2015-05-06 20:49:00 +02:00
|
|
|
#else
|
2015-05-11 20:47:37 +02:00
|
|
|
if (priv->rxena && work_available(&priv->rxwork) && !priv->disconnected)
|
2015-05-06 20:49:00 +02:00
|
|
|
#endif
|
|
|
|
{
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Schedule RX data reception work to occur after a delay. This will
|
|
|
|
* affect our responsive in certain cases. The delayed work, however,
|
|
|
|
* will be cancelled and replaced with immediate work when the upper
|
|
|
|
* layer demands more data.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = work_queue(LPWORK, &priv->rxwork, usbhost_rxdata_work, priv,
|
|
|
|
USBHOST_CDCACM_RXDELAY);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If any bytes were added to the buffer, inform any waiters there there
|
|
|
|
* is new incoming data available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nxfrd)
|
|
|
|
{
|
|
|
|
uart_datareceived(uartdev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The USB CDC/ACM device has been disconnected and the reference count
|
|
|
|
* on the USB host class instance has gone to 1.. Time to destroy the USB
|
|
|
|
* host class instance.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - A reference to the class instance to be destroyed.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_destroy(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv = (FAR struct usbhost_cdcacm_s *)arg;
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Unregister the serial lower half driver */
|
|
|
|
|
|
|
|
usbhost_mkdevname(priv, devname);
|
2023-01-31 13:54:31 +01:00
|
|
|
unregister_driver(devname);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Release the device name used by this connection */
|
|
|
|
|
|
|
|
usbhost_devno_free(priv);
|
|
|
|
|
|
|
|
/* Free the allocated endpoints */
|
|
|
|
|
|
|
|
if (priv->bulkout)
|
|
|
|
{
|
|
|
|
DRVR_EPFREE(hport->drvr, priv->bulkout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->bulkin)
|
|
|
|
{
|
|
|
|
DRVR_EPFREE(hport->drvr, priv->bulkin);
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
if (priv->intin)
|
|
|
|
{
|
|
|
|
DRVR_EPFREE(hport->drvr, priv->intin);
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Free any transfer buffers */
|
|
|
|
|
|
|
|
usbhost_free_buffers(priv);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Destroy the mutex */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Disconnect the USB host device */
|
|
|
|
|
|
|
|
DRVR_DISCONNECT(hport->drvr, hport);
|
|
|
|
|
|
|
|
/* Free the function address assigned to this device */
|
|
|
|
|
|
|
|
usbhost_devaddr_destroy(hport, hport->funcaddr);
|
|
|
|
hport->funcaddr = 0;
|
|
|
|
|
|
|
|
/* And free the class instance. */
|
|
|
|
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_cfgdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the connect() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to provide the device's configuration
|
|
|
|
* descriptor to the class so that the class may initialize properly
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - The USB host class instance.
|
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-03-30 22:20:53 +02:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function will *not* be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
|
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
FAR struct usb_cfgdesc_s *cfgdesc;
|
|
|
|
FAR struct usb_desc_s *desc;
|
2019-02-11 14:08:33 +01:00
|
|
|
struct usbhost_epdesc_s bindesc;
|
|
|
|
struct usbhost_epdesc_s boutdesc;
|
|
|
|
struct usbhost_epdesc_s iindesc;
|
2015-05-06 20:49:00 +02:00
|
|
|
int remaining;
|
2015-05-07 18:02:51 +02:00
|
|
|
uint8_t found = 0;
|
|
|
|
uint8_t currif = 0;
|
2015-05-06 20:49:00 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport &&
|
|
|
|
configdesc != NULL && desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
|
|
|
/* Keep the compiler from complaining about uninitialized variables */
|
|
|
|
|
|
|
|
memset(&bindesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
memset(&boutdesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
memset(&iindesc, 0, sizeof(struct usbhost_epdesc_s));
|
|
|
|
|
|
|
|
/* Verify that we were passed a configuration descriptor */
|
|
|
|
|
|
|
|
cfgdesc = (FAR struct usb_cfgdesc_s *)configdesc;
|
|
|
|
if (cfgdesc->type != USB_DESC_TYPE_CONFIG)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total length of the configuration descriptor (little endian).
|
|
|
|
* It might be a good check to get the number of interfaces here too.
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
remaining = (int)usbhost_getle16(cfgdesc->totallen);
|
|
|
|
|
|
|
|
/* Skip to the next entry descriptor */
|
|
|
|
|
|
|
|
configdesc += cfgdesc->len;
|
|
|
|
remaining -= cfgdesc->len;
|
|
|
|
|
|
|
|
/* Loop where there are more descriptors to examine */
|
|
|
|
|
|
|
|
while (remaining >= sizeof(struct usb_desc_s))
|
|
|
|
{
|
|
|
|
/* What is the next descriptor? */
|
|
|
|
|
|
|
|
desc = (FAR struct usb_desc_s *)configdesc;
|
|
|
|
switch (desc->type)
|
|
|
|
{
|
2015-05-07 18:02:51 +02:00
|
|
|
/* Interface descriptor. The CDC/ACM device may include two
|
|
|
|
* interfaces:
|
|
|
|
*
|
|
|
|
* 1) A data interface which consists of two endpoints (bulk in +
|
|
|
|
* bulk out) and
|
|
|
|
* 2) A control interface which consists of one interrupt in
|
|
|
|
* endpoint.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_INTERFACE:
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usb_ifdesc_s *ifdesc =
|
|
|
|
(FAR struct usb_ifdesc_s *)configdesc;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interface descriptor: class: %d subclass: %d proto: %d\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
ifdesc->classid, ifdesc->subclass, ifdesc->protocol);
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC);
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
/* Check for the CDC/ACM data interface */
|
|
|
|
|
2020-01-09 11:58:07 +01:00
|
|
|
if ((ifdesc->classid == USB_CLASS_CDC_DATA ||
|
|
|
|
ifdesc->classid == USB_CLASS_VENDOR_SPEC) &&
|
2015-05-07 18:02:51 +02:00
|
|
|
(found & USBHOST_DATAIF_FOUND) == 0)
|
|
|
|
{
|
|
|
|
/* Save the data interface number and mark that the data
|
|
|
|
* interface found has been found.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->dataif = ifdesc->ifno;
|
|
|
|
found |= USBHOST_DATAIF_FOUND;
|
|
|
|
currif = USBHOST_DATAIF_FOUND;
|
|
|
|
}
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-07 18:02:51 +02:00
|
|
|
else if (ifdesc->classid == USB_CLASS_CDC &&
|
|
|
|
(found & USBHOST_CTRLIF_FOUND) == 0)
|
|
|
|
{
|
|
|
|
/* Otherwise, tentatively assume that this is the control
|
|
|
|
* interface.
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
priv->ctrlif = ifdesc->ifno;
|
|
|
|
currif = USBHOST_CTRLIF_FOUND;
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-07 18:02:51 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Its something else */
|
|
|
|
|
|
|
|
currif = 0;
|
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Endpoint descriptor. We expect two bulk endpoints, an IN and an
|
|
|
|
* OUT.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_ENDPOINT:
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usb_epdesc_s *epdesc =
|
|
|
|
(FAR struct usb_epdesc_s *)configdesc;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Endpoint descriptor: currif: %02x attr: %02x\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
currif, epdesc->attr);
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
|
|
|
|
|
|
|
|
/* Check for a bulk endpoint. */
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
if (currif == USBHOST_DATAIF_FOUND &&
|
2020-03-30 22:20:53 +02:00
|
|
|
(epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
|
|
|
|
USB_EP_ATTR_XFER_BULK)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2015-05-07 18:02:51 +02:00
|
|
|
/* Yes.. it is a bulk endpoint. IN or OUT? */
|
2015-05-10 00:23:19 +02:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
if (USB_ISEPOUT(epdesc->addr))
|
|
|
|
{
|
|
|
|
/* It is an OUT bulk endpoint. There should be only one
|
|
|
|
* bulk OUT endpoint.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
if ((found & USBHOST_BULKOUT_FOUND) != 0)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Oops.. more than one endpoint. We don't know
|
|
|
|
* what to do with this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
found |= USBHOST_BULKOUT_FOUND;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Save the bulk OUT endpoint information */
|
|
|
|
|
|
|
|
boutdesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
boutdesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2015-05-06 20:49:00 +02:00
|
|
|
boutdesc.in = false;
|
|
|
|
boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK;
|
|
|
|
boutdesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
boutdesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n",
|
2015-05-06 20:49:00 +02:00
|
|
|
boutdesc.addr, boutdesc.mxpacketsize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It is an IN bulk endpoint. There should be only one
|
|
|
|
* bulk IN endpoint.
|
|
|
|
*/
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
if ((found & USBHOST_BULKIN_FOUND) != 0)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Oops.. more than one endpoint. We don't know
|
|
|
|
* what to do with this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
found |= USBHOST_BULKIN_FOUND;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Save the bulk IN endpoint information */
|
|
|
|
|
|
|
|
bindesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
bindesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2015-05-06 20:49:00 +02:00
|
|
|
bindesc.in = 1;
|
|
|
|
bindesc.xfrtype = USB_EP_ATTR_XFER_BULK;
|
|
|
|
bindesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
bindesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n",
|
2015-05-06 20:49:00 +02:00
|
|
|
bindesc.addr, bindesc.mxpacketsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Check for an interrupt IN endpoint. */
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
else if (currif == USBHOST_CTRLIF_FOUND &&
|
2020-01-09 11:58:07 +01:00
|
|
|
(epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
|
|
|
|
USB_EP_ATTR_XFER_INT)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Yes.. it is a interrupt endpoint. IN or OUT? */
|
|
|
|
|
|
|
|
if (USB_ISEPIN(epdesc->addr))
|
|
|
|
{
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2019-02-11 14:08:33 +01:00
|
|
|
/* It is an IN interrupt endpoint. There should be only
|
|
|
|
* one interrupt IN endpoint.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
if ((found & USBHOST_INTIN_FOUND) != 0)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Oops.. more than one. We don't know what to do
|
|
|
|
* with this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
/* Let's pick this interface as the one and only control
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
found |= (USBHOST_CTRLIF_FOUND | USBHOST_INTIN_FOUND);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2022-06-16 14:34:36 +02:00
|
|
|
/* Save the interrupt IN endpoint information */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
iindesc.hport = hport;
|
2020-03-30 22:20:53 +02:00
|
|
|
iindesc.addr = epdesc->addr &
|
|
|
|
USB_EP_ADDR_NUMBER_MASK;
|
2022-06-16 14:34:36 +02:00
|
|
|
iindesc.in = true;
|
2015-05-06 20:49:00 +02:00
|
|
|
iindesc.xfrtype = USB_EP_ATTR_XFER_INT;
|
|
|
|
iindesc.interval = epdesc->interval;
|
2020-03-30 22:20:53 +02:00
|
|
|
iindesc.mxpacketsize =
|
|
|
|
usbhost_getle16(epdesc->mxpacketsize);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n",
|
2022-06-16 14:34:36 +02:00
|
|
|
iindesc.addr, iindesc.mxpacketsize);
|
2015-05-10 16:16:58 +02:00
|
|
|
#else
|
|
|
|
found |= USBHOST_CTRLIF_FOUND;
|
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Other descriptors are just ignored for now */
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we found everything we need with this interface, then break out
|
|
|
|
* of the loop early.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (found == USBHOST_ALLFOUND)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the address of the next descriptor */
|
|
|
|
|
|
|
|
configdesc += desc->len;
|
|
|
|
remaining -= desc->len;
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
/* Sanity checking... did we find all of things that we needed for the
|
2020-02-23 09:50:23 +01:00
|
|
|
* basic CDC/ACM data interface? NOTE: that the Control interface with
|
2015-05-06 20:49:00 +02:00
|
|
|
* the Interrupt IN endpoint is optional.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((found & USBHOST_MINFOUND) != USBHOST_MINFOUND)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Found DATA IF:%s BULK IN:%s BULK OUT:%s\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
(found & USBHOST_DATAIF_FOUND) != 0 ? "YES" : "NO",
|
|
|
|
(found & USBHOST_BULKIN_FOUND) != 0 ? "YES" : "NO",
|
|
|
|
(found & USBHOST_BULKOUT_FOUND) != 0 ? "YES" : "NO");
|
2015-05-06 20:49:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are good... Allocate the endpoints */
|
|
|
|
|
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &boutdesc, &priv->bulkout);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate Bulk OUT endpoint\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &bindesc, &priv->bulkin);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate Bulk IN endpoint\n");
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_EPFREE(hport->drvr, priv->bulkout);
|
2015-05-06 20:49:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-07 18:02:51 +02:00
|
|
|
/* The control interface with interrupt IN endpoint is optional */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2015-05-07 18:02:51 +02:00
|
|
|
if ((found & USBHOST_HAVE_CTRLIF) == USBHOST_HAVE_CTRLIF)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
ret = DRVR_EPALLOC(hport->drvr, &iindesc, &priv->intin);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate Interrupt IN endpoint\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->intin = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Endpoints allocated\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_getle16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get a (possibly unaligned) 16-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* val - A pointer to the first byte of the little endian value.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* A uint16_t representing the whole 16-bit integer value
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
static inline uint16_t usbhost_getle16(FAR const uint8_t *val)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
return (uint16_t)val[1] << 8 | (uint16_t)val[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putle16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 16-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the little endian value.
|
|
|
|
* val - The 16-bit value to be saved.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
static void usbhost_putle16(FAR uint8_t *dest, uint16_t val)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
dest[0] = val & 0xff; /* Little endian means LS byte first in byte stream */
|
|
|
|
dest[1] = val >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_putle32
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put a (possibly unaligned) 32-bit little endian value.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dest - A pointer to the first byte to save the little endian value.
|
|
|
|
* val - The 32-bit value to be saved.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2019-02-11 14:08:33 +01:00
|
|
|
static void usbhost_putle32(FAR uint8_t *dest, uint32_t val)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
/* Little endian means LS halfword first in byte stream */
|
|
|
|
|
|
|
|
usbhost_putle16(dest, (uint16_t)(val & 0xffff));
|
2020-01-09 11:58:07 +01:00
|
|
|
usbhost_putle16(dest + 2, (uint16_t)(val >> 16));
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_alloc_buffers
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate transfer buffer memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-14 14:55:45 +02:00
|
|
|
* On success, zero (OK) is returned. On failure, an negated errno value
|
2015-05-06 20:49:00 +02:00
|
|
|
* is returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_alloc_buffers(FAR struct usbhost_cdcacm_s *priv)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
size_t maxlen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL &&
|
|
|
|
priv->linecode == NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
|
|
|
/* Allocate memory for control requests */
|
|
|
|
|
|
|
|
ret = DRVR_ALLOC(hport->drvr, (FAR uint8_t **)&priv->ctrlreq, &maxlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_ALLOC of ctrlreq failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(maxlen >= sizeof(struct usb_ctrlreq_s));
|
|
|
|
|
|
|
|
/* Allocate buffer for sending line coding data. */
|
|
|
|
|
|
|
|
ret = DRVR_IOALLOC(hport->drvr, &priv->linecode,
|
2015-10-10 18:41:00 +02:00
|
|
|
sizeof(struct cdc_linecoding_s));
|
2015-05-06 20:49:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-02-27 07:52:21 +01:00
|
|
|
uerr("ERROR: DRVR_IOALLOC of line coding failed: %d (%zu bytes)\n",
|
2015-10-10 18:41:00 +02:00
|
|
|
ret, sizeof(struct cdc_linecoding_s));
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Allocate (optional) buffer for receiving line status data. */
|
|
|
|
|
|
|
|
if (priv->intin)
|
|
|
|
{
|
|
|
|
ret = DRVR_IOALLOC(hport->drvr, &priv->notification, MAX_NOTIFICATION);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_IOALLOC of line status failed: %d (%d bytes)\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
ret, MAX_NOTIFICATION);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Set the size of Bulk IN and OUT buffers to the max packet size */
|
|
|
|
|
|
|
|
priv->pktsize = (hport->speed == USB_SPEED_HIGH) ? 512 : 64;
|
|
|
|
|
|
|
|
/* Allocate a RX buffer for Bulk IN transfers */
|
|
|
|
|
|
|
|
ret = DRVR_IOALLOC(hport->drvr, &priv->inbuf, priv->pktsize);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_IOALLOC of Bulk IN buffer failed: %d (%d bytes)\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
ret, priv->pktsize);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Allocate a TX buffer for Bulk IN transfers */
|
|
|
|
|
|
|
|
ret = DRVR_IOALLOC(hport->drvr, &priv->outbuf, priv->pktsize);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_IOALLOC of Bulk OUT buffer failed: %d (%d bytes)\n",
|
2015-05-07 18:02:51 +02:00
|
|
|
ret, priv->pktsize);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
usbhost_free_buffers(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_free_buffers
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free transfer buffer memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to the class instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_free_buffers(FAR struct usbhost_cdcacm_s *priv)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_hubport_s *hport;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
|
|
|
|
if (priv->ctrlreq)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_FREE(hport->drvr, priv->ctrlreq);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->linecode)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_IOFREE(hport->drvr, priv->linecode);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->notification)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_IOFREE(hport->drvr, priv->notification);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->inbuf)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_IOFREE(hport->drvr, priv->inbuf);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->outbuf)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
DRVR_IOFREE(hport->drvr, priv->outbuf);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->pktsize = 0;
|
|
|
|
priv->ctrlreq = NULL;
|
|
|
|
priv->linecode = NULL;
|
|
|
|
priv->notification = NULL;
|
|
|
|
priv->inbuf = NULL;
|
|
|
|
priv->outbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_registry_s methods
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_create
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-30 22:20:53 +02:00
|
|
|
* This function implements the create() method of struct
|
|
|
|
* usbhost_registry_s. The create() method is a callback into the class
|
|
|
|
* implementation. It is used to (1) create a new instance of the USB
|
|
|
|
* host class state and to (2) bind a USB host driver "session" to the
|
|
|
|
* class instance. Use of this create() method will support environments
|
|
|
|
* where there may be multiple USB ports and multiple USB devices
|
|
|
|
* simultaneously connected.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* hport - The hub port that manages the new class instance.
|
|
|
|
* id - In the case where the device supports multiple base classes,
|
|
|
|
* subclasses, or protocols, this specifies which to configure for.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, this function will return a non-NULL instance of struct
|
|
|
|
* usbhost_class_s that can be used by the USB host driver to communicate
|
|
|
|
* with the USB host class. NULL is returned on failure; this function
|
|
|
|
* will fail only if the hport input parameter is NULL or if there are
|
|
|
|
* insufficient resources to create another USB host class instance.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct usbhost_class_s *
|
|
|
|
usbhost_create(FAR struct usbhost_hubport_s *hport,
|
|
|
|
FAR const struct usbhost_id_s *id)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
FAR struct uart_dev_s *uartdev;
|
|
|
|
|
|
|
|
/* Allocate a USB host CDC/ACM class instance */
|
|
|
|
|
|
|
|
priv = usbhost_allocclass();
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
/* Initialize the allocated CDC/ACM class instance */
|
|
|
|
|
|
|
|
memset(priv, 0, sizeof(struct usbhost_cdcacm_s));
|
|
|
|
|
|
|
|
/* Assign a device number to this class instance */
|
|
|
|
|
|
|
|
if (usbhost_devno_alloc(priv) == OK)
|
|
|
|
{
|
2020-01-09 11:58:07 +01:00
|
|
|
/* Initialize class method function pointers */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
priv->usbclass.hport = hport;
|
|
|
|
priv->usbclass.connect = usbhost_connect;
|
|
|
|
priv->usbclass.disconnected = usbhost_disconnected;
|
|
|
|
|
2020-04-09 14:46:24 +02:00
|
|
|
/* The initial reference count is 1...
|
|
|
|
* One reference is held by the driver
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
priv->crefs = 1;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Initialize mutex
|
2020-04-09 14:46:24 +02:00
|
|
|
* (this works okay in the interrupt context)
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Set up the serial lower-half interface */
|
|
|
|
|
|
|
|
uartdev = &priv->uartdev;
|
|
|
|
uartdev->recv.size = CONFIG_USBHOST_CDCACM_RXBUFSIZE;
|
|
|
|
uartdev->recv.buffer = priv->rxbuffer;
|
|
|
|
uartdev->xmit.size = CONFIG_USBHOST_CDCACM_TXBUFSIZE;
|
|
|
|
uartdev->xmit.buffer = priv->txbuffer;
|
|
|
|
uartdev->ops = &g_uart_ops;
|
|
|
|
uartdev->priv = priv;
|
|
|
|
|
|
|
|
/* Set up the initial line status */
|
|
|
|
|
|
|
|
priv->baud = CONFIG_USBHOST_CDCACM_BAUD;
|
|
|
|
priv->nbits = CONFIG_USBHOST_CDCACM_BITS;
|
|
|
|
priv->parity = CONFIG_USBHOST_CDCACM_PARITY;
|
|
|
|
priv->stop2 = CONFIG_USBHOST_CDCACM_2STOP;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
priv->rts = true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return the instance of the USB CDC/ACM class */
|
|
|
|
|
|
|
|
return &priv->usbclass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* An error occurred. Free the allocation and return NULL on all failures */
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
{
|
|
|
|
usbhost_freeclass(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* struct usbhost_class_s methods
|
|
|
|
****************************************************************************/
|
2019-02-11 14:08:33 +01:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the connect() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to provide the device's configuration
|
|
|
|
* descriptor to the class so that the class may initialize properly
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* usbclass - The USB host class entry previously obtained from a call to
|
|
|
|
* create().
|
|
|
|
* configdesc - A pointer to a uint8_t buffer container the configuration
|
|
|
|
* descriptor.
|
|
|
|
* desclen - The length in bytes of the configuration descriptor.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-03-30 22:20:53 +02:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
2019-02-11 14:08:33 +01:00
|
|
|
* NOTE that the class instance remains valid upon return with a failure.
|
|
|
|
* It is the responsibility of the higher level enumeration logic to call
|
2015-05-06 20:49:00 +02:00
|
|
|
* CLASS_DISCONNECTED to free up the class driver resources.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - This function will *not* be called from an interrupt handler.
|
|
|
|
* - If this function returns an error, the USB host controller driver
|
|
|
|
* must call to DISCONNECTED method to recover from the error
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
|
|
|
|
FAR const uint8_t *configdesc, int desclen)
|
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usbhost_cdcacm_s *priv =
|
|
|
|
(FAR struct usbhost_cdcacm_s *)usbclass;
|
2015-05-14 18:22:08 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-05-14 18:22:08 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
char devname[DEV_NAMELEN];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL &&
|
|
|
|
configdesc != NULL &&
|
|
|
|
desclen >= sizeof(struct usb_cfgdesc_s));
|
|
|
|
|
2015-05-14 18:22:08 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
hport = priv->usbclass.hport;
|
|
|
|
DEBUGASSERT(hport);
|
2015-05-14 18:22:08 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the device structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Increment the reference count. This will prevent usbhost_destroy() from
|
|
|
|
* being called asynchronously if the device is removed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
DEBUGASSERT(priv->crefs == 2);
|
|
|
|
|
|
|
|
/* Parse the configuration descriptor to get the bulk I/O endpoints */
|
|
|
|
|
|
|
|
ret = usbhost_cfgdesc(priv, configdesc, desclen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_cfgdesc() failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set aside a transfer buffer for exclusive use by the CDC/ACM driver */
|
|
|
|
|
|
|
|
ret = usbhost_alloc_buffers(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate transfer buffer\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Send the initial line encoding */
|
|
|
|
|
|
|
|
ret = usbhost_linecoding_send(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: usbhost_linecoding_send() failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Register the lower half serial instance with the upper half serial
|
2019-02-11 14:08:33 +01:00
|
|
|
* driver.
|
|
|
|
*/
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
usbhost_mkdevname(priv, devname);
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Register device: %s\n", devname);
|
2015-05-07 19:16:03 +02:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
ret = uart_register(devname, &priv->uartdev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
uerr("ERROR: uart_register() failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Do we have an interrupt IN endpoint? */
|
|
|
|
|
|
|
|
if (priv->intin)
|
|
|
|
{
|
|
|
|
/* Begin monitoring of port status change events */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Start notification monitoring\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
ret = DRVR_ASYNCH(hport->drvr, priv->intin,
|
|
|
|
(FAR uint8_t *)priv->notification,
|
|
|
|
MAX_NOTIFICATION, usbhost_notification_callback,
|
2015-05-09 16:23:11 +02:00
|
|
|
priv);
|
2015-05-06 20:49:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: DRVR_ASYNCH failed: %d\n", ret);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
/* Decrement the reference count. We incremented the reference count
|
|
|
|
* above so that usbhost_destroy() could not be called. We now have to
|
|
|
|
* be concerned about asynchronous modification of crefs because the
|
|
|
|
* serial driver has been registered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs >= 2);
|
|
|
|
priv->crefs--;
|
|
|
|
|
|
|
|
/* Release the semaphore... there is a race condition here.
|
|
|
|
* Decrementing the reference count and releasing the semaphore
|
|
|
|
* allows usbhost_destroy() to execute (on the worker thread);
|
|
|
|
* the class driver instance could get destroyed before we are
|
|
|
|
* ready to handle it!
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_disconnected
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the disconnected() method of struct
|
|
|
|
* usbhost_class_s. This method is a callback into the class
|
|
|
|
* implementation. It is used to inform the class that the USB device has
|
|
|
|
* been disconnected.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* usbclass - The USB host class entry previously obtained from a call to
|
|
|
|
* create().
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function may be called from an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2020-03-30 22:20:53 +02:00
|
|
|
FAR struct usbhost_cdcacm_s *priv =
|
|
|
|
(FAR struct usbhost_cdcacm_s *)usbclass;
|
2015-05-08 18:58:41 +02:00
|
|
|
FAR struct usbhost_hubport_s *hport;
|
2015-05-06 20:49:00 +02:00
|
|
|
irqstate_t flags;
|
2015-05-11 20:47:37 +02:00
|
|
|
int ret;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2015-05-11 20:47:37 +02:00
|
|
|
DEBUGASSERT(priv != NULL && priv->usbclass.hport != NULL);
|
2015-05-08 18:58:41 +02:00
|
|
|
hport = priv->usbclass.hport;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Set an indication to any users of the CDC/ACM device that the device
|
|
|
|
* is no longer available.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->disconnected = true;
|
|
|
|
|
2015-05-11 23:10:41 +02:00
|
|
|
/* Let the upper half driver know that serial device is no longer
|
|
|
|
* connected.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uart_connected(&priv->uartdev, false);
|
|
|
|
|
2015-05-11 20:47:37 +02:00
|
|
|
/* Cancel any ongoing Bulk transfers */
|
|
|
|
|
|
|
|
ret = DRVR_CANCEL(hport->drvr, priv->bulkin);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Bulk IN DRVR_CANCEL failed: %d\n", ret);
|
2015-05-11 20:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = DRVR_CANCEL(hport->drvr, priv->bulkout);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Bulk OUT DRVR_CANCEL failed: %d\n", ret);
|
2015-05-11 20:47:37 +02:00
|
|
|
}
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_INTIN_ENDPOINT
|
2015-05-08 18:58:41 +02:00
|
|
|
/* Cancel any pending asynchronous I/O */
|
|
|
|
|
|
|
|
if (priv->intin)
|
|
|
|
{
|
2023-05-09 11:24:24 +02:00
|
|
|
ret = DRVR_CANCEL(hport->drvr, priv->intin);
|
2015-05-08 18:58:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Interrupt IN DRVR_CANCEL failed: %d\n", ret);
|
2015-05-08 18:58:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Now check the number of references on the class instance. If it is one,
|
|
|
|
* then we can free the class instance now. Otherwise, we will have to
|
|
|
|
* wait until the holders of the references free them by closing the
|
|
|
|
* serial driver.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("crefs: %d\n", priv->crefs);
|
2015-05-06 20:49:00 +02:00
|
|
|
if (priv->crefs == 1)
|
|
|
|
{
|
|
|
|
/* Destroy the class instance. If we are executing from an interrupt
|
|
|
|
* handler, then defer the destruction to the worker thread.
|
|
|
|
* Otherwise, destroy the instance now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (up_interrupt_context())
|
|
|
|
{
|
|
|
|
/* Destroy the instance on the worker thread. */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Queuing destruction: worker %p->%p\n",
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->ntwork.worker, usbhost_destroy);
|
|
|
|
|
|
|
|
DEBUGASSERT(work_available(&priv->ntwork));
|
2020-01-02 17:49:34 +01:00
|
|
|
work_queue(HPWORK, &priv->ntwork, usbhost_destroy, priv, 0);
|
2015-05-11 20:47:37 +02:00
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Do the work now */
|
|
|
|
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Serial Lower-Half Interfaces
|
|
|
|
****************************************************************************/
|
2020-01-09 11:58:07 +01:00
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure the USART baud, bits, parity, etc. This method is called the
|
|
|
|
* first time that the serial port is opened.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_setup(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
|
|
|
|
|
|
|
/* Make sure that we have exclusive access to the private data structure */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Check if the CDC/ACM device is still connected. We need to disable
|
2015-10-04 23:28:54 +02:00
|
|
|
* interrupts momentarily to assure that there are no asynchronous
|
2015-05-06 20:49:00 +02:00
|
|
|
* isconnect events.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
2019-02-11 14:08:33 +01:00
|
|
|
/* No... the block driver is no longer bound to the class. That means
|
|
|
|
* that the USB CDC/ACM device is no longer connected. Refuse any
|
|
|
|
* further attempts to open the driver.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, just increment the reference count on the driver */
|
|
|
|
|
|
|
|
priv->crefs++;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_shutdown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable the USART. This method is called when the serial
|
|
|
|
* port is closed
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_shutdown(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
|
|
|
|
|
|
|
/* Decrement the reference count on the block driver */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs > 1);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
priv->crefs--;
|
|
|
|
|
|
|
|
/* Release the semaphore. The following operations when crefs == 1 are
|
|
|
|
* safe because we know that there is no outstanding open references to
|
|
|
|
* the block driver.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* We need to disable interrupts momentarily to assure that there are
|
|
|
|
* no asynchronous disconnect events.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Check if the USB CDC/ACM device is still connected. If the
|
|
|
|
* CDC/ACM device is not connected and the reference count just
|
|
|
|
* decremented to one, then unregister the block driver and free
|
|
|
|
* the class instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs <= 1 && priv->disconnected)
|
|
|
|
{
|
|
|
|
/* Destroy the class instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->crefs == 1);
|
|
|
|
usbhost_destroy(priv);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_attach
|
|
|
|
*
|
|
|
|
* Description:
|
2019-02-11 14:08:33 +01:00
|
|
|
* Configure the USART to operation in interrupt driven mode. This method
|
|
|
|
* is called when the serial port is opened. Normally, this is just after
|
2015-05-06 20:49:00 +02:00
|
|
|
* the setup() method is called, however, the serial console may operate in
|
|
|
|
* a non-interrupt driven mode during the boot phase.
|
|
|
|
*
|
2019-02-11 14:08:33 +01:00
|
|
|
* RX and TX interrupts are not enabled when by the attach method (unless
|
|
|
|
* the hardware supports multiple levels of interrupt enabling). The RX
|
|
|
|
* and TX interrupts are not enabled until the txint() and rxint() methods
|
|
|
|
* are called.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_attach(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_detach
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Detach USART interrupts. This method is called when the serial port is
|
2020-03-30 22:20:53 +02:00
|
|
|
* closed normally just before the shutdown method is called. The
|
|
|
|
* exception is the serial console which is never shutdown.
|
2015-05-06 20:49:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_detach(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All ioctl calls will be routed through this method
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
2019-02-11 14:08:33 +01:00
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
FAR struct uart_dev_s *uartdev;
|
|
|
|
int ret = 0;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("Entry\n");
|
2015-05-06 20:49:00 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
uartdev = inode->i_private;
|
2019-02-11 14:08:33 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
2022-02-01 17:55:50 +01:00
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Check if the CDC/ACM device is still connected */
|
|
|
|
|
|
|
|
if (priv->disconnected)
|
|
|
|
{
|
|
|
|
/* No... the serial device has been disconnecgted. Refuse to process
|
|
|
|
* any ioctl commands.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ENODEV;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Process the IOCTL by command */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:49:00 +02:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SERIAL_TIOCSERGSTRUCT
|
|
|
|
case TIOCSERGSTRUCT:
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *user =
|
|
|
|
(FAR struct usbhost_cdcacm_s *)arg;
|
|
|
|
if (!user)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(user, uartdev, sizeof(struct usbhost_cdcacm_s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
|
|
|
case TCGETS:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfsetispeed(termiosp, priv->baud);
|
|
|
|
|
|
|
|
termiosp->c_cflag =
|
|
|
|
((priv->parity != 0) ? PARENB : 0) |
|
|
|
|
((priv->parity == 1) ? PARODD : 0) |
|
|
|
|
((priv->stop2) ? CSTOPB : 0)
|
|
|
|
#ifdef CONFIG_SERIAL_OFLOWCONTROL
|
|
|
|
| ((priv->oflow) ? CCTS_OFLOW : 0)
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
| ((priv->iflow) ? CRTS_IFLOW : 0)
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
switch (priv->nbits)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
case 5:
|
|
|
|
termiosp->c_cflag |= CS5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
termiosp->c_cflag |= CS6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
termiosp->c_cflag |= CS7;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* 16-bits? */
|
|
|
|
case 8:
|
|
|
|
termiosp->c_cflag |= CS8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCSETS:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct termios *termiosp = (FAR struct termios *)arg;
|
2015-05-06 20:49:00 +02:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
bool iflow;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!termiosp)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (termiosp->c_cflag & PARENB)
|
|
|
|
{
|
|
|
|
priv->parity = (termiosp->c_cflag & PARODD) ? 1 : 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->parity = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
priv->stop2 = (termiosp->c_cflag & CSTOPB) != 0;
|
2015-05-06 20:49:00 +02:00
|
|
|
#ifdef CONFIG_SERIAL_OFLOWCONTROL
|
|
|
|
priv->oflow = (termiosp->c_cflag & CCTS_OFLOW) != 0;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
iflow = priv->iflow;
|
|
|
|
priv->iflow = (termiosp->c_cflag & CRTS_IFLOW) != 0;
|
|
|
|
#endif
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
switch (termiosp->c_cflag & CSIZE)
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
2019-02-11 14:08:33 +01:00
|
|
|
case CS5:
|
|
|
|
priv->nbits = 5;
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
case CS6:
|
|
|
|
priv->nbits = 6;
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
|
2019-02-11 14:08:33 +01:00
|
|
|
case CS7:
|
|
|
|
priv->nbits = 7;
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-02-11 14:08:33 +01:00
|
|
|
case CS8:
|
|
|
|
priv->nbits = 8;
|
2015-05-06 20:49:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that only cfgetispeed is used because we have knowledge
|
|
|
|
* that only one speed is supported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->baud = cfgetispeed(termiosp);
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
/* Set RTS if input flow control changed */
|
|
|
|
|
|
|
|
if (iflow != !priv->iflow)
|
|
|
|
{
|
|
|
|
priv->rts = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
#ifdef HAVE_CTRL_INTERFACE
|
2015-05-06 20:49:00 +02:00
|
|
|
/* Effect the changes immediately - note that we do not implement
|
|
|
|
* TCSADRAIN / TCSAFLUSH
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = usbhost_linecoding_send(priv);
|
2015-05-07 19:16:03 +02:00
|
|
|
#endif
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_SERIAL_TERMIOS */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBHOST_CDCACM_BREAKS
|
|
|
|
case TIOCSBRK: /* BSD compatibility: Turn break on, unconditionally */
|
|
|
|
{
|
|
|
|
#warning Missing logic
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TIOCCBRK: /* BSD compatibility: Turn break off, unconditionally */
|
|
|
|
{
|
|
|
|
#warning Missing logic
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_rxint
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Call to enable or disable RX interrupts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_rxint(FAR struct uart_dev_s *uartdev, bool enable)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
|
|
|
|
|
|
|
/* Are we enabling or disabling RX reception? */
|
|
|
|
|
|
|
|
if (enable && !priv->rxena)
|
|
|
|
{
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Cancel any pending, delayed RX data reception work */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
work_cancel(LPWORK, &priv->rxwork);
|
2015-05-14 18:22:08 +02:00
|
|
|
|
|
|
|
/* Restart immediate RX data reception work (unless RX flow control
|
2019-02-11 14:08:33 +01:00
|
|
|
* is in effect.
|
2015-05-06 20:49:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
2016-02-24 18:54:02 +01:00
|
|
|
if (priv->rts)
|
2015-05-06 20:49:00 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = work_queue(LPWORK, &priv->rxwork,
|
|
|
|
usbhost_rxdata_work, priv, 0);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!enable && priv->rxena)
|
|
|
|
{
|
|
|
|
/* Cancel any pending RX data reception work */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
work_cancel(LPWORK, &priv->rxwork);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new RX enable state */
|
|
|
|
|
|
|
|
priv->rxena = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_rxavailable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true if the receive buffer is not empty
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool usbhost_rxavailable(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
|
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
|
|
|
return (priv->nrxbytes > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_rxflowcontrol
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when Rx buffer is full (or exceeds configured watermark levels
|
|
|
|
* if CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS is defined).
|
|
|
|
* Return true if UART activated RX flow control to block more incoming
|
|
|
|
* data
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-05-06 20:49:00 +02:00
|
|
|
* uartdev - UART device instance
|
|
|
|
* nbuffered - the number of characters currently buffered
|
|
|
|
* (if CONFIG_SERIAL_IFLOWCONTROL_WATERMARKS is
|
|
|
|
* not defined the value will be 0 for an empty buffer or the
|
|
|
|
* defined buffer size for a full buffer)
|
|
|
|
* upper - true indicates the upper watermark was crossed where
|
|
|
|
* false indicates the lower watermark has been crossed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* true if RX flow control activated.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
static bool usbhost_rxflowcontrol(FAR struct uart_dev_s *uartdev,
|
|
|
|
unsigned int nbuffered, bool upper)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
2023-01-31 13:54:31 +01:00
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Is RX flow control enabled? */
|
|
|
|
|
|
|
|
if (!priv->iflow)
|
|
|
|
{
|
|
|
|
/* Now.. make sure that RTS is set */
|
|
|
|
|
|
|
|
priv->rts = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we enabling or disabling RX flow control? */
|
|
|
|
|
|
|
|
if (priv->rts && upper)
|
|
|
|
{
|
|
|
|
/* RX flow control is not in effect (RTS is true) but we have just
|
|
|
|
* crossed the upper threshold, meaning that we should now clear
|
|
|
|
* RTS.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
priv->rts = false;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
/* Cancel any pending RX data reception work */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
work_cancel(LPWORK, &priv->rxwork);
|
2015-05-06 20:49:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (!priv->rts && !upper)
|
|
|
|
{
|
|
|
|
/* RX flow control is in effect (RTS is false) and we have just
|
|
|
|
* crossed the lower threshold, meaning that we should now set
|
|
|
|
* RTS.
|
|
|
|
*/
|
|
|
|
|
2016-02-24 18:54:02 +01:00
|
|
|
priv->rts = true;
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
/* Restart RX data reception work flow unless RX reception is
|
|
|
|
* disabled.
|
|
|
|
*/
|
|
|
|
|
2015-05-10 16:16:58 +02:00
|
|
|
if (priv->rxena && work_available(&priv->rxwork))
|
2015-05-06 20:49:00 +02:00
|
|
|
{
|
|
|
|
ret = work_queue(LPWORK, &priv->rxwork,
|
|
|
|
usbhost_rxdata_work, priv, 0);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
}
|
2023-01-31 13:54:31 +01:00
|
|
|
|
|
|
|
return false;
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_txint
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Call to enable or disable TX interrupts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbhost_txint(FAR struct uart_dev_s *uartdev, bool enable)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_cdcacm_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(uartdev && uartdev->priv);
|
|
|
|
priv = (FAR struct usbhost_cdcacm_s *)uartdev->priv;
|
|
|
|
|
|
|
|
/* Are we enabling or disabling TX transmission? */
|
|
|
|
|
|
|
|
if (enable && !priv->txena)
|
|
|
|
{
|
2015-05-14 18:22:08 +02:00
|
|
|
/* Cancel any pending, delayed TX data transmission work */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
work_cancel(LPWORK, &priv->txwork);
|
2015-05-14 18:22:08 +02:00
|
|
|
|
|
|
|
/* Restart immediate TX data transmission work */
|
2015-05-06 20:49:00 +02:00
|
|
|
|
|
|
|
ret = work_queue(LPWORK, &priv->txwork,
|
|
|
|
usbhost_txdata_work, priv, 0);
|
|
|
|
DEBUGASSERT(ret >= 0);
|
|
|
|
UNUSED(ret);
|
|
|
|
}
|
|
|
|
else if (!enable && priv->txena)
|
|
|
|
{
|
|
|
|
/* Cancel any pending TX data transmission work */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
work_cancel(LPWORK, &priv->txwork);
|
2015-05-06 20:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new RX enable state */
|
|
|
|
|
|
|
|
priv->txena = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_txready
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true if the transmit data register is not full.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool usbhost_txready(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_txempty
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true if the transmit data buffer is empty
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool usbhost_txempty(FAR struct uart_dev_s *uartdev)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_cdcacm_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the USB host CDC/ACM class. This function should be called
|
|
|
|
* be platform-specific code in order to initialize and register support
|
|
|
|
* for the USB host CDC/ACM class.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success this function will return zero (OK); A negated errno value
|
|
|
|
* will be returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbhost_cdcacm_initialize(void)
|
|
|
|
{
|
|
|
|
/* If we have been configured to use pre-allocated CDC/ACM class instances,
|
|
|
|
* then place all of the pre-allocated USB host CDC/ACM class instances
|
|
|
|
* into a free list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_USBHOST_CDCACM_NPREALLOC > 0
|
|
|
|
FAR struct usbhost_freestate_s *entry;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_freelist = NULL;
|
|
|
|
for (i = 0; i < CONFIG_USBHOST_CDCACM_NPREALLOC; i++)
|
|
|
|
{
|
|
|
|
entry = (FAR struct usbhost_freestate_s *)&g_prealloc[i];
|
|
|
|
entry->flink = g_freelist;
|
|
|
|
g_freelist = entry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Advertise our availability to support (certain) CDC/ACM devices */
|
|
|
|
|
|
|
|
return usbhost_registerclass(&g_cdcacm);
|
|
|
|
}
|
|
|
|
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_USBHOST_CDCACM */
|