You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lkmpg.tex
+37Lines changed: 37 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -896,6 +896,43 @@ \subsection{Registering A Device}
896
896
Second, the newly registered device will have an entry in \verb|/proc/devices|, and we can either make the device file by hand or write a shell script to read the file in and make the device file.
897
897
The third method is that we can have our driver make the device file using the \cpp|device_create| function after a successful registration and \cpp|device_destroy| during the call to \cpp|cleanup_module|.
898
898
899
+
However, \cpp|register_chrdev()| would occupy a range of minor numbers associated with the given major.
900
+
The recommended way to reduce waste for char device registration is using cdev interface.
901
+
902
+
The newer interface completes the char device registration in two distinct steps.
903
+
First, we should register a range of device numbers, which can be completed with \cpp|register_chrdev_region| or \cpp|alloc_chrdev_region|.
904
+
905
+
\begin{code}
906
+
int register_chrdev_region(dev_t from, unsigned count, const char *name);
907
+
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name);
908
+
\end{code}
909
+
910
+
The choose of two different functions depend on whether you know the major numbers for your device.
911
+
Using \cpp|register_chrdev_region| if you know the device major number and \cpp|alloc_chrdev_region| if you would like to allocate a dynamicly-allocated major number.
912
+
913
+
Second, we should initialize the data structure \cpp|struct cdev| for our char device and associate it with the device numbers.
914
+
To initialize the \cpp|struct cdev|, we can achieve by the similar sequence of the following codes.
915
+
916
+
\begin{code}
917
+
struct cdev *my_dev = cdev_alloc();
918
+
my_cdev->ops = &my_fops;
919
+
\end{code}
920
+
921
+
However, the common usage pattern will embed the \cpp|struct cdev| within a device-specific structure of your own.
922
+
In this case, we'll need \cpp|cdev_init| for the initialization.
0 commit comments