2023-07-19 04:09:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbdev/usbdev_fs.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <nuttx/nuttx.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/queue.h>
|
|
|
|
#include <nuttx/mutex.h>
|
|
|
|
#include <nuttx/usb/usbdev.h>
|
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
#include <nuttx/usb/composite.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2024-01-24 11:37:00 +01:00
|
|
|
#include <nuttx/wqueue.h>
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
#include "composite.h"
|
|
|
|
#include "usbdev_fs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Container to support a list of requests */
|
|
|
|
|
|
|
|
struct usbdev_fs_req_s
|
|
|
|
{
|
|
|
|
sq_entry_t node; /* Implements a singly linked list */
|
|
|
|
FAR struct usbdev_req_s *req; /* The contained request */
|
|
|
|
uint16_t offset; /* Offset to valid data in the RX request */
|
|
|
|
};
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
struct usbdev_ctrlreq_s
|
|
|
|
{
|
|
|
|
sq_entry_t node; /* Implements a singly linked list */
|
|
|
|
struct usb_ctrlreq_s req; /* The contained request */
|
|
|
|
};
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/* Manage char device non blocking io */
|
|
|
|
|
|
|
|
typedef struct usbdev_fs_waiter_sem_s
|
|
|
|
{
|
|
|
|
sem_t sem;
|
|
|
|
FAR struct usbdev_fs_waiter_sem_s *next;
|
|
|
|
} usbdev_fs_waiter_sem_t;
|
|
|
|
|
|
|
|
/* This structure describes the char device */
|
|
|
|
|
|
|
|
struct usbdev_fs_ep_s
|
|
|
|
{
|
|
|
|
uint8_t crefs; /* Count of opened instances */
|
2023-11-08 07:44:49 +01:00
|
|
|
bool unlinked; /* Indicates if the driver has been unlinked */
|
2023-07-19 04:09:41 +02:00
|
|
|
mutex_t lock; /* Enforces device exclusive access */
|
|
|
|
FAR struct usbdev_ep_s *ep; /* EP entry */
|
2023-11-08 07:44:49 +01:00
|
|
|
FAR struct usbdev_fs_dev_s *dev; /* USB device */
|
2023-07-19 04:09:41 +02:00
|
|
|
FAR usbdev_fs_waiter_sem_t *sems; /* List of blocking request */
|
|
|
|
struct sq_queue_s reqq; /* Available request containers */
|
|
|
|
FAR struct usbdev_fs_req_s *reqbuffer; /* Request buffer */
|
|
|
|
FAR struct pollfd *fds[CONFIG_USBDEV_FS_NPOLLWAITERS];
|
2024-01-29 04:10:53 +01:00
|
|
|
|
|
|
|
/* These member is valid for endpoint 0 */
|
|
|
|
|
|
|
|
struct sq_queue_s ctrlreqq; /* Available request containers */
|
|
|
|
struct sq_queue_s ctrlreqq_free; /* Available request containers */
|
|
|
|
FAR struct usbdev_ctrlreq_s *ctrlreqbuffer; /* Ctrl Request buffer */
|
2023-07-19 04:09:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct usbdev_fs_dev_s
|
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *cdev;
|
|
|
|
uint8_t config;
|
2024-01-24 11:37:00 +01:00
|
|
|
struct work_s work;
|
2023-07-19 04:09:41 +02:00
|
|
|
struct usbdev_devinfo_s devinfo;
|
|
|
|
FAR struct usbdev_fs_ep_s *eps;
|
2024-04-01 11:42:05 +02:00
|
|
|
bool uninitialized;
|
2023-07-19 04:09:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct usbdev_fs_driver_s
|
|
|
|
{
|
|
|
|
struct usbdevclass_driver_s drvr;
|
|
|
|
struct usbdev_fs_dev_s dev;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* USB class device *********************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_classbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbdev_fs_classunbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static int usbdev_fs_classsetup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR uint8_t *dataout, size_t outlen);
|
|
|
|
static void
|
|
|
|
usbdev_fs_classdisconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbdev_fs_classsuspend(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
static void usbdev_fs_classresume(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev);
|
|
|
|
|
|
|
|
/* Char device Operations ***************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_open(FAR struct file *filep);
|
|
|
|
static int usbdev_fs_close(FAR struct file *filep);
|
|
|
|
static ssize_t usbdev_fs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
static ssize_t usbdev_fs_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
static int usbdev_fs_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* USB class device *********************************************************/
|
|
|
|
|
|
|
|
static const struct usbdevclass_driverops_s g_usbdev_fs_classops =
|
|
|
|
{
|
|
|
|
usbdev_fs_classbind, /* bind */
|
|
|
|
usbdev_fs_classunbind, /* unbind */
|
|
|
|
usbdev_fs_classsetup, /* setup */
|
|
|
|
usbdev_fs_classdisconnect, /* disconnect */
|
|
|
|
usbdev_fs_classsuspend, /* suspend */
|
|
|
|
usbdev_fs_classresume /* resume */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Char device **************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_usbdev_fs_fops =
|
|
|
|
{
|
|
|
|
usbdev_fs_open, /* open */
|
|
|
|
usbdev_fs_close, /* close */
|
|
|
|
usbdev_fs_read, /* read */
|
|
|
|
usbdev_fs_write, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
NULL, /* ioctl */
|
|
|
|
NULL, /* mmap */
|
|
|
|
NULL, /* truncate */
|
|
|
|
usbdev_fs_poll /* poll */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_notify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify threads waiting to read device. This function must be called
|
|
|
|
* with interrupt disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_notify(FAR struct usbdev_fs_ep_s *fs_ep,
|
|
|
|
pollevent_t eventset)
|
|
|
|
{
|
|
|
|
/* Notify all of the waiting readers */
|
|
|
|
|
|
|
|
FAR usbdev_fs_waiter_sem_t *cur_sem = fs_ep->sems;
|
|
|
|
while (cur_sem != NULL)
|
|
|
|
{
|
|
|
|
nxsem_post(&cur_sem->sem);
|
|
|
|
cur_sem = cur_sem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_ep->sems = NULL;
|
|
|
|
|
|
|
|
/* Notify all poll/select waiters */
|
|
|
|
|
|
|
|
poll_notify(fs_ep->fds, CONFIG_USBDEV_FS_NPOLLWAITERS, eventset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_submit_wrreq
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of write request on the bulk IN endpoint.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_submit_wrreq(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_fs_req_s *container,
|
|
|
|
uint16_t len)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_req_s *req = container->req;
|
|
|
|
|
|
|
|
req->len = len;
|
|
|
|
req->flags = 0;
|
|
|
|
req->priv = container;
|
|
|
|
return EP_SUBMIT(ep, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_submit_rdreq
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of read request on the bulk OUT endpoint.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_submit_rdreq(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_fs_req_s *container)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_req_s *req = container->req;
|
|
|
|
|
|
|
|
req->len = ep->maxpacket;
|
|
|
|
return EP_SUBMIT(ep, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_rdcomplete
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of read request on the bulk OUT endpoint.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_rdcomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
if (!ep || !ep->priv || !req)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract references to private data */
|
|
|
|
|
|
|
|
fs_ep = (FAR struct usbdev_fs_ep_s *)ep->fs;
|
|
|
|
container = (FAR struct usbdev_fs_req_s *)req->priv;
|
|
|
|
|
|
|
|
/* Process the received data unless this is some unusual condition */
|
|
|
|
|
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case 0: /* Normal completion */
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSRDCOMPLETE, sq_count(&fs_ep->reqq));
|
|
|
|
|
2023-11-27 09:53:35 +01:00
|
|
|
/* Restart request due to empty frame received */
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
if (req->xfrd <= 0)
|
|
|
|
{
|
|
|
|
goto restart_req;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Queue request and notify readers */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Put request on RX pending queue */
|
|
|
|
|
|
|
|
container->offset = 0;
|
|
|
|
sq_addlast(&container->node, &fs_ep->reqq);
|
|
|
|
|
|
|
|
usbdev_fs_notify(fs_ep, POLLIN);
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSHUTDOWN), 0);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDUNEXPECTED),
|
|
|
|
(uint16_t)-req->result);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
restart_req:
|
|
|
|
|
|
|
|
/* Restart request */
|
|
|
|
|
|
|
|
usbdev_fs_submit_rdreq(fs_ep->ep, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_wrcomplete
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle completion of write request. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_wrcomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
if (!ep || !ep->priv || !req || !req->priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract references to private data */
|
|
|
|
|
|
|
|
fs_ep = (FAR struct usbdev_fs_ep_s *)ep->fs;
|
|
|
|
container = (FAR struct usbdev_fs_req_s *)req->priv;
|
|
|
|
|
|
|
|
/* Return the write request to the free list */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
sq_addlast(&container->node, &fs_ep->reqq);
|
|
|
|
|
|
|
|
/* Check for termination condition */
|
|
|
|
|
|
|
|
switch (req->result)
|
|
|
|
{
|
|
|
|
case OK: /* Normal completion */
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLASSWRCOMPLETE, sq_count(&fs_ep->reqq));
|
|
|
|
|
|
|
|
/* Notify all waiting writers that write req is available */
|
|
|
|
|
|
|
|
usbdev_fs_notify(fs_ep, POLLOUT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -ESHUTDOWN: /* Disconnection */
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRSHUTDOWN),
|
|
|
|
sq_count(&fs_ep->reqq));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Some other error occurred */
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRUNEXPECTED),
|
|
|
|
(uint16_t)-req->result);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_blocking_io
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle read/write blocking io.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_blocking_io(FAR struct usbdev_fs_ep_s *fs_ep,
|
|
|
|
FAR usbdev_fs_waiter_sem_t **list,
|
|
|
|
FAR struct sq_queue_s *queue)
|
|
|
|
{
|
2024-02-07 06:40:17 +01:00
|
|
|
usbdev_fs_waiter_sem_t sem;
|
2023-07-19 04:09:41 +02:00
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
if (!sq_empty(queue))
|
|
|
|
{
|
|
|
|
/* Queue not empty after all */
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&sem.sem, 0, 0);
|
|
|
|
|
|
|
|
/* Register waiter semaphore */
|
|
|
|
|
|
|
|
sem.next = *list;
|
|
|
|
*list = &sem;
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
|
|
|
|
/* Wait for USB device to notify */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&sem.sem);
|
|
|
|
|
|
|
|
/* Interrupted wait, unregister semaphore
|
|
|
|
* TODO ensure that lock wait does not fail (ECANCELED)
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxmutex_lock(&fs_ep->lock);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
FAR usbdev_fs_waiter_sem_t *cur_sem = *list;
|
|
|
|
|
|
|
|
if (cur_sem == &sem)
|
|
|
|
{
|
|
|
|
*list = sem.next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (cur_sem)
|
|
|
|
{
|
|
|
|
if (cur_sem->next == &sem)
|
|
|
|
{
|
|
|
|
cur_sem->next = sem.next;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-07 06:40:17 +01:00
|
|
|
|
|
|
|
cur_sem = cur_sem->next;
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open usbdev fs device. Only one open() instance is supported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = inode->i_private;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&fs_ep->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("entry: <%s> %d\n", inode->i_name, fs_ep->crefs);
|
|
|
|
|
|
|
|
fs_ep->crefs += 1;
|
|
|
|
|
2024-05-13 13:30:47 +02:00
|
|
|
ASSERT(fs_ep->crefs != 0);
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close usbdev fs device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = inode->i_private;
|
2023-11-08 07:44:49 +01:00
|
|
|
FAR struct usbdev_fs_dev_s *fs = fs_ep->dev;
|
2023-07-19 04:09:41 +02:00
|
|
|
int ret;
|
2023-11-08 07:44:49 +01:00
|
|
|
int i;
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&fs_ep->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
finfo("entry: <%s> %d\n", inode->i_name, fs_ep->crefs);
|
|
|
|
|
|
|
|
fs_ep->crefs -= 1;
|
|
|
|
|
2023-11-08 07:44:49 +01:00
|
|
|
if (fs_ep->unlinked && fs_ep->crefs == 0)
|
|
|
|
{
|
|
|
|
bool do_free = true;
|
|
|
|
|
|
|
|
nxmutex_destroy(&fs_ep->lock);
|
2024-01-29 04:10:53 +01:00
|
|
|
for (i = 0; i < fs->devinfo.nendpoints + 1; i++)
|
2023-11-08 07:44:49 +01:00
|
|
|
{
|
|
|
|
if (fs->eps[i].crefs > 0)
|
|
|
|
{
|
|
|
|
do_free = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-01 11:42:05 +02:00
|
|
|
if (do_free && fs->uninitialized)
|
2023-11-08 07:44:49 +01:00
|
|
|
{
|
2023-12-13 05:28:36 +01:00
|
|
|
FAR struct usbdev_fs_driver_s *alloc = container_of(
|
|
|
|
fs, FAR struct usbdev_fs_driver_s, dev);
|
|
|
|
|
2023-11-08 07:44:49 +01:00
|
|
|
kmm_free(fs->eps);
|
|
|
|
fs->eps = NULL;
|
2023-12-13 05:28:36 +01:00
|
|
|
kmm_free(alloc);
|
2023-11-08 07:44:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read usbdev fs device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t usbdev_fs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = inode->i_private;
|
2024-01-29 04:10:53 +01:00
|
|
|
FAR struct sq_queue_s *queue;
|
|
|
|
bool is_ep0 = false;
|
2023-07-19 04:09:41 +02:00
|
|
|
size_t retlen = 0;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&fs_ep->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-08 05:17:55 +01:00
|
|
|
/* Check if the usbdev device has been unbind */
|
|
|
|
|
|
|
|
if (fs_ep->unlinked)
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
queue = &fs_ep->reqq;
|
|
|
|
if (fs_ep == &fs_ep->dev->eps[0])
|
|
|
|
{
|
|
|
|
is_ep0 = true;
|
|
|
|
queue = &fs_ep->ctrlreqq;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/* Check for available data */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (sq_empty(queue))
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* RX queue seems empty. Check again with interrupts disabled */
|
|
|
|
|
|
|
|
ret = usbdev_fs_blocking_io(
|
2024-01-29 04:10:53 +01:00
|
|
|
fs_ep, &fs_ep->sems, queue);
|
2023-07-19 04:09:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 04:10:53 +01:00
|
|
|
while (sq_empty(queue));
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Device ready for read */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
while (!sq_empty(queue))
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
uint16_t reqlen;
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (is_ep0)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_ctrlreq_s *ctrl_container;
|
|
|
|
|
|
|
|
retlen = MIN(sizeof(struct usb_ctrlreq_s), len);
|
|
|
|
|
|
|
|
/* Process each packet in the priv->reqq list */
|
|
|
|
|
|
|
|
ctrl_container = container_of(sq_peek(queue),
|
|
|
|
struct usbdev_ctrlreq_s, node);
|
|
|
|
|
|
|
|
/* Output buffer full */
|
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
memcpy(buffer, &ctrl_container->req, retlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
sq_remfirst(queue);
|
|
|
|
sq_addlast(&ctrl_container->node, &fs_ep->ctrlreqq_free);
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/* Process each packet in the priv->reqq list */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
container = container_of(sq_peek(queue),
|
2023-07-19 04:09:41 +02:00
|
|
|
struct usbdev_fs_req_s, node);
|
|
|
|
|
|
|
|
reqlen = container->req->xfrd - container->offset;
|
|
|
|
|
|
|
|
if (reqlen > len)
|
|
|
|
{
|
|
|
|
/* Output buffer full */
|
|
|
|
|
2024-02-25 11:45:21 +01:00
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
memcpy(&buffer[retlen],
|
|
|
|
&container->req->buf[container->offset],
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
container->offset += len;
|
|
|
|
retlen += len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-25 11:45:21 +01:00
|
|
|
if (buffer != NULL)
|
|
|
|
{
|
|
|
|
memcpy(&buffer[retlen],
|
|
|
|
&container->req->buf[container->offset], reqlen);
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
retlen += reqlen;
|
|
|
|
len -= reqlen;
|
|
|
|
|
|
|
|
/* The entire packet was processed and may be removed from the
|
|
|
|
* pending RX list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FIXME use atomic queue primitives ? */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
2024-01-29 04:10:53 +01:00
|
|
|
sq_remfirst(queue);
|
2023-07-19 04:09:41 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
ret = usbdev_fs_submit_rdreq(fs_ep->ep, container);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* TODO handle error */
|
|
|
|
|
|
|
|
PANIC();
|
|
|
|
}
|
2024-01-10 05:14:32 +01:00
|
|
|
|
|
|
|
/* The container buffer length is less than the maximum length.
|
|
|
|
* It is an independent packet of requests and needs to be
|
|
|
|
* returned directly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (reqlen < fs_ep->ep->maxpacket)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return retlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_write
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write usbdev fs device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t usbdev_fs_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = inode->i_private;
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
FAR struct usbdev_req_s *req;
|
|
|
|
irqstate_t flags;
|
|
|
|
int wlen = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&fs_ep->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-08 05:17:55 +01:00
|
|
|
/* Check if the usbdev device has been unbind */
|
|
|
|
|
|
|
|
if (fs_ep->unlinked)
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/* Check for available write request */
|
|
|
|
|
|
|
|
if (sq_empty(&fs_ep->reqq))
|
|
|
|
{
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* TX queue seems empty. Check again with interrupts disabled */
|
|
|
|
|
|
|
|
ret = usbdev_fs_blocking_io(
|
|
|
|
fs_ep, &fs_ep->sems, &fs_ep->reqq);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sq_empty(&fs_ep->reqq));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device ready for write */
|
|
|
|
|
2024-02-22 15:34:47 +01:00
|
|
|
while (!sq_empty(&fs_ep->reqq))
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
uint16_t cur_len;
|
|
|
|
|
|
|
|
/* Get available TX request slot */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
container = container_of(sq_remfirst(&fs_ep->reqq),
|
|
|
|
struct usbdev_fs_req_s, node);
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
req = container->req;
|
|
|
|
|
|
|
|
/* Fill the request with data */
|
|
|
|
|
|
|
|
if (len > fs_ep->ep->maxpacket)
|
|
|
|
{
|
|
|
|
cur_len = fs_ep->ep->maxpacket;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur_len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(req->buf, &buffer[wlen], cur_len);
|
|
|
|
|
|
|
|
/* Then submit the request to the endpoint */
|
|
|
|
|
|
|
|
ret = usbdev_fs_submit_wrreq(fs_ep->ep, container, cur_len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* TODO add tx request back in txfree queue */
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SUBMITFAIL),
|
|
|
|
(uint16_t)-ret);
|
|
|
|
PANIC();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlen += cur_len;
|
|
|
|
len -= cur_len;
|
2024-02-22 15:34:47 +01:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = wlen;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Poll usbdev fs device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = inode->i_private;
|
|
|
|
pollevent_t eventset;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&fs_ep->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setup)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME only parts of this function required interrupt disabled */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_USBDEV_FS_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!fs_ep->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
fs_ep->fds[i] = fds;
|
|
|
|
fds->priv = &fs_ep->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_USBDEV_FS_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto exit_leave_critical;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
|
2023-11-29 03:01:23 +01:00
|
|
|
/* Check if the usbdev device has been unbind */
|
|
|
|
|
|
|
|
if (fs_ep->unlinked)
|
|
|
|
{
|
|
|
|
eventset |= POLLHUP;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/* Notify the POLLIN/POLLOUT event if at least one request is available */
|
|
|
|
|
2023-11-29 03:01:23 +01:00
|
|
|
else if (!sq_empty(&fs_ep->reqq))
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
if (fs_ep == &fs_ep->dev->eps[0])
|
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
else if (USB_ISEPIN(fs_ep->ep->eplog))
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (!sq_empty(&fs_ep->ctrlreqq))
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
2023-12-26 07:16:13 +01:00
|
|
|
poll_notify(fs_ep->fds, CONFIG_USBDEV_FS_NPOLLWAITERS, eventset);
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
exit_leave_critical:
|
|
|
|
leave_critical_section(flags);
|
|
|
|
errout:
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify usbdev fs device connect state.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_connect(FAR struct usbdev_fs_dev_s *fs, int connect)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep;
|
|
|
|
uint16_t cnt;
|
|
|
|
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
|
|
|
|
if (connect)
|
|
|
|
{
|
2023-12-26 07:16:13 +01:00
|
|
|
/* Notify poll/select with POLLPRI */
|
2023-07-19 04:09:41 +02:00
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
for (cnt = 0; cnt < devinfo->nendpoints + 1; cnt++)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
fs_ep = &fs->eps[cnt];
|
2023-12-26 07:16:13 +01:00
|
|
|
poll_notify(fs_ep->fds, CONFIG_USBDEV_FS_NPOLLWAITERS, POLLPRI);
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Notify all of the char device */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
for (cnt = 0; cnt < devinfo->nendpoints + 1; cnt++)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
fs_ep = &fs->eps[cnt];
|
|
|
|
usbdev_fs_notify(fs_ep, POLLERR | POLLHUP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_ep_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind usbdev fs device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-01-24 11:37:00 +01:00
|
|
|
static int usbdev_fs_ep_bind(FAR struct usbdev_s *dev, uint8_t epno,
|
2023-07-19 04:09:41 +02:00
|
|
|
FAR const struct usbdev_epinfo_s *epinfo,
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep)
|
|
|
|
{
|
2024-07-02 08:36:49 +02:00
|
|
|
#if defined(CONFIG_USBDEV_SUPERSPEED)
|
2024-07-10 14:28:10 +02:00
|
|
|
size_t reqsize = epinfo->sssize;
|
2024-07-02 08:36:49 +02:00
|
|
|
#elif defined(CONFIG_USBDEV_DUALSPEED)
|
2024-07-10 14:28:10 +02:00
|
|
|
size_t reqsize = epinfo->hssize;
|
2023-07-19 04:09:41 +02:00
|
|
|
#else
|
2024-07-10 14:28:10 +02:00
|
|
|
size_t reqsize = epinfo->fssize;
|
2023-07-19 04:09:41 +02:00
|
|
|
#endif
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
/* Initialize fs ep lock */
|
|
|
|
|
|
|
|
nxmutex_init(&fs_ep->lock);
|
|
|
|
|
|
|
|
/* Initialize request queue */
|
|
|
|
|
|
|
|
sq_init(&fs_ep->reqq);
|
2024-01-29 04:10:53 +01:00
|
|
|
sq_init(&fs_ep->ctrlreqq);
|
|
|
|
sq_init(&fs_ep->ctrlreqq_free);
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
/* Pre-allocate the endpoint */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (epno != 0)
|
|
|
|
{
|
|
|
|
fs_ep->ep = DEV_ALLOCEP(dev, epno,
|
|
|
|
USB_ISEPIN(epinfo->desc.addr),
|
|
|
|
epinfo->desc.attr & USB_EP_ATTR_XFERTYPE_MASK);
|
|
|
|
if (fs_ep->ep == NULL)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINALLOCFAIL), 0);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
fs_ep->ep = dev->ep0;
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 09:21:39 +02:00
|
|
|
#ifdef CONFIG_USBDEV_SUPERSPEED
|
|
|
|
if (dev->speed == USB_SPEED_SUPER ||
|
|
|
|
dev->speed == USB_SPEED_SUPER_PLUS)
|
|
|
|
{
|
|
|
|
uint8_t transtpye;
|
|
|
|
|
|
|
|
transtpye = epinfo->desc.attr & USB_EP_ATTR_XFERTYPE_MASK;
|
|
|
|
if (transtpye == USB_EP_ATTR_XFER_BULK)
|
|
|
|
{
|
|
|
|
if (epinfo->compdesc.mxburst >= USB_SS_BULK_EP_MAXBURST)
|
|
|
|
{
|
|
|
|
reqsize = reqsize * USB_SS_BULK_EP_MAXBURST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reqsize = reqsize * (epinfo->compdesc.mxburst + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (transtpye == USB_EP_ATTR_XFER_INT)
|
|
|
|
{
|
|
|
|
if (epinfo->compdesc.mxburst >= USB_SS_INT_EP_MAXBURST)
|
|
|
|
{
|
|
|
|
reqsize = reqsize * USB_SS_INT_EP_MAXBURST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reqsize = reqsize * (epinfo->compdesc.mxburst + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
fs_ep->ep->fs = fs_ep;
|
|
|
|
|
|
|
|
/* Initialize request buffer */
|
|
|
|
|
|
|
|
fs_ep->reqbuffer =
|
|
|
|
kmm_zalloc(sizeof(struct usbdev_fs_req_s) * epinfo->reqnum);
|
|
|
|
if (!fs_ep->reqbuffer)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDALLOCREQ), 0);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (epno == 0)
|
|
|
|
{
|
|
|
|
fs_ep->ctrlreqbuffer =
|
|
|
|
kmm_zalloc(sizeof(struct usbdev_ctrlreq_s) * epinfo->reqnum);
|
|
|
|
if (!fs_ep->ctrlreqbuffer)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDALLOCREQ), 0);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
for (i = 0; i < epinfo->reqnum; i++)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
|
|
|
|
container = &fs_ep->reqbuffer[i];
|
|
|
|
container->req = usbdev_allocreq(fs_ep->ep, reqsize);
|
|
|
|
if (container->req == NULL)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDALLOCREQ), -ENOMEM);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
container->req->priv = container;
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (USB_ISEPIN(epinfo->desc.addr) || epno == 0)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
container->req->callback = usbdev_fs_wrcomplete;
|
|
|
|
sq_addlast(&container->node, &fs_ep->reqq);
|
|
|
|
}
|
2024-01-29 04:10:53 +01:00
|
|
|
|
|
|
|
if (epno == 0)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_ctrlreq_s *ctrl_container;
|
|
|
|
|
|
|
|
ctrl_container = &fs_ep->ctrlreqbuffer[i];
|
|
|
|
sq_addlast(&ctrl_container->node, &fs_ep->ctrlreqq_free);
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs_ep->crefs = 0;
|
2024-01-24 11:37:00 +01:00
|
|
|
return 0;
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_ep_unbind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unbind usbdev fs endpoint.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_ep_unbind(FAR const char *devname,
|
|
|
|
FAR struct usbdev_s *dev,
|
|
|
|
FAR const struct usbdev_epinfo_s *epinfo,
|
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep)
|
|
|
|
{
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
/* Release request buffer */
|
|
|
|
|
2024-01-29 04:18:11 +01:00
|
|
|
nxmutex_lock(&fs_ep->lock);
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
if (fs_ep->reqbuffer)
|
|
|
|
{
|
|
|
|
for (i = 0; i < epinfo->reqnum; i++)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container =
|
|
|
|
&fs_ep->reqbuffer[i];
|
|
|
|
if (container->req)
|
|
|
|
{
|
|
|
|
usbdev_freereq(fs_ep->ep, container->req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kmm_free(fs_ep->reqbuffer);
|
|
|
|
fs_ep->reqbuffer = NULL;
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (fs_ep->ctrlreqbuffer)
|
|
|
|
{
|
|
|
|
kmm_free(fs_ep->ctrlreqbuffer);
|
|
|
|
fs_ep->ctrlreqbuffer = NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
sq_init(&fs_ep->reqq);
|
2024-01-29 04:10:53 +01:00
|
|
|
sq_init(&fs_ep->ctrlreqq);
|
|
|
|
sq_init(&fs_ep->ctrlreqq_free);
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
/* Release endpoint */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
if (fs_ep->ep != dev->ep0 && fs_ep->ep != NULL)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
|
|
|
fs_ep->ep->fs = NULL;
|
|
|
|
DEV_FREEEP(dev, fs_ep->ep);
|
|
|
|
fs_ep->ep = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister_driver(devname);
|
2023-11-08 07:44:49 +01:00
|
|
|
fs_ep->unlinked = true;
|
2023-11-08 05:17:55 +01:00
|
|
|
|
|
|
|
/* Notify the usbdev device has been unbind */
|
|
|
|
|
|
|
|
poll_notify(fs_ep->fds, CONFIG_USBDEV_FS_NPOLLWAITERS, POLLHUP | POLLERR);
|
|
|
|
|
2023-11-08 07:44:49 +01:00
|
|
|
if (fs_ep->crefs <= 0)
|
|
|
|
{
|
2024-01-29 04:18:11 +01:00
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
2023-11-08 07:44:49 +01:00
|
|
|
nxmutex_destroy(&fs_ep->lock);
|
|
|
|
}
|
2024-01-29 04:18:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nxmutex_unlock(&fs_ep->lock);
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classresetconfig
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark the device as not configured and disable all endpoints.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_classresetconfig(FAR struct usbdev_fs_dev_s *fs)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
/* Are we configured? */
|
|
|
|
|
|
|
|
if (fs->config != COMPOSITE_CONFIGIDNONE)
|
|
|
|
{
|
|
|
|
/* Yes.. but not anymore */
|
|
|
|
|
|
|
|
usbdev_fs_connect(fs, 0);
|
|
|
|
|
|
|
|
/* Disable endpoints. This should force completion of all pending
|
|
|
|
* transfers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < devinfo->nendpoints; i++)
|
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
EP_DISABLE(fs->eps[i + 1].ep);
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 11:37:00 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_register_driver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the driver after successful set configuration.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_register_driver(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = arg;
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
2024-01-29 04:10:53 +01:00
|
|
|
char devname[32];
|
|
|
|
int ret;
|
2024-01-24 11:37:00 +01:00
|
|
|
int i;
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
for (i = 0; i < devinfo->nendpoints + 1; i++)
|
2024-01-24 11:37:00 +01:00
|
|
|
{
|
|
|
|
snprintf(devname, sizeof(devname), "%s/ep%d",
|
2024-01-29 04:10:53 +01:00
|
|
|
devinfo->name, i);
|
2024-01-24 11:37:00 +01:00
|
|
|
ret = register_driver(devname, &g_usbdev_fs_fops, 0666, &fs->eps[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
uerr("Failed to register driver:%s, ret:%d\n", devname, ret);
|
|
|
|
while (i--)
|
|
|
|
{
|
|
|
|
snprintf(devname, sizeof(devname), "%s/ep%d",
|
2024-01-29 04:10:53 +01:00
|
|
|
devinfo->name, i);
|
2024-01-24 11:37:00 +01:00
|
|
|
unregister_driver(devname);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classsetconfig
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the device configuration by allocating and configuring endpoints and
|
|
|
|
* by allocating and queue read and write requests.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_classsetconfig(FAR struct usbdev_fs_dev_s *fs,
|
|
|
|
uint8_t config)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
2024-07-03 09:21:39 +02:00
|
|
|
struct usb_ss_epdesc_s epdesc;
|
2023-07-19 04:09:41 +02:00
|
|
|
uint16_t i;
|
|
|
|
uint16_t j;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Discard the previous configuration data */
|
|
|
|
|
|
|
|
usbdev_fs_classresetconfig(fs);
|
|
|
|
|
|
|
|
/* Was this a request to simply discard the current configuration? */
|
|
|
|
|
|
|
|
if (config == COMPOSITE_CONFIGIDNONE)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONFIGNONE), 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < devinfo->nendpoints; i++)
|
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
FAR struct usbdev_fs_ep_s *fs_ep = &fs->eps[i + 1];
|
2023-07-19 04:09:41 +02:00
|
|
|
|
2024-07-03 09:21:39 +02:00
|
|
|
usbdev_copy_epdesc(&epdesc.epdesc, devinfo->epno[i],
|
2024-07-02 08:36:49 +02:00
|
|
|
fs->cdev->usbdev->speed, devinfo->epinfos[i]);
|
2024-07-03 09:21:39 +02:00
|
|
|
ret = EP_CONFIGURE(fs_ep->ep, &epdesc.epdesc,
|
2023-07-19 04:09:41 +02:00
|
|
|
(i == (devinfo->nendpoints - 1)));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINCONFIGFAIL), 0);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs_ep->ep->priv = fs;
|
|
|
|
|
|
|
|
if (USB_ISEPOUT(fs_ep->ep->eplog))
|
|
|
|
{
|
|
|
|
for (j = 0; j < devinfo->epinfos[i]->reqnum; j++)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_req_s *container;
|
|
|
|
container = &fs_ep->reqbuffer[j];
|
|
|
|
container->req->callback = usbdev_fs_rdcomplete;
|
|
|
|
usbdev_fs_submit_rdreq(fs_ep->ep, container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fs->config = config;
|
2024-01-24 11:37:00 +01:00
|
|
|
work_queue(HPWORK, &fs->work, usbdev_fs_register_driver, fs, 0);
|
2023-07-19 04:09:41 +02:00
|
|
|
|
|
|
|
/* We are successfully configured. Char device is now active */
|
|
|
|
|
|
|
|
usbdev_fs_connect(fs, 1);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
usbdev_fs_classresetconfig(fs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classbind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is bound to a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_classbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
2024-01-29 04:10:53 +01:00
|
|
|
struct usbdev_epinfo_s ep0info;
|
2023-07-19 04:09:41 +02:00
|
|
|
uint16_t i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Bind the composite device */
|
|
|
|
|
|
|
|
fs->cdev = dev->ep0->priv;
|
|
|
|
|
|
|
|
/* Initialize fs eqs */
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
fs->eps = kmm_zalloc((devinfo->nendpoints + 1) *
|
|
|
|
sizeof(struct usbdev_fs_ep_s));
|
2023-07-19 04:09:41 +02:00
|
|
|
if (fs->eps == NULL)
|
|
|
|
{
|
|
|
|
uerr("Failed to malloc fs eqs");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < devinfo->nendpoints; i++)
|
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
fs->eps[i + 1].dev = fs;
|
2024-01-24 11:37:00 +01:00
|
|
|
ret = usbdev_fs_ep_bind(dev,
|
2023-07-19 04:09:41 +02:00
|
|
|
devinfo->epno[i],
|
|
|
|
devinfo->epinfos[i],
|
2024-01-29 04:10:53 +01:00
|
|
|
&fs->eps[i + 1]);
|
2023-07-19 04:09:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
uerr("Failed to bind fs ep");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:10:53 +01:00
|
|
|
/* Initialize fs ep0 */
|
|
|
|
|
|
|
|
ep0info.fssize = fs->cdev->cfgdescsize;
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
ep0info.hssize = fs->cdev->cfgdescsize;
|
|
|
|
#endif
|
|
|
|
ep0info.reqnum = CONFIG_USBDEV_FS_NEP0REQS;
|
|
|
|
fs->eps[0].dev = fs;
|
|
|
|
ret = usbdev_fs_ep_bind(dev, 0, &ep0info, &fs->eps[0]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
uerr("Failed to bind fs ep0");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
usbdev_fs_classunbind(driver, dev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classunbind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is unbound from a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_classunbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo = &fs->devinfo;
|
2024-01-29 04:10:53 +01:00
|
|
|
struct usbdev_epinfo_s ep0info;
|
2023-11-08 07:44:49 +01:00
|
|
|
bool do_free = true;
|
2023-07-19 04:09:41 +02:00
|
|
|
char devname[32];
|
|
|
|
uint16_t i;
|
|
|
|
|
|
|
|
if (fs->eps != NULL)
|
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
ep0info.reqnum = CONFIG_USBDEV_FS_NEP0REQS;
|
|
|
|
for (i = 0; i < devinfo->nendpoints + 1; i++)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
FAR const struct usbdev_epinfo_s *epinfo;
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
epinfo = &ep0info;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
epinfo = devinfo->epinfos[i - 1];
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:09:41 +02:00
|
|
|
snprintf(devname, sizeof(devname), "%s/ep%d",
|
2024-01-29 04:10:53 +01:00
|
|
|
devinfo->name, i);
|
|
|
|
usbdev_fs_ep_unbind(devname, dev, epinfo, &fs->eps[i]);
|
2023-11-08 07:44:49 +01:00
|
|
|
if (fs->eps[i].crefs > 0)
|
|
|
|
{
|
|
|
|
do_free = false;
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 07:44:49 +01:00
|
|
|
if (do_free)
|
|
|
|
{
|
|
|
|
kmm_free(fs->eps);
|
|
|
|
fs->eps = NULL;
|
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fs->cdev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classsetup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked for ep0 control requests. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usbdev_fs_classsetup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR uint8_t *dataout, size_t outlen)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
uint16_t value;
|
|
|
|
int ret = -EOPNOTSUPP;
|
|
|
|
|
|
|
|
/* Extract the little-endian 16-bit values to host order */
|
|
|
|
|
|
|
|
value = GETUINT16(ctrl->value);
|
|
|
|
|
|
|
|
if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD &&
|
|
|
|
ctrl->req == USB_REQ_SETCONFIGURATION &&
|
|
|
|
ctrl->type == 0)
|
|
|
|
{
|
|
|
|
ret = usbdev_fs_classsetconfig(fs, value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-29 04:10:53 +01:00
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
FAR struct usbdev_fs_ep_s *ep0 = &fs->eps[0];
|
|
|
|
|
|
|
|
if (!sq_empty(&ep0->ctrlreqq_free))
|
|
|
|
{
|
|
|
|
FAR struct usbdev_ctrlreq_s *container;
|
|
|
|
|
|
|
|
container = container_of(sq_remfirst(&ep0->ctrlreqq_free),
|
|
|
|
struct usbdev_ctrlreq_s, node);
|
|
|
|
|
|
|
|
memcpy(&container->req, ctrl, sizeof(*ctrl));
|
|
|
|
|
|
|
|
sq_addlast(&container->node, &ep0->ctrlreqq);
|
|
|
|
usbdev_fs_notify(ep0, POLLIN);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uerr("Failed to find free control request for ep0");
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returning a negative value will cause a STALL */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classdisconnect
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
usbdev_fs_classdisconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
|
|
|
|
/* Reset the configuration */
|
|
|
|
|
|
|
|
usbdev_fs_classresetconfig(fs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classsuspend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the USB suspend event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_classsuspend(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
|
|
|
|
usbdev_fs_connect(fs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classresume
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the USB resume event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usbdev_fs_classresume(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *fs_drvr = container_of(
|
|
|
|
driver, FAR struct usbdev_fs_driver_s, drvr);
|
|
|
|
FAR struct usbdev_fs_dev_s *fs = &fs_drvr->dev;
|
|
|
|
|
|
|
|
usbdev_fs_connect(fs, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbclass_classobject
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register USB driver and return the class object.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, negative error code on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbdev_fs_classobject(int minor,
|
|
|
|
FAR struct usbdev_devinfo_s *devinfo,
|
|
|
|
FAR struct usbdevclass_driver_s **classdev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *alloc;
|
|
|
|
|
|
|
|
if (devinfo->nendpoints > CONFIG_USBDEV_FS_EPNUM)
|
|
|
|
{
|
|
|
|
uerr("class epnum error");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc = kmm_zalloc(sizeof(struct usbdev_fs_driver_s));
|
|
|
|
if (!alloc)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCDEVSTRUCT), 0);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the caller provided device description */
|
|
|
|
|
|
|
|
memcpy(&alloc->dev.devinfo, devinfo,
|
|
|
|
sizeof(struct usbdev_devinfo_s));
|
|
|
|
|
|
|
|
/* Initialize the USB class driver structure */
|
|
|
|
|
|
|
|
alloc->drvr.ops = &g_usbdev_fs_classops;
|
|
|
|
|
|
|
|
*classdev = &alloc->drvr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_classuninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free allocated class memory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void usbdev_fs_classuninitialize(FAR struct usbdevclass_driver_s *classdev)
|
|
|
|
{
|
|
|
|
FAR struct usbdev_fs_driver_s *alloc = container_of(
|
|
|
|
classdev, FAR struct usbdev_fs_driver_s, drvr);
|
2023-12-13 05:28:36 +01:00
|
|
|
FAR struct usbdev_fs_dev_s *fs = &alloc->dev;
|
|
|
|
int i;
|
2023-07-19 04:09:41 +02:00
|
|
|
|
2024-04-01 11:42:05 +02:00
|
|
|
fs->uninitialized = true;
|
2024-01-29 04:10:53 +01:00
|
|
|
for (i = 0; i < fs->devinfo.nendpoints + 1; i++)
|
2023-07-19 04:09:41 +02:00
|
|
|
{
|
2023-12-13 05:28:36 +01:00
|
|
|
if (fs->eps != NULL && fs->eps[i].crefs > 0)
|
2023-11-29 08:50:32 +01:00
|
|
|
{
|
2023-12-13 05:28:36 +01:00
|
|
|
return;
|
2023-11-29 08:50:32 +01:00
|
|
|
}
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
2023-12-13 05:28:36 +01:00
|
|
|
|
|
|
|
kmm_free(alloc);
|
2023-07-19 04:09:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* USBDEV fs initialize
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, negative error code on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *usbdev_fs_initialize(FAR const struct usbdev_devdescs_s *devdescs,
|
|
|
|
FAR struct composite_devdesc_s *pdevice)
|
|
|
|
{
|
|
|
|
return composite_initialize(devdescs, pdevice, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbdev_fs_uninitialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* USBDEV fs uninitialize
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success, negative error code on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void usbdev_fs_uninitialize(FAR void *handle)
|
|
|
|
{
|
|
|
|
composite_uninitialize(handle);
|
|
|
|
}
|