u_fs.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * u_fs.h
  3. *
  4. * Utility definitions for the FunctionFS
  5. *
  6. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  7. * http://www.samsung.com
  8. *
  9. * Author: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #ifndef U_FFS_H
  16. #define U_FFS_H
  17. #include <linux/usb/composite.h>
  18. #include <linux/list.h>
  19. #include <linux/mutex.h>
  20. #ifdef VERBOSE_DEBUG
  21. #ifndef pr_vdebug
  22. # define pr_vdebug pr_debug
  23. #endif /* pr_vdebug */
  24. # define ffs_dump_mem(prefix, ptr, len) \
  25. print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len)
  26. #else
  27. #ifndef pr_vdebug
  28. # define pr_vdebug(...) do { } while (0)
  29. #endif /* pr_vdebug */
  30. # define ffs_dump_mem(prefix, ptr, len) do { } while (0)
  31. #endif /* VERBOSE_DEBUG */
  32. #define ENTER() pr_vdebug("%s()\n", __func__)
  33. struct f_fs_opts;
  34. struct ffs_dev {
  35. const char *name;
  36. bool name_allocated;
  37. bool mounted;
  38. bool desc_ready;
  39. bool single;
  40. struct ffs_data *ffs_data;
  41. struct f_fs_opts *opts;
  42. struct list_head entry;
  43. int (*ffs_ready_callback)(struct ffs_data *ffs);
  44. void (*ffs_closed_callback)(struct ffs_data *ffs);
  45. void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev);
  46. void (*ffs_release_dev_callback)(struct ffs_dev *dev);
  47. };
  48. extern struct mutex ffs_lock;
  49. static inline void ffs_dev_lock(void)
  50. {
  51. mutex_lock(&ffs_lock);
  52. }
  53. static inline void ffs_dev_unlock(void)
  54. {
  55. mutex_unlock(&ffs_lock);
  56. }
  57. int ffs_name_dev(struct ffs_dev *dev, const char *name);
  58. int ffs_single_dev(struct ffs_dev *dev);
  59. struct ffs_epfile;
  60. struct ffs_function;
  61. enum ffs_state {
  62. /*
  63. * Waiting for descriptors and strings.
  64. *
  65. * In this state no open(2), read(2) or write(2) on epfiles
  66. * may succeed (which should not be the problem as there
  67. * should be no such files opened in the first place).
  68. */
  69. FFS_READ_DESCRIPTORS,
  70. FFS_READ_STRINGS,
  71. /*
  72. * We've got descriptors and strings. We are or have called
  73. * functionfs_ready_callback(). functionfs_bind() may have
  74. * been called but we don't know.
  75. *
  76. * This is the only state in which operations on epfiles may
  77. * succeed.
  78. */
  79. FFS_ACTIVE,
  80. /*
  81. * All endpoints have been closed. This state is also set if
  82. * we encounter an unrecoverable error. The only
  83. * unrecoverable error is situation when after reading strings
  84. * from user space we fail to initialise epfiles or
  85. * functionfs_ready_callback() returns with error (<0).
  86. *
  87. * In this state no open(2), read(2) or write(2) (both on ep0
  88. * as well as epfile) may succeed (at this point epfiles are
  89. * unlinked and all closed so this is not a problem; ep0 is
  90. * also closed but ep0 file exists and so open(2) on ep0 must
  91. * fail).
  92. */
  93. FFS_CLOSING
  94. };
  95. enum ffs_setup_state {
  96. /* There is no setup request pending. */
  97. FFS_NO_SETUP,
  98. /*
  99. * User has read events and there was a setup request event
  100. * there. The next read/write on ep0 will handle the
  101. * request.
  102. */
  103. FFS_SETUP_PENDING,
  104. /*
  105. * There was event pending but before user space handled it
  106. * some other event was introduced which canceled existing
  107. * setup. If this state is set read/write on ep0 return
  108. * -EIDRM. This state is only set when adding event.
  109. */
  110. FFS_SETUP_CANCELLED
  111. };
  112. struct ffs_data {
  113. struct usb_gadget *gadget;
  114. /*
  115. * Protect access read/write operations, only one read/write
  116. * at a time. As a consequence protects ep0req and company.
  117. * While setup request is being processed (queued) this is
  118. * held.
  119. */
  120. struct mutex mutex;
  121. /*
  122. * Protect access to endpoint related structures (basically
  123. * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
  124. * endpoint zero.
  125. */
  126. spinlock_t eps_lock;
  127. /*
  128. * XXX REVISIT do we need our own request? Since we are not
  129. * handling setup requests immediately user space may be so
  130. * slow that another setup will be sent to the gadget but this
  131. * time not to us but another function and then there could be
  132. * a race. Is that the case? Or maybe we can use cdev->req
  133. * after all, maybe we just need some spinlock for that?
  134. */
  135. struct usb_request *ep0req; /* P: mutex */
  136. struct completion ep0req_completion; /* P: mutex */
  137. /* reference counter */
  138. atomic_t ref;
  139. /* how many files are opened (EP0 and others) */
  140. atomic_t opened;
  141. /* EP0 state */
  142. enum ffs_state state;
  143. /*
  144. * Possible transitions:
  145. * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
  146. * happens only in ep0 read which is P: mutex
  147. * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
  148. * happens only in ep0 i/o which is P: mutex
  149. * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELLED -- P: ev.waitq.lock
  150. * + FFS_SETUP_CANCELLED -> FFS_NO_SETUP -- cmpxchg
  151. *
  152. * This field should never be accessed directly and instead
  153. * ffs_setup_state_clear_cancelled function should be used.
  154. */
  155. enum ffs_setup_state setup_state;
  156. /* Events & such. */
  157. struct {
  158. u8 types[4];
  159. unsigned short count;
  160. /* XXX REVISIT need to update it in some places, or do we? */
  161. unsigned short can_stall;
  162. struct usb_ctrlrequest setup;
  163. wait_queue_head_t waitq;
  164. } ev; /* the whole structure, P: ev.waitq.lock */
  165. /* Flags */
  166. unsigned long flags;
  167. #define FFS_FL_CALL_CLOSED_CALLBACK 0
  168. #define FFS_FL_BOUND 1
  169. /* Active function */
  170. struct ffs_function *func;
  171. /*
  172. * Device name, write once when file system is mounted.
  173. * Intended for user to read if she wants.
  174. */
  175. const char *dev_name;
  176. /* Private data for our user (ie. gadget). Managed by user. */
  177. void *private_data;
  178. /* filled by __ffs_data_got_descs() */
  179. /*
  180. * raw_descs is what you kfree, real_descs points inside of raw_descs,
  181. * where full speed, high speed and super speed descriptors start.
  182. * real_descs_length is the length of all those descriptors.
  183. */
  184. const void *raw_descs_data;
  185. const void *raw_descs;
  186. unsigned raw_descs_length;
  187. unsigned fs_descs_count;
  188. unsigned hs_descs_count;
  189. unsigned ss_descs_count;
  190. unsigned short strings_count;
  191. unsigned short interfaces_count;
  192. unsigned short eps_count;
  193. unsigned short _pad1;
  194. /* filled by __ffs_data_got_strings() */
  195. /* ids in stringtabs are set in functionfs_bind() */
  196. const void *raw_strings;
  197. struct usb_gadget_strings **stringtabs;
  198. /*
  199. * File system's super block, write once when file system is
  200. * mounted.
  201. */
  202. struct super_block *sb;
  203. /* File permissions, written once when fs is mounted */
  204. struct ffs_file_perms {
  205. umode_t mode;
  206. kuid_t uid;
  207. kgid_t gid;
  208. } file_perms;
  209. /*
  210. * The endpoint files, filled by ffs_epfiles_create(),
  211. * destroyed by ffs_epfiles_destroy().
  212. */
  213. struct ffs_epfile *epfiles;
  214. };
  215. struct f_fs_opts {
  216. struct usb_function_instance func_inst;
  217. struct ffs_dev *dev;
  218. unsigned refcnt;
  219. bool no_configfs;
  220. };
  221. static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi)
  222. {
  223. return container_of(fi, struct f_fs_opts, func_inst);
  224. }
  225. #endif /* U_FFS_H */