diff --git a/drivers/can/can.c b/drivers/can/can.c index 4556d5690ca..e84729413bb 100644 --- a/drivers/can/can.c +++ b/drivers/can/can.c @@ -390,7 +390,7 @@ static FAR struct can_reader_s *init_can_reader(FAR struct file *filep) reader->fifo.rx_tail = 0; nxsem_init(&reader->fifo.rx_sem, 0, 1); - reader->filep = filep; + filep->f_priv = reader; return reader; } @@ -509,7 +509,8 @@ static int can_close(FAR struct file *filep) list_for_every_safe(&dev->cd_readers, node, tmp) { - if (((FAR struct can_reader_s *)node)->filep == filep) + if (((FAR struct can_reader_s *)node) == + ((FAR struct can_reader_s *)filep->f_priv)) { list_delete(node); kmm_free(node); @@ -517,6 +518,8 @@ static int can_close(FAR struct file *filep) } } + filep->f_priv = NULL; + /* Decrement the references to the driver. If the reference count will * decrement to 0, then uninitialize the driver. */ @@ -574,7 +577,6 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer, FAR struct inode *inode = filep->f_inode; FAR struct can_dev_s *dev = inode->i_private; FAR struct can_reader_s *reader = NULL; - FAR struct list_node *node; FAR struct can_rxfifo_s *fifo; size_t nread; irqstate_t flags; @@ -630,16 +632,8 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer, } #endif /* CONFIG_CAN_ERRORS */ - list_for_every(&dev->cd_readers, node) - { - if (((FAR struct can_reader_s *) node)->filep == filep) - { - reader = (FAR struct can_reader_s *)node; - break; - } - } - - DEBUGASSERT(reader != NULL); + DEBUGASSERT(filep->f_priv != NULL); + reader = (FAR struct can_reader_s *)filep->f_priv; fifo = &reader->fifo; @@ -670,31 +664,31 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer, * in the user buffer. */ - nread = 0; - do - { - /* Will the next message in the FIFO fit into the user buffer? */ + nread = 0; + do + { + /* Will the next message in the FIFO fit into the user buffer? */ - 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); + 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); - if (nread + msglen > buflen) - { - break; - } + if (nread + msglen > buflen) + { + break; + } - /* Copy the message to the user buffer */ + /* Copy the message to the user buffer */ - memcpy(&buffer[nread], msg, msglen); - nread += msglen; + memcpy(&buffer[nread], msg, msglen); + nread += msglen; - /* Increment the head of the circular message buffer */ + /* Increment the head of the circular message buffer */ - if (++fifo->rx_head >= CONFIG_CAN_FIFOSIZE) - { - fifo->rx_head = 0; - } + if (++fifo->rx_head >= CONFIG_CAN_FIFOSIZE) + { + fifo->rx_head = 0; + } } while (fifo->rx_head != fifo->rx_tail); @@ -1027,7 +1021,6 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, FAR struct inode *inode = (FAR struct inode *)filep->f_inode; FAR struct can_dev_s *dev = (FAR struct can_dev_s *)inode->i_private; FAR struct can_reader_s *reader = NULL; - FAR struct list_node *node; pollevent_t eventset; int ndx; int ret; @@ -1042,16 +1035,8 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds, } #endif - list_for_every(&dev->cd_readers, node) - { - if (((FAR struct can_reader_s *)node)->filep == filep) - { - reader = (FAR struct can_reader_s *)node; - break; - } - } - - DEBUGASSERT(reader != NULL); + DEBUGASSERT(filep->f_priv != NULL); + reader = (FAR struct can_reader_s *)filep->f_priv; /* Get exclusive access to the poll structures */ diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h index 69f37470f1e..165204d365a 100644 --- a/include/nuttx/can/can.h +++ b/include/nuttx/can/can.h @@ -561,7 +561,6 @@ struct can_reader_s { struct list_node list; sem_t read_sem; - FAR struct file *filep; struct can_rxfifo_s fifo; /* Describes receive FIFO */ }; @@ -590,6 +589,7 @@ struct can_dev_s }; /* Structures used with ioctl calls */ + /* CANIOC_RTR: */ struct canioc_rtr_s @@ -598,8 +598,9 @@ struct canioc_rtr_s FAR struct can_msg_s *ci_msg; /* The location to return the RTR response */ }; -/* CANIOC_GET_BITTIMING/CANIOC_SET_BITTIMING: */ -/* Bit time = Tquanta * (Sync_Seg + Prop_Seq + Phase_Seg1 + Phase_Seg2) +/* CANIOC_GET_BITTIMING/CANIOC_SET_BITTIMING: + * + * Bit time = Tquanta * (Sync_Seg + Prop_Seq + Phase_Seg1 + Phase_Seg2) * = Tquanta * (TSEG1 + TSEG2 + 1) * Where * TSEG1 = Prop_Seq + Phase_Seg1 @@ -614,8 +615,9 @@ struct canioc_bittiming_s uint8_t bt_sjw; /* Synchronization Jump Width in time quanta */ }; -/* CANIOC_GET_CONNMODES/CANIOC_SET_CONNMODES: */ -/* A CAN device may support loopback and silent mode. Both modes may not be +/* CANIOC_GET_CONNMODES/CANIOC_SET_CONNMODES: + * + * A CAN device may support loopback and silent mode. Both modes may not be * settable independently. */ @@ -654,11 +656,7 @@ struct canioc_stdfilter_s }; /************************************************************************************ - * Public Data - ************************************************************************************/ - -/************************************************************************************ - * Public Functions + * Public Function Prototypes ************************************************************************************/ #undef EXTERN