drums.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * drums.c: Example of how to pass data to a callback, inspired by
  36. * Alessandro Rubini's similar example in his article for Linux
  37. * Magazine (http://www.linux.it/kerneldocs/devfs/)
  38. *
  39. * This example creates a bunch of devices in the /dev/drums
  40. * directory: /dev/drums/bam, /dev/drums/bum, etc. If you cat one of
  41. * these devices, it returns a string that's the same as its name.
  42. *
  43. */
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <errno.h>
  47. #include <unistd.h>
  48. #include "fusd.h"
  49. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  50. /* EXAMPLE START drums.c */
  51. static char *drums_strings[] = {"bam", "bum", "beat", "boom",
  52. "bang", "crash", NULL};
  53. ssize_t drums_read(struct fusd_file_info *file, char *user_buffer,
  54. size_t user_length, loff_t *offset)
  55. {
  56. int len;
  57. char sound[128];
  58. /* file->device_info is what we passed to fusd_register when we
  59. * registered the device */
  60. strcpy(sound, (char *) file->device_info);
  61. strcat(sound, "\n");
  62. /* 1st read returns the sound; 2nd returns EOF */
  63. if (*offset != 0)
  64. return 0;
  65. /* NEVER return more data than the user asked for */
  66. len = MIN(user_length, strlen(sound));
  67. memcpy(user_buffer, sound, len);
  68. *offset += len;
  69. return len;
  70. }
  71. /* EXAMPLE STOP drums.c */
  72. int do_open_or_close(struct fusd_file_info *file)
  73. {
  74. return 0; /* opens and closes always succeed */
  75. }
  76. struct fusd_file_operations drums_fops = {
  77. open: do_open_or_close,
  78. read: drums_read,
  79. close: do_open_or_close
  80. };
  81. /* EXAMPLE START drums.c */
  82. int main(int argc, char *argv[])
  83. {
  84. int i;
  85. char buf[128];
  86. char devname[128];
  87. for (i = 0; drums_strings[i] != NULL; i++) {
  88. sprintf(buf, "/dev/drums/%s", drums_strings[i]);
  89. sprintf(devname, "drum%s", drums_strings[i]);
  90. if (fusd_register(buf, "drums", devname, 0666, drums_strings[i], &drums_fops) < 0)
  91. fprintf(stderr, "%s register failed: %m\n", drums_strings[i]);
  92. }
  93. fprintf(stderr, "calling fusd_run...\n");
  94. fusd_run();
  95. return 0;
  96. }
  97. /* EXAMPLE STOP drums.c */