u_fs.h 8.2 KB

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