Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 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 | |
| 16 | #ifndef U_FFS_H |
| 17 | #define U_FFS_H |
| 18 | |
| 19 | #include <linux/usb/composite.h> |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 20 | #include <linux/list.h> |
| 21 | #include <linux/mutex.h> |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 22 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 23 | #ifdef VERBOSE_DEBUG |
| 24 | #ifndef pr_vdebug |
| 25 | # define pr_vdebug pr_debug |
| 26 | #endif /* pr_vdebug */ |
| 27 | # define ffs_dump_mem(prefix, ptr, len) \ |
| 28 | print_hex_dump_bytes(pr_fmt(prefix ": "), DUMP_PREFIX_NONE, ptr, len) |
| 29 | #else |
| 30 | #ifndef pr_vdebug |
| 31 | # define pr_vdebug(...) do { } while (0) |
| 32 | #endif /* pr_vdebug */ |
| 33 | # define ffs_dump_mem(prefix, ptr, len) do { } while (0) |
| 34 | #endif /* VERBOSE_DEBUG */ |
| 35 | |
| 36 | #define ENTER() pr_vdebug("%s()\n", __func__) |
| 37 | |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 38 | struct f_fs_opts; |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 39 | |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 40 | struct ffs_dev { |
| 41 | const char *name; |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 42 | bool name_allocated; |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 43 | bool mounted; |
| 44 | bool desc_ready; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 45 | bool single; |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 46 | struct ffs_data *ffs_data; |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 47 | struct f_fs_opts *opts; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 48 | struct list_head entry; |
| 49 | |
| 50 | int (*ffs_ready_callback)(struct ffs_data *ffs); |
| 51 | void (*ffs_closed_callback)(struct ffs_data *ffs); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 52 | void *(*ffs_acquire_dev_callback)(struct ffs_dev *dev); |
| 53 | void (*ffs_release_dev_callback)(struct ffs_dev *dev); |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 56 | extern struct mutex ffs_lock; |
| 57 | |
| 58 | static inline void ffs_dev_lock(void) |
| 59 | { |
| 60 | mutex_lock(&ffs_lock); |
| 61 | } |
| 62 | |
| 63 | static inline void ffs_dev_unlock(void) |
| 64 | { |
| 65 | mutex_unlock(&ffs_lock); |
| 66 | } |
| 67 | |
| 68 | struct ffs_dev *ffs_alloc_dev(void); |
| 69 | int ffs_name_dev(struct ffs_dev *dev, const char *name); |
| 70 | int ffs_single_dev(struct ffs_dev *dev); |
| 71 | void ffs_free_dev(struct ffs_dev *dev); |
| 72 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 73 | struct ffs_epfile; |
| 74 | struct ffs_function; |
| 75 | |
| 76 | enum ffs_state { |
| 77 | /* |
| 78 | * Waiting for descriptors and strings. |
| 79 | * |
| 80 | * In this state no open(2), read(2) or write(2) on epfiles |
| 81 | * may succeed (which should not be the problem as there |
| 82 | * should be no such files opened in the first place). |
| 83 | */ |
| 84 | FFS_READ_DESCRIPTORS, |
| 85 | FFS_READ_STRINGS, |
| 86 | |
| 87 | /* |
| 88 | * We've got descriptors and strings. We are or have called |
| 89 | * functionfs_ready_callback(). functionfs_bind() may have |
| 90 | * been called but we don't know. |
| 91 | * |
| 92 | * This is the only state in which operations on epfiles may |
| 93 | * succeed. |
| 94 | */ |
| 95 | FFS_ACTIVE, |
| 96 | |
| 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 | |
| 113 | enum ffs_setup_state { |
| 114 | /* There is no setup request pending. */ |
| 115 | FFS_NO_SETUP, |
| 116 | /* |
| 117 | * User has read events and there was a setup request event |
| 118 | * there. The next read/write on ep0 will handle the |
| 119 | * request. |
| 120 | */ |
| 121 | FFS_SETUP_PENDING, |
| 122 | /* |
| 123 | * There was event pending but before user space handled it |
| 124 | * some other event was introduced which canceled existing |
| 125 | * setup. If this state is set read/write on ep0 return |
| 126 | * -EIDRM. This state is only set when adding event. |
| 127 | */ |
| 128 | FFS_SETUP_CANCELED |
| 129 | }; |
| 130 | |
| 131 | struct ffs_data { |
| 132 | struct usb_gadget *gadget; |
| 133 | |
| 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 | /* |
| 143 | * Protect access to endpoint related structures (basically |
| 144 | * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for |
| 145 | * endpoint zero. |
| 146 | */ |
| 147 | spinlock_t eps_lock; |
| 148 | |
| 149 | /* |
| 150 | * XXX REVISIT do we need our own request? Since we are not |
| 151 | * handling setup requests immediately user space may be so |
| 152 | * slow that another setup will be sent to the gadget but this |
| 153 | * time not to us but another function and then there could be |
| 154 | * a race. Is that the case? Or maybe we can use cdev->req |
| 155 | * after all, maybe we just need some spinlock for that? |
| 156 | */ |
| 157 | struct usb_request *ep0req; /* P: mutex */ |
| 158 | struct completion ep0req_completion; /* P: mutex */ |
| 159 | int ep0req_status; /* P: mutex */ |
| 160 | |
| 161 | /* reference counter */ |
| 162 | atomic_t ref; |
| 163 | /* how many files are opened (EP0 and others) */ |
| 164 | atomic_t opened; |
| 165 | |
| 166 | /* EP0 state */ |
| 167 | enum ffs_state state; |
| 168 | |
| 169 | /* |
| 170 | * Possible transitions: |
| 171 | * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock |
| 172 | * happens only in ep0 read which is P: mutex |
| 173 | * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock |
| 174 | * happens only in ep0 i/o which is P: mutex |
| 175 | * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock |
| 176 | * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg |
| 177 | */ |
| 178 | enum ffs_setup_state setup_state; |
| 179 | |
| 180 | #define FFS_SETUP_STATE(ffs) \ |
| 181 | ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \ |
| 182 | FFS_SETUP_CANCELED, FFS_NO_SETUP)) |
| 183 | |
| 184 | /* Events & such. */ |
| 185 | struct { |
| 186 | u8 types[4]; |
| 187 | unsigned short count; |
| 188 | /* XXX REVISIT need to update it in some places, or do we? */ |
| 189 | unsigned short can_stall; |
| 190 | struct usb_ctrlrequest setup; |
| 191 | |
| 192 | wait_queue_head_t waitq; |
| 193 | } ev; /* the whole structure, P: ev.waitq.lock */ |
| 194 | |
| 195 | /* Flags */ |
| 196 | unsigned long flags; |
| 197 | #define FFS_FL_CALL_CLOSED_CALLBACK 0 |
| 198 | #define FFS_FL_BOUND 1 |
| 199 | |
| 200 | /* Active function */ |
| 201 | struct ffs_function *func; |
| 202 | |
| 203 | /* |
| 204 | * Device name, write once when file system is mounted. |
| 205 | * Intended for user to read if she wants. |
| 206 | */ |
| 207 | const char *dev_name; |
| 208 | /* Private data for our user (ie. gadget). Managed by user. */ |
| 209 | void *private_data; |
| 210 | |
| 211 | /* filled by __ffs_data_got_descs() */ |
| 212 | /* |
| 213 | * Real descriptors are 16 bytes after raw_descs (so you need |
| 214 | * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the |
| 215 | * first full speed descriptor). raw_descs_length and |
| 216 | * raw_fs_descs_length do not have those 16 bytes added. |
| 217 | */ |
| 218 | const void *raw_descs; |
| 219 | unsigned raw_descs_length; |
| 220 | unsigned raw_fs_descs_length; |
| 221 | unsigned fs_descs_count; |
| 222 | unsigned hs_descs_count; |
| 223 | |
| 224 | unsigned short strings_count; |
| 225 | unsigned short interfaces_count; |
| 226 | unsigned short eps_count; |
| 227 | unsigned short _pad1; |
| 228 | |
| 229 | /* filled by __ffs_data_got_strings() */ |
| 230 | /* ids in stringtabs are set in functionfs_bind() */ |
| 231 | const void *raw_strings; |
| 232 | struct usb_gadget_strings **stringtabs; |
| 233 | |
| 234 | /* |
| 235 | * File system's super block, write once when file system is |
| 236 | * mounted. |
| 237 | */ |
| 238 | struct super_block *sb; |
| 239 | |
| 240 | /* File permissions, written once when fs is mounted */ |
| 241 | struct ffs_file_perms { |
| 242 | umode_t mode; |
| 243 | kuid_t uid; |
| 244 | kgid_t gid; |
| 245 | } file_perms; |
| 246 | |
| 247 | /* |
| 248 | * The endpoint files, filled by ffs_epfiles_create(), |
| 249 | * destroyed by ffs_epfiles_destroy(). |
| 250 | */ |
| 251 | struct ffs_epfile *epfiles; |
| 252 | }; |
| 253 | |
| 254 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 255 | struct f_fs_opts { |
| 256 | struct usb_function_instance func_inst; |
| 257 | struct ffs_dev *dev; |
| 258 | unsigned refcnt; |
| 259 | bool no_configfs; |
| 260 | }; |
| 261 | |
| 262 | static inline struct f_fs_opts *to_f_fs_opts(struct usb_function_instance *fi) |
| 263 | { |
| 264 | return container_of(fi, struct f_fs_opts, func_inst); |
| 265 | } |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 266 | |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 267 | #endif /* U_FFS_H */ |