u_fs.h 8.2 KB

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