2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-24 22:51:26 +01:00
|
|
|
* drivers/usbdev/pl2303.c
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2020-03-11 21:44:00 +01: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
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2020-03-11 21:44:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2020-03-11 21:44:00 +01:00
|
|
|
* 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.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2020-03-11 21:44:00 +01:00
|
|
|
* This logic emulates the Prolific PL2303 serial/USB converter
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Included Files
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-10-07 01:12:09 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2008-10-07 01:12:09 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2008-10-07 01:12:09 +02:00
|
|
|
#include <nuttx/arch.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2012-03-21 20:47:23 +01:00
|
|
|
#include <nuttx/serial/serial.h>
|
2010-12-14 04:33:39 +01:00
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbdev.h>
|
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2022-01-18 02:36:37 +01:00
|
|
|
#ifdef CONFIG_PL2303_BOARD_SERIALSTR
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 15:25:14 +01:00
|
|
|
* Pre-processor Definitions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Configuration ************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Number of requests in the write queue */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_NWRREQS
|
|
|
|
# define CONFIG_PL2303_NWRREQS 4
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Number of requests in the read queue */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_NRDREQS
|
|
|
|
# define CONFIG_PL2303_NRDREQS 4
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Logical endpoint numbers / max packet sizes */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_EPINTIN
|
2008-10-09 14:49:11 +02:00
|
|
|
# warning "EPINTIN not defined in the configuration"
|
2012-01-26 00:04:17 +01:00
|
|
|
# define CONFIG_PL2303_EPINTIN 1
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_EPBULKOUT
|
2008-10-09 14:49:11 +02:00
|
|
|
# warning "EPBULKOUT not defined in the configuration"
|
2012-01-26 00:04:17 +01:00
|
|
|
# define CONFIG_PL2303_EPBULKOUT 2
|
2008-10-09 14:49:11 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_EPBULKIN
|
2008-10-09 14:49:11 +02:00
|
|
|
# warning "EPBULKIN not defined in the configuration"
|
2012-01-26 00:04:17 +01:00
|
|
|
# define CONFIG_PL2303_EPBULKIN 3
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-09 23:47:45 +02:00
|
|
|
/* Packet and request buffer sizes */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_EP0MAXPACKET
|
|
|
|
# define CONFIG_PL2303_EP0MAXPACKET 64
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2013-05-29 20:30:37 +02:00
|
|
|
/* Ideally, the BULKOUT request size should *not* be the same size as the
|
|
|
|
* maxpacket size. That is because IN transfers of exactly the maxpacket
|
|
|
|
* size will be followed by a NULL packet. The BULKOUT request buffer
|
|
|
|
* size, on the other hand, is always the same as the maxpacket size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_PL2303_BULKIN_REQLEN
|
|
|
|
# define CONFIG_PL2303_BULKIN_REQLEN 96
|
|
|
|
#endif
|
2008-10-09 23:47:45 +02:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
/* Vendor and product IDs and strings */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_VENDORID
|
|
|
|
# define CONFIG_PL2303_VENDORID 0x067b
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_PRODUCTID
|
|
|
|
# define CONFIG_PL2303_PRODUCTID 0x2303
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_VENDORSTR
|
2008-10-07 01:12:09 +02:00
|
|
|
# warning "No Vendor string specified"
|
2012-01-26 00:04:17 +01:00
|
|
|
# define CONFIG_PL2303_VENDORSTR "NuttX"
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifndef CONFIG_PL2303_PRODUCTSTR
|
2008-10-07 01:12:09 +02:00
|
|
|
# warning "No Product string specified"
|
2012-01-26 00:04:17 +01:00
|
|
|
# define CONFIG_PL2303_PRODUCTSTR "USBdev Serial"
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#undef CONFIG_PL2303_SERIALSTR
|
|
|
|
#define CONFIG_PL2303_SERIALSTR "0"
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#undef CONFIG_PL2303_CONFIGSTR
|
|
|
|
#define CONFIG_PL2303_CONFIGSTR "Bulk"
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* USB Controller */
|
|
|
|
|
2013-09-20 18:00:30 +02:00
|
|
|
#ifdef CONFIG_USBDEV_SELFPOWERED
|
|
|
|
# define PL2303_SELFPOWERED USB_CONFIG_ATTR_SELFPOWER
|
2008-10-07 01:12:09 +02:00
|
|
|
#else
|
2013-09-20 18:00:30 +02:00
|
|
|
# define PL2303_SELFPOWERED (0)
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-20 18:00:30 +02:00
|
|
|
#ifdef CONFIG_USBDEV_REMOTEWAKEUP
|
|
|
|
# define PL2303_REMOTEWAKEUP USB_CONFIG_ATTR_WAKEUP
|
2008-10-07 01:12:09 +02:00
|
|
|
#else
|
2013-09-20 18:00:30 +02:00
|
|
|
# define PL2303_REMOTEWAKEUP (0)
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
|
2008-10-11 21:38:58 +02:00
|
|
|
#ifndef CONFIG_USBDEV_MAXPOWER
|
|
|
|
# define CONFIG_USBDEV_MAXPOWER 100
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Descriptors **************************************************************/
|
2008-10-11 21:38:58 +02:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
/* These settings are not modifiable via the NuttX configuration */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_VERSIONNO (0x0202) /* Device version number */
|
|
|
|
#define PL2303_CONFIGIDNONE (0) /* Config ID means to return to address mode */
|
|
|
|
#define PL2303_CONFIGID (1) /* The only supported configuration ID */
|
|
|
|
#define PL2303_NCONFIGS (1) /* Number of configurations supported */
|
|
|
|
#define PL2303_INTERFACEID (0)
|
|
|
|
#define PL2303_ALTINTERFACEID (0)
|
|
|
|
#define PL2303_NINTERFACES (1) /* Number of interfaces in the configuration */
|
|
|
|
#define PL2303_NENDPOINTS (3) /* Number of endpoints in the interface */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Endpoint configuration */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_EPINTIN_ADDR (USB_DIR_IN|CONFIG_PL2303_EPINTIN)
|
|
|
|
#define PL2303_EPINTIN_ATTR (USB_EP_ATTR_XFER_INT)
|
|
|
|
#define PL2303_EPINTIN_MXPACKET (10)
|
2008-10-09 14:49:11 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_EPOUTBULK_ADDR (CONFIG_PL2303_EPBULKOUT)
|
|
|
|
#define PL2303_EPOUTBULK_ATTR (USB_EP_ATTR_XFER_BULK)
|
2008-10-09 14:49:11 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_EPINBULK_ADDR (USB_DIR_IN|CONFIG_PL2303_EPBULKIN)
|
|
|
|
#define PL2303_EPINBULK_ATTR (USB_EP_ATTR_XFER_BULK)
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* String language */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_STR_LANGUAGE (0x0409) /* en-us */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Descriptor strings */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_MANUFACTURERSTRID (1)
|
|
|
|
#define PL2303_PRODUCTSTRID (2)
|
|
|
|
#define PL2303_SERIALSTRID (3)
|
|
|
|
#define PL2303_CONFIGSTRID (4)
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Buffer big enough for any of our descriptors */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_MXDESCLEN (64)
|
2015-05-13 14:33:17 +02:00
|
|
|
#define PL2303_MAXSTRLEN (PL2303_MXDESCLEN-2)
|
2008-10-11 21:38:58 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Vendor specific control requests *****************************************/
|
2008-10-11 21:38:58 +02:00
|
|
|
|
|
|
|
#define PL2303_CONTROL_TYPE (0x20)
|
|
|
|
#define PL2303_SETLINEREQUEST (0x20) /* OUT, Recipient interface */
|
|
|
|
#define PL2303_GETLINEREQUEST (0x21) /* IN, Recipient interface */
|
|
|
|
#define PL2303_SETCONTROLREQUEST (0x22) /* OUT, Recipient interface */
|
|
|
|
#define PL2303_BREAKREQUEST (0x23) /* OUT, Recipient interface */
|
|
|
|
|
|
|
|
/* Vendor read/write */
|
|
|
|
|
|
|
|
#define PL2303_RWREQUEST_TYPE (0x40)
|
|
|
|
#define PL2303_RWREQUEST (0x01) /* IN/OUT, Recipient device */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Misc Macros **************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* min/max macros */
|
|
|
|
|
|
|
|
#ifndef min
|
|
|
|
# define min(a,b) ((a)<(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef max
|
|
|
|
# define max(a,b) ((a)>(b)?(a):(b))
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Trace values *************************************************************/
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#define PL2303_CLASSAPI_SETUP TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SETUP)
|
|
|
|
#define PL2303_CLASSAPI_SHUTDOWN TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SHUTDOWN)
|
|
|
|
#define PL2303_CLASSAPI_ATTACH TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_ATTACH)
|
|
|
|
#define PL2303_CLASSAPI_DETACH TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_DETACH)
|
|
|
|
#define PL2303_CLASSAPI_IOCTL TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_IOCTL)
|
|
|
|
#define PL2303_CLASSAPI_RECEIVE TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RECEIVE)
|
|
|
|
#define PL2303_CLASSAPI_RXINT TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RXINT)
|
|
|
|
#define PL2303_CLASSAPI_RXAVAILABLE TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RXAVAILABLE)
|
|
|
|
#define PL2303_CLASSAPI_SEND TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SEND)
|
|
|
|
#define PL2303_CLASSAPI_TXINT TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXINT)
|
|
|
|
#define PL2303_CLASSAPI_TXREADY TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXREADY)
|
|
|
|
#define PL2303_CLASSAPI_TXEMPTY TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXEMPTY)
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Private Types
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Container to support a list of requests */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_req_s
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_req_s *flink; /* Implements a singly linked list */
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req; /* The contained request */
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes the internal state of the driver */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_dev_s
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct uart_dev_s serdev; /* Serial device structure */
|
|
|
|
FAR struct usbdev_s *usbdev; /* usbdev driver pointer */
|
2008-10-09 22:24:56 +02:00
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t config; /* Configuration number */
|
2015-10-10 18:41:00 +02:00
|
|
|
uint8_t nwrq; /* Number of queue write requests (in reqlist) */
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t nrdq; /* Number of queue read requests (in epbulkout) */
|
2011-04-07 23:22:06 +02:00
|
|
|
bool rxenabled; /* true: UART RX "interrupts" enabled */
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t linest[7]; /* Fake line status */
|
|
|
|
int16_t rxhead; /* Working head; used when rx int disabled */
|
2008-10-09 22:24:56 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
FAR struct usbdev_ep_s *epintin; /* Interrupt IN endpoint structure */
|
|
|
|
FAR struct usbdev_ep_s *epbulkin; /* Bulk IN endpoint structure */
|
|
|
|
FAR struct usbdev_ep_s *epbulkout; /* Bulk OUT endpoint structure */
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *ctrlreq; /* Control request */
|
|
|
|
struct sq_queue_s reqlist; /* List of write request containers */
|
|
|
|
|
|
|
|
/* Pre-allocated write request containers. The write requests will
|
|
|
|
* be linked in a free list (reqlist), and used to send requests to
|
|
|
|
* EPBULKIN; Read requests will be queued in the EBULKOUT.
|
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_req_s wrreqs[CONFIG_PL2303_NWRREQS];
|
|
|
|
struct pl2303_req_s rdreqs[CONFIG_PL2303_NWRREQS];
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Serial I/O buffers */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
char rxbuffer[CONFIG_PL2303_RXBUFSIZE];
|
|
|
|
char txbuffer[CONFIG_PL2303_TXBUFSIZE];
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The internal version of the class driver */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_driver_s
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
struct usbdevclass_driver_s drvr;
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *dev;
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is what is allocated */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_alloc_s
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
struct pl2303_dev_s dev;
|
|
|
|
struct pl2303_driver_s drvr;
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Private Function Prototypes
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Transfer helpers *********************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
static uint16_t usbclass_fillrequest(FAR struct pl2303_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t *reqbuf, uint16_t reqlen);
|
2012-01-26 00:04:17 +01:00
|
|
|
static int usbclass_sndpacket(FAR struct pl2303_dev_s *priv);
|
|
|
|
static inline int usbclass_recvpacket(FAR struct pl2303_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t *reqbuf, uint16_t reqlen);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Request helpers **********************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static struct usbdev_req_s *usbclass_allocreq(FAR struct usbdev_ep_s *ep,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t len);
|
2008-10-07 01:12:09 +02:00
|
|
|
static void usbclass_freereq(FAR struct usbdev_ep_s *ep,
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Configuration ************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static int usbclass_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc);
|
2008-10-07 01:12:09 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2010-08-19 01:27:36 +02:00
|
|
|
static void usbclass_mkepbulkdesc(const struct usb_epdesc_s *indesc,
|
2010-03-28 23:41:49 +02:00
|
|
|
uint16_t mxpacket, struct usb_epdesc_s *outdesc);
|
|
|
|
static int16_t usbclass_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type);
|
2008-10-07 01:12:09 +02:00
|
|
|
#else
|
2009-12-15 15:25:14 +01:00
|
|
|
static int16_t usbclass_mkcfgdesc(uint8_t *buf);
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
2012-01-26 00:04:17 +01:00
|
|
|
static void usbclass_resetconfig(FAR struct pl2303_dev_s *priv);
|
|
|
|
static int usbclass_setconfig(FAR struct pl2303_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t config);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Completion event handlers ************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
static void usbclass_ep0incomplete(FAR struct usbdev_ep_s *ep,
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req);
|
2008-10-10 18:44:26 +02:00
|
|
|
static void usbclass_rdcomplete(FAR struct usbdev_ep_s *ep,
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req);
|
2008-10-10 18:44:26 +02:00
|
|
|
static void usbclass_wrcomplete(FAR struct usbdev_ep_s *ep,
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* USB class device *********************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
static int usbclass_bind(FAR struct usbdevclass_driver_s *driver,
|
2012-01-26 18:42:44 +01:00
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbclass_unbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static int usbclass_setup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
|
|
|
|
size_t outlen);
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbclass_disconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
2013-01-31 20:20:26 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
static void usbclass_suspend(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbclass_resume(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Serial port **************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int usbser_setup(FAR struct uart_dev_s *dev);
|
|
|
|
static void usbser_shutdown(FAR struct uart_dev_s *dev);
|
|
|
|
static int usbser_attach(FAR struct uart_dev_s *dev);
|
|
|
|
static void usbser_detach(FAR struct uart_dev_s *dev);
|
|
|
|
static void usbser_rxint(FAR struct uart_dev_s *dev, bool enable);
|
|
|
|
static void usbser_txint(FAR struct uart_dev_s *dev, bool enable);
|
|
|
|
static bool usbser_txempty(FAR struct uart_dev_s *dev);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* USB class device *********************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
static const struct usbdevclass_driverops_s g_driverops =
|
|
|
|
{
|
|
|
|
usbclass_bind, /* bind */
|
|
|
|
usbclass_unbind, /* unbind */
|
|
|
|
usbclass_setup, /* setup */
|
|
|
|
usbclass_disconnect, /* disconnect */
|
2013-01-31 20:20:26 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
usbclass_suspend, /* suspend */
|
|
|
|
usbclass_resume, /* resume */
|
|
|
|
#else
|
2008-10-07 01:12:09 +02:00
|
|
|
NULL, /* suspend */
|
|
|
|
NULL, /* resume */
|
2013-01-31 20:20:26 +01:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Serial port **************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
static const struct uart_ops_s g_uartops =
|
|
|
|
{
|
2012-01-26 18:42:44 +01:00
|
|
|
usbser_setup, /* setup */
|
|
|
|
usbser_shutdown, /* shutdown */
|
|
|
|
usbser_attach, /* attach */
|
|
|
|
usbser_detach, /* detach */
|
2008-10-07 01:12:09 +02:00
|
|
|
NULL, /* ioctl */
|
|
|
|
NULL, /* receive */
|
2012-01-26 18:42:44 +01:00
|
|
|
usbser_rxint, /* rxinit */
|
2008-10-07 01:12:09 +02:00
|
|
|
NULL, /* rxavailable */
|
2014-05-08 17:00:33 +02:00
|
|
|
#ifdef CONFIG_SERIAL_IFLOWCONTROL
|
|
|
|
NULL, /* rxflowcontrol */
|
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
NULL, /* send */
|
2012-01-26 18:42:44 +01:00
|
|
|
usbser_txint, /* txinit */
|
2008-10-07 01:12:09 +02:00
|
|
|
NULL, /* txready */
|
2012-01-26 18:42:44 +01:00
|
|
|
usbser_txempty /* txempty */
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* USB descriptor templates these will be copied and modified ***************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
static const struct usb_devdesc_s g_devdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_DEVDESC, /* len */
|
|
|
|
USB_DESC_TYPE_DEVICE, /* type */
|
|
|
|
{LSBYTE(0x0200), MSBYTE(0x0200)}, /* usb */
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_PER_INTERFACE, /* classid */
|
2008-10-07 01:12:09 +02:00
|
|
|
0, /* subclass */
|
|
|
|
0, /* protocol */
|
2012-01-26 00:04:17 +01:00
|
|
|
CONFIG_PL2303_EP0MAXPACKET, /* maxpacketsize */
|
|
|
|
{ LSBYTE(CONFIG_PL2303_VENDORID), /* vendor */
|
|
|
|
MSBYTE(CONFIG_PL2303_VENDORID) },
|
|
|
|
{ LSBYTE(CONFIG_PL2303_PRODUCTID), /* product */
|
|
|
|
MSBYTE(CONFIG_PL2303_PRODUCTID) },
|
|
|
|
{ LSBYTE(PL2303_VERSIONNO), /* device */
|
|
|
|
MSBYTE(PL2303_VERSIONNO) },
|
|
|
|
PL2303_MANUFACTURERSTRID, /* imfgr */
|
|
|
|
PL2303_PRODUCTSTRID, /* iproduct */
|
|
|
|
PL2303_SERIALSTRID, /* serno */
|
|
|
|
PL2303_NCONFIGS /* nconfigs */
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_cfgdesc_s g_cfgdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_CFGDESC, /* len */
|
|
|
|
USB_DESC_TYPE_CONFIG, /* type */
|
|
|
|
{0, 0}, /* totallen -- to be provided */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_NINTERFACES, /* ninterfaces */
|
|
|
|
PL2303_CONFIGID, /* cfgvalue */
|
|
|
|
PL2303_CONFIGSTRID, /* icfg */
|
2013-09-20 18:00:30 +02:00
|
|
|
USB_CONFIG_ATTR_ONE | /* attr */
|
|
|
|
PL2303_SELFPOWERED |
|
|
|
|
PL2303_REMOTEWAKEUP,
|
2008-10-07 01:12:09 +02:00
|
|
|
(CONFIG_USBDEV_MAXPOWER + 1) / 2 /* mxpower */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_ifdesc_s g_ifdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_IFDESC, /* len */
|
|
|
|
USB_DESC_TYPE_INTERFACE, /* type */
|
|
|
|
0, /* ifno */
|
|
|
|
0, /* alt */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_NENDPOINTS, /* neps */
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_VENDOR_SPEC, /* classid */
|
2008-10-07 01:12:09 +02:00
|
|
|
0, /* subclass */
|
|
|
|
0, /* protocol */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_CONFIGSTRID /* iif */
|
2008-10-07 01:12:09 +02:00
|
|
|
};
|
|
|
|
|
2008-10-07 21:05:35 +02:00
|
|
|
static const struct usb_epdesc_s g_epintindesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_EPDESC, /* len */
|
|
|
|
USB_DESC_TYPE_ENDPOINT, /* type */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_EPINTIN_ADDR, /* addr */
|
|
|
|
PL2303_EPINTIN_ATTR, /* attr */
|
|
|
|
{ LSBYTE(PL2303_EPINTIN_MXPACKET), /* maxpacket */
|
|
|
|
MSBYTE(PL2303_EPINTIN_MXPACKET) },
|
2008-10-07 21:05:35 +02:00
|
|
|
1 /* interval */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_epdesc_s g_epbulkoutdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_EPDESC, /* len */
|
|
|
|
USB_DESC_TYPE_ENDPOINT, /* type */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_EPOUTBULK_ADDR, /* addr */
|
|
|
|
PL2303_EPOUTBULK_ATTR, /* attr */
|
2013-09-06 02:00:16 +02:00
|
|
|
{ LSBYTE(64), MSBYTE(64) }, /* maxpacket -- might change to 512 */
|
2008-10-07 21:05:35 +02:00
|
|
|
0 /* interval */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct usb_epdesc_s g_epbulkindesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_EPDESC, /* len */
|
|
|
|
USB_DESC_TYPE_ENDPOINT, /* type */
|
2012-01-26 00:04:17 +01:00
|
|
|
PL2303_EPINBULK_ADDR, /* addr */
|
|
|
|
PL2303_EPINBULK_ATTR, /* attr */
|
2013-09-06 02:00:16 +02:00
|
|
|
{ LSBYTE(64), MSBYTE(64) }, /* maxpacket -- might change to 512 */
|
2008-10-07 21:05:35 +02:00
|
|
|
0 /* interval */
|
|
|
|
};
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
static const struct usb_qualdesc_s g_qualdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_QUALDESC, /* len */
|
|
|
|
USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
|
|
|
|
{LSBYTE(0x0200), MSBYTE(0x0200) }, /* USB */
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_VENDOR_SPEC, /* classid */
|
2008-10-07 01:12:09 +02:00
|
|
|
0, /* subclass */
|
|
|
|
0, /* protocol */
|
2012-01-26 00:04:17 +01:00
|
|
|
CONFIG_PL2303_EP0MAXPACKET, /* mxpacketsize */
|
|
|
|
PL2303_NCONFIGS, /* nconfigs */
|
2008-10-07 01:12:09 +02:00
|
|
|
0, /* reserved */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Private Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-09 23:47:45 +02:00
|
|
|
* Name: usbclass_fillrequest
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* If there is data to send it is copied to the given buffer. Called either
|
|
|
|
* to initiate the first write operation, or from the completion interrupt
|
|
|
|
* handler service consecutive write operations.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
* NOTE:
|
|
|
|
* The USB serial driver does not use the serial drivers uart_xmitchars()
|
|
|
|
* API. That logic is essentially duplicated here because unlike UART
|
|
|
|
* hardware, we need to be able to handle writes not byte-by-byte, but
|
|
|
|
* packet-by-packet.
|
|
|
|
* Unfortunately, that decision also exposes some internals of the serial
|
|
|
|
* driver in the following.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
static uint16_t usbclass_fillrequest(FAR struct pl2303_dev_s *priv,
|
|
|
|
uint8_t *reqbuf,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t reqlen)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR uart_dev_t *serdev = &priv->serdev;
|
|
|
|
FAR struct uart_buffer_s *xmit = &serdev->xmit;
|
2008-10-07 01:12:09 +02:00
|
|
|
irqstate_t flags;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t nbytes = 0;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Disable interrupts */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Transfer bytes while we have bytes available and there is room in the
|
|
|
|
* request
|
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-09 23:47:45 +02:00
|
|
|
while (xmit->head != xmit->tail && nbytes < reqlen)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-09 23:47:45 +02:00
|
|
|
*reqbuf++ = xmit->buffer[xmit->tail];
|
2008-10-07 01:12:09 +02:00
|
|
|
nbytes++;
|
|
|
|
|
|
|
|
/* Increment the tail pointer */
|
|
|
|
|
|
|
|
if (++(xmit->tail) >= xmit->size)
|
|
|
|
{
|
|
|
|
xmit->tail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When all of the characters have been sent from the buffer
|
|
|
|
* disable the "TX interrupt".
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (xmit->head == xmit->tail)
|
|
|
|
{
|
|
|
|
uart_disabletxint(serdev);
|
|
|
|
}
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
/* If any bytes were removed from the buffer, inform any waiters
|
|
|
|
* there there is space available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nbytes)
|
|
|
|
{
|
|
|
|
uart_datasent(serdev);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_sndpacket
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* This function obtains write requests, transfers the TX data into the
|
|
|
|
* request, and submits the requests to the USB controller. This continues
|
|
|
|
* until either (1) there are no further packets available, or (2) there is
|
|
|
|
* not further data to send.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
static int usbclass_sndpacket(FAR struct pl2303_dev_s *priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_ep_s *ep;
|
|
|
|
FAR struct usbdev_req_s *req;
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_req_s *reqcontainer;
|
2012-05-26 00:10:40 +02:00
|
|
|
uint16_t reqlen;
|
2008-10-07 01:12:09 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int len;
|
|
|
|
int ret = OK;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Use our IN endpoint for the transfer */
|
|
|
|
|
|
|
|
ep = priv->epbulkin;
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Loop until either (1) we run out or write requests, or (2)
|
|
|
|
* usbclass_fillrequest() is unable to fill the request with data
|
|
|
|
* (i.e., until there is no more data to be sent).
|
2008-10-07 01:12:09 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("head=%d tail=%d nwrq=%d empty=%d\n",
|
2008-10-08 22:19:04 +02:00
|
|
|
priv->serdev.xmit.head, priv->serdev.xmit.tail,
|
|
|
|
priv->nwrq, sq_empty(&priv->reqlist));
|
|
|
|
|
2012-05-26 00:10:40 +02:00
|
|
|
/* Get the maximum number of bytes that will fit into one bulk IN request */
|
|
|
|
|
2013-05-29 20:30:37 +02:00
|
|
|
reqlen = max(CONFIG_PL2303_BULKIN_REQLEN, ep->maxpacket);
|
2012-05-26 00:10:40 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
while (!sq_empty(&priv->reqlist))
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
/* Peek at the request in the container at the head of the list */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
reqcontainer = (struct pl2303_req_s *)sq_peek(&priv->reqlist);
|
2008-10-08 01:19:27 +02:00
|
|
|
req = reqcontainer->req;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-09 23:47:45 +02:00
|
|
|
/* Fill the request with serial TX data */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-05-26 00:10:40 +02:00
|
|
|
len = usbclass_fillrequest(priv, req->buf, reqlen);
|
2008-10-07 01:12:09 +02:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2008-10-15 21:16:49 +02:00
|
|
|
/* Remove the empty container from the request list */
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
sq_remfirst(&priv->reqlist);
|
2008-10-08 22:19:04 +02:00
|
|
|
priv->nwrq--;
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
/* Then submit the request to the endpoint */
|
|
|
|
|
|
|
|
req->len = len;
|
2009-06-17 01:23:31 +02:00
|
|
|
req->priv = reqcontainer;
|
2008-10-25 16:40:37 +02:00
|
|
|
req->flags = USBDEV_REQFLAGS_NULLPKT;
|
2008-10-07 01:12:09 +02:00
|
|
|
ret = EP_SUBMIT(ep, req);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SUBMITFAIL),
|
|
|
|
(uint16_t) - ret);
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_recvpacket
|
|
|
|
*
|
|
|
|
* Description:
|
2021-03-04 08:02:21 +01:00
|
|
|
* A normal completion event was received by the read completion handler at
|
|
|
|
* the interrupt level (with interrupts disabled). This function handles
|
|
|
|
* the USB packet and provides the received data to the uart RX buffer.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2008-10-09 22:24:56 +02:00
|
|
|
* Assumptions:
|
|
|
|
* Called from the USB interrupt handler with interrupts disabled.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
static inline int usbclass_recvpacket(FAR struct pl2303_dev_s *priv,
|
2009-12-15 15:25:14 +01:00
|
|
|
uint8_t *reqbuf, uint16_t reqlen)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR uart_dev_t *serdev = &priv->serdev;
|
|
|
|
FAR struct uart_buffer_s *recv = &serdev->recv;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t currhead;
|
|
|
|
uint16_t nexthead;
|
|
|
|
uint16_t nbytes = 0;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Get the next head index. During the time that RX interrupts are
|
|
|
|
* disabled, the the serial driver will be extracting data from the
|
|
|
|
* circular buffer and modifying recv.tail. During this time, we should
|
|
|
|
* avoid modifying recv.head; Instead we will use a shadow copy of the
|
|
|
|
* index. When interrupts are restored, the real recv.head will be updated
|
|
|
|
* with this index.
|
2008-10-09 22:24:56 +02:00
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
if (priv->rxenabled)
|
|
|
|
{
|
|
|
|
currhead = recv->head;
|
|
|
|
}
|
|
|
|
else
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-09 22:24:56 +02:00
|
|
|
currhead = priv->rxhead;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Pre-calculate the head index and check for wrap around. We need to do
|
|
|
|
* this so that we can determine if the circular buffer will overrun BEFORE
|
|
|
|
* we overrun the buffer!
|
2008-10-17 19:53:46 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
nexthead = currhead + 1;
|
|
|
|
if (nexthead >= recv->size)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Then copy data into the RX buffer until either: (1) all of the data has
|
|
|
|
* been copied, or (2) the RX buffer is full.
|
|
|
|
* NOTE: If the RX buffer becomes full, then we have overrun the serial
|
|
|
|
* driver and data will be lost.
|
2008-10-07 01:12:09 +02:00
|
|
|
*/
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
while (nexthead != recv->tail && nbytes < reqlen)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
/* Copy one byte to the head of the circular RX buffer */
|
|
|
|
|
2008-10-09 23:47:45 +02:00
|
|
|
recv->buffer[currhead] = *reqbuf++;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Update counts and indices */
|
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
currhead = nexthead;
|
2008-11-18 00:22:27 +01:00
|
|
|
nbytes++;
|
2008-10-17 19:53:46 +02:00
|
|
|
|
|
|
|
/* Increment the head index and check for wrap around */
|
|
|
|
|
|
|
|
nexthead = currhead + 1;
|
|
|
|
if (nexthead >= recv->size)
|
|
|
|
{
|
|
|
|
nexthead = 0;
|
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2008-10-09 22:24:56 +02:00
|
|
|
|
|
|
|
/* Write back the head pointer using the shadow index if RX "interrupts"
|
|
|
|
* are disabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->rxenabled)
|
|
|
|
{
|
|
|
|
recv->head = currhead;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->rxhead = currhead;
|
|
|
|
}
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
/* If data was added to the incoming serial buffer, then wake up any
|
|
|
|
* threads is waiting for incoming data. If we are running in an interrupt
|
|
|
|
* handler, then the serial driver will not run until the interrupt handler
|
|
|
|
* returns.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->rxenabled && nbytes > 0)
|
|
|
|
{
|
|
|
|
uart_datareceived(serdev);
|
|
|
|
}
|
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
/* Return an error if the entire packet could not be transferred */
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
if (nbytes < reqlen)
|
2008-10-09 22:24:56 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RXOVERRUN), 0);
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_allocreq
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a request instance along with its buffer
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static struct usbdev_req_s *usbclass_allocreq(FAR struct usbdev_ep_s *ep,
|
|
|
|
uint16_t len)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
req = EP_ALLOCREQ(ep);
|
|
|
|
if (req != NULL)
|
|
|
|
{
|
|
|
|
req->len = len;
|
|
|
|
req->buf = EP_ALLOCBUFFER(ep, len);
|
|
|
|
if (!req->buf)
|
|
|
|
{
|
|
|
|
EP_FREEREQ(ep, req);
|
|
|
|
req = NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_freereq
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a request instance along with its buffer
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
static void usbclass_freereq(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
if (ep != NULL && req != NULL)
|
|
|
|
{
|
|
|
|
if (req->buf != NULL)
|
|
|
|
{
|
|
|
|
EP_FREEBUFFER(ep, req->buf);
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
EP_FREEREQ(ep, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_mkstrdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Construct a string descriptor
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2022-03-05 18:59:15 +01:00
|
|
|
static int usbclass_mkstrdesc(uint8_t id, FAR struct usb_strdesc_s *strdesc)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2022-03-05 18:59:15 +01:00
|
|
|
FAR uint8_t *data = (FAR uint8_t *)(strdesc + 1);
|
|
|
|
FAR const char *str;
|
2008-10-07 01:12:09 +02:00
|
|
|
int len;
|
|
|
|
int ndata;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
/* Descriptor 0 is the language id */
|
|
|
|
|
2022-03-05 18:59:15 +01:00
|
|
|
strdesc->len = 4;
|
|
|
|
strdesc->type = USB_DESC_TYPE_STRING;
|
|
|
|
data[0] = LSBYTE(PL2303_STR_LANGUAGE);
|
|
|
|
data[1] = MSBYTE(PL2303_STR_LANGUAGE);
|
2008-10-07 01:12:09 +02:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
case PL2303_MANUFACTURERSTRID:
|
|
|
|
str = CONFIG_PL2303_VENDORSTR;
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
case PL2303_PRODUCTSTRID:
|
|
|
|
str = CONFIG_PL2303_PRODUCTSTR;
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
case PL2303_SERIALSTRID:
|
2022-01-18 02:36:37 +01:00
|
|
|
#ifdef CONFIG_PL2303_BOARD_SERIALSTR
|
|
|
|
str = board_usbdev_serialstr();
|
|
|
|
#else
|
2012-01-26 00:04:17 +01:00
|
|
|
str = CONFIG_PL2303_SERIALSTR;
|
2022-01-18 02:36:37 +01:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
case PL2303_CONFIGSTRID:
|
|
|
|
str = CONFIG_PL2303_CONFIGSTR;
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
/* The string is utf16-le. The poor man's utf-8 to utf16-le
|
|
|
|
* conversion below will only handle 7-bit en-us ascii
|
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
len = strlen(str);
|
|
|
|
if (len > (PL2303_MAXSTRLEN / 2))
|
|
|
|
{
|
|
|
|
len = (PL2303_MAXSTRLEN / 2);
|
|
|
|
}
|
2015-05-12 22:46:35 +02:00
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
for (i = 0, ndata = 0; i < len; i++, ndata += 2)
|
|
|
|
{
|
2022-03-05 18:59:15 +01:00
|
|
|
data[ndata] = str[i];
|
|
|
|
data[ndata + 1] = 0;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
strdesc->len = ndata + 2;
|
|
|
|
strdesc->type = USB_DESC_TYPE_STRING;
|
|
|
|
return strdesc->len;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 21:05:35 +02:00
|
|
|
* Name: usbclass_mkepbulkdesc
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Construct the endpoint descriptor
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-07 21:05:35 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2021-03-04 08:02:21 +01:00
|
|
|
static inline void usbclass_mkepbulkdesc(
|
2022-04-04 12:46:23 +02:00
|
|
|
FAR const struct usb_epdesc_s *indesc,
|
2021-03-04 08:02:21 +01:00
|
|
|
uint16_t mxpacket,
|
|
|
|
FAR struct usb_epdesc_s *outdesc)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-07 21:05:35 +02:00
|
|
|
/* Copy the canned descriptor */
|
|
|
|
|
|
|
|
memcpy(outdesc, indesc, USB_SIZEOF_EPDESC);
|
|
|
|
|
|
|
|
/* Then add the correct max packet size */
|
|
|
|
|
|
|
|
outdesc->mxpacketsize[0] = LSBYTE(mxpacket);
|
|
|
|
outdesc->mxpacketsize[1] = MSBYTE(mxpacket);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2008-10-07 21:05:35 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_mkcfgdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Construct the configuration descriptor
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2010-03-28 23:41:49 +02:00
|
|
|
static int16_t usbclass_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type)
|
2008-10-07 01:12:09 +02:00
|
|
|
#else
|
2009-12-15 15:25:14 +01:00
|
|
|
static int16_t usbclass_mkcfgdesc(uint8_t *buf)
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct usb_cfgdesc_s *cfgdesc = (FAR struct usb_cfgdesc_s *)buf;
|
2008-10-07 01:12:09 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2010-03-28 23:41:49 +02:00
|
|
|
bool hispeed = (speed == USB_SPEED_HIGH);
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t bulkmxpacket;
|
2008-10-07 21:05:35 +02:00
|
|
|
#endif
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t totallen;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* This is the total length of the configuration (not necessarily the
|
|
|
|
* size that we will be sending now.
|
|
|
|
*/
|
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
totallen = USB_SIZEOF_CFGDESC + USB_SIZEOF_IFDESC +
|
|
|
|
PL2303_NENDPOINTS * USB_SIZEOF_EPDESC;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Configuration descriptor -- Copy the canned descriptor and fill in the
|
|
|
|
* type (we'll also need to update the size below
|
|
|
|
*/
|
|
|
|
|
|
|
|
memcpy(cfgdesc, &g_cfgdesc, USB_SIZEOF_CFGDESC);
|
|
|
|
buf += USB_SIZEOF_CFGDESC;
|
|
|
|
|
2020-01-31 19:07:39 +01:00
|
|
|
/* Copy the canned interface descriptor */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
memcpy(buf, &g_ifdesc, USB_SIZEOF_IFDESC);
|
|
|
|
buf += USB_SIZEOF_IFDESC;
|
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
/* Make the three endpoint configurations. First, check for switches
|
2008-10-07 01:12:09 +02:00
|
|
|
* between high and full speed
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
if (type == USB_DESC_TYPE_OTHERSPEEDCONFIG)
|
|
|
|
{
|
|
|
|
hispeed = !hispeed;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-07 21:05:35 +02:00
|
|
|
memcpy(buf, &g_epintindesc, USB_SIZEOF_EPDESC);
|
|
|
|
buf += USB_SIZEOF_EPDESC;
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
if (hispeed)
|
|
|
|
{
|
|
|
|
bulkmxpacket = 512;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bulkmxpacket = 64;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
usbclass_mkepbulkdesc(&g_epbulkoutdesc, bulkmxpacket,
|
|
|
|
(FAR struct usb_epdesc_s *)buf);
|
2008-10-07 01:12:09 +02:00
|
|
|
buf += USB_SIZEOF_EPDESC;
|
2021-03-04 08:02:21 +01:00
|
|
|
usbclass_mkepbulkdesc(&g_epbulkindesc, bulkmxpacket,
|
|
|
|
(FAR struct usb_epdesc_s *)buf);
|
2008-10-07 21:05:35 +02:00
|
|
|
#else
|
|
|
|
memcpy(buf, &g_epbulkoutdesc, USB_SIZEOF_EPDESC);
|
2008-10-07 01:12:09 +02:00
|
|
|
buf += USB_SIZEOF_EPDESC;
|
2008-10-07 21:05:35 +02:00
|
|
|
memcpy(buf, &g_epbulkindesc, USB_SIZEOF_EPDESC);
|
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Finally, fill in the total size of the configuration descriptor */
|
|
|
|
|
|
|
|
cfgdesc->totallen[0] = LSBYTE(totallen);
|
|
|
|
cfgdesc->totallen[1] = MSBYTE(totallen);
|
|
|
|
return totallen;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_resetconfig
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark the device as not configured and disable all endpoints.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
static void usbclass_resetconfig(FAR struct pl2303_dev_s *priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
/* Are we configured? */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
if (priv->config != PL2303_CONFIGIDNONE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Yes.. but not anymore */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
priv->config = PL2303_CONFIGIDNONE;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Inform the "upper half" driver that there is no (functional) USB
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
uart_connected(&priv->serdev, false);
|
|
|
|
#endif
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Disable endpoints. This should force completion of all pending
|
2008-10-07 01:12:09 +02:00
|
|
|
* transfers.
|
|
|
|
*/
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
EP_DISABLE(priv->epintin);
|
|
|
|
EP_DISABLE(priv->epbulkin);
|
|
|
|
EP_DISABLE(priv->epbulkout);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_setconfig
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the device configuration by allocating and configuring endpoints and
|
|
|
|
* by allocating and queue read and write requests.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
static int usbclass_setconfig(FAR struct pl2303_dev_s *priv, uint8_t config)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *req;
|
2008-10-07 21:05:35 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2008-10-07 01:12:09 +02:00
|
|
|
struct usb_epdesc_s epdesc;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t bulkmxpacket;
|
2008-10-07 21:05:35 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (priv == NULL)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return -EIO;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (config == priv->config)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
/* Already configured -- Do nothing */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALREADYCONFIGURED), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Discard the previous configuration data */
|
|
|
|
|
|
|
|
usbclass_resetconfig(priv);
|
|
|
|
|
|
|
|
/* Was this a request to simply discard the current configuration? */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
if (config == PL2303_CONFIGIDNONE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONFIGNONE), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-08 01:09:33 +02:00
|
|
|
/* We only accept one configuration */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
if (config != PL2303_CONFIGID)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONFIGIDBAD), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the IN interrupt endpoint */
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
ret = EP_CONFIGURE(priv->epintin, &g_epintindesc, false);
|
2008-10-07 01:12:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPINTINCONFIGFAIL), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epintin->priv = priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Configure the IN bulk endpoint */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2010-08-19 01:27:36 +02:00
|
|
|
if (priv->usbdev->speed == USB_SPEED_HIGH)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
bulkmxpacket = 512;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bulkmxpacket = 64;
|
|
|
|
}
|
|
|
|
|
2008-10-07 21:05:35 +02:00
|
|
|
usbclass_mkepbulkdesc(&g_epbulkindesc, bulkmxpacket, &epdesc);
|
2009-12-15 15:25:14 +01:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkin, &epdesc, false);
|
2008-10-07 21:05:35 +02:00
|
|
|
#else
|
2009-12-15 15:25:14 +01:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkin, &g_epbulkindesc, false);
|
2008-10-07 21:05:35 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINCONFIGFAIL), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epbulkin->priv = priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Configure the OUT bulk endpoint */
|
|
|
|
|
2008-10-07 21:05:35 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
usbclass_mkepbulkdesc(&g_epbulkoutdesc, bulkmxpacket, &epdesc);
|
2009-12-15 15:25:14 +01:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkout, &epdesc, true);
|
2008-10-07 21:05:35 +02:00
|
|
|
#else
|
2009-12-15 15:25:14 +01:00
|
|
|
ret = EP_CONFIGURE(priv->epbulkout, &g_epbulkoutdesc, true);
|
2008-10-07 21:05:35 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKOUTCONFIGFAIL), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epbulkout->priv = priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Queue read requests in the bulk OUT endpoint */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
DEBUGASSERT(priv->nrdq == 0);
|
2012-01-26 00:04:17 +01:00
|
|
|
for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
req = priv->rdreqs[i].req;
|
2008-10-10 18:44:26 +02:00
|
|
|
req->callback = usbclass_rdcomplete;
|
2008-10-08 22:19:04 +02:00
|
|
|
ret = EP_SUBMIT(priv->epbulkout, req);
|
|
|
|
if (ret != OK)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSUBMIT), (uint16_t)-ret);
|
2008-10-08 22:19:04 +02:00
|
|
|
goto errout;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2013-01-31 17:52:20 +01:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
priv->nrdq++;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* We are successfully configured */
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
priv->config = config;
|
2013-01-31 17:52:20 +01:00
|
|
|
|
|
|
|
/* Inform the "upper half" driver that we are "open for business" */
|
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
uart_connected(&priv->serdev, true);
|
|
|
|
#endif
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
usbclass_resetconfig(priv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-09 22:24:56 +02:00
|
|
|
* Name: usbclass_ep0incomplete
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of EP0 control operations
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
static void usbclass_ep0incomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
if (req->result || req->xfrd != req->len)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_REQRESULT),
|
|
|
|
(uint16_t)-req->result);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-10 18:44:26 +02:00
|
|
|
* Name: usbclass_rdcomplete
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2008-10-09 22:24:56 +02:00
|
|
|
* Handle completion of read request on the bulk OUT endpoint. This
|
|
|
|
* is handled like the receipt of serial data on the "UART"
|
2008-10-10 18:44:26 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
static void usbclass_rdcomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-09 22:24:56 +02:00
|
|
|
irqstate_t flags;
|
2008-10-07 01:12:09 +02:00
|
|
|
int ret;
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2009-06-17 01:23:31 +02:00
|
|
|
if (!ep || !ep->priv || !req)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-08 22:19:04 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Extract references to private data */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = (FAR struct pl2303_dev_s *)ep->priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Process the received data unless this is some unusual condition */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-10-08 22:19:04 +02:00
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case 0: /* Normal completion */
|
|
|
|
usbtrace(TRACE_CLASSRDCOMPLETE, priv->nrdq);
|
|
|
|
usbclass_recvpacket(priv, req->buf, req->xfrd);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSHUTDOWN), 0);
|
|
|
|
priv->nrdq--;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-08 22:19:04 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDUNEXPECTED),
|
|
|
|
(uint16_t)-req->result);
|
2008-10-08 22:19:04 +02:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Requeue the read request */
|
|
|
|
|
|
|
|
req->len = ep->maxpacket;
|
|
|
|
ret = EP_SUBMIT(ep, req);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSUBMIT),
|
|
|
|
(uint16_t)-req->result);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-10 18:44:26 +02:00
|
|
|
* Name: usbclass_wrcomplete
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of write request. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
static void usbclass_wrcomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
|
|
|
FAR struct pl2303_req_s *reqcontainer;
|
2008-10-07 01:12:09 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2009-06-17 01:23:31 +02:00
|
|
|
if (!ep || !ep->priv || !req || !req->priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 22:19:04 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-08 22:19:04 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Extract references to our private data */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
priv = (FAR struct pl2303_dev_s *)ep->priv;
|
|
|
|
reqcontainer = (FAR struct pl2303_req_s *)req->priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Return the write request to the free list */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-10 18:41:00 +02:00
|
|
|
sq_addlast((FAR sq_entry_t *)reqcontainer, &priv->reqlist);
|
2008-10-08 22:19:04 +02:00
|
|
|
priv->nwrq++;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Send the next packet unless this was some unusual termination
|
|
|
|
* condition
|
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case OK: /* Normal completion */
|
|
|
|
usbtrace(TRACE_CLASSWRCOMPLETE, priv->nwrq);
|
|
|
|
usbclass_sndpacket(priv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRSHUTDOWN), priv->nwrq);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRUNEXPECTED),
|
|
|
|
(uint16_t)-req->result);
|
2008-10-08 22:19:04 +02:00
|
|
|
break;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* USB Class Driver Methods
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is bound to a USB device driver
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int usbclass_bind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
FAR struct pl2303_dev_s *priv =
|
|
|
|
((FAR struct pl2303_driver_s *)driver)->dev;
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_req_s *reqcontainer;
|
2008-10-08 22:19:04 +02:00
|
|
|
irqstate_t flags;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t reqlen;
|
2008-10-08 22:19:04 +02:00
|
|
|
int ret;
|
|
|
|
int i;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSBIND, 0);
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Bind the structures */
|
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->usbdev = dev;
|
2012-01-26 18:42:44 +01:00
|
|
|
|
|
|
|
/* Save the reference to our private data structure in EP0 so that it
|
2013-01-10 18:37:29 +01:00
|
|
|
* can be recovered in ep0 completion events (Unless we are part of
|
2012-01-26 18:42:44 +01:00
|
|
|
* a composite device and, in that case, the composite device owns
|
|
|
|
* EP0).
|
|
|
|
*/
|
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
dev->ep0->priv = priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
/* Preallocate control request */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
priv->ctrlreq = usbclass_allocreq(dev->ep0, PL2303_MXDESCLEN);
|
2008-10-07 01:12:09 +02:00
|
|
|
if (priv->ctrlreq == NULL)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCCTRLREQ), 0);
|
2008-10-08 22:19:04 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
priv->ctrlreq->callback = usbclass_ep0incomplete;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Pre-allocate all endpoints... the endpoints will not be functional
|
|
|
|
* until the SET CONFIGURATION request is processed in usbclass_setconfig.
|
2014-09-01 01:26:36 +02:00
|
|
|
* This is done here because there may be calls to kmm_malloc and the SET
|
2019-10-08 16:01:30 +02:00
|
|
|
* CONFIGURATION processing probably occurs within interrupt handling
|
2014-09-01 01:26:36 +02:00
|
|
|
* logic where kmm_malloc calls will fail.
|
2008-10-08 22:19:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pre-allocate the IN interrupt endpoint */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
priv->epintin = DEV_ALLOCEP(dev, PL2303_EPINTIN_ADDR,
|
|
|
|
true, USB_EP_ATTR_XFER_INT);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!priv->epintin)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPINTINALLOCFAIL), 0);
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epintin->priv = priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Pre-allocate the IN bulk endpoint */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
priv->epbulkin = DEV_ALLOCEP(dev, PL2303_EPINBULK_ADDR,
|
|
|
|
true, USB_EP_ATTR_XFER_BULK);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!priv->epbulkin)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINALLOCFAIL), 0);
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epbulkin->priv = priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Pre-allocate the OUT bulk endpoint */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
priv->epbulkout = DEV_ALLOCEP(dev, PL2303_EPOUTBULK_ADDR,
|
|
|
|
false, USB_EP_ATTR_XFER_BULK);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!priv->epbulkout)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKOUTALLOCFAIL), 0);
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto errout;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
priv->epbulkout->priv = priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2013-09-06 02:00:16 +02:00
|
|
|
/* Pre-allocate read requests. The buffer size is one full packet. */
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2013-09-06 02:00:16 +02:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
reqlen = 512;
|
|
|
|
#else
|
|
|
|
reqlen = 64;
|
|
|
|
#endif
|
2008-10-09 23:47:45 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
|
2008-10-08 22:19:04 +02:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->rdreqs[i];
|
2008-10-09 23:47:45 +02:00
|
|
|
reqcontainer->req = usbclass_allocreq(priv->epbulkout, reqlen);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (reqcontainer->req == NULL)
|
|
|
|
{
|
2009-10-29 20:25:42 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDALLOCREQ), -ENOMEM);
|
2008-10-08 22:19:04 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-05-29 20:30:37 +02:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
reqcontainer->req->priv = reqcontainer;
|
2008-10-10 18:44:26 +02:00
|
|
|
reqcontainer->req->callback = usbclass_rdcomplete;
|
2008-10-08 22:19:04 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 02:00:16 +02:00
|
|
|
/* Pre-allocate write request containers and put in a free list.
|
|
|
|
* The buffer size should be larger than a full packet. Otherwise,
|
|
|
|
* we will send a bogus null packet at the end of each packet.
|
|
|
|
*
|
|
|
|
* Pick the larger of the max packet size and the configured request
|
|
|
|
* size.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
reqlen = 512;
|
|
|
|
#else
|
|
|
|
reqlen = 64;
|
|
|
|
#endif
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2013-09-06 02:00:16 +02:00
|
|
|
if (CONFIG_PL2303_BULKIN_REQLEN > reqlen)
|
|
|
|
{
|
|
|
|
reqlen = CONFIG_CDCACM_BULKIN_REQLEN;
|
|
|
|
}
|
2008-10-09 23:47:45 +02:00
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
for (i = 0; i < CONFIG_PL2303_NWRREQS; i++)
|
2008-10-08 22:19:04 +02:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->wrreqs[i];
|
2008-10-09 23:47:45 +02:00
|
|
|
reqcontainer->req = usbclass_allocreq(priv->epbulkin, reqlen);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (reqcontainer->req == NULL)
|
|
|
|
{
|
2009-10-29 20:25:42 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRALLOCREQ), -ENOMEM);
|
2008-10-08 22:19:04 +02:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-05-29 20:30:37 +02:00
|
|
|
|
2009-06-17 01:23:31 +02:00
|
|
|
reqcontainer->req->priv = reqcontainer;
|
2008-10-10 18:44:26 +02:00
|
|
|
reqcontainer->req->callback = usbclass_wrcomplete;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-10 18:41:00 +02:00
|
|
|
sq_addlast((FAR sq_entry_t *)reqcontainer, &priv->reqlist);
|
2008-10-08 22:19:04 +02:00
|
|
|
priv->nwrq++; /* Count of write requests available */
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-08 22:19:04 +02:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
/* Report if we are selfpowered */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_SELFPOWERED
|
|
|
|
DEV_SETSELFPOWERED(dev);
|
|
|
|
#endif
|
2010-12-04 18:35:19 +01:00
|
|
|
|
|
|
|
/* And pull-up the data line for the soft connect function */
|
|
|
|
|
|
|
|
DEV_CONNECT(dev);
|
2008-10-07 01:12:09 +02:00
|
|
|
return OK;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
errout:
|
2012-01-26 18:42:44 +01:00
|
|
|
usbclass_unbind(driver, dev);
|
2008-10-08 22:19:04 +02:00
|
|
|
return ret;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_unbind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is unbound from a USB device driver
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbclass_unbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
|
|
|
FAR struct pl2303_req_s *reqcontainer;
|
2008-10-08 22:19:04 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSUNBIND, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev || !dev->ep0)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct pl2303_driver_s *)driver)->dev;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Make sure that we are not already unbound */
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
if (priv != NULL)
|
|
|
|
{
|
2008-10-18 01:07:39 +02:00
|
|
|
/* Make sure that the endpoints have been unconfigured. If
|
|
|
|
* we were terminated gracefully, then the configuration should
|
|
|
|
* already have been reset. If not, then calling usbclass_resetconfig
|
|
|
|
* should cause the endpoints to immediately terminate all
|
|
|
|
* transfers and return the requests to us (with result == -ESHUTDOWN)
|
|
|
|
*/
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2008-10-18 01:07:39 +02:00
|
|
|
usbclass_resetconfig(priv);
|
|
|
|
up_mdelay(50);
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2008-10-18 01:07:39 +02:00
|
|
|
/* Free the interrupt IN endpoint */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
if (priv->epintin)
|
|
|
|
{
|
|
|
|
DEV_FREEEP(dev, priv->epintin);
|
|
|
|
priv->epintin = NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-18 01:07:39 +02:00
|
|
|
/* Free the bulk IN endpoint */
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
if (priv->epbulkin)
|
|
|
|
{
|
|
|
|
DEV_FREEEP(dev, priv->epbulkin);
|
|
|
|
priv->epbulkin = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the pre-allocated control request */
|
|
|
|
|
|
|
|
if (priv->ctrlreq != NULL)
|
|
|
|
{
|
|
|
|
usbclass_freereq(dev->ep0, priv->ctrlreq);
|
2008-10-20 22:42:27 +02:00
|
|
|
priv->ctrlreq = NULL;
|
2008-10-08 22:19:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free pre-allocated read requests (which should all have
|
|
|
|
* been returned to the free list at this time -- we don't check)
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->nrdq == 0);
|
2012-01-26 00:04:17 +01:00
|
|
|
for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
|
2008-10-08 22:19:04 +02:00
|
|
|
{
|
|
|
|
reqcontainer = &priv->rdreqs[i];
|
|
|
|
if (reqcontainer->req)
|
|
|
|
{
|
|
|
|
usbclass_freereq(priv->epbulkout, reqcontainer->req);
|
|
|
|
reqcontainer->req = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the bulk OUT endpoint */
|
|
|
|
|
|
|
|
if (priv->epbulkout)
|
|
|
|
{
|
|
|
|
DEV_FREEEP(dev, priv->epbulkout);
|
|
|
|
priv->epbulkout = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free write requests that are not in use (which should be all
|
|
|
|
* of them
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-26 00:04:17 +01:00
|
|
|
DEBUGASSERT(priv->nwrq == CONFIG_PL2303_NWRREQS);
|
2008-10-08 22:19:04 +02:00
|
|
|
while (!sq_empty(&priv->reqlist))
|
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
reqcontainer = (struct pl2303_req_s *)sq_remfirst(&priv->reqlist);
|
2008-10-08 22:19:04 +02:00
|
|
|
if (reqcontainer->req != NULL)
|
|
|
|
{
|
|
|
|
usbclass_freereq(priv->epbulkin, reqcontainer->req);
|
|
|
|
priv->nwrq--; /* Number of write requests queued */
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
DEBUGASSERT(priv->nwrq == 0);
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Clear out all data in the circular buffer */
|
|
|
|
|
|
|
|
priv->serdev.xmit.head = 0;
|
|
|
|
priv->serdev.xmit.tail = 0;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked for ep0 control requests. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int usbclass_setup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR uint8_t *dataout, size_t outlen)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
FAR struct usbdev_req_s *ctrlreq;
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t value;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t len;
|
2008-10-07 01:12:09 +02:00
|
|
|
int ret = -EOPNOTSUPP;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev || !dev->ep0 || !ctrl)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
2008-10-08 01:09:33 +02:00
|
|
|
return -EIO;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
usbtrace(TRACE_CLASSSETUP, ctrl->req);
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct pl2303_driver_s *)driver)->dev;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (!priv || !priv->ctrlreq)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
ctrlreq = priv->ctrlreq;
|
|
|
|
|
|
|
|
/* Extract the little-endian 16-bit values to host order */
|
|
|
|
|
|
|
|
value = GETUINT16(ctrl->value);
|
|
|
|
index = GETUINT16(ctrl->index);
|
|
|
|
len = GETUINT16(ctrl->len);
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
|
2008-10-07 01:12:09 +02:00
|
|
|
ctrl->type, ctrl->req, value, index, len);
|
|
|
|
|
|
|
|
switch (ctrl->type & USB_REQ_TYPE_MASK)
|
|
|
|
{
|
2020-03-11 21:44:00 +01:00
|
|
|
/* Standard Requests */
|
2008-10-17 19:53:46 +02:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
case USB_REQ_TYPE_STANDARD:
|
|
|
|
{
|
|
|
|
switch (ctrl->req)
|
|
|
|
{
|
|
|
|
case USB_REQ_GETDESCRIPTOR:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* The value field specifies the descriptor type in the MS byte
|
|
|
|
* and the descriptor index in the LS byte
|
|
|
|
* (order is little endian)
|
2008-10-07 01:12:09 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
switch (ctrl->value[1])
|
|
|
|
{
|
|
|
|
case USB_DESC_TYPE_DEVICE:
|
|
|
|
{
|
|
|
|
ret = USB_SIZEOF_DEVDESC;
|
|
|
|
memcpy(ctrlreq->buf, &g_devdesc, ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
case USB_DESC_TYPE_DEVICEQUALIFIER:
|
|
|
|
{
|
|
|
|
ret = USB_SIZEOF_QUALDESC;
|
|
|
|
memcpy(ctrlreq->buf, &g_qualdesc, ret);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_OTHERSPEEDCONFIG:
|
|
|
|
#endif /* CONFIG_USBDEV_DUALSPEED */
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_CONFIG:
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2021-03-04 08:02:21 +01:00
|
|
|
ret = usbclass_mkcfgdesc(ctrlreq->buf,
|
|
|
|
dev->speed, ctrl->req);
|
2008-10-07 01:12:09 +02:00
|
|
|
#else
|
|
|
|
ret = usbclass_mkcfgdesc(ctrlreq->buf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_STRING:
|
|
|
|
{
|
|
|
|
/* index == language code. */
|
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
ret = usbclass_mkstrdesc(ctrl->value[0],
|
|
|
|
(struct usb_strdesc_s *)ctrlreq->buf);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_GETUNKNOWNDESC),
|
|
|
|
value);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == 0)
|
|
|
|
{
|
|
|
|
ret = usbclass_setconfig(priv, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == USB_DIR_IN)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
*(FAR uint8_t *)ctrlreq->buf = priv->config;
|
2008-10-07 01:12:09 +02:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SETINTERFACE:
|
|
|
|
{
|
|
|
|
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE)
|
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
if (priv->config == PL2303_CONFIGID &&
|
|
|
|
index == PL2303_INTERFACEID &&
|
|
|
|
value == PL2303_ALTINTERFACEID)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbclass_resetconfig(priv);
|
|
|
|
usbclass_setconfig(priv, priv->config);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GETINTERFACE:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
if (ctrl->type == (USB_DIR_IN | USB_REQ_RECIPIENT_INTERFACE) &&
|
2012-01-26 00:04:17 +01:00
|
|
|
priv->config == PL2303_CONFIGIDNONE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
if (index != PL2303_INTERFACEID)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
ret = -EDOM;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
*(FAR uint8_t *) ctrlreq->buf = PL2303_ALTINTERFACEID;
|
2008-10-07 01:12:09 +02:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDSTDREQ),
|
|
|
|
ctrl->req);
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-03-11 21:44:00 +01:00
|
|
|
/* PL2303 Vendor-Specific Requests */
|
2008-10-17 19:53:46 +02:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
case PL2303_CONTROL_TYPE:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
if ((ctrl->type & USB_REQ_RECIPIENT_MASK) ==
|
|
|
|
USB_REQ_RECIPIENT_INTERFACE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
switch (ctrl->req)
|
|
|
|
{
|
|
|
|
case PL2303_SETLINEREQUEST:
|
|
|
|
{
|
|
|
|
memcpy(priv->linest, ctrlreq->buf, min(len, 7));
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL2303_GETLINEREQUEST:
|
|
|
|
{
|
|
|
|
memcpy(ctrlreq->buf, priv->linest, 7);
|
|
|
|
ret = 7;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL2303_SETCONTROLREQUEST:
|
|
|
|
case PL2303_BREAKREQUEST:
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDCLASSREQ),
|
|
|
|
ctrl->type);
|
2008-10-07 01:12:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PL2303_RWREQUEST_TYPE:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
if ((ctrl->type & USB_REQ_RECIPIENT_MASK) ==
|
|
|
|
USB_REQ_RECIPIENT_DEVICE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
if (ctrl->req == PL2303_RWREQUEST)
|
|
|
|
{
|
|
|
|
if ((ctrl->type & USB_DIR_IN) != 0)
|
|
|
|
{
|
2020-03-11 21:44:00 +01:00
|
|
|
if (ctrl->value[0] == 0x84)
|
|
|
|
{
|
|
|
|
*ctrlreq->buf = 0x02;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else if (ctrl->value[0] == 0x94)
|
|
|
|
{
|
|
|
|
*ctrlreq->buf = 0x00;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else if (ctrl->value[0] == 0x83)
|
|
|
|
{
|
|
|
|
*ctrlreq->buf = 0xff;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else if (ctrl->value[0] == 0x86)
|
|
|
|
{
|
|
|
|
*ctrlreq->buf = 0xaa;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(FAR uint32_t *)ctrlreq->buf = 0xdeadbe02;
|
|
|
|
ret = 4;
|
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDCLASSREQ),
|
|
|
|
ctrl->type);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDTYPE), ctrl->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-10-17 19:53:46 +02:00
|
|
|
/* Respond to the setup command if data was returned. On an error return
|
|
|
|
* value (ret < 0), the USB driver will stall.
|
|
|
|
*/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2008-10-25 16:40:37 +02:00
|
|
|
ctrlreq->len = min(len, ret);
|
|
|
|
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
|
|
|
|
ret = EP_SUBMIT(dev->ep0, ctrlreq);
|
2008-10-07 01:12:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPRESPQ), (uint16_t)-ret);
|
2008-10-07 01:12:09 +02:00
|
|
|
ctrlreq->result = OK;
|
2008-10-09 22:24:56 +02:00
|
|
|
usbclass_ep0incomplete(dev->ep0, ctrlreq);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbclass_disconnect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked after all transfers have been stopped, when the host is
|
|
|
|
* disconnected. This function is probably called from the context of an
|
|
|
|
* interrupt handler.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbclass_disconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSDISCONNECT, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev || !dev->ep0)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
#endif
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct pl2303_driver_s *)driver)->dev;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Inform the "upper half serial driver that we have lost the USB serial
|
|
|
|
* connection.
|
|
|
|
*/
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
uart_connected(&priv->serdev, false);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Reset the configuration */
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
usbclass_resetconfig(priv);
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
/* Clear out all outgoing data in the circular buffer */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
priv->serdev.xmit.head = 0;
|
|
|
|
priv->serdev.xmit.tail = 0;
|
2014-06-06 18:34:58 +02:00
|
|
|
priv->rxhead = 0;
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2010-12-04 18:35:19 +01:00
|
|
|
|
|
|
|
/* Perform the soft connect function so that we will we can be
|
|
|
|
* re-enumerated.
|
|
|
|
*/
|
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
DEV_CONNECT(dev);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2013-01-31 20:20:26 +01:00
|
|
|
* Name: usbclass_suspend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the USB suspend event.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
static void usbclass_suspend(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
2013-04-12 16:38:52 +02:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSSUSPEND, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2013-01-31 20:20:26 +01:00
|
|
|
if (!driver || !dev)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2013-01-31 20:20:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct pl2303_driver_s *)driver)->dev;
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
/* And let the "upper half" driver now that we are suspended */
|
|
|
|
|
|
|
|
uart_connected(&priv->serdev, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2013-01-31 20:20:26 +01:00
|
|
|
* Name: usbclass_resume
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the USB resume event.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
static void usbclass_resume(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
2013-04-12 16:38:52 +02:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSRESUME, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2013-01-31 20:20:26 +01:00
|
|
|
if (!driver || !dev)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-03-11 21:44:00 +01:00
|
|
|
}
|
2013-01-31 20:20:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct pl2303_driver_s *)driver)->dev;
|
2013-01-31 20:20:26 +01:00
|
|
|
|
|
|
|
/* Are we still configured? */
|
|
|
|
|
|
|
|
if (priv->config != PL2303_CONFIGIDNONE)
|
|
|
|
{
|
|
|
|
/* Yes.. let the "upper half" know that have resumed */
|
|
|
|
|
|
|
|
uart_connected(&priv->serdev, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Serial Device Methods
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_setup
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This method is called the first time that the serial port is opened.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int usbser_setup(FAR struct uart_dev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_SETUP, 0);
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!dev || !dev->priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return -EIO;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2008-10-08 01:09:33 +02:00
|
|
|
#endif
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = (FAR struct pl2303_dev_s *)dev->priv;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
|
|
|
/* Check if we have been configured */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
if (priv->config == PL2303_CONFIGIDNONE)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SETUPNOTCONNECTED), 0);
|
2008-10-08 01:09:33 +02:00
|
|
|
return -ENOTCONN;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_shutdown
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This method is called when the serial port is closed. This operation
|
|
|
|
* is very simple for the USB serial backend because the serial driver
|
|
|
|
* has already assured that the TX data has full drained -- it calls
|
2012-01-26 18:42:44 +01:00
|
|
|
* usbser_txempty() until that function returns true before calling this
|
2008-10-07 01:12:09 +02:00
|
|
|
* function.
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbser_shutdown(FAR struct uart_dev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_SHUTDOWN, 0);
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!dev || !dev->priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_attach
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Does not apply to the USB serial class device
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int usbser_attach(FAR struct uart_dev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_ATTACH, 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_detach
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-10-10 18:41:00 +02:00
|
|
|
* Does not apply to the USB serial class device
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbser_detach(FAR struct uart_dev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_DETACH, 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_rxint
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called by the serial driver to enable or disable RX interrupts. We, of
|
|
|
|
* course, have no RX interrupts but must behave consistently. This method
|
|
|
|
* is called under the conditions:
|
|
|
|
*
|
2012-01-26 18:42:44 +01:00
|
|
|
* 1. With enable==true when the port is opened (just after usbser_setup
|
|
|
|
* and usbser_attach are called called)
|
2009-12-15 15:25:14 +01:00
|
|
|
* 2. With enable==false while transferring data from the RX buffer
|
|
|
|
* 2. With enable==true while waiting for more incoming data
|
2012-01-26 18:42:44 +01:00
|
|
|
* 3. With enable==false when the port is closed (just before usbser_detach
|
|
|
|
* and usbser_shutdown are called).
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbser_rxint(FAR struct uart_dev_s *dev, bool enable)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-09 22:24:56 +02:00
|
|
|
FAR uart_dev_t *serdev;
|
|
|
|
irqstate_t flags;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_RXINT, (uint16_t)enable);
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!dev || !dev->priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = (FAR struct pl2303_dev_s *)dev->priv;
|
2008-10-09 22:24:56 +02:00
|
|
|
serdev = &priv->serdev;
|
2008-10-08 22:19:04 +02:00
|
|
|
|
2008-10-09 22:24:56 +02:00
|
|
|
/* We need exclusive access to the RX buffer and private structure
|
|
|
|
* in the following.
|
2008-10-07 01:12:09 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-10-07 01:12:09 +02:00
|
|
|
if (enable)
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
/* RX "interrupts" are enabled. Is this a transition from disabled
|
|
|
|
* to enabled state?
|
|
|
|
*/
|
2008-10-09 22:24:56 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if (!priv->rxenabled)
|
|
|
|
{
|
|
|
|
/* Yes. During the time that RX interrupts are disabled, the
|
|
|
|
* the serial driver will be extracting data from the circular
|
|
|
|
* buffer and modifying recv.tail. During this time, we
|
|
|
|
* should avoid modifying recv.head; When interrupts are restored,
|
|
|
|
* we can update the head pointer for all of the data that we
|
2020-02-23 09:50:23 +01:00
|
|
|
* put into circular buffer while "interrupts" were disabled.
|
2015-10-10 18:41:00 +02:00
|
|
|
*/
|
2008-10-09 22:24:56 +02:00
|
|
|
|
|
|
|
if (priv->rxhead != serdev->recv.head)
|
|
|
|
{
|
|
|
|
serdev->recv.head = priv->rxhead;
|
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
/* Yes... signal the availability of new data */
|
2008-10-09 22:24:56 +02:00
|
|
|
|
2008-11-18 00:22:27 +01:00
|
|
|
uart_datareceived(serdev);
|
2008-10-09 22:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* RX "interrupts are no longer disabled */
|
|
|
|
|
2011-04-07 23:22:06 +02:00
|
|
|
priv->rxenabled = true;
|
2008-10-09 22:24:56 +02:00
|
|
|
}
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2008-10-09 22:24:56 +02:00
|
|
|
|
|
|
|
/* RX "interrupts" are disabled. Is this a transition from enabled
|
|
|
|
* to disabled state?
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (priv->rxenabled)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-09 22:24:56 +02:00
|
|
|
/* Yes. During the time that RX interrupts are disabled, the
|
|
|
|
* the serial driver will be extracting data from the circular
|
|
|
|
* buffer and modifying recv.tail. During this time, we
|
|
|
|
* should avoid modifying recv.head; When interrupts are disabled,
|
|
|
|
* we use a shadow index and continue adding data to the circular
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->rxhead = serdev->recv.head;
|
2011-04-07 23:22:06 +02:00
|
|
|
priv->rxenabled = false;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_txint
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called by the serial driver to enable or disable TX interrupts. We, of
|
|
|
|
* course, have no TX interrupts but must behave consistently. Initially,
|
|
|
|
* TX interrupts are disabled. This method is called under the conditions:
|
|
|
|
*
|
2009-12-15 15:25:14 +01:00
|
|
|
* 1. With enable==false while transferring data into the TX buffer
|
|
|
|
* 2. With enable==true when data may be taken from the buffer.
|
|
|
|
* 3. With enable==false when the TX buffer is empty
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void usbser_txint(FAR struct uart_dev_s *dev, bool enable)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_dev_s *priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_TXINT, (uint16_t)enable);
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2008-10-08 22:19:04 +02:00
|
|
|
/* Sanity checks */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-08 22:19:04 +02:00
|
|
|
if (!dev || !dev->priv)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* Extract references to private data */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = (FAR struct pl2303_dev_s *)dev->priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* If the new state is enabled and if there is data in the XMIT buffer,
|
|
|
|
* send the next packet now.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("enable=%d head=%d tail=%d\n",
|
2008-10-08 22:19:04 +02:00
|
|
|
enable, priv->serdev.xmit.head, priv->serdev.xmit.tail);
|
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
if (enable && priv->serdev.xmit.head != priv->serdev.xmit.tail)
|
|
|
|
{
|
|
|
|
usbclass_sndpacket(priv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2012-01-26 18:42:44 +01:00
|
|
|
* Name: usbser_txempty
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2009-12-15 15:25:14 +01:00
|
|
|
* Return true when all data has been sent. This is called from the
|
2008-10-07 01:12:09 +02:00
|
|
|
* serial driver when the driver is closed. It will call this API
|
2021-03-04 08:02:21 +01:00
|
|
|
* periodically until it reports true. NOTE that the serial driver takes
|
|
|
|
* all responsibility for flushing TX data through the hardware so we can
|
|
|
|
* be a bit sloppy about that.
|
2008-10-07 01:12:09 +02:00
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static bool usbser_txempty(FAR struct uart_dev_s *dev)
|
2008-10-07 01:12:09 +02:00
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct pl2303_dev_s *priv = (FAR struct pl2303_dev_s *)dev->priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2013-01-10 18:37:29 +01:00
|
|
|
usbtrace(PL2303_CLASSAPI_TXEMPTY, 0);
|
2008-10-08 01:09:33 +02:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-10-07 01:12:09 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
2009-12-15 15:25:14 +01:00
|
|
|
return true;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* When all of the allocated write requests have been returned to the
|
|
|
|
* reqlist, then there is no longer any TX data in flight.
|
|
|
|
*/
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
return priv->nwrq >= CONFIG_PL2303_NWRREQS;
|
2008-10-07 01:12:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Public Functions
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/****************************************************************************
|
2008-10-07 01:12:09 +02:00
|
|
|
* Name: usbdev_serialinitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register USB serial port (and USB serial console if so configured).
|
|
|
|
*
|
2021-03-04 08:02:21 +01:00
|
|
|
****************************************************************************/
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
int usbdev_serialinitialize(int minor)
|
|
|
|
{
|
2012-01-26 00:04:17 +01:00
|
|
|
FAR struct pl2303_alloc_s *alloc;
|
|
|
|
FAR struct pl2303_dev_s *priv;
|
|
|
|
FAR struct pl2303_driver_s *drvr;
|
2008-10-07 01:12:09 +02:00
|
|
|
char devname[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate the structures needed */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
alloc = (FAR struct pl2303_alloc_s *)
|
|
|
|
kmm_malloc(sizeof(struct pl2303_alloc_s));
|
2008-10-07 01:12:09 +02:00
|
|
|
if (!alloc)
|
|
|
|
{
|
2008-10-08 01:09:33 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCDEVSTRUCT), 0);
|
2008-10-07 01:12:09 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience pointers into the allocated blob */
|
|
|
|
|
|
|
|
priv = &alloc->dev;
|
|
|
|
drvr = &alloc->drvr;
|
|
|
|
|
|
|
|
/* Initialize the USB serial driver structure */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
memset(priv, 0, sizeof(struct pl2303_dev_s));
|
2008-10-07 01:12:09 +02:00
|
|
|
sq_init(&priv->reqlist);
|
|
|
|
|
|
|
|
/* Fake line status */
|
|
|
|
|
|
|
|
priv->linest[0] = (115200) & 0xff; /* Baud=115200 */
|
|
|
|
priv->linest[1] = (115200 >> 8) & 0xff;
|
|
|
|
priv->linest[2] = (115200 >> 16) & 0xff;
|
|
|
|
priv->linest[3] = (115200 >> 24) & 0xff;
|
|
|
|
priv->linest[4] = 0; /* One stop bit */
|
|
|
|
priv->linest[5] = 0; /* No parity */
|
2015-10-10 18:41:00 +02:00
|
|
|
priv->linest[6] = 8; /* 8 data bits */
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Initialize the serial driver sub-structure */
|
|
|
|
|
2013-01-31 17:52:20 +01:00
|
|
|
#ifdef CONFIG_SERIAL_REMOVABLE
|
|
|
|
priv->serdev.disconnected = true;
|
|
|
|
#endif
|
|
|
|
priv->serdev.recv.size = CONFIG_PL2303_RXBUFSIZE;
|
|
|
|
priv->serdev.recv.buffer = priv->rxbuffer;
|
|
|
|
priv->serdev.xmit.size = CONFIG_PL2303_TXBUFSIZE;
|
|
|
|
priv->serdev.xmit.buffer = priv->txbuffer;
|
|
|
|
priv->serdev.ops = &g_uartops;
|
|
|
|
priv->serdev.priv = priv;
|
2008-10-07 01:12:09 +02:00
|
|
|
|
|
|
|
/* Initialize the USB class driver structure */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
drvr->drvr.speed = USB_SPEED_HIGH;
|
|
|
|
#else
|
|
|
|
drvr->drvr.speed = USB_SPEED_FULL;
|
|
|
|
#endif
|
|
|
|
drvr->drvr.ops = &g_driverops;
|
|
|
|
drvr->dev = priv;
|
|
|
|
|
|
|
|
/* Register the USB serial class driver */
|
|
|
|
|
|
|
|
ret = usbdev_register(&drvr->drvr);
|
|
|
|
if (ret)
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_DEVREGISTER), (uint16_t)-ret);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register the USB serial console */
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifdef CONFIG_PL2303_CONSOLE
|
2012-03-06 21:21:57 +01:00
|
|
|
priv->serdev.isconsole = true;
|
|
|
|
ret = uart_register("/dev/console", &priv->serdev);
|
2008-10-07 01:12:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONSOLEREGISTER),
|
|
|
|
(uint16_t)-ret);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout_with_class;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Register the single port supported by this implementation */
|
|
|
|
|
|
|
|
sprintf(devname, "/dev/ttyUSB%d", minor);
|
|
|
|
ret = uart_register(devname, &priv->serdev);
|
|
|
|
if (ret)
|
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UARTREGISTER), (uint16_t)-ret);
|
2008-10-07 01:12:09 +02:00
|
|
|
goto errout_with_class;
|
|
|
|
}
|
2020-03-11 21:44:00 +01:00
|
|
|
|
2008-10-07 01:12:09 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_class:
|
|
|
|
usbdev_unregister(&drvr->drvr);
|
|
|
|
errout_with_alloc:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(alloc);
|
2008-10-07 01:12:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|