pkarashchenko commented on a change in pull request #5718:
URL: https://github.com/apache/incubator-nuttx/pull/5718#discussion_r824989145



##########
File path: drivers/can/can.c
##########
@@ -407,46 +406,48 @@ static int can_open(FAR struct file *filep)
    * for this device, then perform hardware initialization.
    */
 
-  if (list_is_empty(&dev->cd_readers))
+  caninfo("ocount: %u\n", dev->cd_crefs);
+
+  if (dev->cd_crefs >= 255)
     {
-      caninfo("ocount: %d\n", 0);
+      /* Limit to no more than 255 opens */
 
+      ret = -EMFILE;
+      goto errout;
+    }
+  else
+    {
       flags = enter_critical_section();
-      ret = dev_setup(dev);
-      if (ret >= 0)
+
+      if (dev->cd_crefs == 0)
         {
-          /* Mark the FIFOs empty */
+          ret = dev_setup(dev);
+          if (ret == OK)
+            {
+              /* Mark the FIFOs empty */
 
-          dev->cd_xmit.tx_head  = 0;
-          dev->cd_xmit.tx_queue = 0;
-          dev->cd_xmit.tx_tail  = 0;
+              dev->cd_xmit.tx_head  = 0;
+              dev->cd_xmit.tx_queue = 0;
+              dev->cd_xmit.tx_tail  = 0;
 
-          /* Finally, Enable the CAN RX interrupt */
+              /* Finally, Enable the CAN RX interrupt */
 
-          dev_rxint(dev, true);
+              dev_rxint(dev, true);
+            }
         }
 
-      list_add_head(&dev->cd_readers,
-                    (FAR struct list_node *)init_can_reader(filep));
-
-      leave_critical_section(flags);
-    }
-  else
-    {
-      tmp = list_length(&dev->cd_readers);
-      caninfo("ocount: %d\n", tmp);
-
-      if (tmp >= 255)
+      if (ret == OK)

Review comment:
       yes. this is expected. The reason for this check is not to increment 
reference counter if `dev_setup()` fails during the first init. I want to avoid 
a situation when failure is returned, but reference counter is incremented.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to