资源描述:
《open系统调用在内核中的流程分析.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、一)驱动注册open函数都干了些什么?register_chrdev->cdev_add->kobj_mapfile:fs/char_dev.cintregister_chrdev(unsignedintmajor,constchar*name,conststructfile_operations*fops){structchar_device_struct*cd;structcdev*cdev;char*s;interr=-ENOMEM;cd=__register_chrdev_region(major,0,256,name);if(IS
2、_ERR(cd))returnPTR_ERR(cd);cdev=cdev_alloc();if(!cdev)gotoout2;cdev->owner=fops->owner;cdev->ops=fops;//注意,在后面的chrdev_open会从cdev再得到fops...}file:fs/char_dev.cintcdev_add(structcdev*p,dev_tdev,unsignedcount){p->dev=dev;p->count=count;returnkobj_map(cdev_map,dev,count,NULL,ex
3、act_match,exact_lock,p);}file:fs/char_dev.cstaticstructkobject*exact_match(dev_tdev,int*part,void*data){structcdev*p=data;return&p->kobj;}file:drivers/base/map.cintkobj_map(structkobj_map*domain,dev_tdev,unsignedlongrange,structmodule*module,kobj_probe_t*probe,int(*lock)(d
4、ev_t,void*),void*data){unsignedn=MAJOR(dev+range-1)-MAJOR(dev)+1;unsignedindex=MAJOR(dev);unsignedi;structprobe*p;if(n>255)n=255;p=kmalloc(sizeof(structprobe)*n,GFP_KERNEL);if(p==NULL)return-ENOMEM;for(i=0;iowner=module;p->get=probe;//此处其实就是exact_matchp->loc
5、k=lock;p->dev=dev;p->range=range;p->data=data;}mutex_lock(domain->lock);for(i=0,p-=n;iprobes[index%255];while(*s&&(*s)->rangenext;p->next=*s;*s=p;}mutex_unlock(domain->lock);return0;}【参考UnderstandingTheLinuxKernel
6、13.5.CharacterDeviceDrivers】Thedevicedrivermodeldefinesakobjectmappingdomainforthecharacterdevices,whichisrepresentedbyadescriptoroftypekobj_mapandisreferencedbythecdev_mapglobalvariable.Thekobj_mapdescriptorincludesahashtableof255entriesindexedbythemajornumberoftheinterva
7、ls.Thehashtablestoresobjectsoftypeprobe,oneforeachregisteredrangeofmajorandminornumbers,whosefieldsarelistedinTable13-9.Whenthekobj_map()functionisinvoked,thespecifiedintervalofdevicenumbersisaddedtothehashtable.Thedatafieldofthecorrespondingprobeobjectpointstothecdevdescr
8、iptorofthedevicedriver.Thevalueofthisfieldispassedtothegetandlockmethodswhentheyareexecut