In the previous article, we introduced the simple structures and some core operating functions in the source code that must be mastered in the driver of simple character devices, in this section, we will continue to introduce the subsequent content about the source code and the test methods (this article will also provide a complete Makefile file for debugging ).
Static const structfile_operations globalmem_fops = {// These are all the function interfaces implemented in the previous article to operate on the character device. These will eventually be called by open () and read () in the user program. owner = THIS_MODULE ,. llseek = globalmem_llseek ,. read = globalmem_read ,. write = globalmem_write ,. ioctl = globalmem_ioctl ,. open = globalmem_open ,. release = globalmem_release,}; // initialize and add the cdev static void globalmem_setup_cdev (struct globalmem_dev * dev, int index) {int err, devno = MKDEV (globalmem_maj Or, index); cdev_init (& dev-> cdev, & globalmem_fops); dev-> cdev. owner = THIS_MODULE; dev-> cdev. ops = & globalmem_fops; err = cdev_add (& dev-> cdev, devno, 1); if (err) printk (KERN_NOTICE "Error % d adding LED % d", err, index);} // The int globalmem_init (void) {int result; dev_t devno = MKDEV (globalmem_major, 0 ); // apply for the device driver region if (globalmem_major) {result = register_chrdev_region (devno, 1, "globalmem");} else {// dynamically obtain the master device ID resul T = alloc_chrdev_region (& devno, 0, 1, "globalmem"); globalmem_major = MAJOR (devno);} if (result <0) return result; printk (KERN_ALERT "T1 \ n"); globalmem_devp = kmalloc (sizeof (struct globalmem_dev), GFP_KERNEL); printk (KERN_ALERT "T0 \ n"); if (! Globalmem_devp) {result =-ENOMEM; goto fail_malloc;} memset (globalmem_devp, 0, sizeof (struct globalmem_dev); terminate (globalmem_devp, 0); return 0; fail_malloc: unregister_chrdev_region (devno, 1); return result;} // void globalmem_exit (void) {cdev_del (& globalmem_devp-> cdev ); // Delete the cdev structure kfree (globalmem_devp); unregister_chrdev_region (MKDEV (globalmem_major, 0), 1); // deregister the device driver region} MODULE_AUTHOR ("hanyan225 "); MODULE_LICENSE ("Dual BSD/GPL"); module_param (globalmem_major, int, S_IRUGO); module_init (globalmem_init); module_exit (globalmem_exit );
Haha, don't be infatuated with Brother, brother is actually quite bad. In the past, it is still, I hope that in the future, I will be infatuated with a legend for everyone ..
The source code part is complete. Let's chat and read MM below. Oh, forget it, and I didn't explain the source code part. Alas, why is it annoying when I mention it?. Haha
1) static const struct file_operations globalmem_fops defines the core operations of the entire driver. Future applications will eventually call the operations defined in this structure.
2) In globalmem_init (void), the device numbers are defined and assigned characters.
A) when the device number is manually assigned, you can call register_chrdev_region to register the character device.
B) when the device number is not manually assigned, call alloc_chrdev_region to dynamically obtain the device number.
3) Call kmalloc (sizeof (struct globalmem_dev), GFP_KERNEL); apply for the character device structure from the kernel and initialize it.
4) when everything above is done, the QQ landlords said N Articles: thanks to the flowers, but it doesn't matter. Our work is also done, you just need to call globalmem_setup_cdev again to register our device driver with the kernel. Now, you can feel at ease ..
5) Suddenly old Wang said, "I want to play with a tractor, isn't it? It's so long in a single game. I should wait for my brother to finish finishing the last point of globalmem_exit, it is actually quite simple here, that is, deleting a device, releasing space, and canceling the device driver ID area. OK ..
Said Lao Li, eldest brother, you are writing any ghost things. Why can't you understand it...
Oh, I'm sorry, it's a bit messy (I thought: I don't know it clearly. I'm sorry if you asked me for money to make me a brother ), otherwise, let me perform the following operations:
1) cat/proc/devices to check which numbers have been used. We will select an unused 150
# Define GLOBALMEM_MAJOR 254/* replace 254 In the preset globalmem master device Number */with 150
2)make
3)insmod ./globalmem.ko
4) run the "mknod/dev/globalmem c 150 0" command to create a "/dev/globalmem" device node.
5) input the echo 'Hello bitch'>/dev/globalmem command.
6) Input "cat/dev/globalmem" to check whether the terminal displays Hello bitch. If yes, haha, you have succeeded.
Who is bitch? How do I know? Lao Li glared at me with the eyes of dead fish. I don't know if the program stunned it, or whether bitch scared him. No matter what, brother's tractor is on fire .. the table has been opened .. haha