i**p 发帖数: 902 | 1 【 以下文字转载自 Linux 讨论区 】
发信人: isup (No), 信区: Linux
标 题: 并口驱动的一个问题
发信站: BBS 未名空间站 (Wed May 21 21:03:59 2014, 美东)
最近编译运行了《Essential Linux Device Drivers》第5章的例子,
Driver for the Parallel LED Board (led.c)。程序不复杂。其中有一个函数led_
attach(struct parport *port) 是由parport_register_driver(&led_driver)注册的。
请问,led_attach()是何时被调用的?或者说怎么引起系统调用这个函数?
static void led_attach(struct parport *port)
{
/* Register the parallel LED device with parport */
pdev = parport_register_device(port, DEVICE_NAME,
led_preempt, NULL, NULL, 0, NULL);
if (pdev == NULL) printk("Bad registern");
} | w***g 发帖数: 5958 | 2 我觉得是insmod后的某一步被调用的。
的。
【在 i**p 的大作中提到】 : 【 以下文字转载自 Linux 讨论区 】 : 发信人: isup (No), 信区: Linux : 标 题: 并口驱动的一个问题 : 发信站: BBS 未名空间站 (Wed May 21 21:03:59 2014, 美东) : 最近编译运行了《Essential Linux Device Drivers》第5章的例子, : Driver for the Parallel LED Board (led.c)。程序不复杂。其中有一个函数led_ : attach(struct parport *port) 是由parport_register_driver(&led_driver)注册的。 : 请问,led_attach()是何时被调用的?或者说怎么引起系统调用这个函数? : static void led_attach(struct parport *port) : {
| i**p 发帖数: 902 | 3 insmod succeeds. I can see the _init is called, but attch() is never called
though it is registed in _init().
After a while I write to the device, I can see open(), write() and release()
but
still no attach().
Here is what the book says.
"When the kernel finds the LED board during led_attach(), it registers the
device by invoking parport_register_device()"
The LED board is simply a LED circuit. I do have it attached.
【在 w***g 的大作中提到】 : 我觉得是insmod后的某一步被调用的。 : : 的。
| w***g 发帖数: 5958 | 4 我也不懂,google出来了http://www.spinics.net/lists/newbies/msg38087.html
called
()
【在 i**p 的大作中提到】 : insmod succeeds. I can see the _init is called, but attch() is never called : though it is registed in _init(). : After a while I write to the device, I can see open(), write() and release() : but : still no attach(). : Here is what the book says. : "When the kernel finds the LED board during led_attach(), it registers the : device by invoking parport_register_device()" : The LED board is simply a LED circuit. I do have it attached.
| i**p 发帖数: 902 | 5 Thanks!
【在 w***g 的大作中提到】 : 我也不懂,google出来了http://www.spinics.net/lists/newbies/msg38087.html : : called : ()
| i**p 发帖数: 902 | 6 I double checked my code again. Though I use the same name as the link http://www.spinics.net/lists/newbies/msg38087.html suggested, the
led_attach() is not called.
code is here.
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "led"
static dev_t dev_number; /* Allotted device number */
static struct class *led_class; /* Class to which this device belongs */
struct cdev led_cdev; /* Associated cdev */
struct pardevice *pdev; /* Parallel port device */
/* LED open */
int led_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_open(major: %d, minor: %d)n",
current->pid, current->comm, imajor(inode), iminor(inode));
return 0;
}
/* Write to the LED */
ssize_t led_write(struct file *file, const char *buf, size_t count, loff_t *
ppos)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_write(count: %d, *ppos: %lld)n",
current->pid, current->comm, count, *ppos);
return count;
}
/* Release the device */
int led_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_release()n", current->pid, current
->comm);
return 0;
}
/* File Operations */
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
static int led_preempt(void *handle)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_preempt()n", current->pid, current
->comm);
return 1;
}
/* Parport attach method */
static void led_attach(struct parport *port)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_attach()n", current->pid, current-
>comm);
/* Register the parallel LED device with parport */
pdev = parport_register_device(port, DEVICE_NAME,
led_preempt, NULL,
NULL, 0, NULL);
if (pdev == NULL) printk("Bad registern");
}
/* Parport detach method */
static void led_detach(struct parport *port)
{
printk(KERN_INFO "(pid:%d, cmd:%s)led_detach() Port Detachedn", current-
>pid, current->comm);
/* Do nothing */
parport_unregister_device(pdev);
}
/* Parport driver operations */
static struct parport_driver led_driver = {
.name = DEVICE_NAME,
.attach = led_attach,
.detach = led_detach,
};
/* Driver Initialization */
int __init led_init(void)
{
printk("led_init()n");
/* Request dynamic allocation of a device major number */
if (alloc_chrdev_region(&dev_number, 0, 1, DEVICE_NAME) < 0) {
printk(KERN_DEBUG "Can't register devicen");
return -1;
}
/* Create the led class */
led_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(led_class)) printk("Bad class createn");
/* Connect the file operations with the cdev */
cdev_init(&led_cdev, &led_fops);
led_cdev.owner = THIS_MODULE;
/* Connect the major/minor number to the cdev */
if (cdev_add(&led_cdev, dev_number, 1)) {
printk("Bad cdev addn");
return 1;
}
//class_device_create(led_class, NULL, dev_number,
device_create(led_class, NULL, dev_number, NULL, DEVICE_NAME);
/* Register this driver with parport */
// int parport_register_driver (struct parport_driver * drv);
if (parport_register_driver(&led_driver)) {
printk(KERN_ERR "Bad Parport Registern");
return -EIO;
}
return 0;
}
/* Driver Exit */
void __exit led_cleanup(void)
{
printk("led_cleanup()n");
//void parport_unregister_driver (struct parport_driver * arg);
parport_unregister_driver(&led_driver);
//class_device_destroy(led_class, MKDEV(MAJOR(dev_number), 0));
device_destroy(led_class, dev_number);
cdev_del(&led_cdev);
class_destroy(led_class);
unregister_chrdev_region(dev_number, 1);
return;
}
module_init(led_init);
module_exit(led_cleanup);
MODULE_LICENSE("Dual BSD/GPL");
【在 i**p 的大作中提到】 : Thanks!
|
|