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
To read or write attributes, \cpp|show()| or \cpp|store()| method must be specified when declaring the attribute.
1292
+
For the common cases \src{include/linux/sysfs.h} provides convenience macros (\cpp|__ATTR|, \cpp|__ATTR_RO|, \cpp|__ATTR_WO|, etc.) to make defining attributes easier as well as making code more concise and readable.
1293
+
1260
1294
An example of a hello world module which includes the creation of a variable accessible via sysfs is given below.
1261
1295
1262
1296
\samplec{examples/hello-sysfs.c}
@@ -1293,6 +1327,12 @@ \section{sysfs: Interacting with your module}
1293
1327
sudo rmmod hello_sysfs
1294
1328
\end{codebash}
1295
1329
1330
+
In the above case, we use a simple kobject to create a directory under sysfs, and communicate with its attributes.
1331
+
Since Linux v2.6.0, the \cpp|kobject| structure made its appearance.
1332
+
It was initially meant as a simple way of unifying kernel code which manages reference counted objects.
1333
+
After a bit of mission creep, it is now the glue that holds much of the device model and its sysfs interface together.
1334
+
For more information about kobject and sysfs, see \src{Documentation/driver-api/driver-model/driver.rst} and \url{https://lwn.net/Articles/51437/}.
1335
+
1296
1336
\section{Talking To Device Files}
1297
1337
\label{sec:device_files}
1298
1338
Device files are supposed to represent physical devices.
vinput devices are created and destroyed using sysfs.
1898
1938
And, event injection is done through a \verb|/dev| node.
1899
1939
The device name will be used by the userland to export a new virtual input device.
1940
+
1941
+
The \cpp|class_attribute| structure is similar to other attribute types we talked about in section \ref{sec:sysfs}:
1942
+
1943
+
\begin{code}
1944
+
struct class_attribute {
1945
+
struct attribute attr;
1946
+
ssize_t (*show)(struct class *class, struct class_attribute *attr,
1947
+
char *buf);
1948
+
ssize_t (*store)(struct class *class, struct class_attribute *attr,
1949
+
const char *buf, size_t count);
1950
+
};
1951
+
\end{code}
1952
+
1953
+
In \verb|vinput.c|, the macro \cpp|CLASS_ATTR_WO(export/unexport)| defined in \src{include/linux/device.h} (in this case, \verb|device.h| is included in \src{include/linux/input.h}) will generate the \cpp|class_attribute| structures which are named \verb|class_attr_export/unexport|.
1954
+
Then, put them into \cpp|vinput_class_attrs| array and the macro \cpp|ATTRIBUTE_GROUPS(vinput_class)| will generate the \cpp|struct attribute_group vinput_class_group| that should be assigned in \cpp|vinput_class|.
1955
+
Finally, call \cpp|class_register(&vinput_class)| to create attributes in sysfs.
1956
+
1900
1957
To create a \verb|vinputX| sysfs entry and \verb|/dev| node.
0 commit comments