瀏覽代碼

修复钱箱问题。支持一个进程读,一个进程写

liu qidong 3 周之前
父節點
當前提交
b162b9ec86
共有 1 個文件被更改,包括 9 次插入3 次删除
  1. 9 3
      cash_drawers.c

+ 9 - 3
cash_drawers.c

@@ -36,6 +36,7 @@ struct cashd_device {
     unsigned int status_changed;
     int last_status;
     int first_read;
+    int open_count;
     struct delayed_work delay_work1;
     struct delayed_work delay_work2;
     void __iomem *gpio_ctl_reg_base;
@@ -100,6 +101,7 @@ static int cashd_open(struct inode *inode, struct file *filp)
             break;
         }
     }
+    dev->open_count++;
     
     if (!dev) {
         pr_err("cashd: Device not initialized for minor %d\n", minor);
@@ -107,7 +109,6 @@ static int cashd_open(struct inode *inode, struct file *filp)
     }
 
     filp->private_data = dev;
-    dev->first_read = 0;
     pr_info("cashd: Device /dev/cashd%d opened\n", minor);
     
     return 0;
@@ -115,8 +116,12 @@ static int cashd_open(struct inode *inode, struct file *filp)
 
 static int cashd_release(struct inode *inode, struct file *filp)
 {
-    unsigned int minor = iminor(inode);
-    pr_info("cashd: Device /dev/cashd%d closed\n", minor);
+    struct cashd_device *dev = filp->private_data;
+    dev->open_count--;
+    if(dev->open_count == 0)
+    {        
+        dev->first_read = 0;
+    }
     return 0;
 }
 
@@ -329,6 +334,7 @@ int cashd_init(void)
             cashd_devices[i]->gpio_status_reg_base = ioremap(GPIO1_STATUS, 4);            
         }
         cashd_devices[i]->last_status = readl(cashd_devices[i]->gpio_status_reg_base) & 0x2;
+        cashd_devices[i]->open_count = 0;
         schedule_delayed_work(&cashd_devices[i]->delay_work2, msecs_to_jiffies(100));
         
         pr_info("cashd: Created /dev/cashd%d\n", i);