Thread Synch AOSV 2020  1.2
LKM for exchanging messages between threads
main_device.h
Go to the documentation of this file.
1 
6 #ifndef MAIN_DEV_H
7 #define MAIN_DEV_H
8 
9 
10 #include <linux/module.h> /* MODULE_*, module_* */
11 #include <linux/kernel.h> /* We're doing kernel work */
12 #include <linux/fs.h> /* file_operations, alloc_chrdev_region, unregister_chrdev_region */
13 #include <linux/cdev.h> /* cdev, dev_init(), cdev_add(), cdev_del() */
14 #include <linux/device.h> /* class_create(), class_destroy(), device_create(), device_destroy() */
15 #include <linux/slab.h> /* kmalloc(), kfree() */
16 #include <linux/uaccess.h> /* copy_from_user(), copy_to_user() */
17 #include <linux/semaphore.h> /* used acces to semaphore, process management syncronization behaviour */
18 
19 #include <linux/ioctl.h>
20 #include <linux/idr.h>
21 
22 
23 
24 #include "group_manager.h"
25 
26 
27 /*------------------------------------------------------------------------------
28  Error Codes
29 ------------------------------------------------------------------------------*/
30 #ifndef DISABLE_SYSFS
31  #include "sysfs.h"
32 #endif
33 
34 #define INVALID_IOCTL_COMMAND -1
35 #define ALLOC_ERR -2
36 #define USER_COPY_ERR -3
37 #define IDR_ERR -4
38 #define MEM_ACCESS_ERR -5
39 #define CLASS_EXISTS -10
40 #define CLASS_ERR -11
41 #define DEV_CREATION_ERR -12
42 #define CDEV_ALLOC_ERR -13
43 #define GROUP_EXISTS -14
44 
45 
46 
47 /*------------------------------------------------------------------------------
48  IOCTL
49 ------------------------------------------------------------------------------*/
50 
51 #define IOCTL_INSTALL_GROUP _IOW('X', 99, group_t*)
52 #define IOCTL_GET_GROUP_ID _IOW('X', 100, group_t*)
53 
54 
55 /*------------------------------------------------------------------------------
56  Defined Macros
57 ------------------------------------------------------------------------------*/
58 #define D_DEV_NAME "main_thread_synch"
59 #define D_DEV_MAJOR (0)
60 #define D_DEV_MINOR (0)
61 #define D_DEV_NUM (1)
64 #define CLASS_NAME "thread_synch"
67 #define GRP_MIN_ID 0
68 #define GRP_MAX_ID 255
70 /*------------------------------------------------------------------------------
71  Type Definition
72 ------------------------------------------------------------------------------*/
73 
74 
82 typedef struct t_main_sync {
83  dev_t dev;
84  struct cdev cdev;
85  int minor;
87  struct idr group_map;
89  struct semaphore sem;
91 } main_sync_t;
92 
93 /*------------------------------------------------------------------------------
94  Prototype Declaration
95 ------------------------------------------------------------------------------*/
96 int mainInit(void);
97 void mainExit(void);
98 
99 void initializeMainDevice(void);
100 int installGroup(const group_t new_group);
101 
102 static int mainOpen(struct inode *inode, struct file *filep);
103 static int mainRelease(struct inode *inode, struct file *filep);
104 static ssize_t mainWrite(struct file *filep, const char __user *buf, size_t count, loff_t *f_pos);
105 static ssize_t mainRead(struct file *filep, char __user *buf, size_t count, loff_t *f_pos);
106 
107 static long int mainDeviceIoctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param);
108 
109 
110 static int sRegisterMainDev(void);
111 static void sUnregisterMainDev(void);
112 
113 
114 
115 
116 /*------------------------------------------------------------------------------
117  Global Variables
118 ------------------------------------------------------------------------------*/
119 static struct class *main_class;
120 static struct cdev *main_cdev;
121 static int main_dev_major = D_DEV_MAJOR;
122 static int main_dev_minor = D_DEV_MINOR;
125 static struct device *main_device; //Used for "parent" field in device_create;
126 
127 //Main device global pointer
128 static main_sync_t main_device_data; //TODO use an array to manage multiple main device
129 
130 static struct file_operations main_fops;
131 
132 extern struct class *group_device_class;
133 
134 
135 
136 #endif //MAIN_DEV_H
initializeMainDevice
void initializeMainDevice(void)
Init Main Device members.
Definition: main_device.c:107
t_main_sync::group_map
struct idr group_map
Definition: main_device.h:87
mainInit
int mainInit(void)
Kernel Module Init.
Definition: main_device.c:27
sysfs.h
Setup the required sysfs interfaces for the module.
t_main_sync
Main device structure.
Definition: main_device.h:82
D_DEV_MINOR
#define D_DEV_MINOR
Definition: main_device.h:60
t_main_sync::minor
int minor
Definition: main_device.h:85
installGroup
int installGroup(const group_t new_group)
Install a group for the provided 'group_t' descriptor.
Definition: main_device.c:389
D_DEV_MAJOR
#define D_DEV_MAJOR
Definition: main_device.h:59
group_t
System-wide descriptor of a group.
Definition: types.h:139
mainExit
void mainExit(void)
Kernel Module Exit.
Definition: main_device.c:55
group_manager.h
Handles all procedures releated to file operation issued on a group device.
t_main_sync::cdev
struct cdev cdev
Definition: main_device.h:84
t_main_sync::sem
struct semaphore sem
Definition: main_device.h:89