2013-07-05 37 views
0

我有一個gpio設備。 我有一個內核模塊來處理這個設備。一個物理設備 - >兩個字符設備

//headers 
#include <linux/module.h> 
#include <linux/init.h> 
... 

//declarations 
static si_t *gpio_sih; 
static int gpio_major; 
static struct class *gpiodev_class = NULL; 

//some functions for open, read, write, etc 
... 

static struct file_operations gpio_fops = {...} // <- look up 

static int __init 
gpio_init(void) 
{ 
    if (!(gpio_sih = si_kattach(SI_OSH))) 
     return -ENODEV; 
    if ((gpio_major = register_chrdev(0, "gpio", &gpio_fops)) < 0) 
     return gpio_major; 
    gpiodev_class = class_create(THIS_MODULE, "gpio"); 
    if (IS_ERR(gpiodev_class)) { 
     printk("Error creating gpio class\n"); 
     return -1; 
    } 
    class_device_create(gpiodev_class, NULL, MKDEV(gpio_major, 0), NULL, "gpio"); 
    return 0; 
} 

static void __exit 
gpio_exit(void) 
{ 
    if (gpiodev_class != NULL) { 
     class_device_destroy(gpiodev_class, MKDEV(gpio_major, 0)); 
     class_destroy(gpiodev_class); 
    } 
    gpiodev_class = NULL; 
    if (gpio_major >= 0) 
     unregister_chrdev(gpio_major, "gpio"); 
    si_detach(gpio_sih); 
} 

module_init(gpio_init); 
module_exit(gpio_exit); 

我需要與另外這款物理設備工作的另一個內核模塊,但它必須在系統中的另一個字符設備與其他文件操作。我該怎麼辦?用類比法寫一個模塊?

//headers 
#include <linux/module.h> 
#include <linux/init.h> 
... 

//declarations 
static si_t *another_gpio_sih; 
static int another_gpio_major; 
static struct class *another_gpiodev_class = NULL; 

//some functions for another_open, another_read, another_write, another_etc 
... 

static struct file_operations another_gpio_fops = {...} // <- look up 

static int __init 
another_gpio_init(void) 
{ 
    if (!(another_gpio_sih = si_kattach(SI_OSH))) 
     return -ENODEV; 
    if ((another_gpio_major = register_chrdev(0, "another_gpio", &another_gpio_fops)) < 0) 
     return gpio_major; 
    another_gpiodev_class = class_create(THIS_MODULE, "another_gpio"); 
    if (IS_ERR(another_gpiodev_class)) { 
     printk("Error creating gpio class\n"); 
     return -1; 
    } 
    class_device_create(another_gpiodev_class, NULL, MKDEV(another_gpio_major, 0), NULL, "another_gpio"); 
    return 0; 
} 

static void __exit 
another_gpio_exit(void) 
{ 
    if (another_gpiodev_class != NULL) { 
     class_device_destroy(another_gpiodev_class, MKDEV(another_gpio_major, 0)); 
     class_destroy(another_gpiodev_class); 
    } 
    another_gpiodev_class = NULL; 
    if (another_gpio_major >= 0) 
     unregister_chrdev(another_gpio_major, "another_gpio"); 
    si_detach(another_gpio_sih); 
} 

module_init(another_gpio_init); 
module_exit(another_gpio_exit); 

或者使用導出全局變量?

EXPORT_SYMBOL(gpio_sih); 
EXPORT_SYMBOL(gpiodev_class); 

extern static si_t *another_gpio_sih; 
extern static struct class *another_gpiodev_class; 

感謝。

回答

0

首先爲什麼需要所有設備?爲什麼不使用標準內核接口: /sys/class/gpio?

它允許從用戶空間讀取,寫入和等待改變gpio。

二。如果你有兩個具有幾乎相同功能的字符設備,爲什麼不在一個驅動程序中處理它們, 將它們設置爲一個主號碼和兩個不同的次要號碼,正確實現從file_operation打開模擬,並且一切正常。

+0

/sys/class/gpio是在第一個模塊中創建的,如果我理解正確的話。這是開發人員的驅動程序。 我的任務是編寫一個額外的功能,用於在_single_模塊中處理值gpio-ports。這是一項要求。 要在另一個模塊中操作設備,我必須導出所有需要的處理程序或創建新的,不是嗎? 如果您將您的建議轉移到我的問題,那麼我必須執行導出並創建一個具有不同次要編號的設備。這樣對嗎? – someuser

+0

我們之間有一些誤會。 1)通過/ sys/class/gpio工作,你只需要調用gpiochip_add。根據你的代碼你不是。所以你發明了一些自行車與用戶空間合作而不是標準的方式。 – fghj

+0

2)你導出字符設備,通常這是用來幫助用戶空間程序處理一些東西(在你的情況下gpio值)。這是不希望你使用字符設備作爲模塊之間的合作的方法。如果我們討論gpio,那麼在一個驅動程序中共享這些東西的正常方式是gpiochip_add,而gpio_request_one,gpio_get_value,gpio_set_value等等。我想當你談到另一個字符設備時,你想從同一個設備中導出更多的gpi。 – fghj