u_fs.h 8.2 KB

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