examples/kerneltest/kernel_module.c
changeset 467 40efa79d27dd
parent 391 7802a7d5584f
equal deleted inserted replaced
466:b71655eca5e2 467:40efa79d27dd
    15 static struct cdev *canftest_cdev;
    15 static struct cdev *canftest_cdev;
    16 static struct task_struct *thread_start_p, *thread_stop_p;
    16 static struct task_struct *thread_start_p, *thread_stop_p;
    17 static DECLARE_MUTEX (canftest_mutex);
    17 static DECLARE_MUTEX (canftest_mutex);
    18 static int canftest_stopped = 1;
    18 static int canftest_stopped = 1;
    19 
    19 
       
    20 int thread_start (void* data);
       
    21 int thread_stop (void* data);
       
    22 
    20 // handler processing write() requests from user-space
    23 // handler processing write() requests from user-space
    21 ssize_t canftest_write(struct file *filp, const char __user *buf, size_t count,
    24 ssize_t canftest_write(struct file *filp, const char __user *buf, size_t count,
    22 		       loff_t *f_pos)
    25 		       loff_t *f_pos)
    23 {
    26 {
    24 	int cmd;
    27 	int cmd;
    28 		return -EFAULT;
    31 		return -EFAULT;
    29 
    32 
    30 	// process integer as command
    33 	// process integer as command
    31 	switch (cmd) {
    34 	switch (cmd) {
    32 		case CMD_START:
    35 		case CMD_START:
       
    36 			if (!canftest_stopped) break;
       
    37 			thread_start_p = kthread_create (thread_start, NULL, "canftest_start");
       
    38 
       
    39 			if (PTR_ERR(thread_start_p) == -ENOMEM) {
       
    40 				printk(KERN_WARNING "canftest: error creating start thread\n");
       
    41 				return -ENOMEM;
       
    42 			}
       
    43 
    33 			wake_up_process (thread_start_p);
    44 			wake_up_process (thread_start_p);
    34 			break;
    45 			break;
       
    46 
    35 		case CMD_STOP:
    47 		case CMD_STOP:
    36 			if (canftest_stopped) break;
    48 			if (canftest_stopped) break;
       
    49 			thread_stop_p = kthread_create (thread_stop, NULL, "canftest_stop");
       
    50 
       
    51 			if (PTR_ERR(thread_stop_p) == -ENOMEM) {
       
    52 				printk(KERN_WARNING "canftest: error creating stop thread\n");
       
    53 				return -ENOMEM;
       
    54 			}
       
    55 
    37 			wake_up_process (thread_stop_p);
    56 			wake_up_process (thread_stop_p);
    38 			break;
    57 			break;
       
    58 
    39 		// ignore new line character
    59 		// ignore new line character
    40 		case 10:
    60 		case 10:
    41 			break;
    61 			break;
       
    62 
    42 		default:
    63 		default:
    43 			printk("canftest: bad command %d\n", cmd);
    64 			printk("canftest: bad command %d\n", cmd);
    44 			break;
    65 			break;
    45 	}
    66 	}
    46 	
    67 	
   112 	if (ret) {
   133 	if (ret) {
   113 		printk(KERN_WARNING "canftest: error %d adding char device\n", ret);
   134 		printk(KERN_WARNING "canftest: error %d adding char device\n", ret);
   114 		return ret;
   135 		return ret;
   115 	}
   136 	}
   116 
   137 
   117 	thread_start_p = kthread_create (thread_start, NULL, "canftest_start");
       
   118 	thread_stop_p = kthread_create (thread_stop, NULL, "canftest_stop");
       
   119 	
       
   120 	if (PTR_ERR(thread_start_p) == -ENOMEM || PTR_ERR(thread_stop_p) == -ENOMEM) {
       
   121 		printk(KERN_WARNING "canftest: error creating threads\n");
       
   122 		return -ENOMEM;
       
   123 	}
       
   124 
       
   125 	return 0;
   138 	return 0;
   126 }
   139 }
   127 
   140 
   128 void cleanup_module(void)
   141 void cleanup_module(void)
   129 {
   142 {