Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
embedded-linux
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Maul
embedded-linux
Commits
e7e1aa6b
Commit
e7e1aa6b
authored
4 months ago
by
David Maul
Browse files
Options
Downloads
Patches
Plain Diff
feat: buffer messages in userspace and improve message handling in kernel module
parent
d4413e6a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
kernel/kmod.c
+187
-151
187 additions, 151 deletions
kernel/kmod.c
with
187 additions
and
151 deletions
kernel/kmod.c
+
187
−
151
View file @
e7e1aa6b
#include
<linux/module.h>
#include
<linux/kernel.h>
#include
<linux/fs.h>
#include
"../common/include/data.h"
#include
"../common/include/message.h"
#include
<linux/cdev.h>
#include
<linux/vmalloc.h>
#include
<linux/uaccess.h>
#include
<linux/crc32.h>
#include
<linux/delay.h>
#include
<linux/fs.h>
#include
<linux/gpio.h>
#include
<linux/hrtimer.h>
#include
<linux/kernel.h>
#include
<linux/kthread.h>
#include
<linux/gpio.h>
#include
<linux/delay.h>
#include
<linux/crc32.h>
#include
"../common/include/message.h"
#include
<linux/module.h>
#include
<linux/uaccess.h>
#include
<linux/vmalloc.h>
#define DEVICE_NAME "amogus"
#define CLASS_NAME "amogus_class"
#define SLOTS 4
#define SLOT_INTERVAL_NS
25
0000
#define SLOT_INTERVAL_NS
100
0000
#define GPIO_PIN 12
#define MAX_VALUES 16
#define GPIO_PIN 575
static
int
major_number
;
static
struct
class
*
class
=
NULL
;
static
struct
cdev
mycdev
;
static
message_t
*
message
;
typedef
struct
packet
{
data_t
data
;
uint32_t
crc
;
}
packet_t
;
static
packet_t
*
packet
;
static
struct
hrtimer
timer
;
static
ktime_t
interval
;
...
...
@@ -35,43 +43,55 @@ static atomic_t wake_counter = ATOMIC_INIT(0);
static
int
slot
=
0
;
typedef
struct
packet
{
message_t
data
;
uint32_t
crc
;
}
packet_t
;
static
int
device_open
(
struct
inode
*
inode
,
struct
file
*
file
)
{
return
0
;
}
static
int
device_open
(
struct
inode
*
inode
,
struct
file
*
file
)
{
return
0
;
}
static
int
device_release
(
struct
inode
*
inode
,
struct
file
*
file
)
{
return
0
;
}
static
int
device_release
(
struct
inode
*
inode
,
struct
file
*
file
)
{
return
0
;
static
uint32_t
calculate_crc32
(
const
data_t
*
data
)
{
uint32_t
crc
=
crc32
(
0
,
(
void
*
)
&
data
->
sensor_id
,
sizeof
(
uint8_t
));
crc
=
crc32
(
crc
,
(
void
*
)
&
data
->
count
,
sizeof
(
uint8_t
));
crc
=
crc32
(
crc
,
(
void
*
)
&
data
->
values
,
sizeof
(
message_t
)
*
data
->
count
);
return
crc
;
}
static
ssize_t
device_write
(
struct
file
*
filp
,
const
char
*
input
,
size_t
length
,
loff_t
*
offset
)
{
if
(
length
!=
sizeof
(
message_t
))
{
static
ssize_t
device_write
(
struct
file
*
filp
,
const
char
*
input
,
size_t
length
,
loff_t
*
offset
)
{
if
(
length
!=
sizeof
(
data_t
))
{
printk
(
KERN_ALERT
"lkm: Tried to write more bytes than allowed
\n
"
);
return
-
EFAULT
;
}
if
(
copy_from_user
((
void
*
)
message
,
input
,
length
))
{
if
(((
data_t
*
)
input
)
->
count
>=
MAX_VALUES
)
{
printk
(
KERN_ALERT
"lkm: Tried to write more messages than allowed
\n
"
);
return
-
EFAULT
;
}
if
(
copy_from_user
((
void
*
)
&
(
packet
->
data
.
sensor_id
),
(
void
*
)
&
(((
data_t
*
)
input
)
->
sensor_id
),
sizeof
(
uint8_t
)))
{
return
-
EFAULT
;
}
if
(
copy_from_user
((
void
*
)
&
(
packet
->
data
.
count
),
(
void
*
)
&
(((
data_t
*
)
input
)
->
count
),
sizeof
(
uint8_t
)))
{
return
-
EFAULT
;
}
if
(
copy_from_user
((
void
*
)
packet
->
data
.
values
,
(
void
*
)((
data_t
*
)
input
)
->
values
,
sizeof
(
message_t
)
*
packet
->
data
.
count
))
{
return
-
EFAULT
;
}
packet
->
crc
=
calculate_crc32
(
&
packet
->
data
);
printk
(
KERN_INFO
"lkm: received write of size %zu
\n
"
,
length
);
return
length
;
}
static
const
struct
file_operations
fops
=
{
.
write
=
device_write
,
.
open
=
device_open
,
.
release
=
device_release
};
.
write
=
device_write
,
.
open
=
device_open
,
.
release
=
device_release
};
static
enum
hrtimer_restart
timer_callback
(
struct
hrtimer
*
timer
)
{
atomic_inc
(
&
wake_counter
);
...
...
@@ -97,16 +117,21 @@ static void send_byte(uint8_t byte) {
send_bit
(
1
);
}
static
void
send_data
(
const
void
*
data
,
size_t
length
)
{
const
uint8_t
*
bytes
=
(
const
uint8_t
*
)
data
;
static
void
send_data
(
const
packet_t
*
packet
)
{
send_byte
(
packet
->
data
.
sensor_id
);
send_byte
(
packet
->
data
.
count
);
uint8_t
*
bytes
=
(
uint8_t
*
)
packet
->
data
.
values
;
for
(
size_t
i
=
0
;
i
<
length
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
sizeof
(
message_t
)
*
packet
->
data
.
count
;
i
++
)
{
send_byte
(
bytes
[
i
]);
}
}
static
uint32_t
calculate_crc32
(
const
void
*
data
,
size_t
length
){
return
crc32
(
0
,
data
,
length
);
bytes
=
(
uint8_t
*
)
&
packet
->
crc
;
for
(
size_t
i
=
0
;
i
<
sizeof
(
uint32_t
);
i
++
)
{
send_byte
(
bytes
[
i
]);
}
}
static
int
timed_thread_fn
(
void
*
args
)
{
...
...
@@ -115,12 +140,7 @@ static int timed_thread_fn(void *args) {
atomic_dec
(
&
wake_counter
);
if
(
slot
==
0
)
{
packet_t
packet
;
packet
.
data
=
*
message
;
packet
.
crc
=
calculate_crc32
(
&
packet
.
data
,
sizeof
(
&
packet
.
data
));
send_data
(
&
packet
,
sizeof
(
packet
));
send_data
(
packet
);
}
slot
=
(
slot
+
1
)
%
SLOTS
;
...
...
@@ -129,19 +149,29 @@ static int timed_thread_fn(void *args) {
return
0
;
}
static
int
__init
lkm_init
(
void
)
{
static
int
__init
lkm_init
(
void
)
{
dev_t
dev
;
message
=
(
message
_t
*
)
vmalloc
(
sizeof
(
message
_t
));
packet
=
(
packet
_t
*
)
vmalloc
(
sizeof
(
packet
_t
));
if
(
message
==
NULL
)
{
return
-
1
;
if
(
packet
==
NULL
)
{
return
-
EFAULT
;
}
packet
->
data
.
sensor_id
=
0x7C
;
packet
->
data
.
count
=
0
;
packet
->
data
.
values
=
(
message_t
*
)
vmalloc
(
sizeof
(
message_t
)
*
MAX_VALUES
);
if
(
packet
->
data
.
values
==
NULL
)
{
vfree
(
packet
);
return
-
EFAULT
;
}
// Dynamically allocate major number
if
(
alloc_chrdev_region
(
&
dev
,
0
,
1
,
DEVICE_NAME
)
<
0
)
{
vfree
(
message
);
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
printk
(
KERN_ALERT
"Failed to allocate major number
\n
"
);
return
-
1
;
}
...
...
@@ -149,14 +179,17 @@ static int __init lkm_init(void)
class
=
class_create
(
CLASS_NAME
);
if
(
IS_ERR
(
class
))
{
vfree
(
message
);
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
unregister_chrdev_region
(
MKDEV
(
major_number
,
0
),
1
);
printk
(
KERN_ALERT
"Failed to create device class
\n
"
);
return
PTR_ERR
(
class
);
}
if
(
device_create
(
class
,
NULL
,
MKDEV
(
major_number
,
0
),
NULL
,
DEVICE_NAME
)
==
NULL
)
{
vfree
(
message
);
if
(
device_create
(
class
,
NULL
,
MKDEV
(
major_number
,
0
),
NULL
,
DEVICE_NAME
)
==
NULL
)
{
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
class_destroy
(
class
);
unregister_chrdev_region
(
MKDEV
(
major_number
,
0
),
1
);
printk
(
KERN_ALERT
"Failed to create device
\n
"
);
...
...
@@ -165,7 +198,8 @@ static int __init lkm_init(void)
cdev_init
(
&
mycdev
,
&
fops
);
if
(
cdev_add
(
&
mycdev
,
MKDEV
(
major_number
,
0
),
1
)
<
0
)
{
vfree
(
message
);
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
device_destroy
(
class
,
MKDEV
(
major_number
,
0
));
class_destroy
(
class
);
unregister_chrdev_region
(
MKDEV
(
major_number
,
0
),
1
);
...
...
@@ -176,7 +210,8 @@ static int __init lkm_init(void)
printk
(
KERN_INFO
"lkm: device created successfully
\n
"
);
if
(
gpio_request
(
GPIO_PIN
,
"GPIO_PIN"
)
<
0
)
{
vfree
(
message
);
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
cdev_del
(
&
mycdev
);
device_destroy
(
class
,
MKDEV
(
major_number
,
0
));
class_destroy
(
class
);
...
...
@@ -188,7 +223,8 @@ static int __init lkm_init(void)
timed_thread
=
kthread_run
(
timed_thread_fn
,
NULL
,
"timed_thread"
);
if
(
IS_ERR
(
timed_thread
))
{
vfree
(
message
);
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
cdev_del
(
&
mycdev
);
device_destroy
(
class
,
MKDEV
(
major_number
,
0
));
class_destroy
(
class
);
...
...
@@ -205,9 +241,9 @@ static int __init lkm_init(void)
return
0
;
}
static
void
__exit
lkm_exit
(
void
)
{
vfree
(
message
);
static
void
__exit
lkm_exit
(
void
)
{
vfree
(
packet
->
data
.
values
);
vfree
(
packet
);
cdev_del
(
&
mycdev
);
device_destroy
(
class
,
MKDEV
(
major_number
,
0
));
class_destroy
(
class
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment