1 #include <linux/module.h>
2 #include <linux/kthread.h>
4 #include <linux/cdev.h>
5 #include <asm/uaccess.h>
7 #include "TestMasterSlave.h"
8 #include "console/console.h"
12 MODULE_LICENSE("GPL");
14 static dev_t canftest_dev;
15 static struct cdev *canftest_cdev;
16 static struct task_struct *thread_start_p, *thread_stop_p;
17 static DECLARE_MUTEX (canftest_mutex);
18 static int canftest_stopped = 1;
20 int thread_start (void* data);
21 int thread_stop (void* data);
23 // handler processing write() requests from user-space
24 ssize_t canftest_write(struct file *filp, const char __user *buf, size_t count,
29 // get integer from user-space
30 if (get_user (cmd, buf))
33 // process integer as command
36 if (!canftest_stopped) break;
37 thread_start_p = kthread_create (thread_start, NULL, "canftest_start");
39 if (PTR_ERR(thread_start_p) == -ENOMEM) {
40 printk(KERN_WARNING "canftest: error creating start thread\n");
44 wake_up_process (thread_start_p);
48 if (canftest_stopped) break;
49 thread_stop_p = kthread_create (thread_stop, NULL, "canftest_stop");
51 if (PTR_ERR(thread_stop_p) == -ENOMEM) {
52 printk(KERN_WARNING "canftest: error creating stop thread\n");
56 wake_up_process (thread_stop_p);
59 // ignore new line character
64 printk("canftest: bad command %d\n", cmd);
71 // register write() handler
72 static struct file_operations canftest_fops = {
74 .write = canftest_write,
77 // start TestMasterSlave example
78 int thread_start (void* data)
82 down_interruptible (&canftest_mutex);
84 ret=TestMasterSlave_start();
90 // increment module usage count
91 try_module_get(THIS_MODULE);
99 // finish TestMasterSlave example
100 int thread_stop (void* data)
102 down_interruptible (&canftest_mutex);
104 TestMasterSlave_stop();
105 canftest_stopped = 1;
107 // decrement usage count
108 module_put(THIS_MODULE);
110 up (&canftest_mutex);
115 int init_module(void)
119 // get major device number dynamically
120 ret = alloc_chrdev_region(&canftest_dev, minor, 1, DEVICE_NAME);
121 major = MAJOR(canftest_dev);
123 printk(KERN_WARNING "canftest: can't get major %d\n", major);
127 canftest_cdev = cdev_alloc( );
128 canftest_cdev->owner = THIS_MODULE;
129 canftest_cdev->ops = &canftest_fops;
131 // register new character device
132 ret = cdev_add (canftest_cdev, canftest_dev, 1);
134 printk(KERN_WARNING "canftest: error %d adding char device\n", ret);
141 void cleanup_module(void)
143 // unregister major device number and character device
144 unregister_chrdev_region(canftest_dev, 1);
145 cdev_del(canftest_cdev);