[FE training-materials-updates] [git/training-materials] master: Input lab simplifications (121af51)

Michael Opdenacker michael.opdenacker at free-electrons.com
Fri Dec 9 00:10:28 CET 2016


Repository : git://git.free-electrons.com/training-materials.git
On branch  : master
Link       : http://git.free-electrons.com/training-materials/commit/?id=121af515bc30f9cadb774fc2084f3607d822c32c

>---------------------------------------------------------------

commit 121af515bc30f9cadb774fc2084f3607d822c32c
Author: Michael Opdenacker <michael.opdenacker at free-electrons.com>
Date:   Thu Dec 8 23:42:58 2016 +0100

    Input lab simplifications
    
    - Knowing that polled_input device unregistration is automatic
      if devm_input_allocate_polled_device() was used.
    
    Signed-off-by: Michael Opdenacker <michael.opdenacker at free-electrons.com>


>---------------------------------------------------------------

121af515bc30f9cadb774fc2084f3607d822c32c
 .../kernel-i2c-communication.tex                   |   6 ++
 labs/kernel-i2c-input-interface/device-pointers.c  |   4 -
 .../input-device-attributes.c                      |   1 +
 .../kernel-i2c-input-interface.tex                 | 104 ++++++++++-----------
 slides/kernel-input/kernel-input.tex               |   3 +-
 5 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/labs/kernel-i2c-communication/kernel-i2c-communication.tex b/labs/kernel-i2c-communication/kernel-i2c-communication.tex
index 87f531c..ea50950 100644
--- a/labs/kernel-i2c-communication/kernel-i2c-communication.tex
+++ b/labs/kernel-i2c-communication/kernel-i2c-communication.tex
@@ -15,6 +15,12 @@ compiling (stay in the \code{nunchuk} branch), and in
 \code{~/linux-kernel-labs/modules/nfsroot/root/nunchuk} for module compiling
 (use two different terminals).
 
+\section{Remove debugging messages}
+
+Now that we have checked that the \code{probe()} and \code{remove()} functions
+are called, remove the \code{pr_info()} messages that you added to
+trace the execution of these functions.
+
 \section{Find pin muxing configuration information for i2c1}
 
 As you found in the previous lab, we now managed to have our nunchuk
diff --git a/labs/kernel-i2c-input-interface/device-pointers.c b/labs/kernel-i2c-input-interface/device-pointers.c
index 9e594da..46f0d9d 100644
--- a/labs/kernel-i2c-input-interface/device-pointers.c
+++ b/labs/kernel-i2c-input-interface/device-pointers.c
@@ -1,6 +1,2 @@
 nunchuk->i2c_client = client;
-nunchuk->polled_input = polled_input;
 polled_input->private = nunchuk;
-i2c_set_clientdata(client, nunchuk);
-input = polled_input->input;
-input->dev.parent = &client->dev;
diff --git a/labs/kernel-i2c-input-interface/input-device-attributes.c b/labs/kernel-i2c-input-interface/input-device-attributes.c
index 8dea4e1..87257c0 100644
--- a/labs/kernel-i2c-input-interface/input-device-attributes.c
+++ b/labs/kernel-i2c-input-interface/input-device-attributes.c
@@ -1,3 +1,4 @@
+input = polled_input->input;
 input->name = "Wii Nunchuk";
 input->id.bustype = BUS_I2C;
 
diff --git a/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex b/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
index 237f436..375f342 100644
--- a/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
+++ b/labs/kernel-i2c-input-interface/kernel-i2c-input-interface.tex
@@ -68,40 +68,70 @@ situations properly.
 
 \section{Implement the remove() function}
 
-We now need to unregister and release the resources allocated
+In this function, we need to unregister and release the resources allocated
 and registered in the \code{probe()} routine.
 
-Releasing will be done automatically when the device structures
-are destroyed when the module is removed, thanks to the use
-of the \code{devm_} allocation functions,
+Fortunately, in our case, there's nothing to do, as everything
+was allocated with \code{devm_} functions. Even the unregistration
+of the \code{input_polled_dev} structure is automated. 
 
-However, unregistration will not be trivial in the way we
-implemented the \code{probe()} routine.  As we have to support
-multiple devices, we chose not to use global variables, and as a
-consequence, we can't use such global variables to access the
-corresponding resources.
+Recompile your module, and load it and remove it multiple times, to
+make sure that everything is properly registered and automatically
+unregistered.
+
+\section{Add proper input device registration information}
+
+We actually need to add more information to the input structure before
+registering it. That's why we are getting the below warnings:
+
+\begin{verbatim}
+input: Unspecified device as /devices/virtual/input/input0
+\end{verbatim}
+
+Add the below lines of code (still before device registration, of
+course):
+
+\sourcecode{labs/kernel-i2c-input-interface/input-device-attributes.c}
+
+Recompile and reload your driver. You should now see in the kernel log
+that the \code{Unspecified device} type is replaced by
+\code{Wii Nunchuck} and that the physical path of the device is reported
+too.
+
+\section{Implement the polling routine}
+
+It's time to implement the routine which will poll the nunchuk registers
+at a regular interval.
+
+Create a \code{nunchuck_poll()} function with the right prototype (find
+it by looking at the definition of the \code{input_polled_dev} structure.
+
+In this function, you will have to read the nunchuk registers. However,
+as you can see, the prototype of the \code{poll()} routine doesn't
+carry any information about the \code{i2c_client} structure you will
+need to communicate with the device. That's normal as the input
+subsystem is generic, and can't be bound to any specific bus.
 
 This raises a very important aspect of the device model: the need to
 keep pointers between {\em physical} devices (devices as handled by the
-physical bus, I2C in our case) and {\em logical} devices (devices handled by subsystems,
-like the input subsystem in our case).
+physical bus, I2C in our case) and {\em logical} devices (devices
+handled by subsystems, like the input subsystem in our case).
 
-This way, when the \code{remove()} routine is called (typically if the
-bus detects the removal of a device), we can find out which logical
-device to unregister. Conversely, when we have an event on the logical
-side (such as opening or closing an input device for the first time),
-we can find out which i2c slave this corresponds to, to do the specific
-things with the hardware.
+This way, when the \code{remove()} routine is called, we can find out
+which logical device to unregister (though that's not necessary in our
+case as logical device unregistration is automatic). Conversely, when we
+have an event on the logical side (such as running the polling
+function), we can find out which i2c slave this corresponds to,
+to communicate with the hardware.
 
 This need is typically implemented by creating a {\em private} data
 structure to manage our device and implement such pointers between
 the physical and logical worlds.
 
-Add the below definition to your code:
+Add the below global definition to your code:
 
 \begin{verbatim}
 struct nunchuk_dev {
-        struct input_polled_dev *polled_input;
         struct i2c_client *i2c_client;
 };
 \end{verbatim}
@@ -125,7 +155,7 @@ We haven't explained the \code{dev_*} logging routines yet either
 associated to).  For the moment, just use the above code. You will get
 all the details later.
 
-Now implement the pointers:
+Now implement the pointers that we need:
 
 \sourcecode{labs/kernel-i2c-input-interface/device-pointers.c}
 
@@ -133,40 +163,6 @@ Make sure you add this code before registering the input device. You
 don't want to enable a device with incomplete information or when it is
 not completely yet (there could be race conditions).
 
-With all this in place, you now have everything you need to implement
-the \code{remove()} routine. Look at the I2C and input slides for
-examples, or directly find your examples in the Linux kernel code!
-
-Recompile your module, and load it and remove it multiple times, to
-make sure that everything is properly registered and unregistered.
-
-\section{Add proper input device registration information}
-
-We actually need to add more information to the input structure before
-registering it. That's why we are getting the below warnings:
-
-\begin{verbatim}
-input: Unspecified device as /devices/virtual/input/input0
-\end{verbatim}
-
-Add the below lines of code (still before device registration, of
-course):
-
-\sourcecode{labs/kernel-i2c-input-interface/input-device-attributes.c}
-
-Recompile and reload your driver. You should now see in the kernel log
-that the \code{Unspecified device} type is replaced by
-\code{Wii Nunchuck} and that the physical path of the device is reported
-too.
-
-\section{Implement the polling routine}
-
-It's time to implement the routine which will poll the nunchuk registers
-at a regular interval.
-
-Create a \code{nunchuck_poll()} function with the right prototype (find
-it by looking at the definition of the \code{input_polled_dev} structure.
-
 First, add lines retrieving the I2C physical device from the
 \code{input_polled_dev} structure. That's where you will need your
 private \code{nunchuk} structure.
diff --git a/slides/kernel-input/kernel-input.tex b/slides/kernel-input/kernel-input.tex
index 96ba590..a522111 100644
--- a/slides/kernel-input/kernel-input.tex
+++ b/slides/kernel-input/kernel-input.tex
@@ -166,7 +166,8 @@ struct input_polled_dev {
   \item The device registration/unregistration is done with:
     \begin{itemize}
     \item \code{input_register_polled_device(struct input_polled_dev *dev)}.
-    \item \code{input_unregister_polled_device(struct input_polled_dev *dev)}
+    \item Unregistration is automatic after using
+      \kfunc{devm_input_allocate_polled_device}!
     \end{itemize}
   \end{itemize}
 \end{frame}




More information about the training-materials-updates mailing list