drums.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * $Id$
  44. */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <unistd.h>
  49. #include "fusd.h"
  50. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  51. /* EXAMPLE START drums.c */
  52. static char *drums_strings[] = {"bam", "bum", "beat", "boom",
  53. "bang", "crash", NULL};
  54. int drums_read(struct fusd_file_info *file, char *user_buffer,
  55. size_t user_length, loff_t *offset)
  56. {
  57. int len;
  58. char sound[128];
  59. /* file->device_info is what we passed to fusd_register when we
  60. * registered the device */
  61. strcpy(sound, (char *) file->device_info);
  62. strcat(sound, "\n");
  63. /* 1st read returns the sound; 2nd returns EOF */
  64. if (*offset != 0)
  65. return 0;
  66. /* NEVER return more data than the user asked for */
  67. len = MIN(user_length, strlen(sound));
  68. memcpy(user_buffer, sound, len);
  69. *offset += len;
  70. return len;
  71. }
  72. /* EXAMPLE STOP drums.c */
  73. int do_open_or_close(struct fusd_file_info *file)
  74. {
  75. return 0; /* opens and closes always succeed */
  76. }
  77. struct fusd_file_operations drums_fops = {
  78. open: do_open_or_close,
  79. read: drums_read,
  80. close: do_open_or_close
  81. };
  82. /* EXAMPLE START drums.c */
  83. int main(int argc, char *argv[])
  84. {
  85. int i;
  86. char buf[128];
  87. char devname[128];
  88. for (i = 0; drums_strings[i] != NULL; i++) {
  89. sprintf(buf, "/dev/drums/%s", drums_strings[i]);
  90. sprintf(devname, "drum%s", drums_strings[i]);
  91. if (fusd_register(buf, "drums", devname, 0666, drums_strings[i], &drums_fops) < 0)
  92. fprintf(stderr, "%s register failed: %m\n", drums_strings[i]);
  93. }
  94. fprintf(stderr, "calling fusd_run...\n");
  95. fusd_run();
  96. return 0;
  97. }
  98. /* EXAMPLE STOP drums.c */