kfusd.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. /**** Function Prototypes ****/
  130. STATIC int maybe_free_fusd_dev(fusd_dev_t *fusd_dev);
  131. STATIC int find_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file);
  132. STATIC int free_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file);
  133. STATIC int fusd_fops_call_send(fusd_file_t *fusd_file_arg,
  134. fusd_msg_t *fusd_msg, struct fusd_transaction** transaction);
  135. STATIC int fusd_fops_call_wait(fusd_file_t *fusd_file_arg,
  136. fusd_msg_t **fusd_msg_reply, struct fusd_transaction* transaction);
  137. STATIC void fusd_fops_call_done(fusd_file_t *fusd_file);
  138. STATIC void fusd_forge_close(fusd_msg_t *msg, fusd_dev_t *fusd_dev);
  139. STATIC int fusd_add_transaction(fusd_file_t *fusd_file, int transid, int subcmd, int size, struct fusd_transaction** out_transaction);
  140. STATIC void fusd_cleanup_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction);
  141. STATIC void fusd_remove_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction);
  142. STATIC struct fusd_transaction* fusd_find_transaction(fusd_file_t *fusd_file, int transid);
  143. STATIC struct fusd_transaction* fusd_find_transaction_by_pid(fusd_file_t *fusd_file, int pid);
  144. /**** Utility functions & macros ****/
  145. # ifdef CONFIG_FUSD_USE_WAKEUPSYNC
  146. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible_sync(x)
  147. # else
  148. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible(x)
  149. # endif /* CONFIG_FUSD_USE_WAKEUPSYNC */
  150. # ifdef CONFIG_FUSD_DEBUG
  151. static void rdebug_real(char *fmt, ...)
  152. __attribute__ ((format (printf, 1, 2)));
  153. # define RDEBUG(message_level, args...) do { \
  154. if (fusd_debug_level >= message_level) rdebug_real(args); \
  155. } while(0)
  156. # else
  157. # define RDEBUG(message_level, args...)
  158. # endif /* CONFIG_FUSD_DEBUG */
  159. # define ZOMBIE(fusd_dev) ((fusd_dev)->zombie)
  160. # define GET_FUSD_DEV(candidate, fusd_dev) do { \
  161. fusd_dev = candidate; \
  162. if (fusd_dev == NULL || fusd_dev->magic != FUSD_DEV_MAGIC) \
  163. goto invalid_dev; \
  164. } while (0)
  165. # define GET_FUSD_FILE_AND_DEV(candidate, fusd_file, fusd_dev) do { \
  166. fusd_file = candidate; \
  167. if (fusd_file == NULL || fusd_file->magic != FUSD_FILE_MAGIC) \
  168. goto invalid_file; \
  169. GET_FUSD_DEV(fusd_file->fusd_dev, fusd_dev); \
  170. if (fusd_dev->version != fusd_file->fusd_dev_version) \
  171. goto invalid_file; \
  172. } while (0)
  173. # define LOCK_FUSD_DEV(fusd_dev) \
  174. do { down(&fusd_dev->dev_sem); \
  175. if (ZOMBIE(fusd_dev)) { up(&fusd_dev->dev_sem); goto zombie_dev; } \
  176. } while (0)
  177. /* rawlock does not do a zombie check */
  178. # define RAWLOCK_FUSD_DEV(fusd_dev) \
  179. do { down(&fusd_dev->dev_sem); } while (0)
  180. # define UNLOCK_FUSD_DEV(fusd_dev) \
  181. do { up(&fusd_dev->dev_sem); } while (0)
  182. # define LOCK_FUSD_FILE(fusd_file) \
  183. do { down(&fusd_file->file_sem); \
  184. } while (0)
  185. # define UNLOCK_FUSD_FILE(fusd_file) \
  186. do { up(&fusd_file->file_sem); } while (0)
  187. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  188. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  189. KFREE(fusd_msgc); \
  190. } while (0)
  191. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  192. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  193. KFREE(fusd_msgc); \
  194. } while (0)
  195. # define NAME(fusd_dev) ((fusd_dev)->name == NULL ? \
  196. "<noname>" : (fusd_dev)->name)
  197. # ifdef CONFIG_FUSD_MEMDEBUG
  198. static int fusd_mem_init(void);
  199. static void fusd_mem_cleanup(void);
  200. static void fusd_mem_add(void *ptr, int line, int size);
  201. static void fusd_mem_del(void *ptr);
  202. static void *fusd_kmalloc(size_t size, int type, int line);
  203. static void fusd_kfree(void *ptr);
  204. static void *fusd_vmalloc(size_t size, int line);
  205. static void fusd_vfree(void *ptr);
  206. # define KMALLOC(size, type) fusd_kmalloc(size, type, __LINE__)
  207. # define KFREE(ptr) fusd_kfree(ptr)
  208. # define VMALLOC(size) fusd_vmalloc(size, __LINE__)
  209. # define VFREE(ptr) fusd_vfree(ptr)
  210. # else /* no memory debugging */
  211. # define KMALLOC(size, type) kmalloc(size, type)
  212. # define KFREE(ptr) kfree(ptr)
  213. /*# define VMALLOC(size) vmalloc(size)*/
  214. # define VMALLOC(size) kmalloc(size, GFP_KERNEL)
  215. # define VFREE(ptr) kfree(ptr)
  216. # endif /* CONFIG_FUSD_MEMDEBUG */
  217. /* Functions like this should be in the kernel, but they are not. Sigh. */
  218. # ifdef CONFIG_SMP
  219. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  220. DECLARE_MUTEX(atomic_ops);
  221. #else
  222. DEFINE_SEMAPHORE(atomic_ops);
  223. #endif
  224. static __inline__ int atomic_inc_and_ret(int *i)
  225. {
  226. int val;
  227. down(&atomic_ops);
  228. val = (++(*i));
  229. up(&atomic_ops);
  230. return val;
  231. }
  232. # else
  233. static __inline__ int atomic_inc_and_ret(int *i)
  234. {
  235. return (++(*i));
  236. }
  237. # endif
  238. #endif /* __KFUSD_H__ */