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 * drums2.c: Another example of how to pass data to a callback,
38 * inspired by Alessandro Rubini's similar example in his article for
39 * Linux Magazine (http://www.linux.it/kerneldocs/devfs/)
40 *
41 * Like the original drums.c, this example creates a bunch of devices
42 * in the /dev/drums directory: /dev/drums/bam, /dev/drums/bum, etc.
43 * However, it also uses the private_data structure to keep per-file
44 * state, and return a string unique to each user of the device.
45 *
46 * Note, unlike the original drums.c, this driver does not use *offset
47 * to remember if this user has read before; cat /dev/drums/X will
48 * read infinitely
49 *
50 * $Id: drums2.c,v 1.7 2007-12-08 04:05:01 girod Exp $
51 */
52
53 #include <stdio.h>
54 #include <string.h>
55 #include <errno.h>
56 #include <unistd.h>
57
58 #include "fusd.h"
59
60 #define MIN(x, y) ((x) < (y) ? (x) : (y))
61
62
63 /* EXAMPLE START drums2.c */
64 struct drum_info {
65 char *name;
66 int num_users;
67 } drums[] = {
68 { "bam", 0 },
69 { "bum", 0 },
70 /* ... */
71 /* EXAMPLE STOP */
72 { "beat", 0 },
73 { "boom", 0 },
74 { "bang", 0 },
75 { "crash", 0 },
76 /* EXAMPLE START drums2.c */
77 { NULL, 0 }
78 };
79
80 int drums_open(struct fusd_file_info *file)
81 {
82 /* file->device_info is what we passed to fusd_register when we
83 * registered the device. It's a pointer into the "drums" struct. */
84 struct drum_info *d = (struct drum_info *) file->device_info;
85
86 /* Store this user's unique user number in their private_data */
87 file->private_data = (void *)(size_t) ++(d->num_users);
88
89 return 0; /* return success */
90 }
91
92 ssize_t drums_read(struct fusd_file_info *file, char *user_buffer,
93 size_t user_length, loff_t *offset)
94 {
95 struct drum_info *d = (struct drum_info *) file->device_info;
96 int len;
97 char sound[128];
98
99 sprintf(sound, "You are user %d to hear a drum go '%s'!\n",
100 (int)(size_t) file->private_data, d->name);
101
102 len = MIN(user_length, strlen(sound));
103 memcpy(user_buffer, sound, len);
104 return len;
105 }
106 /* EXAMPLE STOP */
107
108
109 int drums_close(struct fusd_file_info *file)
110 {
111 return 0; /* closes always succeed */
112 }
113
114
115 struct fusd_file_operations drums_fops = {
116 open: drums_open,
117 read: drums_read,
118 close: drums_close
119 };
120
121 /* EXAMPLE START drums2.c */
122
123 int main(int argc, char *argv[])
124 {
125 struct drum_info *d;
126 char buf[128];
127
128 for (d = drums; d->name != NULL; d++) {
129 sprintf(buf, "/dev/drums/%s", d->name);
130 if (fusd_register(buf, 0666, d, &drums_fops) < 0)
131 fprintf(stderr, "%s register failed: %m\n", d->name);
132 }
133 /* ... */
134 /* EXAMPLE STOP */
135
136 fprintf(stderr, "calling fusd_run...\n");
137 fusd_run();
138 return 0;
139 }
140
141
142
143
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.