Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * complete.c -- the writers awake the readers
  3  *
  4  * Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet
  5  * Copyright (C) 2003 O'Reilly & Associates
  6  *
  7  * The source code in this file can be freely used, adapted,
  8  * and redistributed in source or binary form, so long as an
  9  * acknowledgment appears in derived source files.  The citation
 10  * should list that the code comes from the book "Linux Device
 11  * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 12  * by O'Reilly & Associates.   No warranty is attached;
 13  * we cannot take responsibility for errors or fitness for use.
 14  *
 15  * $Id: complete.c,v 1.2 2004/09/26 07:02:43 gregkh Exp $
 16  */
 17 
 18 #include <linux/module.h>
 19 #include <linux/init.h>
 20 
 21 #include <linux/sched.h>  /* current and everything */
 22 #include <linux/kernel.h> /* printk() */
 23 #include <linux/fs.h>     /* everything... */
 24 #include <linux/types.h>  /* size_t */
 25 #include <linux/completion.h>
 26 
 27 MODULE_LICENSE("Dual BSD/GPL");
 28 
 29 static int complete_major = 0;
 30 
 31 DECLARE_COMPLETION(comp);
 32 
 33 ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
 34 {
 35         printk(KERN_DEBUG "process %i (%s) going to sleep\n",
 36                         current->pid, current->comm);
 37         wait_for_completion(&comp);
 38         printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
 39         return 0; /* EOF */
 40 }
 41 
 42 ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,
 43                 loff_t *pos)
 44 {
 45         printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
 46                         current->pid, current->comm);
 47         complete(&comp);
 48         return count; /* succeed, to avoid retrial */
 49 }
 50 
 51 
 52 struct file_operations complete_fops = {
 53         .owner = THIS_MODULE,
 54         .read =  complete_read,
 55         .write = complete_write,
 56 };
 57 
 58 
 59 int complete_init(void)
 60 {
 61         int result;
 62 
 63         /*
 64          * Register your major, and accept a dynamic number
 65          */
 66         result = register_chrdev(complete_major, "complete", &complete_fops);
 67         if (result < 0)
 68                 return result;
 69         if (complete_major == 0)
 70                 complete_major = result; /* dynamic */
 71         return 0;
 72 }
 73 
 74 void complete_cleanup(void)
 75 {
 76         unregister_chrdev(complete_major, "complete");
 77 }
 78 
 79 module_init(complete_init);
 80 module_exit(complete_cleanup);
 81 
 82 
  This page was automatically generated by the LXR engine.