drums2.c 4.1 KB

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