Graphical Cdev_add () function

Source: Internet
Author: User

graphical Cdev_add () function




You need to use the Cdev_add () function provided by the kernel to add your own CDEV structure When writing character drivers .


1. Cdev_add () after function . We finally formed the structure as above . It's a Hash linked list structure .

so who is the hash value? This hash only extracts a master device number with a maximum value of 254.

2. when adding a new device . we will first use the main device number %255 to get an array subscript, and then directly access the subscript in the

Structprobe the address of a pointer to a struct type .


3. Structprobe the address of the pointer to the struct type . gave a level two pointer s.


4. There are two types of situations :

4.1. . (*s) ==null words . (*s)->range < range End Loop .

Direct the (*s) assign a value to p→next, that is, to speak p->next= NULL; How to modify (*s) value , modify the address .





< Span lang= "ZH-CN" > We need to assign the prob structure P , prob value modified to p .

then we need to use the address of the prob saved in S. through this address . to modify the value of the . Prob.

that is, modify the original of the first-hand pointer .

int Kobj_map (struct kobj_map *domain, dev_t Dev, unsigned long range, struct module *module, kobj_probe_t *probe, Int (*lock) (dev_t, void *), void *data) {unsigned n = MAJOR (dev + range-1)-MAJOR (dev) + 1;unsigned index = MAJOR (dev); nsigned i;struct probe *p;if (n > 255) n = 255;p = kmalloc (sizeof (struct probe) * N, Gfp_kernel); if (p = = NULL) return-e Nomem;for (i = 0; i < n; i++, p++) {P->owner = Module;p->get = Probe;p->lock = Lock;p->dev = Dev;p->rang E = Range;p->data = data;} Mutex_lock (Domain->lock); for (i = 0, p-= n; i < n; i++, p++, index++) {//Find a probe pointer to the location that corresponds to the main device number. or This means                                 The address of the pin. struct probe **s = &domain->probes[index% 255]; Determines whether the *s is empty. Stops the loop if it is empty. or out of range to stop the loop. while (*s && (*s)->range < range)//otherwise s = & (*s)->next; Straight                         To S is empty. That is to find the tail of the list. S = & (*s)->next;   If there are no elements in the list, speak p->next = *s; that is, p->next = NULL;                  Then change the original level of the pointer is *s = p; P->next = NULL;                       *s = p;                                       P->next = *s; *s = P;} Mutex_unlock (domain->lock); return 0;} Add Cdev to Kobj_map, this map is a single-linked list of hashes.





Graphical Cdev_add () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.