本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/z2007b/archive/2011/05/19/6433003.aspx
//*************************************************************************
Linux核心大講堂 (一) 裝置驅動的基石驅動模型(7)
終於快講完了,這一節其實就是讓上節我們所說的四大天王露下小臉。源碼分析請自行完成吧。我建議大家可以試著先看一下代碼,如果能看懂,那就把我寫的那破玩意刪掉,自已實現一遍,如果不能看懂,那馬上回過去從第一小節重新看過,耐心,一定要有耐心,沒耐心,啥都完了。帖上原碼:
Makefile:</p><p>obj-m+=wwhs_bus.o</p><p>KERNELDIR=/opt/kernel/linux-2.6.38/linux-2.6.38.5</p><p>PWD:=$(shell pwd)</p><p>all:</p><p>make -C $(KERNELDIR) M=$(PWD) modules</p><p>clean: </p><p>rm -rf *.o* *.ko* *.mod.c *.cmd *.symvers .tmp_versions .*.cmd</p><p>wwhs_public.h:</p><p>#include <linux/module.h></p><p>#include <linux/device.h></p><p>#include <linux/kernel.h></p><p>#include <linux/init.h></p><p>#include <linux/stat.h></p><p>#include <linux/slab.h></p><p>#include <linux/kobject.h></p><p>#include <linux/klist.h></p><p>#include <linux/kdev_t.h></p><p>#include <linux/fs.h></p><p>#include <linux/major.h></p><p>#include <linux/kprobes.h></p><p>#define wwhs_dbg(dbgbuf) printk(KERN_ERR"wwhs:%s/n",dbgbuf);</p><p>#include "../wwhs_public.h"</p><p>#define WWHS_MAJOR 83</p><p>void wwhs_release(struct device *dev)</p><p>{</p><p>printk(KERN_ERR"wwhs:dev:%s is release/n",dev->kobj.name);</p><p>}</p><p>struct device wwhs_bus = {</p><p>.init_name = "wwhs",</p><p>.release = wwhs_release,</p><p>};</p><p>static struct device wwhs_dev = {</p><p>.init_name = "wwhsled0",</p><p>.release = wwhs_release,</p><p>};</p><p>static int wwhs_bus_match(struct device *dev, struct device_driver *drv)</p><p>{</p><p>int ret = -1;</p><p>ret = strncmp(dev->kobj.name, drv->name, strlen(drv->name));</p><p>printk(KERN_ERR"wwhs:dev->kobj.name:%s drv->name:%s ret:%d/n",dev->kobj.name,drv->name,ret);</p><p>return !ret;</p><p>}</p><p>static int wwhs_bus_probe(struct device *dev)</p><p>{</p><p>int ret = -1;</p><p>if (dev->driver->probe)</p><p>ret = dev->driver->probe(dev);</p><p>return ret;</p><p>}</p><p>static struct bus_type wwhs_bus_type = {</p><p>.name = "wwhs",</p><p>.match = wwhs_bus_match,</p><p>};</p><p>static struct class *wwhs_class;</p><p>static int wwhs_bus_register()</p><p>{</p><p>int ret = -1;</p><p>wwhs_bus.class = wwhs_class;</p><p>ret = device_register(&wwhs_bus);</p><p>if (ret) goto device_error;</p><p>ret = bus_register(&wwhs_bus_type);</p><p> if (ret) goto bus_error;</p><p>return ret;</p><p>bus_error:</p><p>device_unregister(&wwhs_bus);</p><p>device_error:</p><p>return ret;</p><p>}</p><p>static void wwhs_bus_unregister()</p><p>{</p><p>device_unregister(&wwhs_bus);</p><p>bus_unregister(&wwhs_bus_type);</p><p>}</p><p>static int wwhs_class_register(void)</p><p>{</p><p>wwhs_class = class_create(THIS_MODULE, "wwhsclass");</p><p>if (IS_ERR(wwhs_class)){</p><p>printk(KERN_ERR"wwhs: class create error/n");</p><p>return PTR_ERR(wwhs_class);</p><p>}</p><p>printk(KERN_ERR"wwhs: class create success/n");</p><p>return 0;</p><p>}</p><p>static void wwhs_class_unregister()</p><p>{</p><p>class_destroy(wwhs_class);</p><p>}</p><p>static int wwhs_device_register(struct device *dev)</p><p>{ </p><p>dev->devt = MKDEV(WWHS_MAJOR,1);</p><p>dev->bus = &wwhs_bus_type;</p><p>dev->parent = &wwhs_bus;</p><p>return device_register(dev);</p><p>}</p><p>static void wwhs_device_unregister(struct device *wwhs_dev)</p><p>{</p><p>device_unregister(wwhs_dev); </p><p>}</p><p>static struct device_driver wwhs_drv = {</p><p>.name = "wwhsled",</p><p>};</p><p>static wwhs_drv_probe(struct device *dev)</p><p>{</p><p>printk(KERN_ERR"wwhs:dev:%s is probe/n",dev->kobj.name);</p><p>return 0;</p><p>}</p><p>static wwhs_drv_remove(struct device *dev)</p><p>{</p><p>printk(KERN_ERR"wwhs:dev:%s is remove/n",dev->kobj.name);</p><p>return 0;</p><p>}</p><p>static int wwhs_driver_register(struct device_driver *wwhs_driver)</p><p>{</p><p>int ret = -1;</p><p>wwhs_driver->bus = &wwhs_bus_type;</p><p>wwhs_driver->owner = THIS_MODULE;</p><p>wwhs_driver->probe = wwhs_drv_probe;</p><p>wwhs_driver->remove = wwhs_drv_remove;</p><p>ret = driver_register(wwhs_driver);</p><p>return ret;</p><p>}</p><p>static void wwhs_driver_unregister(struct device_driver *wwhs_driver)</p><p>{</p><p>driver_unregister(wwhs_driver);</p><p>}</p><p>static int __init wwhs_init()</p><p>{</p><p>int ret = -1;</p><p>ret = wwhs_class_register();</p><p>if (ret) goto error;</p><p>ret = wwhs_bus_register();</p><p>if (ret) goto error;</p><p>ret = wwhs_device_register(&wwhs_dev);</p><p>if (ret) goto bus_error;</p><p>ret = wwhs_driver_register(&wwhs_drv);</p><p>if (ret) goto device_error;</p><p>return ret;</p><p>device_error:</p><p>wwhs_device_unregister(&wwhs_dev);</p><p>bus_error:</p><p>wwhs_bus_unregister();</p><p>class_error:</p><p>wwhs_class_unregister();</p><p>error:</p><p>return ret;</p><p>}</p><p>static void __exit wwhs_exit()</p><p>{</p><p>wwhs_device_unregister(&wwhs_dev);</p><p>wwhs_driver_unregister(&wwhs_drv);</p><p>wwhs_bus_unregister();</p><p>wwhs_class_unregister();</p><p>}</p><p>module_init(wwhs_init);</p><p>module_exit(wwhs_exit);</p><p>MODULE_AUTHOR("wwhs");</p><p>MODULE_DESCRIPTION("wwhs_bus test");</p><p>MODULE_LICENSE("GPL");
上面就是四大天王小露一下臉,至此,驅動模型的分析要告一段落了。如果根著看了的朋友,我相信肯定是有所收穫的。謝謝各位。你們的支援是我前進的動力。^_^
裝置驅動模型就先講到這裡了,如果大家有什麼好的建議,請進:
http://blog.csdn.net/z2007b/archive/2011/05/19/6431791.aspx
這裡提出來。你們想要瞭解的,才是我想寫的,要不然我寫一堆大家都不感興趣的東西,就沒有價值了。謝謝。
上面範例程式碼:
http://d.download.csdn.net/down/3285515/z2007b