├── .gitignore
├── Dockerfile
├── LICENSE
├── Makefile
├── README.md
├── scripts
└── build-kernel.sh
└── src
├── char_simple.c
├── hello-2.c
├── hello-3.c
├── hello_simple.c
├── jiffies_1.c
├── led.c
├── led.h
├── null_pointer.c
├── timer_hiresolution.c
└── timer_simple.c
/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 | *.ko
3 | *cmd
4 | *.swp
5 | *mod*
6 | *.symvers
7 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM centos:latest
2 |
3 | RUN yum -y groupinstall "Development Tools"
4 | RUN yum -y install ncurses-devel \
5 | hmaccalc \
6 | zlib-devel \
7 | binutils-devel \
8 | elfutils-libelf-devel \
9 | bc \
10 | openssl-devel \
11 | kernel-devel \
12 | kernel-headers
13 |
14 | COPY scripts/build-kernel.sh /
15 | CMD /build-kernel.sh
16 | # copy from linux/ the generated uimage to your mount volume
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | ifeq ($(strip $(MODULE)),)
2 | obj-m += src/hello_simple.o
3 | else
4 | obj-m += $(MODULE).o
5 | endif
6 |
7 |
8 | KERNEL_SRC = /lib/modules/$(shell uname -r)/build
9 |
10 | all:
11 | make -C $(KERNEL_SRC) M=$(PWD) modules
12 |
13 | clean:
14 | make -C $(KERNEL_SRC) M=$(PWD) clean
15 |
16 | image:
17 | docker build -t build_kernel .
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Linux Device Drivers Tutorial
2 |
3 | Hi, this is a compendium of multiple didactic Linux Kernel modules, I am doing
4 | this due to the fact that many times somebody says that the LDD book is too
5 | complicated, well here is a series of examples that might be helpful.
6 |
7 | In my opinion a really good start for really newbies Linux Modules Developers
8 | WAS (past time on purpose) The Linux Kernel Module Programming Guide
9 | (http://www.tldp.org/LDP/lkmpg/2.6/html/). However, this guide is based on the
10 | Kernel 2.6.. which has many deprecated parts in relationship with the 3.~
11 | kernel
12 |
13 | Well this project tries to update the source code of that guide to the latest
14 | a version of the kernel (please update and improve as possible), hopefully, if
15 | many folks agree the guide will be updated, just as its authors wanted.
16 |
17 |
18 | ## SET UP:
19 |
20 | * Fedora
21 |
22 | Download all the kernel dev packages (in case of Fedora):
23 |
24 | https://fedoraproject.org/wiki/Building_a_custom_kernel
25 |
26 | You might have some problems with the livna.repo, please temporary remove it
27 |
28 | * CentOS
29 |
30 | In case of CentOS:
31 |
32 | ```
33 | yum -y groupinstall "Development Tools"
34 | yum -y install ncurses-devel \
35 | hmaccalc \
36 | zlib-devel \
37 | binutils-devel \
38 | elfutils-libelf-devel \
39 | bc \
40 | openssl-devel \
41 | kernel-devel \
42 | kernel-headers
43 | ```
44 |
45 | ### BUILD KERNEL
46 |
47 | If you want to build the kernel we have a Dockerfile image, just run:
48 |
49 | ```
50 | make iamge
51 | docker run build_kernel:latest
52 | ```
53 |
54 | Is important to customize a shared volume to copy the kernel uimage,
55 | the build_kernel docker image just build the upstream kernel
56 | (adjust the git clone to your needs)
57 |
58 |
59 |
60 | ## BUILD MODULES:
61 |
62 | 1) make MODULE="" // DO NOT add the .c, just the name of
63 | the module. If no name is passed the makefile will build the basic hello
64 | world module
65 |
66 | 2) make clean: will clean the module
67 |
68 | ## INSTALL MODULES:
69 |
70 | insmod module.ko
71 | modprobe module.ko
72 |
73 | REMOVE:
74 |
75 | rmmod module
76 |
77 | Check printk message:
78 |
79 | dmesg
80 | or
81 | cat /var/log/syslog
82 |
83 |
--------------------------------------------------------------------------------
/scripts/build-kernel.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | echo "hi there"
4 |
5 | git clone --depth 1 https://github.com/torvalds/linux.git
6 | cd /linux/ && yes "" | make oldconfig
7 | cd /linux/ && make
8 |
9 |
--------------------------------------------------------------------------------
/src/char_simple.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | // if new versions of kernel (> 4.12 ) #include
5 | #include /* for put_user */
6 |
7 | #define AUTOR "Victor Rodriguez"
8 | #define DESCRIPCION "Char Driver Hello World"
9 | #define DEVICE_NAME "mychardriver"
10 | #define MAX 1024
11 |
12 | static int num_mayor;
13 | static unsigned long procfs_buffer_size = 0;
14 | static char buffer_data[MAX];
15 |
16 | static char msg[MAX]; /* The msg the device will give when asked */
17 | static char *msg_Ptr;
18 |
19 | static int device_open(struct inode *inode, struct file *file);
20 | static int device_release(struct inode *inode, struct file *file);
21 | static ssize_t device_read(struct file *, char *, size_t, loff_t *);
22 | static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
23 |
24 |
25 | static struct file_operations fops= {
26 | .open = device_open,
27 | .release = device_release,
28 | .read = device_read,
29 | .write = device_write,
30 | };
31 |
32 |
33 | static int __init hola_inicio(void)
34 | {
35 |
36 | printk(KERN_INFO "Hello from the Kernel !!! (how cool is that) \n");
37 |
38 | /*Ask for a mayor number to the kernel */
39 |
40 | num_mayor=register_chrdev(0, DEVICE_NAME, &fops);
41 | printk(KERN_INFO "Major Number = %d \n",num_mayor);
42 | printk(KERN_INFO "Name = %s \n",DEVICE_NAME);
43 | printk(KERN_INFO "Generate the device file with\
44 | mknod /dev/%s c %d 0 \n",DEVICE_NAME,num_mayor);
45 |
46 | return 0 ;
47 |
48 | }
49 |
50 | static void __exit hola_fin(void)
51 | {
52 | unregister_chrdev(num_mayor, DEVICE_NAME);
53 | printk(KERN_INFO "Good Bye , modulo desinstalled \n");
54 | }
55 |
56 | module_init(hola_inicio);
57 | module_exit(hola_fin);
58 |
59 |
60 | /* Methods */
61 |
62 | static int device_open(struct inode *inode, struct file *file)
63 | {
64 | sprintf(msg,buffer_data);
65 | msg_Ptr = msg;
66 | return 0;
67 | }
68 |
69 | static int device_release(struct inode *inode, struct file *file)
70 | {
71 | return 0;
72 | }
73 |
74 | static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
75 | char *buf, /* buffer to fill with data */
76 | size_t len, /* length of the buffer */
77 | loff_t * off)
78 | {
79 | int bytes_read = 0;
80 |
81 | if (*msg_Ptr == 0)
82 | return 0;
83 |
84 |
85 | while (len && *msg_Ptr) {
86 |
87 | /*
88 | * The buffer is in the user data segment, not the kernel
89 | * segment so "*" assignment won't work. We have to use
90 | * put_user which copies data from the kernel data segment to
91 | * the user data segment.
92 | */
93 | put_user(*(msg_Ptr++), buf++);
94 |
95 | len--;
96 | bytes_read++;
97 | }
98 |
99 | /*
100 | * Most read functions return the number of bytes put into the buffer
101 | */
102 | return bytes_read;
103 |
104 |
105 | }
106 |
107 | static ssize_t device_write(struct file *filp,
108 | const char *buf,
109 | size_t len,
110 | loff_t * off)
111 | {
112 |
113 | procfs_buffer_size = len;
114 |
115 | if ( copy_from_user(buffer_data, buf, procfs_buffer_size) ) {
116 | return -EFAULT;
117 | }
118 | *off += len;
119 |
120 | pr_info("user input string: %s\n",buffer_data);
121 | pr_info("user input string len: %lu\n",procfs_buffer_size);
122 |
123 | return procfs_buffer_size;
124 | }
125 |
126 |
127 | MODULE_LICENSE("GPL");
128 | MODULE_AUTHOR(AUTOR); /* Who wrote this module? */
129 | MODULE_DESCRIPTION(DESCRIPCION); /* What does this module do */
130 |
131 | /*
132 | * This module uses /dev/mychardriver. The MODULE_SUPPORTED_DEVICE macro might
133 | * be used in the future to help automatic configuration of modules, buti is
134 | * currently unused other than for documentation purposes.
135 | */
136 |
137 | MODULE_SUPPORTED_DEVICE("mychardriver");
138 |
--------------------------------------------------------------------------------
/src/hello-2.c:
--------------------------------------------------------------------------------
1 | /*
2 | * hello-2.c - Demonstrating the module_init() and module_exit() macros.
3 | * This is preferred over using init_module() and cleanup_module().
4 | */
5 |
6 | #include /* Needed by all modules */
7 | #include /* Needed for KERN_INFO */
8 | #include /* Needed for the macros */
9 |
10 | static int __init hello_2_init(void)
11 | {
12 | printk(KERN_INFO "Hello, world 2\n");
13 | return 0;
14 | }
15 |
16 | static void __exit hello_2_exit(void)
17 | {
18 | printk(KERN_INFO "Goodbye, world 2\n");
19 | }
20 |
21 | module_init(hello_2_init);
22 | module_exit(hello_2_exit);
23 |
--------------------------------------------------------------------------------
/src/hello-3.c:
--------------------------------------------------------------------------------
1 | /*
2 | * hello-3.c - Illustrating the __init, __initdata and __exit macros.
3 | */
4 |
5 | #include /* Needed by all modules */
6 | #include /* Needed for KERN_INFO */
7 | #include /* Needed for the macros */
8 |
9 | static int hello3_data __initdata = 3;
10 |
11 | static int __init hello_3_init(void)
12 | {
13 | printk(KERN_INFO "Hello, world %d\n", hello3_data);
14 | return 0;
15 | }
16 |
17 | static void __exit hello_3_exit(void)
18 | {
19 | printk(KERN_INFO "Goodbye, world 3\n");
20 | }
21 |
22 | module_init(hello_3_init);
23 | module_exit(hello_3_exit);
24 |
--------------------------------------------------------------------------------
/src/hello_simple.c:
--------------------------------------------------------------------------------
1 | /*
2 | * * hello-1.c - The simplest kernel module.
3 | * */
4 | #include /* Needed by all modules */
5 | #include /* Needed for KERN_INFO */
6 |
7 | int init_module(void)
8 | {
9 | printk(KERN_INFO "Hello world 1.\n");
10 |
11 | /*
12 | * * A non 0 return means init_module failed; module can't be
13 | * loaded.
14 | * */
15 | return 0;
16 | }
17 |
18 | void cleanup_module(void)
19 | {
20 | printk(KERN_INFO "Goodbye world 1.\n");
21 | }
22 |
--------------------------------------------------------------------------------
/src/jiffies_1.c:
--------------------------------------------------------------------------------
1 | /*
2 | * hello-1.c - The simplest kernel module.
3 | */
4 | #include /* Needed by all modules */
5 | #include /* Needed for KERN_INFO */
6 | #include
7 |
8 | #define DELAY 3
9 |
10 | int init_module(void)
11 | {
12 | int n = 10;
13 |
14 | printk(KERN_INFO "Hello world 1.\n");
15 | unsigned long j, stamp_1, stamp_half, stamp_n,jifdone;
16 | j = jiffies; /* read the current value */
17 | stamp_1 = j + HZ; /* 1 second in the future */
18 | stamp_half = j + HZ/2; /* half a second */
19 | stamp_n = j + n * HZ / 1000; /* n milliseconds */
20 |
21 | printk(KERN_INFO "Current time = %lu \n",j);
22 | printk(KERN_INFO "1 second in the future = %lu \n",stamp_1 );
23 | printk(KERN_INFO "half a second= %lu \n",stamp_half );
24 | printk(KERN_INFO "n milliseconds = %lu \n",stamp_n );
25 |
26 | /*
27 | jifdone = jiffies + DELAY * HZ;
28 |
29 | while ( time_before( jiffies,jifdone))
30 | {
31 | printk(KERN_INFO "Waiting for Nothing .\n");
32 | }
33 | */
34 | return 0;
35 | }
36 |
37 | void cleanup_module(void)
38 | {
39 | printk(KERN_INFO "Goodbye world 1.\n");
40 | }
41 |
--------------------------------------------------------------------------------
/src/led.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include /* for put_user */
5 | #include
6 | #include
7 |
8 | #include "led.h"
9 |
10 | #define AUTOR "Victor Rodriguez"
11 | #define DESCRIPCION "Driver ioctl"
12 | #define MAXIMO 1024
13 |
14 | /*Turn On a Led */
15 |
16 |
17 | //#define PIN_LED GPIO_TO_PIN(6, 13)
18 | //#define MUX_LED DA850_GPIO6_13
19 |
20 |
21 | #define PIN_LED GPIO_TO_PIN(6, 12)
22 | #define MUX_LED DA850_GPIO6_12
23 |
24 |
25 | /*Varaiables */
26 | static int status;
27 | static int num_mayor;
28 | static unsigned long procfs_buffer_size = 0;
29 | static char buffer_data[MAXIMO];
30 | static char msg[MAXIMO]; /* The msg the device will give when asked */
31 | static char *msg_Ptr;
32 |
33 | /*Metods faile operation*/
34 | static int device_open(struct inode *inode, struct file *file);
35 | static int device_release(struct inode *inode, struct file *file);
36 | static ssize_t device_read(struct file *, char *, size_t, loff_t *);
37 | static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
38 | static int device_ioctl(struct inode *inode,struct file *file,unsigned int ioctl_num,unsigned long ioctl_param);
39 |
40 | /*Metodos to turn on a led*/
41 | static int led_on(void);
42 | static int led_off(void);
43 |
44 | /*File operation structure */
45 | static struct file_operations fops= {
46 | .open = device_open,
47 | .release = device_release,
48 | .read = device_read,
49 | .write = device_write,
50 | .ioctl = device_ioctl,
51 | };
52 |
53 |
54 | static int __init hola_inicio(void)
55 | {
56 |
57 |
58 | num_mayor=register_chrdev(MAJOR_NUM, DEVICE_NAME, &fops);
59 | printk(KERN_INFO "The mayor number is %d \n",MAJOR_NUM);
60 | printk(KERN_INFO "The minor number is %s \n",DEVICE_NAME);
61 | printk(KERN_INFO "Generete the device driver with mknod %s c %d 0 \n",DEVICE_NAME,MAJOR_NUM);
62 |
63 | return 0 ;
64 |
65 | }
66 |
67 | static void __exit hola_fin(void)
68 | {
69 | unregister_chrdev(num_mayor, DEVICE_NAME);
70 | }
71 |
72 | module_init(hola_inicio);
73 | module_exit(hola_fin);
74 |
75 |
76 |
77 | /*Metods faile operation*/
78 |
79 | static int device_open(struct inode *inode, struct file *file)
80 | {
81 | sprintf(msg,buffer_data);
82 | msg_Ptr = msg;
83 | return 0;
84 | }
85 |
86 | static int device_release(struct inode *inode, struct file *file)
87 | {
88 | return 0;
89 | }
90 |
91 | static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
92 | char *buf, /* buffer to fill with data */
93 | size_t len, /* length of the buffer */
94 | loff_t * off)
95 | {
96 | int bytes_read = 0;
97 |
98 | /*Turn on led */
99 | led_on();
100 |
101 | if (*msg_Ptr == 0)
102 | return 0;
103 |
104 |
105 | while (len && *msg_Ptr) {
106 |
107 | /*
108 | * The buffer is in the user data segment, not the kernel
109 | * segment so "*" assignment won't work. We have to use
110 | * put_user which copies data from the kernel data segment to
111 | * the user data segment.
112 | */
113 | put_user(*(msg_Ptr++), buf++);
114 |
115 | len--;
116 | bytes_read++;
117 | }
118 |
119 | /*
120 | * Most read functions return the number of bytes put into the buffer
121 | */
122 | return bytes_read;
123 |
124 |
125 | }
126 |
127 | static ssize_t device_write(struct file *filp,
128 | const char *buf,
129 | size_t len,
130 | loff_t * off)
131 | {
132 |
133 | procfs_buffer_size = len;
134 |
135 | if ( copy_from_user(buffer_data, buf, procfs_buffer_size) ) {
136 | return -EFAULT;
137 | }
138 | *off += len;
139 |
140 | /*Turn of led */
141 | led_off();
142 | return procfs_buffer_size;
143 | }
144 |
145 | int device_ioctl(struct inode *inode, // see include/linux/fs.h
146 | struct file *file, // ditto
147 | unsigned int ioctl_num, // number and param for ioctl
148 | unsigned long ioctl_param)
149 | {
150 |
151 |
152 | switch (ioctl_num)
153 | {
154 | case IOCTL_SET_MSG:
155 |
156 |
157 | if(ioctl_param == 1)
158 | {
159 | led_on();
160 | }
161 |
162 | if (ioctl_param == 0)
163 | {
164 | led_off();
165 | }
166 |
167 | break;
168 |
169 | case IOCTL_GET_MSG:
170 | /*Get the value */
171 | printk(KERN_INFO "Entro en IOCTL_GET_MSG \n");
172 | status = gpio_get_value(PIN_LED);
173 | printk(KERN_INFO "El valor del pin 6,13 es %d \n", status);
174 | return status;
175 | break;
176 |
177 | }
178 |
179 | return 0;
180 | }
181 |
182 | static int led_on(void)
183 | {
184 |
185 |
186 | int ret;
187 |
188 | /*Free gpio */
189 | gpio_free(PIN_LED);
190 |
191 | /*Configure in mux.h */
192 | ret = davinci_cfg_reg (MUX_LED);
193 | if (ret<0)
194 | {
195 | printk(KERN_INFO "No es posible configurar el led 1\n");
196 | return ret;
197 | }
198 |
199 | /*Ask permision to turn it on */
200 | ret = gpio_request(PIN_LED, "led");
201 | if (ret)
202 | {
203 | printk(KERN_INFO "No es posible configurar el led 2\n");
204 | return ret;
205 | }
206 |
207 |
208 |
209 | /*Configure as out put */
210 | gpio_direction_output(PIN_LED, 1);
211 |
212 | /*TURN ON */
213 | gpio_set_value(PIN_LED, 1);
214 |
215 |
216 | return 0 ;
217 |
218 | }
219 |
220 | static int led_off(void)
221 | {
222 | int ret;
223 |
224 | /*Free gpio */
225 | gpio_free(PIN_LED);
226 |
227 | /*Configure in mux.h */
228 | ret = davinci_cfg_reg (MUX_LED);
229 | if (ret<0)
230 | {
231 | printk(KERN_INFO "No es posible configurar el led 1\n");
232 | return ret;
233 | }
234 |
235 | /*Configure as out put */
236 | ret = gpio_request(PIN_LED, "led");
237 | if (ret)
238 | {
239 | printk(KERN_INFO "No es posible configurar el led 2\n");
240 | return ret;
241 | }
242 |
243 |
244 |
245 | /*Configure as out put */
246 | gpio_direction_output(PIN_LED, 1);
247 |
248 | /*TURN OFF */
249 | gpio_set_value(PIN_LED, 0);
250 |
251 |
252 | return 0 ;
253 |
254 | }
255 |
256 | MODULE_LICENSE("GPL");
257 | MODULE_AUTHOR(AUTOR); /* Who wrote this module? */
258 | MODULE_DESCRIPTION(DESCRIPCION); /* What does this module do */
259 | MODULE_SUPPORTED_DEVICE("ioctl-hawkboard");
260 |
--------------------------------------------------------------------------------
/src/led.h:
--------------------------------------------------------------------------------
1 | #ifndef LEDDEV_H
2 | #define LEDDEV_H
3 |
4 | #include
5 |
6 | #define MAJOR_NUM 100
7 | #define DEVICE_NAME "ioctl_led_hawkboard"
8 |
9 | #define IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *)
10 | #define IOCTL_GET_MSG _IOW(MAJOR_NUM, 1, char *)
11 |
12 | #endif
13 |
--------------------------------------------------------------------------------
/src/null_pointer.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | static int test_module_init(void)
6 | {
7 | int *p = 1;
8 | printk("%d\n", *p);
9 | return 0;
10 | }
11 | static void test_module_exit(void)
12 | {
13 | return;
14 | }
15 |
16 | module_init(test_module_init);
17 | module_exit(test_module_exit);
18 |
--------------------------------------------------------------------------------
/src/timer_hiresolution.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 |
6 | MODULE_LICENSE("GPL");
7 |
8 | #define MS_TO_NS(x) (x * 1E6L)
9 |
10 | static struct hrtimer hr_timer;
11 |
12 | enum hrtimer_restart my_hrtimer_callback( struct hrtimer *timer )
13 | {
14 | printk( "my_hrtimer_callback called (%ld).\n", jiffies );
15 |
16 | return HRTIMER_NORESTART;
17 | }
18 |
19 | int init_module( void )
20 | {
21 | ktime_t ktime;
22 | unsigned long delay_in_ms = 200L;
23 |
24 | printk("HR Timer module installing\n");
25 |
26 | ktime = ktime_set( 0, MS_TO_NS(delay_in_ms) );
27 |
28 | hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
29 |
30 | hr_timer.function = &my_hrtimer_callback;
31 |
32 | printk( "Starting timer to fire in %ldms (%ld)\n", delay_in_ms, jiffies );
33 |
34 | hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
35 |
36 | return 0;
37 | }
38 |
39 | void cleanup_module( void )
40 | {
41 | int ret;
42 |
43 | ret = hrtimer_cancel( &hr_timer );
44 | if (ret) printk("The timer was still in use...\n");
45 |
46 | printk("HR Timer module uninstalling\n");
47 |
48 | return;
49 | }
50 |
51 |
--------------------------------------------------------------------------------
/src/timer_simple.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | MODULE_LICENSE("GPL");
6 |
7 | static struct timer_list my_timer;
8 |
9 | void my_timer_callback( unsigned long data )
10 | {
11 | printk( "my_timer_callback called (%ld).\n", jiffies );
12 | }
13 |
14 | int init_module( void )
15 | {
16 | int ret;
17 |
18 | printk("Timer module installing\n");
19 |
20 | // my_timer.function, my_timer.data
21 | setup_timer( &my_timer, my_timer_callback, 0 );
22 |
23 | printk( "Starting timer to fire in 200ms (%ld)\n", jiffies );
24 | ret = mod_timer( &my_timer, jiffies + msecs_to_jiffies(15000) );
25 | if (ret) printk("Error in mod_timer\n");
26 |
27 | return 0;
28 | }
29 |
30 | void cleanup_module( void )
31 | {
32 | int ret;
33 | /*
34 | ret = del_timer( &my_timer );
35 | if (ret) printk("The timer is still in use...\n");
36 | */
37 | printk("Timer module uninstalling\n");
38 |
39 | return;
40 | }
41 |
42 |
--------------------------------------------------------------------------------