kfusd.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 User-Space Devices
  32. *
  33. * Jeremy Elson <jelson@circlemud.org>
  34. * Copyright (c) Sensoria Corporation 2001
  35. *
  36. * Private header file used by the Linux Kernel Module
  37. *
  38. */
  39. #ifndef __KFUSD_H__
  40. # define __KFUSD_H__
  41. # include "fusd_msg.h"
  42. # include <linux/version.h>
  43. /* magic numbers for structure checking; unique w.r.t
  44. * /usr/src/linux/Documentation/magic-number.txt */
  45. # define FUSD_DEV_MAGIC 0x8b43a123
  46. # define FUSD_FILE_MAGIC 0x613aa8fe
  47. /* number of devices that can be created with fusd */
  48. # define MAX_FUSD_DEVICES 128
  49. /* number of times each device can be opened simultaneously */
  50. # define MIN_FILEARRAY_SIZE 8 /* initialize allocation */
  51. # define MAX_FILEARRAY_SIZE 1024 /* maximum it can grow to */
  52. /* maximum read/write size we're willing to service */
  53. # define MAX_RW_SIZE (1024*128)
  54. /********************** Structure Definitions *******************************/
  55. /* Container for a fusd msg */
  56. typedef struct fusd_msgC_s_t fusd_msgC_t;
  57. struct fusd_msgC_s_t {
  58. fusd_msg_t fusd_msg; /* the message itself */
  59. fusd_msgC_t *next; /* pointer to next one in the list */
  60. /* 1-bit flags */
  61. unsigned int peeked:1; /* has the first half of this been read? */
  62. };
  63. struct fusd_transaction
  64. {
  65. struct list_head list;
  66. long transid;
  67. int subcmd;
  68. int pid;
  69. int size;
  70. fusd_msg_t* msg_in;
  71. };
  72. /* magical forward declarations to break the circular dependency */
  73. struct fusd_dev_t_s;
  74. typedef struct fusd_dev_t_s fusd_dev_t;
  75. struct CLASS;
  76. struct device;
  77. /* state kept per opened file (i.e., an instance of a device) */
  78. typedef struct {
  79. /* general state management */
  80. int magic; /* magic number for sanity checking */
  81. fusd_dev_t *fusd_dev; /* fusd device associated with this file */
  82. long fusd_dev_version; /* version number of fusd device */
  83. void *private_data; /* the user's private data (we ignore it) */
  84. struct file *file; /* kernel's file pointer for this file */
  85. int index; /* our index in our device's file array */
  86. struct semaphore file_sem; /* Semaphore for file structure */
  87. int cached_poll_state; /* Latest result from a poll diff req */
  88. int last_poll_sent; /* Last polldiff request we sent */
  89. /* structures used for messaging */
  90. wait_queue_head_t file_wait; /* Wait on this for a user->kernel msg */
  91. wait_queue_head_t poll_wait; /* Given to kernel for poll() queue */
  92. struct list_head transactions;
  93. struct semaphore transactions_sem;
  94. } fusd_file_t;
  95. /* state kept per device registered under fusd */
  96. struct fusd_dev_t_s {
  97. int magic; /* Magic number for sanity checking */
  98. long version; /* Instance number of this device */
  99. int zombie; /* Is the device dead? */
  100. pid_t pid; /* PID of device driver */
  101. struct task_struct* task;
  102. char *name; /* Name of the device under devfs (/dev) */
  103. char *class_name;
  104. char *dev_name;
  105. struct CLASS *clazz;
  106. int owns_class;
  107. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
  108. struct class_device *device;
  109. #else
  110. struct device *device;
  111. #endif
  112. void *private_data; /* User's private data */
  113. struct cdev* handle;
  114. dev_t dev_id;
  115. fusd_file_t **files; /* Array of this device's open files */
  116. int array_size; /* Size of the array pointed to by 'files' */
  117. int num_files; /* Number of array entries that are valid */
  118. int open_in_progress; /* File is referencing this struct,
  119. but not yet part of the file array */
  120. /* messaging */
  121. fusd_msgC_t *msg_head; /* linked list head for message queue */
  122. fusd_msgC_t *msg_tail; /* linked list tail for message queue */
  123. /* synchronization */
  124. wait_queue_head_t dev_wait; /* Wait queue for kernel->user msgs */
  125. struct semaphore dev_sem; /* Sempahore for device structure */
  126. /* pointer to allow a dev to be placed on a dev_list */
  127. struct list_head devlist;
  128. };
  129. /**** Utility functions & macros ****/
  130. # ifdef CONFIG_FUSD_USE_WAKEUPSYNC
  131. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible_sync(x)
  132. # else
  133. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible(x)
  134. # endif /* CONFIG_FUSD_USE_WAKEUPSYNC */
  135. # ifdef CONFIG_FUSD_DEBUG
  136. static void rdebug_real(char *fmt, ...)
  137. __attribute__ ((format (printf, 1, 2)));
  138. # define RDEBUG(message_level, args...) do { \
  139. if (fusd_debug_level >= message_level) rdebug_real(args); \
  140. } while(0)
  141. # else
  142. # define RDEBUG(message_level, args...)
  143. # endif /* CONFIG_FUSD_DEBUG */
  144. # define ZOMBIE(fusd_dev) ((fusd_dev)->zombie)
  145. # define GET_FUSD_DEV(candidate, fusd_dev) do { \
  146. fusd_dev = candidate; \
  147. if (fusd_dev == NULL || fusd_dev->magic != FUSD_DEV_MAGIC) \
  148. goto invalid_dev; \
  149. } while (0)
  150. # define GET_FUSD_FILE_AND_DEV(candidate, fusd_file, fusd_dev) do { \
  151. fusd_file = candidate; \
  152. if (fusd_file == NULL || fusd_file->magic != FUSD_FILE_MAGIC) \
  153. goto invalid_file; \
  154. GET_FUSD_DEV(fusd_file->fusd_dev, fusd_dev); \
  155. if (fusd_dev->version != fusd_file->fusd_dev_version) \
  156. goto invalid_file; \
  157. } while (0)
  158. # define LOCK_FUSD_DEV(fusd_dev) \
  159. do { down(&fusd_dev->dev_sem); \
  160. if (ZOMBIE(fusd_dev)) { up(&fusd_dev->dev_sem); goto zombie_dev; } \
  161. } while (0)
  162. /* rawlock does not do a zombie check */
  163. # define RAWLOCK_FUSD_DEV(fusd_dev) \
  164. do { down(&fusd_dev->dev_sem); } while (0)
  165. # define UNLOCK_FUSD_DEV(fusd_dev) \
  166. do { up(&fusd_dev->dev_sem); } while (0)
  167. # define LOCK_FUSD_FILE(fusd_file) \
  168. do { down(&fusd_file->file_sem); \
  169. } while (0)
  170. # define UNLOCK_FUSD_FILE(fusd_file) \
  171. do { up(&fusd_file->file_sem); } while (0)
  172. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  173. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  174. KFREE(fusd_msgc); \
  175. } while (0)
  176. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  177. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  178. KFREE(fusd_msgc); \
  179. } while (0)
  180. # define NAME(fusd_dev) ((fusd_dev)->name == NULL ? \
  181. "<noname>" : (fusd_dev)->name)
  182. # ifdef CONFIG_FUSD_MEMDEBUG
  183. static int fusd_mem_init(void);
  184. static void fusd_mem_cleanup(void);
  185. static void fusd_mem_add(void *ptr, int line, int size);
  186. static void fusd_mem_del(void *ptr);
  187. static void *fusd_kmalloc(size_t size, int type, int line);
  188. static void fusd_kfree(void *ptr);
  189. static void *fusd_vmalloc(size_t size, int line);
  190. static void fusd_vfree(void *ptr);
  191. # define KMALLOC(size, type) fusd_kmalloc(size, type, __LINE__)
  192. # define KFREE(ptr) fusd_kfree(ptr)
  193. # define VMALLOC(size) fusd_vmalloc(size, __LINE__)
  194. # define VFREE(ptr) fusd_vfree(ptr)
  195. # else /* no memory debugging */
  196. # define KMALLOC(size, type) kmalloc(size, type)
  197. # define KFREE(ptr) kfree(ptr)
  198. /*# define VMALLOC(size) vmalloc(size)*/
  199. # define VMALLOC(size) kmalloc(size, GFP_KERNEL)
  200. # define VFREE(ptr) kfree(ptr)
  201. # endif /* CONFIG_FUSD_MEMDEBUG */
  202. /* Functions like this should be in the kernel, but they are not. Sigh. */
  203. # ifdef CONFIG_SMP
  204. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  205. DECLARE_MUTEX(atomic_ops);
  206. #else
  207. DEFINE_SEMAPHORE(atomic_ops);
  208. #endif
  209. static __inline__ int atomic_inc_and_ret(int *i)
  210. {
  211. int val;
  212. down(&atomic_ops);
  213. val = (++(*i));
  214. up(&atomic_ops);
  215. return val;
  216. }
  217. # else
  218. static __inline__ int atomic_inc_and_ret(int *i)
  219. {
  220. return (++(*i));
  221. }
  222. # endif
  223. #endif /* __KFUSD_H__ */