Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1286365
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: LINUX

2011-02-20 19:48:30

/****************************************
 *	PCI接口的char设备驱动模板
 *  . PCI接口和platform很类似: 
 * 		pci_register_driver(struct pci_driver *pci_drv);
 *		pci_unregister_driver(struct pci_driver *pci_drv);
 *  . 主要实现以下两部分:
 * 		1. struct pci_driver的成员函数(主要是probe, remove);
 *		2. 字符设备的struct file_operations成员函数.
 ****************************************/

/* 指明该驱动程序适用于哪一些PCI设备 */
static struct pci_device_id xxx_pci_tbl[] __initdata = {
	{PCI_VENDOR_ID_DEMO, PCI_DEVICE_ID_DEMO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEMO},
	{0, }
};

/* 中断处理函数 */
static void xxx_interrupt(int irqno, void *dev_id)
{
	...
}

/* char设备struct file_operations open成员函数*/
static int xxx_open(struct inode *inode, struct file *filp)
{
	/* 申请中断,注册中断处理程序 */
	request_irq(xxx_irq, &xxx_interrupt, ...);
	...
}

/* 字符设备struct file_operations ioctl成员函数 */
static int xxx_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
	...
}

...		/* 字符设备struct file_operations read、write、mmap等成员函数 */

/* 设备文件操作接口 */
static struct file_operations xxx_fops = {
	owner:	THIS_MODULE,
	read:	xxx_read,
	write:	xxx_write,
	ioctl:	xxx_ioctl,
	mmap:	xxx_mmap,
	open:	xxx_open,
	release:	xxx_release,
	/* ... */
};

/* struct pci_driver的probe成员函数 */
static int __init xxx_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
{
	pci_enable_device(pci_dev);				/* 启动PCI设备 */

	/* 读取PCI配置信息 */
	iobase = pci_resource_start(pci_dev, 1);
	ioend = pci_resource_end(pci_dev, 1);
	ioflags = pci_resource_flags(pci_dev, 1);
	iolen = pci_resource_len(pci_dev, 1);
		...

	pci_set_master(pci_dev);				/* 设置成总线主DMA模式*/

	pci_register_regions(pci_dev);			/* 申请I/O资源 */

	/* 注册字符设备 */
	cdev_init(xxx_cdev, &xxx_fops);
	register_chrdev_region(xxx_dev_no, 1, ...);
	cdev_add(xxx_cdev);

	return 0;
}

/* struct pci_driver的remove成员函数 */
static int __init xxx_release(struct pci_dev *pdev)
{
	pci_release_regions(pdev);				/* 释放I/O资源 */
	pci_disable_device(pdev);				/* 禁止PCI设备 */
	unregister_chrdev_region(xxx_dev_no, 1);		/* 释放占用的设备号 */
	cdev_del(&xxx_dev.cdev);				/* 注销字符设备 */
	...
	return 0;
}

/* 设备模块信息 */
static struct pci_driver xxx_pci_driver = {
	name:		xxx_MODULE_NAME,
	id_table:	xxx_pci_tbl,
	probe:		xxx_probe,
	remove:		xxx_remove
};

static int __init xxx_init_module(void)
{
	pci_register_driver(&xxx_pci_driver);
}

static void __init xxx_cleanup_module(void)
{
	pci_unregister_driver(&xxx_pci_driver);
}

module_init(xxx_init_module);
moduel_exit(xxx_cleanup_module);


阅读(365) | 评论(0) | 转发(0) |
0

上一篇:drivernote

下一篇:英语单词

给主人留下些什么吧!~~