~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
cvs/emstar/fusd/examples/drums.c


  1 /*
  2  *
  3  * Copyright (c) 2003 The Regents of the University of California.  All 
  4  * rights reserved.
  5  *
  6  * Redistribution and use in source and binary forms, with or without
  7  * modification, are permitted provided that the following conditions
  8  * are met:
  9  *
 10  * - Redistributions of source code must retain the above copyright
 11  *   notice, this list of conditions and the following disclaimer.
 12  *
 13  * - Neither the name of the University nor the names of its
 14  *   contributors may be used to endorse or promote products derived
 15  *   from this software without specific prior written permission.
 16  *
 17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
 18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 20  * PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  *
 29  */
 30  
 31 
 32 /*
 33  * FUSD - The Framework for UserSpace Devices - Example program
 34  *
 35  * Jeremy Elson <jelson@circlemud.org>
 36  *
 37  * drums.c: Example of how to pass data to a callback, inspired by
 38  * Alessandro Rubini's similar example in his article for Linux
 39  * Magazine (http://www.linux.it/kerneldocs/devfs/)
 40  *
 41  * This example creates a bunch of devices in the /dev/drums
 42  * directory: /dev/drums/bam, /dev/drums/bum, etc.  If you cat one of
 43  * these devices, it returns a string that's the same as its name.
 44  *
 45  * $Id: drums.c,v 1.5 2007-12-08 04:05:01 girod Exp $
 46  */
 47 
 48 #include <stdio.h>
 49 #include <string.h>
 50 #include <errno.h>
 51 #include <unistd.h>
 52 
 53 #include "fusd.h"
 54 
 55 #define MIN(x, y) ((x) < (y) ? (x) : (y))
 56 
 57 
 58 /* EXAMPLE START drums.c */
 59 static char *drums_strings[] = {"bam", "bum", "beat", "boom",
 60                                 "bang", "crash", NULL};
 61 
 62 ssize_t drums_read(struct fusd_file_info *file, char *user_buffer,
 63                size_t user_length, loff_t *offset)
 64 {
 65   int len;
 66   char sound[128];
 67 
 68   /* file->device_info is what we passed to fusd_register when we
 69    * registered the device */
 70   strcpy(sound, (char *) file->device_info);
 71   strcat(sound, "\n");
 72 
 73   /* 1st read returns the sound; 2nd returns EOF */
 74   if (*offset != 0)
 75     return 0;
 76 
 77   /* NEVER return more data than the user asked for */
 78   len = MIN(user_length, strlen(sound));
 79   memcpy(user_buffer, sound, len);
 80   *offset += len;
 81   return len;
 82 }
 83 
 84 /* EXAMPLE STOP drums.c */
 85 
 86 
 87 int do_open_or_close(struct fusd_file_info *file)
 88 {
 89   return 0; /* opens and closes always succeed */
 90 }
 91 
 92 
 93 struct fusd_file_operations drums_fops = {
 94   open: do_open_or_close,
 95   read: drums_read,
 96   close: do_open_or_close
 97 };
 98 
 99 /* EXAMPLE START drums.c */
100 int main(int argc, char *argv[])
101 {
102   int i;
103   char buf[128];
104 
105   for (i = 0; drums_strings[i] != NULL; i++) {
106     sprintf(buf, "/dev/drums/%s", drums_strings[i]);
107     if (fusd_register(buf, 0666, drums_strings[i], &drums_fops) < 0)
108       fprintf(stderr, "%s register failed: %m\n", drums_strings[i]);
109   }
110 
111   fprintf(stderr, "calling fusd_run...\n");
112   fusd_run();
113   return 0;
114 }
115 /* EXAMPLE STOP drums.c */
116 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.