2008-02-08 01:56:16 +01:00
|
|
|
/****************************************************************************
|
2017-05-12 17:04:52 +02:00
|
|
|
* drivers/can/can.c
|
2008-02-08 01:56:16 +01:00
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-02-08 01:56:16 +01:00
|
|
|
*
|
2021-09-28 11:40:59 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-21 13:45:47 +02:00
|
|
|
*
|
2021-09-28 11:40:59 +02: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-02-08 01:56:16 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2020-11-25 08:49:31 +01:00
|
|
|
#include <inttypes.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-02-08 01:56:16 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-01-03 05:26:40 +01:00
|
|
|
#include <time.h>
|
2008-02-08 01:56:16 +01:00
|
|
|
#include <fcntl.h>
|
2011-08-18 16:07:52 +02:00
|
|
|
#include <assert.h>
|
2016-06-21 13:45:47 +02:00
|
|
|
#include <poll.h>
|
2008-02-08 01:56:16 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2008-02-18 21:44:30 +01:00
|
|
|
#include <nuttx/arch.h>
|
2021-01-03 05:26:40 +01:00
|
|
|
#include <nuttx/clock.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-11-03 18:00:47 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2017-05-12 17:04:52 +02:00
|
|
|
#include <nuttx/can/can.h>
|
2019-04-12 19:13:42 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
#ifdef CONFIG_CAN_TXREADY
|
|
|
|
# include <nuttx/wqueue.h>
|
|
|
|
#endif
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2011-12-21 16:50:06 +01:00
|
|
|
#ifdef CONFIG_CAN
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/****************************************************************************
|
2009-12-15 15:25:14 +01:00
|
|
|
* Pre-processor Definitions
|
2008-02-08 01:56:16 +01:00
|
|
|
****************************************************************************/
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_TXREADY
|
|
|
|
# if !defined(CONFIG_SCHED_WORKQUEUE)
|
|
|
|
# error Work queue support required in this configuration
|
|
|
|
# undef CONFIG_CAN_TXREADY
|
|
|
|
# undef CONFIG_CAN_TXREADY_LOPRI
|
|
|
|
# undef CONFIG_CAN_TXREADY_HIPRI
|
|
|
|
# elif defined(CONFIG_CAN_TXREADY_LOPRI)
|
|
|
|
# undef CONFIG_CAN_TXREADY_HIPRI
|
|
|
|
# ifdef CONFIG_SCHED_LPWORK
|
|
|
|
# define CANWORK LPWORK
|
|
|
|
# else
|
|
|
|
# error Low priority work queue support required in this configuration
|
|
|
|
# undef CONFIG_CAN_TXREADY
|
|
|
|
# undef CONFIG_CAN_TXREADY_LOPRI
|
|
|
|
# endif
|
|
|
|
# elif defined(CONFIG_CAN_TXREADY_HIPRI)
|
|
|
|
# ifdef CONFIG_SCHED_HPWORK
|
|
|
|
# define CANWORK HPWORK
|
|
|
|
# else
|
|
|
|
# error High priority work queue support required in this configuration
|
|
|
|
# undef CONFIG_CAN_TXREADY
|
|
|
|
# undef CONFIG_CAN_TXREADY_HIPRI
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# error No work queue selection
|
|
|
|
# undef CONFIG_CAN_TXREADY
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2011-12-21 16:50:06 +01:00
|
|
|
/* Timing Definitions *******************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2009-12-13 16:13:37 +01:00
|
|
|
#define HALF_SECOND_MSEC 500
|
|
|
|
#define HALF_SECOND_USEC 500000L
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
/* CAN helpers */
|
|
|
|
|
|
|
|
static uint8_t can_dlc2bytes(uint8_t dlc);
|
|
|
|
#if 0 /* Not used */
|
|
|
|
static uint8_t can_bytes2dlc(uint8_t nbytes);
|
|
|
|
#endif
|
2015-11-14 17:33:02 +01:00
|
|
|
#ifdef CONFIG_CAN_TXREADY
|
|
|
|
static void can_txready_work(FAR void *arg);
|
|
|
|
#endif
|
2015-08-05 21:56:48 +02:00
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
static int can_open(FAR struct file *filep);
|
|
|
|
static int can_close(FAR struct file *filep);
|
2014-09-17 16:35:03 +02:00
|
|
|
static ssize_t can_read(FAR struct file *filep, FAR char *buffer,
|
2016-06-21 13:45:47 +02:00
|
|
|
size_t buflen);
|
2008-02-08 01:56:16 +01:00
|
|
|
static int can_xmit(FAR struct can_dev_s *dev);
|
2014-09-17 16:35:03 +02:00
|
|
|
static ssize_t can_write(FAR struct file *filep,
|
2016-06-21 13:45:47 +02:00
|
|
|
FAR const char *buffer, size_t buflen);
|
2021-01-03 05:26:40 +01:00
|
|
|
static inline ssize_t can_rtrread(FAR struct file *filep,
|
2016-06-21 13:45:47 +02:00
|
|
|
FAR struct canioc_rtr_s *rtr);
|
2014-09-17 16:35:03 +02:00
|
|
|
static int can_ioctl(FAR struct file *filep, int cmd,
|
2016-06-21 13:45:47 +02:00
|
|
|
unsigned long arg);
|
2020-04-01 06:56:56 +02:00
|
|
|
static int can_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds,
|
2016-06-21 13:45:47 +02:00
|
|
|
bool setup);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-10 17:22:49 +02:00
|
|
|
static const struct file_operations g_canops =
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
|
|
|
can_open, /* open */
|
|
|
|
can_close, /* close */
|
|
|
|
can_read, /* read */
|
|
|
|
can_write, /* write */
|
2016-06-21 13:45:47 +02:00
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
can_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
can_poll /* poll */
|
2008-02-08 01:56:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
/****************************************************************************
|
2015-08-06 00:22:40 +02:00
|
|
|
* Name: can_dlc2bytes
|
2015-08-05 21:56:48 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* In the CAN FD format, the coding of the DLC differs from the standard
|
|
|
|
* CAN format. The DLC codes 0 to 8 have the same coding as in standard
|
|
|
|
* CAN. But the codes 9 to 15 all imply a data field of 8 bytes with
|
|
|
|
* standard CAN. In CAN FD mode, the values 9 to 15 are encoded to values
|
|
|
|
* in the range 12 to 64.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-08-06 00:22:40 +02:00
|
|
|
* dlc - the DLC value to convert to a byte count
|
2015-08-05 21:56:48 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-08-06 00:22:40 +02:00
|
|
|
* The number of bytes corresponding to the DLC value.
|
2015-08-05 21:56:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t can_dlc2bytes(uint8_t dlc)
|
|
|
|
{
|
|
|
|
if (dlc > 8)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_CAN_FD
|
|
|
|
switch (dlc)
|
|
|
|
{
|
|
|
|
case 9:
|
|
|
|
return 12;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
case 10:
|
|
|
|
return 16;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
case 11:
|
|
|
|
return 20;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
case 12:
|
|
|
|
return 24;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
case 13:
|
|
|
|
return 32;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
case 14:
|
|
|
|
return 48;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
default:
|
|
|
|
case 15:
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return 8;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return dlc;
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:22:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_bytes2dlc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* In the CAN FD format, the coding of the DLC differs from the standard
|
|
|
|
* CAN format. The DLC codes 0 to 8 have the same coding as in standard
|
|
|
|
* CAN. But the codes 9 to 15 all imply a data field of 8 bytes with
|
|
|
|
* standard CAN. In CAN FD mode, the values 9 to 15 are encoded to values
|
|
|
|
* in the range 12 to 64.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-08-06 00:22:40 +02:00
|
|
|
* nbytes - the byte count to convert to a DLC value
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The encoded DLC value corresponding to at least that number of bytes.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-05 21:56:48 +02:00
|
|
|
#if 0 /* Not used */
|
|
|
|
static uint8_t can_bytes2dlc(FAR struct sam_can_s *priv, uint8_t nbytes)
|
|
|
|
{
|
|
|
|
if (nbytes <= 8)
|
|
|
|
{
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_CAN_FD
|
|
|
|
else if (nbytes <= 12)
|
|
|
|
{
|
|
|
|
return 9;
|
|
|
|
}
|
|
|
|
else if (nbytes <= 16)
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
else if (nbytes <= 20)
|
|
|
|
{
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
else if (nbytes <= 24)
|
|
|
|
{
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
else if (nbytes <= 32)
|
|
|
|
{
|
|
|
|
return 13;
|
|
|
|
}
|
|
|
|
else if (nbytes <= 48)
|
|
|
|
{
|
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
else /* if (nbytes <= 64) */
|
|
|
|
{
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_txready_work
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function performs deferred processing from can_txready. See the
|
2020-07-08 20:28:29 +02:00
|
|
|
* description of can_txready below for additional information.
|
2015-11-14 17:33:02 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_TXREADY
|
|
|
|
static void can_txready_work(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct can_dev_s *dev = (FAR struct can_dev_s *)arg;
|
|
|
|
irqstate_t flags;
|
2015-11-15 13:36:30 +01:00
|
|
|
int ret;
|
2015-11-14 17:33:02 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
caninfo("xmit head: %d queue: %d tail: %d\n",
|
|
|
|
dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue,
|
|
|
|
dev->cd_xmit.tx_tail);
|
2015-11-14 17:33:02 +01:00
|
|
|
|
|
|
|
/* Verify that the xmit FIFO is not empty. The following operations must
|
|
|
|
* be performed with interrupt disabled.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2015-11-14 17:33:02 +01:00
|
|
|
if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
|
|
|
|
{
|
|
|
|
/* Send the next message in the FIFO. */
|
|
|
|
|
2015-11-15 13:36:30 +01:00
|
|
|
ret = can_xmit(dev);
|
2015-11-14 17:33:02 +01:00
|
|
|
|
2015-11-15 13:36:30 +01:00
|
|
|
/* If the message was successfully queued in the H/W FIFO, then
|
|
|
|
* can_txdone() should have been called. If the S/W FIFO were
|
|
|
|
* full before then there should now be free space in the S/W FIFO.
|
|
|
|
*/
|
2015-11-14 17:33:02 +01:00
|
|
|
|
2015-11-15 13:36:30 +01:00
|
|
|
if (ret >= 0)
|
2015-11-14 17:33:02 +01:00
|
|
|
{
|
2015-11-15 13:36:30 +01:00
|
|
|
/* Are there any threads waiting for space in the TX FIFO? */
|
2015-11-14 17:33:02 +01:00
|
|
|
|
2015-11-15 13:36:30 +01:00
|
|
|
if (dev->cd_ntxwaiters > 0)
|
|
|
|
{
|
|
|
|
/* Yes.. Inform them that new xmit space is available */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&dev->cd_xmit.tx_sem);
|
2015-11-15 13:36:30 +01:00
|
|
|
}
|
2015-11-14 17:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2015-11-14 17:33:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
static FAR struct can_reader_s *init_can_reader(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct can_reader_s *reader = kmm_zalloc(sizeof(struct can_reader_s));
|
|
|
|
DEBUGASSERT(reader != NULL);
|
|
|
|
|
|
|
|
reader->fifo.rx_head = 0;
|
|
|
|
reader->fifo.rx_tail = 0;
|
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
nxsem_init(&reader->fifo.rx_sem, 0, 0);
|
2020-01-17 16:46:39 +01:00
|
|
|
filep->f_priv = reader;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called whenever the CAN device is opened.
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
static int can_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
2020-07-08 20:28:29 +02:00
|
|
|
irqstate_t flags;
|
2016-06-21 13:45:47 +02:00
|
|
|
int ret;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/* If the port is the middle of closing, wait until the close is finished */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->cd_closelock);
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ret < 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2016-06-21 13:45:47 +02:00
|
|
|
return ret;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2020-07-08 20:28:29 +02:00
|
|
|
/* If this is the first time that the driver has been opened
|
|
|
|
* for this device, then perform hardware initialization.
|
2016-06-21 13:45:47 +02:00
|
|
|
*/
|
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
caninfo("ocount: %u\n", dev->cd_crefs);
|
|
|
|
|
|
|
|
if (dev->cd_crefs >= 255)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2022-03-11 16:46:20 +01:00
|
|
|
/* Limit to no more than 255 opens */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
ret = -EMFILE;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-08 20:28:29 +02:00
|
|
|
flags = enter_critical_section();
|
2022-03-11 16:46:20 +01:00
|
|
|
|
|
|
|
if (dev->cd_crefs == 0)
|
2008-02-08 18:25:29 +01:00
|
|
|
{
|
2022-03-11 16:46:20 +01:00
|
|
|
ret = dev_setup(dev);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Mark the FIFOs empty */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
dev->cd_xmit.tx_head = 0;
|
|
|
|
dev->cd_xmit.tx_queue = 0;
|
|
|
|
dev->cd_xmit.tx_tail = 0;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
/* Finally, Enable the CAN RX interrupt */
|
2015-02-24 03:14:39 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
dev_rxint(dev, true);
|
|
|
|
}
|
2020-07-08 20:28:29 +02:00
|
|
|
}
|
2015-02-24 03:14:39 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
if (ret == OK)
|
2016-06-21 13:45:47 +02:00
|
|
|
{
|
2022-03-11 16:46:20 +01:00
|
|
|
dev->cd_crefs++;
|
2015-02-24 02:28:52 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
/* Update the reader list only if driver was open for reading */
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
if ((filep->f_oflags & O_RDOK) != 0)
|
|
|
|
{
|
|
|
|
list_add_head(&dev->cd_readers,
|
|
|
|
(FAR struct list_node *)init_can_reader(filep));
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2020-07-08 20:28:29 +02:00
|
|
|
leave_critical_section(flags);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2020-07-08 20:28:29 +02:00
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->cd_closelock);
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This routine is called when the CAN device is closed.
|
|
|
|
* It waits for the last remaining data to be sent.
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
static int can_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
|
|
|
irqstate_t flags;
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct list_node *node;
|
|
|
|
FAR struct list_node *tmp;
|
2016-06-21 13:45:47 +02:00
|
|
|
int ret;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-07-08 20:28:29 +02:00
|
|
|
#ifdef CONFIG_DEBUG_CAN_INFO
|
2022-03-11 16:46:20 +01:00
|
|
|
caninfo("ocount: %u\n", dev->cd_crefs);
|
2020-07-08 20:28:29 +02:00
|
|
|
#endif
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->cd_closelock);
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ret < 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2016-06-21 13:45:47 +02:00
|
|
|
return ret;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
list_for_every_safe(&dev->cd_readers, node, tmp)
|
|
|
|
{
|
2020-01-17 16:46:39 +01:00
|
|
|
if (((FAR struct can_reader_s *)node) ==
|
|
|
|
((FAR struct can_reader_s *)filep->f_priv))
|
2019-04-12 19:13:42 +02:00
|
|
|
{
|
|
|
|
list_delete(node);
|
|
|
|
kmm_free(node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
filep->f_priv = NULL;
|
2022-03-11 16:46:20 +01:00
|
|
|
dev->cd_crefs--;
|
2020-01-17 16:46:39 +01:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
/* De-initialize the driver if there are no more readers */
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
if (dev->cd_crefs > 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2016-06-21 13:45:47 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/* Stop accepting input */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
dev_rxint(dev, false);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/* Now we wait for the transmit FIFO to clear */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
while (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
|
|
|
|
{
|
2023-09-15 21:10:07 +02:00
|
|
|
nxsig_usleep(HALF_SECOND_USEC);
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/* And wait for the TX hardware FIFO to drain */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
while (!dev_txempty(dev))
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(HALF_SECOND_USEC);
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/* Free the IRQ and disable the CAN device */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
flags = enter_critical_section(); /* Disable interrupts */
|
|
|
|
dev_shutdown(dev); /* Disable the CAN */
|
|
|
|
leave_critical_section(flags);
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->cd_closelock);
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read standard CAN messages
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
static ssize_t can_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2022-03-11 16:46:20 +01:00
|
|
|
FAR struct can_reader_s *reader;
|
2019-04-24 21:54:30 +02:00
|
|
|
FAR struct can_rxfifo_s *fifo;
|
2019-04-12 19:13:42 +02:00
|
|
|
size_t nread;
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret = 0;
|
2020-07-08 20:28:29 +02:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
|
|
|
#endif
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-10-31 12:36:40 +01:00
|
|
|
caninfo("buflen: %zu\n", buflen);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* The caller must provide enough memory to catch the smallest possible
|
|
|
|
* message. This is not a system error condition, but we won't permit
|
|
|
|
* it, Hence we return 0.
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen >= CAN_MSGLEN(0))
|
|
|
|
{
|
2022-03-11 16:46:20 +01:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
|
|
|
reader = (FAR struct can_reader_s *)filep->f_priv;
|
|
|
|
|
|
|
|
fifo = &reader->fifo;
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* Interrupts must be disabled while accessing the cd_recv FIFO */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2016-05-11 15:10:17 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
2022-03-11 16:46:20 +01:00
|
|
|
|
|
|
|
/* Check for reader fifo overflow */
|
|
|
|
|
|
|
|
if (fifo->rx_overflow)
|
|
|
|
{
|
|
|
|
dev->cd_error |= CAN_ERROR5_RXOVERFLOW;
|
|
|
|
fifo->rx_overflow = false;
|
|
|
|
}
|
|
|
|
|
2016-05-11 15:10:17 +02:00
|
|
|
/* Check for internal errors */
|
|
|
|
|
|
|
|
if (dev->cd_error != 0)
|
|
|
|
{
|
|
|
|
FAR struct can_msg_s *msg;
|
|
|
|
|
|
|
|
/* Detected an internal driver error. Generate a
|
|
|
|
* CAN_ERROR_MESSAGE
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (buflen < CAN_MSGLEN(CAN_ERROR_DLC))
|
|
|
|
{
|
|
|
|
goto return_with_irqdisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = (FAR struct can_msg_s *)buffer;
|
|
|
|
msg->cm_hdr.ch_id = CAN_ERROR_INTERNAL;
|
|
|
|
msg->cm_hdr.ch_dlc = CAN_ERROR_DLC;
|
|
|
|
msg->cm_hdr.ch_rtr = 0;
|
|
|
|
msg->cm_hdr.ch_error = 1;
|
|
|
|
#ifdef CONFIG_CAN_EXTID
|
|
|
|
msg->cm_hdr.ch_extid = 0;
|
|
|
|
#endif
|
|
|
|
msg->cm_hdr.ch_unused = 0;
|
|
|
|
memset(&(msg->cm_data), 0, CAN_ERROR_DLC);
|
|
|
|
msg->cm_data[5] = dev->cd_error;
|
|
|
|
|
|
|
|
/* Reset the error flag */
|
|
|
|
|
|
|
|
dev->cd_error = 0;
|
|
|
|
|
|
|
|
ret = CAN_MSGLEN(CAN_ERROR_DLC);
|
|
|
|
goto return_with_irqdisabled;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CAN_ERRORS */
|
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
if ((filep->f_oflags & O_NONBLOCK) != 0)
|
2021-01-02 15:38:59 +01:00
|
|
|
{
|
|
|
|
ret = nxsem_trywait(&fifo->rx_sem);
|
|
|
|
}
|
|
|
|
else
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait(&fifo->rx_sem);
|
2021-01-02 15:38:59 +01:00
|
|
|
}
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto return_with_irqdisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fifo->rx_head == fifo->rx_tail)
|
|
|
|
{
|
|
|
|
canerr("RX FIFO sem posted but FIFO is empty.\n");
|
|
|
|
goto return_with_irqdisabled;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The cd_recv FIFO is not empty. Copy all buffered data that will fit
|
|
|
|
* in the user buffer.
|
|
|
|
*/
|
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
nread = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Will the next message in the FIFO fit into the user buffer? */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
FAR struct can_msg_s *msg = &fifo->rx_buffer[fifo->rx_head];
|
|
|
|
int nbytes = can_dlc2bytes(msg->cm_hdr.ch_dlc);
|
|
|
|
int msglen = CAN_MSGLEN(nbytes);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
if (nread + msglen > buflen)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
/* Copy the message to the user buffer */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
memcpy(&buffer[nread], msg, msglen);
|
|
|
|
nread += msglen;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
/* Increment the head of the circular message buffer */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
if (++fifo->rx_head >= CONFIG_CAN_FIFOSIZE)
|
|
|
|
{
|
|
|
|
fifo->rx_head = 0;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2019-04-12 19:13:42 +02:00
|
|
|
while (fifo->rx_head != fifo->rx_tail);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
if (fifo->rx_head != fifo->rx_tail)
|
|
|
|
{
|
|
|
|
/* The user's buffer was too small, so some messages remain in the
|
|
|
|
* FIFO. Post the semaphore so future calls to poll() or read()
|
|
|
|
* don't block.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&fifo->rx_sem);
|
2021-01-02 15:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes that were read. */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
ret = nread;
|
|
|
|
|
|
|
|
return_with_irqdisabled:
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_xmit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send the message at the head of the cd_xmit FIFO
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called with interrupts disabled
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
static int can_xmit(FAR struct can_dev_s *dev)
|
|
|
|
{
|
2012-01-11 06:03:51 +01:00
|
|
|
int tmpndx;
|
|
|
|
int ret = -EBUSY;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
caninfo("xmit head: %d queue: %d tail: %d\n",
|
|
|
|
dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, dev->cd_xmit.tx_tail);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
/* If there is nothing to send, then just disable interrupts and return */
|
|
|
|
|
|
|
|
if (dev->cd_xmit.tx_head == dev->cd_xmit.tx_tail)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(dev->cd_xmit.tx_queue == dev->cd_xmit.tx_head);
|
2015-11-12 14:46:05 +01:00
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
#ifndef CONFIG_CAN_TXREADY
|
2015-11-12 14:46:05 +01:00
|
|
|
/* We can disable CAN TX interrupts -- unless there is a H/W FIFO. In
|
|
|
|
* that case, TX interrupts must stay enabled until the H/W FIFO is
|
|
|
|
* fully emptied.
|
|
|
|
*/
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
dev_txint(dev, false);
|
2015-11-12 14:46:05 +01:00
|
|
|
#endif
|
2012-01-11 06:03:51 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have already queued all of the data in the TX fifo.
|
|
|
|
*
|
2019-04-12 19:13:42 +02:00
|
|
|
* tx_tail: Incremented in can_write each time a message is queued in the
|
|
|
|
* FIFO
|
2012-01-11 06:03:51 +01:00
|
|
|
* tx_head: Incremented in can_txdone each time a message completes
|
|
|
|
* tx_queue: Incremented each time that a message is sent to the hardware.
|
|
|
|
*
|
|
|
|
* Logically (ignoring buffer wrap-around): tx_head <= tx_queue <= tx_tail
|
|
|
|
* tx_head == tx_queue == tx_tail means that the FIFO is empty
|
|
|
|
* tx_head < tx_queue == tx_tail means that all data has been queued, but
|
|
|
|
* we are still waiting for transmissions to complete.
|
2012-01-10 23:29:39 +01:00
|
|
|
*/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
while (dev->cd_xmit.tx_queue != dev->cd_xmit.tx_tail && dev_txready(dev))
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2015-02-24 02:28:52 +01:00
|
|
|
/* No.. The FIFO should not be empty in this case */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
DEBUGASSERT(dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
/* Increment the FIFO queue index before sending (because dev_send()
|
2015-11-03 17:52:58 +01:00
|
|
|
* might call can_txdone()).
|
2012-01-11 06:03:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
tmpndx = dev->cd_xmit.tx_queue;
|
|
|
|
if (++dev->cd_xmit.tx_queue >= CONFIG_CAN_FIFOSIZE)
|
|
|
|
{
|
|
|
|
dev->cd_xmit.tx_queue = 0;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
/* Send the next message at the FIFO queue index */
|
|
|
|
|
|
|
|
ret = dev_send(dev, &dev->cd_xmit.tx_buffer[tmpndx]);
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ret < 0)
|
2012-01-11 06:03:51 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
canerr("dev_send failed: %d\n", ret);
|
2012-01-11 06:03:51 +01:00
|
|
|
break;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2012-01-11 06:03:51 +01:00
|
|
|
|
|
|
|
/* Make sure that TX interrupts are enabled */
|
|
|
|
|
|
|
|
dev_txint(dev, true);
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_write
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
static ssize_t can_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2012-01-11 06:03:51 +01:00
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
|
|
|
FAR struct can_txfifo_s *fifo = &dev->cd_xmit;
|
|
|
|
FAR struct can_msg_s *msg;
|
|
|
|
bool inactive;
|
|
|
|
ssize_t nsent = 0;
|
|
|
|
irqstate_t flags;
|
|
|
|
int nexttail;
|
2015-08-05 21:56:48 +02:00
|
|
|
int nbytes;
|
2012-01-11 06:03:51 +01:00
|
|
|
int msglen;
|
|
|
|
int ret = 0;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-10-31 12:36:40 +01:00
|
|
|
caninfo("buflen: %zu\n", buflen);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* Interrupts must disabled throughout the following */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* Check if the TX is inactive when we started. In certain race conditions,
|
|
|
|
* there may be a pending interrupt to kick things back off, but we will
|
|
|
|
* be sure here that there is not. That the hardware is IDLE and will
|
|
|
|
* need to be kick-started.
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
2012-01-10 23:29:39 +01:00
|
|
|
inactive = dev_txempty(dev);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/* Add the messages to the FIFO. Ignore any trailing messages that are
|
|
|
|
* shorter than the minimum.
|
|
|
|
*/
|
|
|
|
|
2021-03-19 11:19:49 +01:00
|
|
|
while (((ssize_t)buflen - nsent) >= CAN_MSGLEN(0))
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2014-09-17 16:35:03 +02:00
|
|
|
/* Check if adding this new message would over-run the drivers ability
|
|
|
|
* to enqueue xmit data.
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
nexttail = fifo->tx_tail + 1;
|
2008-02-08 01:56:16 +01:00
|
|
|
if (nexttail >= CONFIG_CAN_FIFOSIZE)
|
|
|
|
{
|
|
|
|
nexttail = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:47:40 +02:00
|
|
|
/* If the XMIT FIFO becomes full, then wait for space to become
|
|
|
|
* available.
|
|
|
|
*/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
while (nexttail == fifo->tx_head)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
|
|
|
/* The transmit FIFO is full -- was non-blocking mode selected? */
|
|
|
|
|
2015-11-03 17:52:58 +01:00
|
|
|
if ((filep->f_oflags & O_NONBLOCK) != 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
|
|
|
if (nsent == 0)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nsent;
|
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
goto return_with_irqdisabled;
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
/* If the TX hardware was inactive when we started, then we will
|
|
|
|
* have start the XMIT sequence generate the TX done interrupts
|
|
|
|
* needed to clear the FIFO.
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
2012-01-10 23:29:39 +01:00
|
|
|
if (inactive)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
can_xmit(dev);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for a message to be sent */
|
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
DEBUGASSERT(dev->cd_ntxwaiters < 255);
|
|
|
|
dev->cd_ntxwaiters++;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait(&fifo->tx_sem);
|
2016-06-21 13:45:47 +02:00
|
|
|
dev->cd_ntxwaiters--;
|
|
|
|
if (ret < 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2016-06-21 13:45:47 +02:00
|
|
|
goto return_with_irqdisabled;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-check the FIFO state */
|
|
|
|
|
2012-01-10 23:29:39 +01:00
|
|
|
inactive = dev_txempty(dev);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here if there is space at the end of the FIFO. Add the new
|
|
|
|
* CAN message at the tail of the FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
msg = (FAR struct can_msg_s *)&buffer[nsent];
|
2015-08-05 21:56:48 +02:00
|
|
|
nbytes = can_dlc2bytes(msg->cm_hdr.ch_dlc);
|
|
|
|
msglen = CAN_MSGLEN(nbytes);
|
2012-01-11 06:03:51 +01:00
|
|
|
memcpy(&fifo->tx_buffer[fifo->tx_tail], msg, msglen);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/* Increment the tail of the circular buffer */
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
fifo->tx_tail = nexttail;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/* Increment the number of bytes that were sent */
|
|
|
|
|
|
|
|
nsent += msglen;
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/* We get here after all messages have been added to the FIFO. Check if
|
2016-06-21 13:45:47 +02:00
|
|
|
* we need to kick off the XMIT sequence.
|
2015-10-04 23:04:00 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (inactive)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
can_xmit(dev);
|
2015-10-04 23:04:00 +02:00
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
/* Return the number of bytes that were sent */
|
|
|
|
|
|
|
|
ret = nsent;
|
|
|
|
|
|
|
|
return_with_irqdisabled:
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_rtrread
|
|
|
|
*
|
|
|
|
* Description:
|
2014-09-17 16:35:03 +02:00
|
|
|
* Read RTR messages. The RTR message is a special message -- it is an
|
|
|
|
* outgoing message that says "Please re-transmit the message with the
|
|
|
|
* same identifier as this message. So the RTR read is really a
|
|
|
|
* send-wait-receive operation.
|
2008-02-08 01:56:16 +01:00
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2021-01-03 05:26:40 +01:00
|
|
|
static inline ssize_t can_rtrread(FAR struct file *filep,
|
|
|
|
FAR struct canioc_rtr_s *request)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2021-01-03 05:26:40 +01:00
|
|
|
FAR struct can_dev_s *dev = filep->f_inode->i_private;
|
2008-02-08 01:56:16 +01:00
|
|
|
FAR struct can_rtrwait_s *wait = NULL;
|
|
|
|
irqstate_t flags;
|
|
|
|
int i;
|
2021-01-03 05:26:40 +01:00
|
|
|
int sval;
|
2008-02-08 01:56:16 +01:00
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
/* Disable interrupts through this operation */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* Find an available slot in the pending RTR list */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
|
|
|
|
{
|
|
|
|
FAR struct can_rtrwait_s *tmp = &dev->cd_rtr[i];
|
2021-01-03 05:26:40 +01:00
|
|
|
|
|
|
|
ret = nxsem_get_value(&tmp->cr_sem, &sval);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sval == 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2021-01-03 05:26:40 +01:00
|
|
|
/* No one is waiting on RTR transaction; take it. */
|
|
|
|
|
|
|
|
tmp->cr_msg = request->ci_msg;
|
2008-02-08 01:56:16 +01:00
|
|
|
dev->cd_npendrtr++;
|
2021-01-03 05:26:40 +01:00
|
|
|
|
|
|
|
wait = tmp;
|
2008-02-08 01:56:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
{
|
2021-01-03 05:26:40 +01:00
|
|
|
/* Send the remote transmission request with the "old method" unless
|
|
|
|
* the lower-half driver indicates otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev->cd_ops->co_remoterequest != NULL)
|
|
|
|
{
|
|
|
|
if (request->ci_msg->cm_hdr.ch_id < CAN_MAX_STDMSGID
|
|
|
|
#ifdef CONFIG_CAN_EXTID
|
|
|
|
&& !request->ci_msg->cm_hdr.ch_extid
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ret = dev_remoterequest(dev,
|
|
|
|
(uint16_t)(request->ci_msg->cm_hdr.ch_id));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_CAN_USE_RTR
|
|
|
|
/* Temporarily set the RTR bit, then send the remote transmission
|
|
|
|
* request message with the lower-half driver's regular function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
request->ci_msg->cm_hdr.ch_rtr = 1;
|
|
|
|
ret = can_write(filep,
|
2021-03-23 20:13:09 +01:00
|
|
|
(const char *) request->ci_msg,
|
2021-01-03 05:26:40 +01:00
|
|
|
CAN_MSGLEN(request->ci_msg->cm_hdr.ch_dlc));
|
|
|
|
request->ci_msg->cm_hdr.ch_rtr = 0;
|
|
|
|
#else
|
|
|
|
canerr("Error: Driver needs CONFIG_CAN_USE_RTR.\n");
|
|
|
|
ret = -ENOSYS;
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ret >= 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
|
|
|
/* Then wait for the response */
|
|
|
|
|
2022-05-10 07:20:32 +02:00
|
|
|
ret = nxsem_tickwait(&wait->cr_sem,
|
|
|
|
SEC2TICK(request->ci_timeout.tv_sec) +
|
|
|
|
NSEC2TICK(request->ci_timeout.tv_nsec));
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_ioctl
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
static int can_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
2015-02-24 02:28:52 +01:00
|
|
|
int ret = OK;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
caninfo("cmd: %d arg: %ld\n", cmd, arg);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* Handle built-in ioctl commands */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2015-08-05 21:00:31 +02:00
|
|
|
/* CANIOC_RTR: Send the remote transmission request and wait for the
|
|
|
|
* response. Argument is a reference to struct canioc_rtr_s
|
2014-09-17 16:35:03 +02:00
|
|
|
* (casting to uintptr_t first eliminates complaints on some
|
|
|
|
* architectures where the sizeof long is different from the size of
|
|
|
|
* a pointer).
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
2015-08-05 21:00:31 +02:00
|
|
|
case CANIOC_RTR:
|
2021-01-03 05:26:40 +01:00
|
|
|
ret = can_rtrread(filep,
|
|
|
|
(FAR struct canioc_rtr_s *)((uintptr_t)arg));
|
2008-02-08 01:56:16 +01:00
|
|
|
break;
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* Not a "built-in" ioctl command.. perhaps it is unique to this
|
2015-08-05 21:00:31 +02:00
|
|
|
* lower-half, device driver.
|
2014-09-17 16:35:03 +02:00
|
|
|
*/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
ret = dev_ioctl(dev, cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int can_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct inode *inode = (FAR struct inode *)filep->f_inode;
|
2023-08-29 06:16:49 +02:00
|
|
|
FAR struct can_dev_s *dev = inode->i_private;
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct can_reader_s *reader = NULL;
|
2016-06-21 13:45:47 +02:00
|
|
|
pollevent_t eventset;
|
|
|
|
int ndx;
|
2021-01-02 15:38:59 +01:00
|
|
|
int sval;
|
2020-07-05 18:10:54 +02:00
|
|
|
irqstate_t flags;
|
2016-06-21 13:45:47 +02:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Some sanity checking */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
if (dev == NULL || fds == NULL)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
/* Ensure exclusive access to FIFO indices - don't want can_receive or
|
|
|
|
* can_read changing them in the middle of the comparison
|
|
|
|
*/
|
|
|
|
|
2020-07-05 18:10:54 +02:00
|
|
|
flags = enter_critical_section();
|
|
|
|
|
2020-01-17 16:46:39 +01:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
|
|
|
reader = (FAR struct can_reader_s *)filep->f_priv;
|
2019-04-12 19:13:42 +02:00
|
|
|
|
2016-06-21 13:45:47 +02:00
|
|
|
/* Get exclusive access to the poll structures */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->cd_polllock);
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* A signal received while waiting for access to the poll data
|
|
|
|
* will abort the operation
|
|
|
|
*/
|
|
|
|
|
2020-07-05 18:10:54 +02:00
|
|
|
goto return_with_irqdisabled;
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we setting up the poll? Or tearing it down? */
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_CAN_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (dev->cd_fds[i] == NULL)
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
dev->cd_fds[i] = fds;
|
|
|
|
fds->priv = &dev->cd_fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_CAN_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should we immediately notify on any of the requested events?
|
|
|
|
* First, check if the xmit buffer is full.
|
|
|
|
*
|
|
|
|
* Get exclusive access to the cd_xmit buffer indices. NOTE: that
|
|
|
|
* we do not let this wait be interrupted by a signal (we probably
|
|
|
|
* should, but that would be a little awkward).
|
|
|
|
*/
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
|
|
|
|
DEBUGASSERT(dev->cd_ntxwaiters < 255);
|
|
|
|
dev->cd_ntxwaiters++;
|
|
|
|
do
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_wait(&dev->cd_xmit.tx_sem);
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
|
|
|
while (ret < 0);
|
|
|
|
dev->cd_ntxwaiters--;
|
|
|
|
|
2021-10-21 04:55:54 +02:00
|
|
|
ndx = dev->cd_xmit.tx_tail + 1;
|
2016-06-21 13:45:47 +02:00
|
|
|
if (ndx >= CONFIG_CAN_FIFOSIZE)
|
|
|
|
{
|
|
|
|
ndx = 0;
|
|
|
|
}
|
|
|
|
|
2021-10-21 04:55:54 +02:00
|
|
|
if (ndx != dev->cd_xmit.tx_head)
|
2016-06-21 13:45:47 +02:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&dev->cd_xmit.tx_sem);
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
/* Check whether there are messages in the RX FIFO. */
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
ret = nxsem_get_value(&reader->fifo.rx_sem, &sval);
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
if (ret < 0)
|
2016-06-21 13:45:47 +02:00
|
|
|
{
|
2022-07-13 19:34:54 +02:00
|
|
|
DEBUGPANIC();
|
2021-01-02 15:38:59 +01:00
|
|
|
goto return_with_irqdisabled;
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
2021-01-02 15:38:59 +01:00
|
|
|
else if (sval > 0)
|
|
|
|
{
|
|
|
|
if (reader->fifo.rx_head != reader->fifo.rx_tail)
|
|
|
|
{
|
|
|
|
/* No need to wait, just notify the application immediately */
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2021-01-02 15:38:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
canerr("RX FIFO sem not locked but FIFO is empty.\n");
|
|
|
|
}
|
|
|
|
}
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->cd_fds, CONFIG_CAN_NPOLLWAITERS, eventset);
|
2016-06-21 13:45:47 +02:00
|
|
|
}
|
|
|
|
else if (fds->priv != NULL)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll */
|
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2016-06-21 13:45:47 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
if (slot == NULL)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->cd_polllock);
|
2020-07-05 18:10:54 +02:00
|
|
|
|
|
|
|
return_with_irqdisabled:
|
|
|
|
leave_critical_section(flags);
|
2016-06-21 13:45:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register serial console and serial ports.
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
int can_register(FAR const char *path, FAR struct can_dev_s *dev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize the CAN device structure */
|
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
dev->cd_crefs = 0;
|
2015-07-16 03:52:08 +02:00
|
|
|
dev->cd_npendrtr = 0;
|
2022-03-11 16:46:20 +01:00
|
|
|
dev->cd_ntxwaiters = 0;
|
2016-05-11 15:10:17 +02:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
|
|
|
dev->cd_error = 0;
|
|
|
|
#endif
|
2022-03-11 16:46:20 +01:00
|
|
|
list_initialize(&dev->cd_readers);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2016-11-03 18:00:47 +01:00
|
|
|
/* Initialize semaphores */
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->cd_xmit.tx_sem, 0, 1);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->cd_closelock);
|
|
|
|
nxmutex_init(&dev->cd_polllock);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
|
|
|
|
{
|
2016-11-04 00:48:36 +01:00
|
|
|
/* Initialize wait semaphores. These semaphores are used for signaling
|
2016-11-03 18:00:47 +01:00
|
|
|
* and should not have priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&dev->cd_rtr[i].cr_sem, 0, 0);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize/reset the CAN hardware */
|
|
|
|
|
|
|
|
dev_reset(dev);
|
|
|
|
|
|
|
|
/* Register the CAN device */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
caninfo("Registering %s\n", path);
|
2008-02-08 01:56:16 +01:00
|
|
|
return register_driver(path, &g_canops, 0666, dev);
|
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called from the CAN interrupt handler when new read data is available
|
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* Input Parameters:
|
2008-02-08 01:56:16 +01:00
|
|
|
* dev - CAN driver state structure
|
|
|
|
* hdr - CAN message header
|
|
|
|
* data - CAN message data (if DLC > 0)
|
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* Returned Value:
|
|
|
|
* OK on success; a negated errno on failure.
|
|
|
|
*
|
2008-02-08 01:56:16 +01:00
|
|
|
* Assumptions:
|
|
|
|
* CAN interrupts are disabled.
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
int can_receive(FAR struct can_dev_s *dev, FAR struct can_hdr_s *hdr,
|
|
|
|
FAR uint8_t *data)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct can_rxfifo_s *fifo;
|
2012-01-11 06:03:51 +01:00
|
|
|
FAR uint8_t *dest;
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct list_node *node;
|
2019-04-24 22:08:14 +02:00
|
|
|
FAR struct list_node *tmp;
|
2012-01-11 06:03:51 +01:00
|
|
|
int nexttail;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode = -ENOMEM;
|
2012-01-11 06:03:51 +01:00
|
|
|
int i;
|
2023-06-25 06:05:17 +02:00
|
|
|
int j;
|
2021-01-03 05:26:40 +01:00
|
|
|
int sval;
|
|
|
|
int ret;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2020-11-25 08:49:31 +01:00
|
|
|
caninfo("ID: %" PRId32 " DLC: %d\n", (uint32_t)hdr->ch_id, hdr->ch_dlc);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* Check if adding this new message would over-run the drivers ability to
|
|
|
|
* enqueue read data.
|
2008-02-08 01:56:16 +01:00
|
|
|
*/
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/* First, check if this response matches any RTR response that we may be
|
|
|
|
* waiting for.
|
|
|
|
*/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
if (dev->cd_npendrtr > 0)
|
|
|
|
{
|
|
|
|
/* There are pending RTR requests -- search the lists of requests
|
|
|
|
* and see any any matches this new message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_CAN_NPENDINGRTR; i++)
|
|
|
|
{
|
2021-01-03 05:26:40 +01:00
|
|
|
FAR struct can_rtrwait_s *wait = &dev->cd_rtr[i];
|
|
|
|
FAR struct can_msg_s *waitmsg = wait->cr_msg;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2021-01-03 05:26:40 +01:00
|
|
|
/* Check if the entry is in use and whether the ID matches */
|
|
|
|
|
|
|
|
ret = nxsem_get_value(&wait->cr_sem, &sval);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2021-01-03 05:26:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (sval < 0
|
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
|
|
|
&& hdr->ch_error == false
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_CAN_EXTID
|
|
|
|
&& waitmsg->cm_hdr.ch_extid == hdr->ch_extid
|
|
|
|
#endif
|
|
|
|
&& waitmsg->cm_hdr.ch_id == hdr->ch_id)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2015-08-07 19:41:31 +02:00
|
|
|
int nbytes;
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* We have the response... copy the data to the user's buffer */
|
|
|
|
|
2021-01-03 05:26:40 +01:00
|
|
|
memcpy(&waitmsg->cm_hdr, hdr, sizeof(struct can_hdr_s));
|
2015-08-07 19:41:31 +02:00
|
|
|
|
|
|
|
nbytes = can_dlc2bytes(hdr->ch_dlc);
|
2023-06-25 06:05:17 +02:00
|
|
|
for (j = 0, dest = waitmsg->cm_data; j < nbytes; j++)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
|
|
|
*dest++ = *data++;
|
|
|
|
}
|
|
|
|
|
2015-07-16 03:52:08 +02:00
|
|
|
dev->cd_npendrtr--;
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2021-01-03 05:26:40 +01:00
|
|
|
/* Restart the waiting thread and mark the entry unused */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&wait->cr_sem);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
list_for_every_safe(&dev->cd_readers, node, tmp)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2019-04-12 19:13:42 +02:00
|
|
|
FAR struct can_reader_s *reader = (FAR struct can_reader_s *)node;
|
|
|
|
fifo = &reader->fifo;
|
2015-08-07 19:41:31 +02:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
nexttail = fifo->rx_tail + 1;
|
|
|
|
if (nexttail >= CONFIG_CAN_FIFOSIZE)
|
|
|
|
{
|
|
|
|
nexttail = 0;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
/* Refuse the new data if the FIFO is full */
|
2015-08-07 19:41:31 +02:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
if (nexttail != fifo->rx_head)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2019-04-12 19:13:42 +02:00
|
|
|
int nbytes;
|
|
|
|
|
|
|
|
/* Add the new, decoded CAN message at the tail of the FIFO.
|
|
|
|
*
|
|
|
|
* REVISIT: In the CAN FD format, the coding of the DLC differs
|
|
|
|
* from the standard CAN format. The DLC codes 0 to 8 have the
|
|
|
|
* same coding as in standard CAN, the codes 9 to 15, which in
|
|
|
|
* standard CAN all code a data field of 8 bytes, are encoded:
|
|
|
|
*
|
|
|
|
* 9->12, 10->16, 11->20, 12->24, 13->32, 14->48, 15->64
|
|
|
|
*/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
memcpy(&fifo->rx_buffer[fifo->rx_tail].cm_hdr, hdr,
|
|
|
|
sizeof(struct can_hdr_s));
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
nbytes = can_dlc2bytes(hdr->ch_dlc);
|
|
|
|
memcpy(fifo->rx_buffer[fifo->rx_tail].cm_data, data, nbytes);
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
/* Increment the tail of the circular buffer */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
fifo->rx_tail = nexttail;
|
2015-07-16 02:20:08 +02:00
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can read from the
|
|
|
|
* cd_recv buffer
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->cd_fds, CONFIG_CAN_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2020-07-05 18:04:47 +02:00
|
|
|
sval = 0;
|
|
|
|
if (nxsem_get_value(&fifo->rx_sem, &sval) < 0)
|
|
|
|
{
|
2022-07-13 19:34:54 +02:00
|
|
|
DEBUGPANIC();
|
2020-07-05 18:04:47 +02:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
|
|
|
/* Report unspecified error */
|
|
|
|
|
|
|
|
dev->cd_error |= CAN_ERROR5_UNSPEC;
|
|
|
|
#endif
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:38:59 +01:00
|
|
|
/* Unlock the binary semaphore, waking up can_read if it is
|
|
|
|
* blocked. If can_read were not blocked, we would not be
|
|
|
|
* executing this because interrupts would be disabled.
|
2019-04-12 19:13:42 +02:00
|
|
|
*/
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2020-07-05 19:56:24 +02:00
|
|
|
if (sval <= 0)
|
2019-04-12 19:13:42 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&fifo->rx_sem);
|
2019-04-12 19:13:42 +02:00
|
|
|
}
|
2016-06-21 13:45:47 +02:00
|
|
|
|
2019-04-12 19:13:42 +02:00
|
|
|
errcode = OK;
|
|
|
|
}
|
2016-05-11 15:10:17 +02:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
2019-04-12 19:13:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Report rx overflow error */
|
2016-05-11 15:10:17 +02:00
|
|
|
|
2022-03-11 16:46:20 +01:00
|
|
|
fifo->rx_overflow = true;
|
2019-04-12 19:13:42 +02:00
|
|
|
}
|
2016-05-11 15:10:17 +02:00
|
|
|
#endif
|
2019-04-12 19:13:42 +02:00
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2016-06-11 22:40:07 +02:00
|
|
|
return errcode;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
/****************************************************************************
|
2008-02-08 01:56:16 +01:00
|
|
|
* Name: can_txdone
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-08 14:43:57 +01:00
|
|
|
* Called when the hardware has processed the outgoing TX message. This
|
|
|
|
* normally means that the CAN messages was sent out on the wire. But
|
|
|
|
* if the CAN hardware supports a H/W TX FIFO, then this call may mean
|
|
|
|
* only that the CAN message has been added to the H/W FIFO. In either
|
|
|
|
* case, the upper-half CAN driver can remove the outgoing message from
|
|
|
|
* the S/W FIFO and discard it.
|
|
|
|
*
|
|
|
|
* This function may be called in different contexts, depending upon the
|
|
|
|
* nature of the underlying CAN hardware.
|
|
|
|
*
|
|
|
|
* 1. No H/W TX FIFO (CONFIG_CAN_TXREADY not defined)
|
|
|
|
*
|
|
|
|
* This function is only called from the CAN interrupt handler at the
|
|
|
|
* completion of a send operation.
|
|
|
|
*
|
|
|
|
* can_write() -> can_xmit() -> dev_send()
|
|
|
|
* CAN interrupt -> can_txdone()
|
|
|
|
*
|
|
|
|
* If the CAN hardware is busy, then the call to dev_send() will
|
|
|
|
* fail, the S/W TX FIFO will accumulate outgoing messages, and the
|
|
|
|
* thread calling can_write() may eventually block waiting for space in
|
|
|
|
* the S/W TX FIFO.
|
|
|
|
*
|
|
|
|
* When the CAN hardware completes the transfer and processes the
|
|
|
|
* CAN interrupt, the call to can_txdone() will make space in the S/W
|
|
|
|
* TX FIFO and will awaken the waiting can_write() thread.
|
2008-02-08 01:56:16 +01:00
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* 2a. H/W TX FIFO (CONFIG_CAN_TXREADY=y) and S/W TX FIFO not full
|
|
|
|
*
|
|
|
|
* This function will be called back from dev_send() immediately when a
|
|
|
|
* new CAN message is added to H/W TX FIFO:
|
|
|
|
*
|
|
|
|
* can_write() -> can_xmit() -> dev_send() -> can_txdone()
|
|
|
|
*
|
|
|
|
* When the H/W TX FIFO becomes full, dev_send() will fail and
|
|
|
|
* can_txdone() will not be called. In this case the S/W TX FIFO will
|
|
|
|
* accumulate outgoing messages, and the thread calling can_write() may
|
|
|
|
* eventually block waiting for space in the S/W TX FIFO.
|
|
|
|
*
|
|
|
|
* 2b. H/W TX FIFO (CONFIG_CAN_TXREADY=y) and S/W TX FIFO full
|
|
|
|
*
|
|
|
|
* In this case, the thread calling can_write() is blocked waiting for
|
2015-11-14 17:33:02 +01:00
|
|
|
* space in the S/W TX FIFO. can_txdone() will be called, indirectly,
|
|
|
|
* from can_txready_work() running on the thread of the work queue.
|
2015-11-08 14:43:57 +01:00
|
|
|
*
|
2015-11-14 17:33:02 +01:00
|
|
|
* CAN interrupt -> can_txready() -> Schedule can_txready_work()
|
|
|
|
* can_txready_work() -> can_xmit() -> dev_send() -> can_txdone()
|
2015-11-08 14:43:57 +01:00
|
|
|
*
|
|
|
|
* The call dev_send() should not fail in this case and the subsequent
|
|
|
|
* call to can_txdone() will make space in the S/W TX FIFO and will
|
|
|
|
* awaken the waiting thread.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2011-12-22 19:28:13 +01:00
|
|
|
* dev - The specific CAN device
|
2008-02-08 01:56:16 +01:00
|
|
|
* hdr - The 16-bit CAN header
|
|
|
|
* data - An array contain the CAN data.
|
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* Returned Value:
|
2008-02-08 01:56:16 +01:00
|
|
|
* OK on success; a negated errno on failure.
|
|
|
|
*
|
2015-11-10 14:41:40 +01:00
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled. This is required by can_xmit() which is called
|
|
|
|
* by this function. Interrupts are explicitly disabled when called
|
|
|
|
* through can_write(). Interrupts are expected be disabled when called
|
|
|
|
* from the CAN interrupt handler.
|
|
|
|
*
|
2014-09-17 16:35:03 +02:00
|
|
|
****************************************************************************/
|
2008-02-08 01:56:16 +01:00
|
|
|
|
|
|
|
int can_txdone(FAR struct can_dev_s *dev)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
caninfo("xmit head: %d queue: %d tail: %d\n",
|
|
|
|
dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, dev->cd_xmit.tx_tail);
|
2011-12-22 19:28:13 +01:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* Verify that the xmit FIFO is not empty */
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2015-11-03 17:52:58 +01:00
|
|
|
/* The tx_queue index is incremented each time can_xmit() queues
|
|
|
|
* the transmission. When can_txdone() is called, the tx_queue
|
|
|
|
* index should always have been advanced beyond the current tx_head
|
|
|
|
* index.
|
|
|
|
*/
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
DEBUGASSERT(dev->cd_xmit.tx_head != dev->cd_xmit.tx_queue);
|
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
/* Remove the message at the head of the xmit FIFO */
|
|
|
|
|
2012-01-11 06:03:51 +01:00
|
|
|
if (++dev->cd_xmit.tx_head >= CONFIG_CAN_FIFOSIZE)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2012-01-11 06:03:51 +01:00
|
|
|
dev->cd_xmit.tx_head = 0;
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the next message in the FIFO */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
can_xmit(dev);
|
2011-08-18 16:07:52 +02:00
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
/* Notify all poll/select waiters that they can write to the cd_xmit
|
|
|
|
* buffer
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->cd_fds, CONFIG_CAN_NPOLLWAITERS, POLLOUT);
|
2020-09-26 14:25:03 +02:00
|
|
|
|
2011-08-18 16:07:52 +02:00
|
|
|
/* Are there any threads waiting for space in the TX FIFO? */
|
|
|
|
|
2014-09-17 16:35:03 +02:00
|
|
|
if (dev->cd_ntxwaiters > 0)
|
2008-02-08 01:56:16 +01:00
|
|
|
{
|
2011-08-18 16:07:52 +02:00
|
|
|
/* Yes.. Inform them that new xmit space is available */
|
2008-02-08 01:56:16 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxsem_post(&dev->cd_xmit.tx_sem);
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
2008-02-08 01:56:16 +01:00
|
|
|
}
|
2014-09-17 16:35:03 +02:00
|
|
|
|
2008-02-08 01:56:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:52:58 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: can_txready
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called from the CAN interrupt handler at the completion of a send
|
|
|
|
* operation. This interface is needed only for CAN hardware that
|
2020-02-23 09:50:23 +01:00
|
|
|
* supports queueing of outgoing messages in a H/W FIFO.
|
2015-11-03 17:52:58 +01:00
|
|
|
*
|
|
|
|
* The CAN upper half driver also supports a queue of output messages in a
|
|
|
|
* S/W FIFO. Messages are added to that queue when when can_write() is
|
|
|
|
* called and removed from the queue in can_txdone() when each TX message
|
|
|
|
* is complete.
|
|
|
|
*
|
|
|
|
* After each message is added to the S/W FIFO, the CAN upper half driver
|
|
|
|
* will attempt to send the message by calling into the lower half driver.
|
|
|
|
* That send will not be performed if the lower half driver is busy, i.e.,
|
|
|
|
* if dev_txready() returns false. In that case, the number of messages in
|
|
|
|
* the S/W FIFO can grow. If the S/W FIFO becomes full, then can_write()
|
|
|
|
* will wait for space in the S/W FIFO.
|
|
|
|
*
|
|
|
|
* If the CAN hardware does not support a H/W FIFO then busy means that
|
|
|
|
* the hardware is actively sending the message and is guaranteed to
|
|
|
|
* become non-busy (i.e, dev_txready()) when the send transfer completes
|
|
|
|
* and can_txdone() is called. So the call to can_txdone() means that the
|
|
|
|
* transfer has completed and also that the hardware is ready to accept
|
|
|
|
* another transfer.
|
|
|
|
*
|
|
|
|
* If the CAN hardware supports a H/W FIFO, can_txdone() is not called
|
2020-02-22 19:31:14 +01:00
|
|
|
* when the transfer is complete, but rather when the transfer is queued in
|
2015-11-03 17:52:58 +01:00
|
|
|
* the H/W FIFO. When the H/W FIFO becomes full, then dev_txready() will
|
2020-04-01 06:56:56 +02:00
|
|
|
* report false and the number of queued messages in the S/W FIFO will
|
|
|
|
* grow.
|
2015-11-03 17:52:58 +01:00
|
|
|
*
|
|
|
|
* There is no mechanism in this case to inform the upper half driver when
|
|
|
|
* the hardware is again available, when there is again space in the H/W
|
|
|
|
* FIFO. can_txdone() will not be called again. If the S/W FIFO becomes
|
|
|
|
* full, then the upper half driver will wait for space to become
|
|
|
|
* available, but there is no event to awaken it and the driver will hang.
|
|
|
|
*
|
|
|
|
* Enabling this feature adds support for the can_txready() interface.
|
|
|
|
* This function is called from the lower half driver's CAN interrupt
|
|
|
|
* handler each time a TX transfer completes. This is a sure indication
|
|
|
|
* that the H/W FIFO is no longer full. can_txready() will then awaken
|
|
|
|
* the can_write() logic and the hang condition is avoided.
|
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* Input Parameters:
|
2015-11-03 17:52:58 +01:00
|
|
|
* dev - The specific CAN device
|
|
|
|
*
|
2015-11-08 14:43:57 +01:00
|
|
|
* Returned Value:
|
2015-11-03 17:52:58 +01:00
|
|
|
* OK on success; a negated errno on failure.
|
|
|
|
*
|
2015-11-10 14:41:40 +01:00
|
|
|
* Assumptions:
|
2015-11-15 13:36:30 +01:00
|
|
|
* Interrupts are disabled. This function may execute in the context of
|
|
|
|
* and interrupt handler.
|
2015-11-10 14:41:40 +01:00
|
|
|
*
|
2015-11-03 17:52:58 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_TXREADY
|
|
|
|
int can_txready(FAR struct can_dev_s *dev)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
caninfo("xmit head: %d queue: %d tail: %d waiters: %d\n",
|
|
|
|
dev->cd_xmit.tx_head, dev->cd_xmit.tx_queue, dev->cd_xmit.tx_tail,
|
|
|
|
dev->cd_ntxwaiters);
|
2015-11-03 17:52:58 +01:00
|
|
|
|
2015-11-10 14:41:40 +01:00
|
|
|
/* Verify that the xmit FIFO is not empty. This is safe because interrupts
|
|
|
|
* are always disabled when calling into can_xmit(); this cannot collide
|
|
|
|
* with ongoing activity from can_write().
|
|
|
|
*/
|
2015-11-03 17:52:58 +01:00
|
|
|
|
2015-11-10 14:41:40 +01:00
|
|
|
if (dev->cd_xmit.tx_head != dev->cd_xmit.tx_tail)
|
2015-11-03 17:52:58 +01:00
|
|
|
{
|
2015-11-14 17:33:02 +01:00
|
|
|
/* Is work already scheduled? */
|
2015-11-10 14:41:40 +01:00
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
if (work_available(&dev->cd_work))
|
2015-11-10 14:41:40 +01:00
|
|
|
{
|
2015-11-14 17:33:02 +01:00
|
|
|
/* Yes... schedule to perform can_txready() work on the worker
|
|
|
|
* thread. Although data structures are protected by disabling
|
|
|
|
* interrupts, the can_xmit() operations may involve semaphore
|
|
|
|
* operations and, hence, should not be done at the interrupt
|
|
|
|
* level.
|
2015-11-12 14:46:05 +01:00
|
|
|
*/
|
|
|
|
|
2015-11-14 17:33:02 +01:00
|
|
|
ret = work_queue(CANWORK, &dev->cd_work, can_txready_work, dev, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
2015-11-10 14:41:40 +01:00
|
|
|
}
|
2015-11-03 17:52:58 +01:00
|
|
|
}
|
2015-11-12 14:46:05 +01:00
|
|
|
else
|
|
|
|
{
|
2015-11-14 17:33:02 +01:00
|
|
|
/* There should not be any threads waiting for space in the S/W TX
|
2019-04-12 19:13:42 +02:00
|
|
|
* FIFO is it is empty. However, an assertion would fire in certain
|
|
|
|
* race conditions, i.e, when all waiters have been awakened but
|
|
|
|
* have not yet had a chance to decrement cd_ntxwaiters.
|
2015-11-14 17:33:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0 /* REVISIT */
|
2015-11-12 14:46:05 +01:00
|
|
|
/* When the H/W FIFO has been emptied, we can disable further TX
|
|
|
|
* interrupts.
|
2015-11-14 17:33:02 +01:00
|
|
|
*
|
|
|
|
* REVISIT: The fact that the S/W FIFO is empty does not mean that
|
|
|
|
* the H/W FIFO is also empty. If we really want this to work this
|
|
|
|
* way, then we would probably need and additional parameter to tell
|
|
|
|
* us if the H/W FIFO is empty.
|
2015-11-12 14:46:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
dev_txint(dev, false);
|
2015-11-12 14:53:42 +01:00
|
|
|
#endif
|
2015-11-14 17:33:02 +01:00
|
|
|
}
|
2015-11-03 17:52:58 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CAN_TXREADY */
|
2011-12-21 16:50:06 +01:00
|
|
|
#endif /* CONFIG_CAN */
|