فهرست منبع

添加switch模块读取GPIO状态的功能

monkeylqd 2 هفته پیش
والد
کامیت
6d2d74626c
1فایلهای تغییر یافته به همراه28 افزوده شده و 59 حذف شده
  1. 28 59
      switches.c

+ 28 - 59
switches.c

@@ -79,8 +79,22 @@ unsigned int switch_readl(unsigned int addr)
 }
 int get_switch_gpio_status(void)
 {
+    dev->switch_status[0] = 0;
+    dev->switch_status[1] = 0;
     // 这里需要待完善,确认熊工提供的地址和API文档的对应关系
-    dev->switch_status[0] = readl(SWITCH_F12);
+    dev->switch_status[0] |= (switch_readl(SWITCH_F12)&0x02) >> 1;
+    dev->switch_status[0] |= switch_readl(SWITCH_F16)&0x02;
+    dev->switch_status[0] |= (switch_readl(SWITCH_F17)&0x02) << 1;
+    dev->switch_status[0] |= (switch_readl(SWITCH_F18)%0x02) << 2;
+
+    
+    dev->switch_status[1] |= (switch_readl(SWITCH_ID0)&0x02) >> 1;
+    dev->switch_status[1] |= switch_readl(SWITCH_ID1)&0x02;
+    dev->switch_status[1] |= (switch_readl(SWITCH_ID2)&0x02) << 1;
+    dev->switch_status[1] |= (switch_readl(SWITCH_ID3)%0x02) << 2;
+    dev->switch_status[1] |= (switch_readl(SWITCH_ID4)%0x02) << 3;
+
+
     return 0;
 }
 
@@ -119,30 +133,22 @@ static ssize_t switches_read(struct file *filp, char __user *buf,
     size_t available;
     int read_count = 0;
     int ret = 0;
-
-    if (mutex_lock_interruptible(&dev->lock))
-        return -ERESTARTSYS;
-
-    // 这里read_count没有固定为4个字节,是为了便于后续扩展
-    if (count > dev->size)
-    {
-        read_count = dev->size;
-    }
-    else
+    int i = 0;
+    // ret = copy_to_user(buf, dev->switch_status, read_count);
+    // if (ret != 0)
+    // {
+    //     printk(KERN_INFO "switches: copy_to_user failed\n");
+    //     goto out;
+    // }
+    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
+    ret = get_switch_gpio_status();
+    for(i = 0; i < 5; i++)
     {
-        read_count = count;
-    }
-    ret = copy_to_user(buf, dev->buffer, read_count);
-    if (ret != 0)
-    {
-        printk(KERN_INFO "switches: copy_to_user failed\n");
-        goto out;
+        printk("switch_status[%d]=%08x\n", i, dev->switch_status[i]);
     }
+    // printk(KERN_INFO "switches: Read %zu bytes\n", count);
 
-    printk(KERN_INFO "switches: Read %zu bytes\n", count);
 
-out:
-    mutex_unlock(&dev->lock);
     return ret;
 }
 
@@ -150,44 +156,7 @@ out:
 static ssize_t switches_write(struct file *filp, const char __user *buf,
                               size_t count, loff_t *f_pos)
 {
-    struct switches_dev *dev = filp->private_data;
-    ssize_t retval = 0;
-    size_t available;
-    int ret = 0;
-
-    // 加锁
-    if (mutex_lock_interruptible(&dev->lock))
-    {
-        return -ERESTARTSYS;
-    }
-
-    // 计算可写数据量
-    if (count > BUFFER_SIZE)
-    {
-        count = BUFFER_SIZE;
-    }
-
-    // 拷贝数据
-    ret = copy_from_user(dev->buffer, buf, count);
-    if (ret != 0)
-    {
-        printk(KERN_INFO "switches: copy_from_user failed\n");
-        goto out;
-    }
-
-    dev->size = count;
-
-    printk(KERN_INFO "switches: Written %zu bytes\n", count);
-
-out:
-    mutex_unlock(&dev->lock);
-
-    // 调度延迟工作
-    if (ret == 0)
-    {
-        schedule_delayed_work(&dev->delay_work1, msecs_to_jiffies(10));
-    }
-    return count;
+    return -1;
 }
 
 static unsigned int switches_poll(struct file *filp, poll_table *wait)