3 * Copyright (c) 2003 The Regents of the University of California. All
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
33 * FUSD - The Framework for UserSpace Devices - Example program
35 * Jeremy Elson <jelson@circlemud.org>
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/)
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.
55 #define MIN(x, y) ((x) < (y) ? (x) : (y))
58 /* EXAMPLE START drums.c */
59 static char *drums_strings
[] = {"bam", "bum", "beat", "boom",
60 "bang", "crash", NULL
};
62 ssize_t
drums_read(struct fusd_file_info
*file
, char *user_buffer
,
63 size_t user_length
, loff_t
*offset
)
68 /* file->device_info is what we passed to fusd_register when we
69 * registered the device */
70 strcpy(sound
, (char *) file
->device_info
);
73 /* 1st read returns the sound; 2nd returns EOF */
77 /* NEVER return more data than the user asked for */
78 len
= MIN(user_length
, strlen(sound
));
79 memcpy(user_buffer
, sound
, len
);
84 /* EXAMPLE STOP drums.c */
87 int do_open_or_close(struct fusd_file_info
*file
)
89 return 0; /* opens and closes always succeed */
93 struct fusd_file_operations drums_fops
= {
94 open
: do_open_or_close
,
96 close
: do_open_or_close
99 /* EXAMPLE START drums.c */
100 int main(int argc
, char *argv
[])
106 for (i
= 0; drums_strings
[i
] != NULL
; i
++) {
107 sprintf(buf
, "/dev/drums/%s", drums_strings
[i
]);
108 sprintf(devname
, "drum%s", drums_strings
[i
]);
109 if (fusd_register(buf
, "drums", devname
, 0666, drums_strings
[i
], &drums_fops
) < 0)
110 fprintf(stderr
, "%s register failed: %m\n", drums_strings
[i
]);
113 fprintf(stderr
, "calling fusd_run...\n");
117 /* EXAMPLE STOP drums.c */