Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1 | /* |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2 | * f_fs.c -- user mode file system API for USB composite function controllers |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 5 | * Author: Michal Nazarewicz <mina86@mina86.com> |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 6 | * |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 7 | * Based on inode.c (GadgetFS) which was: |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 8 | * Copyright (C) 2003-2004 David Brownell |
| 9 | * Copyright (C) 2003 Agilent Technologies |
| 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 as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
| 18 | /* #define DEBUG */ |
| 19 | /* #define VERBOSE_DEBUG */ |
| 20 | |
| 21 | #include <linux/blkdev.h> |
Randy Dunlap | b060869 | 2010-05-10 10:51:36 -0700 | [diff] [blame] | 22 | #include <linux/pagemap.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Koen Beel | 560f118 | 2012-05-30 20:43:37 +0200 | [diff] [blame] | 24 | #include <linux/hid.h> |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 25 | #include <linux/module.h> |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 27 | |
| 28 | #include <linux/usb/composite.h> |
| 29 | #include <linux/usb/functionfs.h> |
| 30 | |
Andrzej Pietrasiewicz | e72c39c | 2013-12-03 15:15:31 +0100 | [diff] [blame] | 31 | #include "u_fs.h" |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 32 | #include "configfs.h" |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 33 | |
| 34 | #define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */ |
| 35 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 36 | /* Variable Length Array Macros **********************************************/ |
| 37 | #define vla_group(groupname) size_t groupname##__next = 0 |
| 38 | #define vla_group_size(groupname) groupname##__next |
| 39 | |
| 40 | #define vla_item(groupname, type, name, n) \ |
| 41 | size_t groupname##_##name##__offset = ({ \ |
| 42 | size_t align_mask = __alignof__(type) - 1; \ |
| 43 | size_t offset = (groupname##__next + align_mask) & ~align_mask;\ |
| 44 | size_t size = (n) * sizeof(type); \ |
| 45 | groupname##__next = offset + size; \ |
| 46 | offset; \ |
| 47 | }) |
| 48 | |
| 49 | #define vla_item_with_sz(groupname, type, name, n) \ |
| 50 | size_t groupname##_##name##__sz = (n) * sizeof(type); \ |
| 51 | size_t groupname##_##name##__offset = ({ \ |
| 52 | size_t align_mask = __alignof__(type) - 1; \ |
| 53 | size_t offset = (groupname##__next + align_mask) & ~align_mask;\ |
| 54 | size_t size = groupname##_##name##__sz; \ |
| 55 | groupname##__next = offset + size; \ |
| 56 | offset; \ |
| 57 | }) |
| 58 | |
| 59 | #define vla_ptr(ptr, groupname, name) \ |
| 60 | ((void *) ((char *)ptr + groupname##_##name##__offset)) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 61 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 62 | /* Reference counter handling */ |
| 63 | static void ffs_data_get(struct ffs_data *ffs); |
| 64 | static void ffs_data_put(struct ffs_data *ffs); |
| 65 | /* Creates new ffs_data object. */ |
| 66 | static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc)); |
| 67 | |
| 68 | /* Opened counter handling. */ |
| 69 | static void ffs_data_opened(struct ffs_data *ffs); |
| 70 | static void ffs_data_closed(struct ffs_data *ffs); |
| 71 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 72 | /* Called with ffs->mutex held; take over ownership of data. */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 73 | static int __must_check |
| 74 | __ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len); |
| 75 | static int __must_check |
| 76 | __ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len); |
| 77 | |
| 78 | |
| 79 | /* The function structure ***************************************************/ |
| 80 | |
| 81 | struct ffs_ep; |
| 82 | |
| 83 | struct ffs_function { |
| 84 | struct usb_configuration *conf; |
| 85 | struct usb_gadget *gadget; |
| 86 | struct ffs_data *ffs; |
| 87 | |
| 88 | struct ffs_ep *eps; |
| 89 | u8 eps_revmap[16]; |
| 90 | short *interfaces_nums; |
| 91 | |
| 92 | struct usb_function function; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | static struct ffs_function *ffs_func_from_usb(struct usb_function *f) |
| 97 | { |
| 98 | return container_of(f, struct ffs_function, function); |
| 99 | } |
| 100 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 101 | |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 102 | static inline enum ffs_setup_state |
| 103 | ffs_setup_state_clear_cancelled(struct ffs_data *ffs) |
| 104 | { |
| 105 | return (enum ffs_setup_state) |
| 106 | cmpxchg(&ffs->setup_state, FFS_SETUP_CANCELLED, FFS_NO_SETUP); |
| 107 | } |
| 108 | |
| 109 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 110 | static void ffs_func_eps_disable(struct ffs_function *func); |
| 111 | static int __must_check ffs_func_eps_enable(struct ffs_function *func); |
| 112 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 113 | static int ffs_func_bind(struct usb_configuration *, |
| 114 | struct usb_function *); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 115 | static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned); |
| 116 | static void ffs_func_disable(struct usb_function *); |
| 117 | static int ffs_func_setup(struct usb_function *, |
| 118 | const struct usb_ctrlrequest *); |
| 119 | static void ffs_func_suspend(struct usb_function *); |
| 120 | static void ffs_func_resume(struct usb_function *); |
| 121 | |
| 122 | |
| 123 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num); |
| 124 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf); |
| 125 | |
| 126 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 127 | /* The endpoints structures *************************************************/ |
| 128 | |
| 129 | struct ffs_ep { |
| 130 | struct usb_ep *ep; /* P: ffs->eps_lock */ |
| 131 | struct usb_request *req; /* P: epfile->mutex */ |
| 132 | |
| 133 | /* [0]: full speed, [1]: high speed */ |
| 134 | struct usb_endpoint_descriptor *descs[2]; |
| 135 | |
| 136 | u8 num; |
| 137 | |
| 138 | int status; /* P: epfile->mutex */ |
| 139 | }; |
| 140 | |
| 141 | struct ffs_epfile { |
| 142 | /* Protects ep->ep and ep->req. */ |
| 143 | struct mutex mutex; |
| 144 | wait_queue_head_t wait; |
| 145 | |
| 146 | struct ffs_data *ffs; |
| 147 | struct ffs_ep *ep; /* P: ffs->eps_lock */ |
| 148 | |
| 149 | struct dentry *dentry; |
| 150 | |
| 151 | char name[5]; |
| 152 | |
| 153 | unsigned char in; /* P: ffs->eps_lock */ |
| 154 | unsigned char isoc; /* P: ffs->eps_lock */ |
| 155 | |
| 156 | unsigned char _pad; |
| 157 | }; |
| 158 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 159 | static int __must_check ffs_epfiles_create(struct ffs_data *ffs); |
| 160 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count); |
| 161 | |
| 162 | static struct inode *__must_check |
| 163 | ffs_sb_create_file(struct super_block *sb, const char *name, void *data, |
| 164 | const struct file_operations *fops, |
| 165 | struct dentry **dentry_p); |
| 166 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 167 | /* Devices management *******************************************************/ |
| 168 | |
| 169 | DEFINE_MUTEX(ffs_lock); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 170 | EXPORT_SYMBOL(ffs_lock); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 171 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 172 | static struct ffs_dev *_ffs_find_dev(const char *name); |
| 173 | static struct ffs_dev *_ffs_alloc_dev(void); |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 174 | static int _ffs_name_dev(struct ffs_dev *dev, const char *name); |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 175 | static void _ffs_free_dev(struct ffs_dev *dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 176 | static void *ffs_acquire_dev(const char *dev_name); |
| 177 | static void ffs_release_dev(struct ffs_data *ffs_data); |
| 178 | static int ffs_ready(struct ffs_data *ffs); |
| 179 | static void ffs_closed(struct ffs_data *ffs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 180 | |
| 181 | /* Misc helper functions ****************************************************/ |
| 182 | |
| 183 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) |
| 184 | __attribute__((warn_unused_result, nonnull)); |
Al Viro | 260ef31 | 2012-09-26 21:43:45 -0400 | [diff] [blame] | 185 | static char *ffs_prepare_buffer(const char __user *buf, size_t len) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 186 | __attribute__((warn_unused_result, nonnull)); |
| 187 | |
| 188 | |
| 189 | /* Control file aka ep0 *****************************************************/ |
| 190 | |
| 191 | static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req) |
| 192 | { |
| 193 | struct ffs_data *ffs = req->context; |
| 194 | |
| 195 | complete_all(&ffs->ep0req_completion); |
| 196 | } |
| 197 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 198 | static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len) |
| 199 | { |
| 200 | struct usb_request *req = ffs->ep0req; |
| 201 | int ret; |
| 202 | |
| 203 | req->zero = len < le16_to_cpu(ffs->ev.setup.wLength); |
| 204 | |
| 205 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 206 | |
| 207 | req->buf = data; |
| 208 | req->length = len; |
| 209 | |
Marek Szyprowski | ce1fd35 | 2011-01-28 13:55:36 +0100 | [diff] [blame] | 210 | /* |
| 211 | * UDC layer requires to provide a buffer even for ZLP, but should |
| 212 | * not use it at all. Let's provide some poisoned pointer to catch |
| 213 | * possible bug in the driver. |
| 214 | */ |
| 215 | if (req->buf == NULL) |
| 216 | req->buf = (void *)0xDEADBABE; |
| 217 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 218 | reinit_completion(&ffs->ep0req_completion); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 219 | |
| 220 | ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC); |
| 221 | if (unlikely(ret < 0)) |
| 222 | return ret; |
| 223 | |
| 224 | ret = wait_for_completion_interruptible(&ffs->ep0req_completion); |
| 225 | if (unlikely(ret)) { |
| 226 | usb_ep_dequeue(ffs->gadget->ep0, req); |
| 227 | return -EINTR; |
| 228 | } |
| 229 | |
| 230 | ffs->setup_state = FFS_NO_SETUP; |
| 231 | return ffs->ep0req_status; |
| 232 | } |
| 233 | |
| 234 | static int __ffs_ep0_stall(struct ffs_data *ffs) |
| 235 | { |
| 236 | if (ffs->ev.can_stall) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 237 | pr_vdebug("ep0 stall\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 238 | usb_ep_set_halt(ffs->gadget->ep0); |
| 239 | ffs->setup_state = FFS_NO_SETUP; |
| 240 | return -EL2HLT; |
| 241 | } else { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 242 | pr_debug("bogus ep0 stall!\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 243 | return -ESRCH; |
| 244 | } |
| 245 | } |
| 246 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 247 | static ssize_t ffs_ep0_write(struct file *file, const char __user *buf, |
| 248 | size_t len, loff_t *ptr) |
| 249 | { |
| 250 | struct ffs_data *ffs = file->private_data; |
| 251 | ssize_t ret; |
| 252 | char *data; |
| 253 | |
| 254 | ENTER(); |
| 255 | |
| 256 | /* Fast check if setup was canceled */ |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 257 | if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 258 | return -EIDRM; |
| 259 | |
| 260 | /* Acquire mutex */ |
| 261 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); |
| 262 | if (unlikely(ret < 0)) |
| 263 | return ret; |
| 264 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 265 | /* Check state */ |
| 266 | switch (ffs->state) { |
| 267 | case FFS_READ_DESCRIPTORS: |
| 268 | case FFS_READ_STRINGS: |
| 269 | /* Copy data */ |
| 270 | if (unlikely(len < 16)) { |
| 271 | ret = -EINVAL; |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | data = ffs_prepare_buffer(buf, len); |
Tobias Klauser | 537baab | 2010-12-09 15:52:39 +0100 | [diff] [blame] | 276 | if (IS_ERR(data)) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 277 | ret = PTR_ERR(data); |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | /* Handle data */ |
| 282 | if (ffs->state == FFS_READ_DESCRIPTORS) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 283 | pr_info("read descriptors\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 284 | ret = __ffs_data_got_descs(ffs, data, len); |
| 285 | if (unlikely(ret < 0)) |
| 286 | break; |
| 287 | |
| 288 | ffs->state = FFS_READ_STRINGS; |
| 289 | ret = len; |
| 290 | } else { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 291 | pr_info("read strings\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 292 | ret = __ffs_data_got_strings(ffs, data, len); |
| 293 | if (unlikely(ret < 0)) |
| 294 | break; |
| 295 | |
| 296 | ret = ffs_epfiles_create(ffs); |
| 297 | if (unlikely(ret)) { |
| 298 | ffs->state = FFS_CLOSING; |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | ffs->state = FFS_ACTIVE; |
| 303 | mutex_unlock(&ffs->mutex); |
| 304 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 305 | ret = ffs_ready(ffs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 306 | if (unlikely(ret < 0)) { |
| 307 | ffs->state = FFS_CLOSING; |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags); |
| 312 | return len; |
| 313 | } |
| 314 | break; |
| 315 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 316 | case FFS_ACTIVE: |
| 317 | data = NULL; |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 318 | /* |
| 319 | * We're called from user space, we can use _irq |
| 320 | * rather then _irqsave |
| 321 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 322 | spin_lock_irq(&ffs->ev.waitq.lock); |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 323 | switch (ffs_setup_state_clear_cancelled(ffs)) { |
Michal Nazarewicz | e46318a | 2014-02-10 10:42:40 +0100 | [diff] [blame] | 324 | case FFS_SETUP_CANCELLED: |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 325 | ret = -EIDRM; |
| 326 | goto done_spin; |
| 327 | |
| 328 | case FFS_NO_SETUP: |
| 329 | ret = -ESRCH; |
| 330 | goto done_spin; |
| 331 | |
| 332 | case FFS_SETUP_PENDING: |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | /* FFS_SETUP_PENDING */ |
| 337 | if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) { |
| 338 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 339 | ret = __ffs_ep0_stall(ffs); |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | /* FFS_SETUP_PENDING and not stall */ |
| 344 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); |
| 345 | |
| 346 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 347 | |
| 348 | data = ffs_prepare_buffer(buf, len); |
Tobias Klauser | 537baab | 2010-12-09 15:52:39 +0100 | [diff] [blame] | 349 | if (IS_ERR(data)) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 350 | ret = PTR_ERR(data); |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | spin_lock_irq(&ffs->ev.waitq.lock); |
| 355 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 356 | /* |
| 357 | * We are guaranteed to be still in FFS_ACTIVE state |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 358 | * but the state of setup could have changed from |
Michal Nazarewicz | e46318a | 2014-02-10 10:42:40 +0100 | [diff] [blame] | 359 | * FFS_SETUP_PENDING to FFS_SETUP_CANCELLED so we need |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 360 | * to check for that. If that happened we copied data |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 361 | * from user space in vain but it's unlikely. |
| 362 | * |
| 363 | * For sure we are not in FFS_NO_SETUP since this is |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 364 | * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP |
| 365 | * transition can be performed and it's protected by |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 366 | * mutex. |
| 367 | */ |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 368 | if (ffs_setup_state_clear_cancelled(ffs) == |
| 369 | FFS_SETUP_CANCELLED) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 370 | ret = -EIDRM; |
| 371 | done_spin: |
| 372 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 373 | } else { |
| 374 | /* unlocks spinlock */ |
| 375 | ret = __ffs_ep0_queue_wait(ffs, data, len); |
| 376 | } |
| 377 | kfree(data); |
| 378 | break; |
| 379 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 380 | default: |
| 381 | ret = -EBADFD; |
| 382 | break; |
| 383 | } |
| 384 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 385 | mutex_unlock(&ffs->mutex); |
| 386 | return ret; |
| 387 | } |
| 388 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 389 | static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf, |
| 390 | size_t n) |
| 391 | { |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 392 | /* |
| 393 | * We are holding ffs->ev.waitq.lock and ffs->mutex and we need |
| 394 | * to release them. |
| 395 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 396 | struct usb_functionfs_event events[n]; |
| 397 | unsigned i = 0; |
| 398 | |
| 399 | memset(events, 0, sizeof events); |
| 400 | |
| 401 | do { |
| 402 | events[i].type = ffs->ev.types[i]; |
| 403 | if (events[i].type == FUNCTIONFS_SETUP) { |
| 404 | events[i].u.setup = ffs->ev.setup; |
| 405 | ffs->setup_state = FFS_SETUP_PENDING; |
| 406 | } |
| 407 | } while (++i < n); |
| 408 | |
| 409 | if (n < ffs->ev.count) { |
| 410 | ffs->ev.count -= n; |
| 411 | memmove(ffs->ev.types, ffs->ev.types + n, |
| 412 | ffs->ev.count * sizeof *ffs->ev.types); |
| 413 | } else { |
| 414 | ffs->ev.count = 0; |
| 415 | } |
| 416 | |
| 417 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 418 | mutex_unlock(&ffs->mutex); |
| 419 | |
| 420 | return unlikely(__copy_to_user(buf, events, sizeof events)) |
| 421 | ? -EFAULT : sizeof events; |
| 422 | } |
| 423 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 424 | static ssize_t ffs_ep0_read(struct file *file, char __user *buf, |
| 425 | size_t len, loff_t *ptr) |
| 426 | { |
| 427 | struct ffs_data *ffs = file->private_data; |
| 428 | char *data = NULL; |
| 429 | size_t n; |
| 430 | int ret; |
| 431 | |
| 432 | ENTER(); |
| 433 | |
| 434 | /* Fast check if setup was canceled */ |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 435 | if (ffs_setup_state_clear_cancelled(ffs) == FFS_SETUP_CANCELLED) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 436 | return -EIDRM; |
| 437 | |
| 438 | /* Acquire mutex */ |
| 439 | ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK); |
| 440 | if (unlikely(ret < 0)) |
| 441 | return ret; |
| 442 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 443 | /* Check state */ |
| 444 | if (ffs->state != FFS_ACTIVE) { |
| 445 | ret = -EBADFD; |
| 446 | goto done_mutex; |
| 447 | } |
| 448 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 449 | /* |
| 450 | * We're called from user space, we can use _irq rather then |
| 451 | * _irqsave |
| 452 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 453 | spin_lock_irq(&ffs->ev.waitq.lock); |
| 454 | |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 455 | switch (ffs_setup_state_clear_cancelled(ffs)) { |
Michal Nazarewicz | e46318a | 2014-02-10 10:42:40 +0100 | [diff] [blame] | 456 | case FFS_SETUP_CANCELLED: |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 457 | ret = -EIDRM; |
| 458 | break; |
| 459 | |
| 460 | case FFS_NO_SETUP: |
| 461 | n = len / sizeof(struct usb_functionfs_event); |
| 462 | if (unlikely(!n)) { |
| 463 | ret = -EINVAL; |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) { |
| 468 | ret = -EAGAIN; |
| 469 | break; |
| 470 | } |
| 471 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 472 | if (wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, |
| 473 | ffs->ev.count)) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 474 | ret = -EINTR; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | return __ffs_ep0_read_events(ffs, buf, |
| 479 | min(n, (size_t)ffs->ev.count)); |
| 480 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 481 | case FFS_SETUP_PENDING: |
| 482 | if (ffs->ev.setup.bRequestType & USB_DIR_IN) { |
| 483 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 484 | ret = __ffs_ep0_stall(ffs); |
| 485 | goto done_mutex; |
| 486 | } |
| 487 | |
| 488 | len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength)); |
| 489 | |
| 490 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 491 | |
| 492 | if (likely(len)) { |
| 493 | data = kmalloc(len, GFP_KERNEL); |
| 494 | if (unlikely(!data)) { |
| 495 | ret = -ENOMEM; |
| 496 | goto done_mutex; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | spin_lock_irq(&ffs->ev.waitq.lock); |
| 501 | |
| 502 | /* See ffs_ep0_write() */ |
Michal Nazarewicz | a7ecf05 | 2014-02-10 10:42:41 +0100 | [diff] [blame^] | 503 | if (ffs_setup_state_clear_cancelled(ffs) == |
| 504 | FFS_SETUP_CANCELLED) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 505 | ret = -EIDRM; |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | /* unlocks spinlock */ |
| 510 | ret = __ffs_ep0_queue_wait(ffs, data, len); |
| 511 | if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len))) |
| 512 | ret = -EFAULT; |
| 513 | goto done_mutex; |
| 514 | |
| 515 | default: |
| 516 | ret = -EBADFD; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | spin_unlock_irq(&ffs->ev.waitq.lock); |
| 521 | done_mutex: |
| 522 | mutex_unlock(&ffs->mutex); |
| 523 | kfree(data); |
| 524 | return ret; |
| 525 | } |
| 526 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 527 | static int ffs_ep0_open(struct inode *inode, struct file *file) |
| 528 | { |
| 529 | struct ffs_data *ffs = inode->i_private; |
| 530 | |
| 531 | ENTER(); |
| 532 | |
| 533 | if (unlikely(ffs->state == FFS_CLOSING)) |
| 534 | return -EBUSY; |
| 535 | |
| 536 | file->private_data = ffs; |
| 537 | ffs_data_opened(ffs); |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 542 | static int ffs_ep0_release(struct inode *inode, struct file *file) |
| 543 | { |
| 544 | struct ffs_data *ffs = file->private_data; |
| 545 | |
| 546 | ENTER(); |
| 547 | |
| 548 | ffs_data_closed(ffs); |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 553 | static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value) |
| 554 | { |
| 555 | struct ffs_data *ffs = file->private_data; |
| 556 | struct usb_gadget *gadget = ffs->gadget; |
| 557 | long ret; |
| 558 | |
| 559 | ENTER(); |
| 560 | |
| 561 | if (code == FUNCTIONFS_INTERFACE_REVMAP) { |
| 562 | struct ffs_function *func = ffs->func; |
| 563 | ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV; |
Andrzej Pietrasiewicz | 92b0abf | 2012-03-28 09:30:50 +0200 | [diff] [blame] | 564 | } else if (gadget && gadget->ops->ioctl) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 565 | ret = gadget->ops->ioctl(gadget, code, value); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 566 | } else { |
| 567 | ret = -ENOTTY; |
| 568 | } |
| 569 | |
| 570 | return ret; |
| 571 | } |
| 572 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 573 | static const struct file_operations ffs_ep0_operations = { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 574 | .llseek = no_llseek, |
| 575 | |
| 576 | .open = ffs_ep0_open, |
| 577 | .write = ffs_ep0_write, |
| 578 | .read = ffs_ep0_read, |
| 579 | .release = ffs_ep0_release, |
| 580 | .unlocked_ioctl = ffs_ep0_ioctl, |
| 581 | }; |
| 582 | |
| 583 | |
| 584 | /* "Normal" endpoints operations ********************************************/ |
| 585 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 586 | static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req) |
| 587 | { |
| 588 | ENTER(); |
| 589 | if (likely(req->context)) { |
| 590 | struct ffs_ep *ep = _ep->driver_data; |
| 591 | ep->status = req->status ? req->status : req->actual; |
| 592 | complete(req->context); |
| 593 | } |
| 594 | } |
| 595 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 596 | static ssize_t ffs_epfile_io(struct file *file, |
| 597 | char __user *buf, size_t len, int read) |
| 598 | { |
| 599 | struct ffs_epfile *epfile = file->private_data; |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 600 | struct usb_gadget *gadget = epfile->ffs->gadget; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 601 | struct ffs_ep *ep; |
| 602 | char *data = NULL; |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 603 | ssize_t ret, data_len; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 604 | int halt; |
| 605 | |
Michal Nazarewicz | 7fa6803 | 2013-12-09 15:55:36 -0800 | [diff] [blame] | 606 | /* Are we still active? */ |
| 607 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) { |
| 608 | ret = -ENODEV; |
| 609 | goto error; |
| 610 | } |
| 611 | |
| 612 | /* Wait for endpoint to be enabled */ |
| 613 | ep = epfile->ep; |
| 614 | if (!ep) { |
| 615 | if (file->f_flags & O_NONBLOCK) { |
| 616 | ret = -EAGAIN; |
| 617 | goto error; |
| 618 | } |
| 619 | |
| 620 | ret = wait_event_interruptible(epfile->wait, (ep = epfile->ep)); |
| 621 | if (ret) { |
| 622 | ret = -EINTR; |
| 623 | goto error; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* Do we halt? */ |
| 628 | halt = !read == !epfile->in; |
| 629 | if (halt && epfile->isoc) { |
| 630 | ret = -EINVAL; |
| 631 | goto error; |
| 632 | } |
| 633 | |
| 634 | /* Allocate & copy */ |
| 635 | if (!halt) { |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 636 | /* |
| 637 | * Controller may require buffer size to be aligned to |
| 638 | * maxpacketsize of an out endpoint. |
| 639 | */ |
| 640 | data_len = read ? usb_ep_align_maybe(gadget, ep->ep, len) : len; |
| 641 | |
| 642 | data = kmalloc(data_len, GFP_KERNEL); |
Michal Nazarewicz | 7fa6803 | 2013-12-09 15:55:36 -0800 | [diff] [blame] | 643 | if (unlikely(!data)) |
| 644 | return -ENOMEM; |
| 645 | |
| 646 | if (!read && unlikely(copy_from_user(data, buf, len))) { |
| 647 | ret = -EFAULT; |
| 648 | goto error; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* We will be using request */ |
| 653 | ret = ffs_mutex_lock(&epfile->mutex, file->f_flags & O_NONBLOCK); |
| 654 | if (unlikely(ret)) |
| 655 | goto error; |
| 656 | |
| 657 | spin_lock_irq(&epfile->ffs->eps_lock); |
| 658 | |
| 659 | if (epfile->ep != ep) { |
| 660 | /* In the meantime, endpoint got disabled or changed. */ |
| 661 | ret = -ESHUTDOWN; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 662 | spin_unlock_irq(&epfile->ffs->eps_lock); |
Michal Nazarewicz | 7fa6803 | 2013-12-09 15:55:36 -0800 | [diff] [blame] | 663 | } else if (halt) { |
| 664 | /* Halt */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 665 | if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep)) |
| 666 | usb_ep_set_halt(ep->ep); |
| 667 | spin_unlock_irq(&epfile->ffs->eps_lock); |
| 668 | ret = -EBADMSG; |
| 669 | } else { |
| 670 | /* Fire the request */ |
| 671 | DECLARE_COMPLETION_ONSTACK(done); |
| 672 | |
| 673 | struct usb_request *req = ep->req; |
| 674 | req->context = &done; |
| 675 | req->complete = ffs_epfile_io_complete; |
| 676 | req->buf = data; |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 677 | req->length = data_len; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 678 | |
| 679 | ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC); |
| 680 | |
| 681 | spin_unlock_irq(&epfile->ffs->eps_lock); |
| 682 | |
| 683 | if (unlikely(ret < 0)) { |
| 684 | /* nop */ |
| 685 | } else if (unlikely(wait_for_completion_interruptible(&done))) { |
| 686 | ret = -EINTR; |
| 687 | usb_ep_dequeue(ep->ep, req); |
| 688 | } else { |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 689 | /* |
| 690 | * XXX We may end up silently droping data here. |
| 691 | * Since data_len (i.e. req->length) may be bigger |
| 692 | * than len (after being rounded up to maxpacketsize), |
| 693 | * we may end up with more data then user space has |
| 694 | * space for. |
| 695 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 696 | ret = ep->status; |
| 697 | if (read && ret > 0 && |
Michal Nazarewicz | 219580e | 2013-12-09 15:55:37 -0800 | [diff] [blame] | 698 | unlikely(copy_to_user(buf, data, |
| 699 | min_t(size_t, ret, len)))) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 700 | ret = -EFAULT; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | mutex_unlock(&epfile->mutex); |
| 705 | error: |
| 706 | kfree(data); |
| 707 | return ret; |
| 708 | } |
| 709 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 710 | static ssize_t |
| 711 | ffs_epfile_write(struct file *file, const char __user *buf, size_t len, |
| 712 | loff_t *ptr) |
| 713 | { |
| 714 | ENTER(); |
| 715 | |
| 716 | return ffs_epfile_io(file, (char __user *)buf, len, 0); |
| 717 | } |
| 718 | |
| 719 | static ssize_t |
| 720 | ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr) |
| 721 | { |
| 722 | ENTER(); |
| 723 | |
| 724 | return ffs_epfile_io(file, buf, len, 1); |
| 725 | } |
| 726 | |
| 727 | static int |
| 728 | ffs_epfile_open(struct inode *inode, struct file *file) |
| 729 | { |
| 730 | struct ffs_epfile *epfile = inode->i_private; |
| 731 | |
| 732 | ENTER(); |
| 733 | |
| 734 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) |
| 735 | return -ENODEV; |
| 736 | |
| 737 | file->private_data = epfile; |
| 738 | ffs_data_opened(epfile->ffs); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static int |
| 744 | ffs_epfile_release(struct inode *inode, struct file *file) |
| 745 | { |
| 746 | struct ffs_epfile *epfile = inode->i_private; |
| 747 | |
| 748 | ENTER(); |
| 749 | |
| 750 | ffs_data_closed(epfile->ffs); |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 755 | static long ffs_epfile_ioctl(struct file *file, unsigned code, |
| 756 | unsigned long value) |
| 757 | { |
| 758 | struct ffs_epfile *epfile = file->private_data; |
| 759 | int ret; |
| 760 | |
| 761 | ENTER(); |
| 762 | |
| 763 | if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) |
| 764 | return -ENODEV; |
| 765 | |
| 766 | spin_lock_irq(&epfile->ffs->eps_lock); |
| 767 | if (likely(epfile->ep)) { |
| 768 | switch (code) { |
| 769 | case FUNCTIONFS_FIFO_STATUS: |
| 770 | ret = usb_ep_fifo_status(epfile->ep->ep); |
| 771 | break; |
| 772 | case FUNCTIONFS_FIFO_FLUSH: |
| 773 | usb_ep_fifo_flush(epfile->ep->ep); |
| 774 | ret = 0; |
| 775 | break; |
| 776 | case FUNCTIONFS_CLEAR_HALT: |
| 777 | ret = usb_ep_clear_halt(epfile->ep->ep); |
| 778 | break; |
| 779 | case FUNCTIONFS_ENDPOINT_REVMAP: |
| 780 | ret = epfile->ep->num; |
| 781 | break; |
| 782 | default: |
| 783 | ret = -ENOTTY; |
| 784 | } |
| 785 | } else { |
| 786 | ret = -ENODEV; |
| 787 | } |
| 788 | spin_unlock_irq(&epfile->ffs->eps_lock); |
| 789 | |
| 790 | return ret; |
| 791 | } |
| 792 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 793 | static const struct file_operations ffs_epfile_operations = { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 794 | .llseek = no_llseek, |
| 795 | |
| 796 | .open = ffs_epfile_open, |
| 797 | .write = ffs_epfile_write, |
| 798 | .read = ffs_epfile_read, |
| 799 | .release = ffs_epfile_release, |
| 800 | .unlocked_ioctl = ffs_epfile_ioctl, |
| 801 | }; |
| 802 | |
| 803 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 804 | /* File system and super block operations ***********************************/ |
| 805 | |
| 806 | /* |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 807 | * Mounting the file system creates a controller file, used first for |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 808 | * function configuration then later for event monitoring. |
| 809 | */ |
| 810 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 811 | static struct inode *__must_check |
| 812 | ffs_sb_make_inode(struct super_block *sb, void *data, |
| 813 | const struct file_operations *fops, |
| 814 | const struct inode_operations *iops, |
| 815 | struct ffs_file_perms *perms) |
| 816 | { |
| 817 | struct inode *inode; |
| 818 | |
| 819 | ENTER(); |
| 820 | |
| 821 | inode = new_inode(sb); |
| 822 | |
| 823 | if (likely(inode)) { |
| 824 | struct timespec current_time = CURRENT_TIME; |
| 825 | |
Al Viro | 12ba8d1 | 2010-10-27 04:19:36 +0100 | [diff] [blame] | 826 | inode->i_ino = get_next_ino(); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 827 | inode->i_mode = perms->mode; |
| 828 | inode->i_uid = perms->uid; |
| 829 | inode->i_gid = perms->gid; |
| 830 | inode->i_atime = current_time; |
| 831 | inode->i_mtime = current_time; |
| 832 | inode->i_ctime = current_time; |
| 833 | inode->i_private = data; |
| 834 | if (fops) |
| 835 | inode->i_fop = fops; |
| 836 | if (iops) |
| 837 | inode->i_op = iops; |
| 838 | } |
| 839 | |
| 840 | return inode; |
| 841 | } |
| 842 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 843 | /* Create "regular" file */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 844 | static struct inode *ffs_sb_create_file(struct super_block *sb, |
| 845 | const char *name, void *data, |
| 846 | const struct file_operations *fops, |
| 847 | struct dentry **dentry_p) |
| 848 | { |
| 849 | struct ffs_data *ffs = sb->s_fs_info; |
| 850 | struct dentry *dentry; |
| 851 | struct inode *inode; |
| 852 | |
| 853 | ENTER(); |
| 854 | |
| 855 | dentry = d_alloc_name(sb->s_root, name); |
| 856 | if (unlikely(!dentry)) |
| 857 | return NULL; |
| 858 | |
| 859 | inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms); |
| 860 | if (unlikely(!inode)) { |
| 861 | dput(dentry); |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | d_add(dentry, inode); |
| 866 | if (dentry_p) |
| 867 | *dentry_p = dentry; |
| 868 | |
| 869 | return inode; |
| 870 | } |
| 871 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 872 | /* Super block */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 873 | static const struct super_operations ffs_sb_operations = { |
| 874 | .statfs = simple_statfs, |
| 875 | .drop_inode = generic_delete_inode, |
| 876 | }; |
| 877 | |
| 878 | struct ffs_sb_fill_data { |
| 879 | struct ffs_file_perms perms; |
| 880 | umode_t root_mode; |
| 881 | const char *dev_name; |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 882 | struct ffs_data *ffs_data; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 883 | }; |
| 884 | |
| 885 | static int ffs_sb_fill(struct super_block *sb, void *_data, int silent) |
| 886 | { |
| 887 | struct ffs_sb_fill_data *data = _data; |
| 888 | struct inode *inode; |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 889 | struct ffs_data *ffs = data->ffs_data; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 890 | |
| 891 | ENTER(); |
| 892 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 893 | ffs->sb = sb; |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 894 | data->ffs_data = NULL; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 895 | sb->s_fs_info = ffs; |
| 896 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 897 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 898 | sb->s_magic = FUNCTIONFS_MAGIC; |
| 899 | sb->s_op = &ffs_sb_operations; |
| 900 | sb->s_time_gran = 1; |
| 901 | |
| 902 | /* Root inode */ |
| 903 | data->perms.mode = data->root_mode; |
| 904 | inode = ffs_sb_make_inode(sb, NULL, |
| 905 | &simple_dir_operations, |
| 906 | &simple_dir_inode_operations, |
| 907 | &data->perms); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 908 | sb->s_root = d_make_root(inode); |
| 909 | if (unlikely(!sb->s_root)) |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 910 | return -ENOMEM; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 911 | |
| 912 | /* EP0 file */ |
| 913 | if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs, |
| 914 | &ffs_ep0_operations, NULL))) |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 915 | return -ENOMEM; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 916 | |
| 917 | return 0; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 920 | static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts) |
| 921 | { |
| 922 | ENTER(); |
| 923 | |
| 924 | if (!opts || !*opts) |
| 925 | return 0; |
| 926 | |
| 927 | for (;;) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 928 | unsigned long value; |
Michal Nazarewicz | afd2e18 | 2013-01-09 10:17:47 +0100 | [diff] [blame] | 929 | char *eq, *comma; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 930 | |
| 931 | /* Option limit */ |
| 932 | comma = strchr(opts, ','); |
| 933 | if (comma) |
| 934 | *comma = 0; |
| 935 | |
| 936 | /* Value limit */ |
| 937 | eq = strchr(opts, '='); |
| 938 | if (unlikely(!eq)) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 939 | pr_err("'=' missing in %s\n", opts); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 940 | return -EINVAL; |
| 941 | } |
| 942 | *eq = 0; |
| 943 | |
| 944 | /* Parse value */ |
Michal Nazarewicz | afd2e18 | 2013-01-09 10:17:47 +0100 | [diff] [blame] | 945 | if (kstrtoul(eq + 1, 0, &value)) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 946 | pr_err("%s: invalid value: %s\n", opts, eq + 1); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | /* Interpret option */ |
| 951 | switch (eq - opts) { |
| 952 | case 5: |
| 953 | if (!memcmp(opts, "rmode", 5)) |
| 954 | data->root_mode = (value & 0555) | S_IFDIR; |
| 955 | else if (!memcmp(opts, "fmode", 5)) |
| 956 | data->perms.mode = (value & 0666) | S_IFREG; |
| 957 | else |
| 958 | goto invalid; |
| 959 | break; |
| 960 | |
| 961 | case 4: |
| 962 | if (!memcmp(opts, "mode", 4)) { |
| 963 | data->root_mode = (value & 0555) | S_IFDIR; |
| 964 | data->perms.mode = (value & 0666) | S_IFREG; |
| 965 | } else { |
| 966 | goto invalid; |
| 967 | } |
| 968 | break; |
| 969 | |
| 970 | case 3: |
Eric W. Biederman | b9b73f7 | 2012-06-14 01:19:23 -0700 | [diff] [blame] | 971 | if (!memcmp(opts, "uid", 3)) { |
| 972 | data->perms.uid = make_kuid(current_user_ns(), value); |
| 973 | if (!uid_valid(data->perms.uid)) { |
| 974 | pr_err("%s: unmapped value: %lu\n", opts, value); |
| 975 | return -EINVAL; |
| 976 | } |
Benoit Goby | b810075 | 2013-01-08 19:57:09 -0800 | [diff] [blame] | 977 | } else if (!memcmp(opts, "gid", 3)) { |
Eric W. Biederman | b9b73f7 | 2012-06-14 01:19:23 -0700 | [diff] [blame] | 978 | data->perms.gid = make_kgid(current_user_ns(), value); |
| 979 | if (!gid_valid(data->perms.gid)) { |
| 980 | pr_err("%s: unmapped value: %lu\n", opts, value); |
| 981 | return -EINVAL; |
| 982 | } |
Benoit Goby | b810075 | 2013-01-08 19:57:09 -0800 | [diff] [blame] | 983 | } else { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 984 | goto invalid; |
Benoit Goby | b810075 | 2013-01-08 19:57:09 -0800 | [diff] [blame] | 985 | } |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 986 | break; |
| 987 | |
| 988 | default: |
| 989 | invalid: |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 990 | pr_err("%s: invalid option\n", opts); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 991 | return -EINVAL; |
| 992 | } |
| 993 | |
| 994 | /* Next iteration */ |
| 995 | if (!comma) |
| 996 | break; |
| 997 | opts = comma + 1; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1003 | /* "mount -t functionfs dev_name /dev/function" ends up here */ |
| 1004 | |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1005 | static struct dentry * |
| 1006 | ffs_fs_mount(struct file_system_type *t, int flags, |
| 1007 | const char *dev_name, void *opts) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct ffs_sb_fill_data data = { |
| 1010 | .perms = { |
| 1011 | .mode = S_IFREG | 0600, |
Eric W. Biederman | b9b73f7 | 2012-06-14 01:19:23 -0700 | [diff] [blame] | 1012 | .uid = GLOBAL_ROOT_UID, |
| 1013 | .gid = GLOBAL_ROOT_GID, |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1014 | }, |
| 1015 | .root_mode = S_IFDIR | 0500, |
| 1016 | }; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1017 | struct dentry *rv; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1018 | int ret; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1019 | void *ffs_dev; |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 1020 | struct ffs_data *ffs; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1021 | |
| 1022 | ENTER(); |
| 1023 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1024 | ret = ffs_fs_parse_opts(&data, opts); |
| 1025 | if (unlikely(ret < 0)) |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1026 | return ERR_PTR(ret); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1027 | |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 1028 | ffs = ffs_data_new(); |
| 1029 | if (unlikely(!ffs)) |
| 1030 | return ERR_PTR(-ENOMEM); |
| 1031 | ffs->file_perms = data.perms; |
| 1032 | |
| 1033 | ffs->dev_name = kstrdup(dev_name, GFP_KERNEL); |
| 1034 | if (unlikely(!ffs->dev_name)) { |
| 1035 | ffs_data_put(ffs); |
| 1036 | return ERR_PTR(-ENOMEM); |
| 1037 | } |
| 1038 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 1039 | ffs_dev = ffs_acquire_dev(dev_name); |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 1040 | if (IS_ERR(ffs_dev)) { |
| 1041 | ffs_data_put(ffs); |
| 1042 | return ERR_CAST(ffs_dev); |
| 1043 | } |
| 1044 | ffs->private_data = ffs_dev; |
| 1045 | data.ffs_data = ffs; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1046 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1047 | rv = mount_nodev(t, flags, &data, ffs_sb_fill); |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 1048 | if (IS_ERR(rv) && data.ffs_data) { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 1049 | ffs_release_dev(data.ffs_data); |
Al Viro | 2606b28 | 2013-09-20 17:14:21 +0100 | [diff] [blame] | 1050 | ffs_data_put(data.ffs_data); |
| 1051 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1052 | return rv; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | ffs_fs_kill_sb(struct super_block *sb) |
| 1057 | { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1058 | ENTER(); |
| 1059 | |
| 1060 | kill_litter_super(sb); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1061 | if (sb->s_fs_info) { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 1062 | ffs_release_dev(sb->s_fs_info); |
Al Viro | 5b5f956 | 2012-01-08 15:38:27 -0500 | [diff] [blame] | 1063 | ffs_data_put(sb->s_fs_info); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1064 | } |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static struct file_system_type ffs_fs_type = { |
| 1068 | .owner = THIS_MODULE, |
| 1069 | .name = "functionfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1070 | .mount = ffs_fs_mount, |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1071 | .kill_sb = ffs_fs_kill_sb, |
| 1072 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 1073 | MODULE_ALIAS_FS("functionfs"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1074 | |
| 1075 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1076 | /* Driver's main init/cleanup functions *************************************/ |
| 1077 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1078 | static int functionfs_init(void) |
| 1079 | { |
| 1080 | int ret; |
| 1081 | |
| 1082 | ENTER(); |
| 1083 | |
| 1084 | ret = register_filesystem(&ffs_fs_type); |
| 1085 | if (likely(!ret)) |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1086 | pr_info("file system registered\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1087 | else |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1088 | pr_err("failed registering file system (%d)\n", ret); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1089 | |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | static void functionfs_cleanup(void) |
| 1094 | { |
| 1095 | ENTER(); |
| 1096 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1097 | pr_info("unloading\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1098 | unregister_filesystem(&ffs_fs_type); |
| 1099 | } |
| 1100 | |
| 1101 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1102 | /* ffs_data and ffs_function construction and destruction code **************/ |
| 1103 | |
| 1104 | static void ffs_data_clear(struct ffs_data *ffs); |
| 1105 | static void ffs_data_reset(struct ffs_data *ffs); |
| 1106 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1107 | static void ffs_data_get(struct ffs_data *ffs) |
| 1108 | { |
| 1109 | ENTER(); |
| 1110 | |
| 1111 | atomic_inc(&ffs->ref); |
| 1112 | } |
| 1113 | |
| 1114 | static void ffs_data_opened(struct ffs_data *ffs) |
| 1115 | { |
| 1116 | ENTER(); |
| 1117 | |
| 1118 | atomic_inc(&ffs->ref); |
| 1119 | atomic_inc(&ffs->opened); |
| 1120 | } |
| 1121 | |
| 1122 | static void ffs_data_put(struct ffs_data *ffs) |
| 1123 | { |
| 1124 | ENTER(); |
| 1125 | |
| 1126 | if (unlikely(atomic_dec_and_test(&ffs->ref))) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1127 | pr_info("%s(): freeing\n", __func__); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1128 | ffs_data_clear(ffs); |
Andi Kleen | 647d558 | 2012-03-16 12:01:02 -0700 | [diff] [blame] | 1129 | BUG_ON(waitqueue_active(&ffs->ev.waitq) || |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1130 | waitqueue_active(&ffs->ep0req_completion.wait)); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 1131 | kfree(ffs->dev_name); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1132 | kfree(ffs); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1136 | static void ffs_data_closed(struct ffs_data *ffs) |
| 1137 | { |
| 1138 | ENTER(); |
| 1139 | |
| 1140 | if (atomic_dec_and_test(&ffs->opened)) { |
| 1141 | ffs->state = FFS_CLOSING; |
| 1142 | ffs_data_reset(ffs); |
| 1143 | } |
| 1144 | |
| 1145 | ffs_data_put(ffs); |
| 1146 | } |
| 1147 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1148 | static struct ffs_data *ffs_data_new(void) |
| 1149 | { |
| 1150 | struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL); |
| 1151 | if (unlikely(!ffs)) |
Felipe Balbi | f8800d4 | 2013-12-12 12:15:43 -0600 | [diff] [blame] | 1152 | return NULL; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1153 | |
| 1154 | ENTER(); |
| 1155 | |
| 1156 | atomic_set(&ffs->ref, 1); |
| 1157 | atomic_set(&ffs->opened, 0); |
| 1158 | ffs->state = FFS_READ_DESCRIPTORS; |
| 1159 | mutex_init(&ffs->mutex); |
| 1160 | spin_lock_init(&ffs->eps_lock); |
| 1161 | init_waitqueue_head(&ffs->ev.waitq); |
| 1162 | init_completion(&ffs->ep0req_completion); |
| 1163 | |
| 1164 | /* XXX REVISIT need to update it in some places, or do we? */ |
| 1165 | ffs->ev.can_stall = 1; |
| 1166 | |
| 1167 | return ffs; |
| 1168 | } |
| 1169 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1170 | static void ffs_data_clear(struct ffs_data *ffs) |
| 1171 | { |
| 1172 | ENTER(); |
| 1173 | |
| 1174 | if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags)) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 1175 | ffs_closed(ffs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1176 | |
| 1177 | BUG_ON(ffs->gadget); |
| 1178 | |
| 1179 | if (ffs->epfiles) |
| 1180 | ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count); |
| 1181 | |
| 1182 | kfree(ffs->raw_descs); |
| 1183 | kfree(ffs->raw_strings); |
| 1184 | kfree(ffs->stringtabs); |
| 1185 | } |
| 1186 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1187 | static void ffs_data_reset(struct ffs_data *ffs) |
| 1188 | { |
| 1189 | ENTER(); |
| 1190 | |
| 1191 | ffs_data_clear(ffs); |
| 1192 | |
| 1193 | ffs->epfiles = NULL; |
| 1194 | ffs->raw_descs = NULL; |
| 1195 | ffs->raw_strings = NULL; |
| 1196 | ffs->stringtabs = NULL; |
| 1197 | |
| 1198 | ffs->raw_descs_length = 0; |
| 1199 | ffs->raw_fs_descs_length = 0; |
| 1200 | ffs->fs_descs_count = 0; |
| 1201 | ffs->hs_descs_count = 0; |
| 1202 | |
| 1203 | ffs->strings_count = 0; |
| 1204 | ffs->interfaces_count = 0; |
| 1205 | ffs->eps_count = 0; |
| 1206 | |
| 1207 | ffs->ev.count = 0; |
| 1208 | |
| 1209 | ffs->state = FFS_READ_DESCRIPTORS; |
| 1210 | ffs->setup_state = FFS_NO_SETUP; |
| 1211 | ffs->flags = 0; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev) |
| 1216 | { |
Michal Nazarewicz | fd7c9a0 | 2010-06-16 12:08:00 +0200 | [diff] [blame] | 1217 | struct usb_gadget_strings **lang; |
| 1218 | int first_id; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1219 | |
| 1220 | ENTER(); |
| 1221 | |
| 1222 | if (WARN_ON(ffs->state != FFS_ACTIVE |
| 1223 | || test_and_set_bit(FFS_FL_BOUND, &ffs->flags))) |
| 1224 | return -EBADFD; |
| 1225 | |
Michal Nazarewicz | fd7c9a0 | 2010-06-16 12:08:00 +0200 | [diff] [blame] | 1226 | first_id = usb_string_ids_n(cdev, ffs->strings_count); |
| 1227 | if (unlikely(first_id < 0)) |
| 1228 | return first_id; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1229 | |
| 1230 | ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL); |
| 1231 | if (unlikely(!ffs->ep0req)) |
| 1232 | return -ENOMEM; |
| 1233 | ffs->ep0req->complete = ffs_ep0_complete; |
| 1234 | ffs->ep0req->context = ffs; |
| 1235 | |
Michal Nazarewicz | fd7c9a0 | 2010-06-16 12:08:00 +0200 | [diff] [blame] | 1236 | lang = ffs->stringtabs; |
| 1237 | for (lang = ffs->stringtabs; *lang; ++lang) { |
| 1238 | struct usb_string *str = (*lang)->strings; |
| 1239 | int id = first_id; |
| 1240 | for (; str->s; ++id, ++str) |
| 1241 | str->id = id; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | ffs->gadget = cdev->gadget; |
Michal Nazarewicz | fd7c9a0 | 2010-06-16 12:08:00 +0200 | [diff] [blame] | 1245 | ffs_data_get(ffs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1249 | static void functionfs_unbind(struct ffs_data *ffs) |
| 1250 | { |
| 1251 | ENTER(); |
| 1252 | |
| 1253 | if (!WARN_ON(!ffs->gadget)) { |
| 1254 | usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req); |
| 1255 | ffs->ep0req = NULL; |
| 1256 | ffs->gadget = NULL; |
Andrzej Pietrasiewicz | e2190a9 | 2012-03-12 12:55:41 +0100 | [diff] [blame] | 1257 | clear_bit(FFS_FL_BOUND, &ffs->flags); |
Dan Carpenter | df49899 | 2013-08-23 11:16:15 +0300 | [diff] [blame] | 1258 | ffs_data_put(ffs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1262 | static int ffs_epfiles_create(struct ffs_data *ffs) |
| 1263 | { |
| 1264 | struct ffs_epfile *epfile, *epfiles; |
| 1265 | unsigned i, count; |
| 1266 | |
| 1267 | ENTER(); |
| 1268 | |
| 1269 | count = ffs->eps_count; |
Thomas Meyer | 9823a52 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 1270 | epfiles = kcalloc(count, sizeof(*epfiles), GFP_KERNEL); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1271 | if (!epfiles) |
| 1272 | return -ENOMEM; |
| 1273 | |
| 1274 | epfile = epfiles; |
| 1275 | for (i = 1; i <= count; ++i, ++epfile) { |
| 1276 | epfile->ffs = ffs; |
| 1277 | mutex_init(&epfile->mutex); |
| 1278 | init_waitqueue_head(&epfile->wait); |
| 1279 | sprintf(epfiles->name, "ep%u", i); |
| 1280 | if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile, |
| 1281 | &ffs_epfile_operations, |
| 1282 | &epfile->dentry))) { |
| 1283 | ffs_epfiles_destroy(epfiles, i - 1); |
| 1284 | return -ENOMEM; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | ffs->epfiles = epfiles; |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1292 | static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count) |
| 1293 | { |
| 1294 | struct ffs_epfile *epfile = epfiles; |
| 1295 | |
| 1296 | ENTER(); |
| 1297 | |
| 1298 | for (; count; --count, ++epfile) { |
| 1299 | BUG_ON(mutex_is_locked(&epfile->mutex) || |
| 1300 | waitqueue_active(&epfile->wait)); |
| 1301 | if (epfile->dentry) { |
| 1302 | d_delete(epfile->dentry); |
| 1303 | dput(epfile->dentry); |
| 1304 | epfile->dentry = NULL; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | kfree(epfiles); |
| 1309 | } |
| 1310 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 1311 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1312 | static void ffs_func_eps_disable(struct ffs_function *func) |
| 1313 | { |
| 1314 | struct ffs_ep *ep = func->eps; |
| 1315 | struct ffs_epfile *epfile = func->ffs->epfiles; |
| 1316 | unsigned count = func->ffs->eps_count; |
| 1317 | unsigned long flags; |
| 1318 | |
| 1319 | spin_lock_irqsave(&func->ffs->eps_lock, flags); |
| 1320 | do { |
| 1321 | /* pending requests get nuked */ |
| 1322 | if (likely(ep->ep)) |
| 1323 | usb_ep_disable(ep->ep); |
| 1324 | epfile->ep = NULL; |
| 1325 | |
| 1326 | ++ep; |
| 1327 | ++epfile; |
| 1328 | } while (--count); |
| 1329 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); |
| 1330 | } |
| 1331 | |
| 1332 | static int ffs_func_eps_enable(struct ffs_function *func) |
| 1333 | { |
| 1334 | struct ffs_data *ffs = func->ffs; |
| 1335 | struct ffs_ep *ep = func->eps; |
| 1336 | struct ffs_epfile *epfile = ffs->epfiles; |
| 1337 | unsigned count = ffs->eps_count; |
| 1338 | unsigned long flags; |
| 1339 | int ret = 0; |
| 1340 | |
| 1341 | spin_lock_irqsave(&func->ffs->eps_lock, flags); |
| 1342 | do { |
| 1343 | struct usb_endpoint_descriptor *ds; |
| 1344 | ds = ep->descs[ep->descs[1] ? 1 : 0]; |
| 1345 | |
| 1346 | ep->ep->driver_data = ep; |
Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 1347 | ep->ep->desc = ds; |
| 1348 | ret = usb_ep_enable(ep->ep); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1349 | if (likely(!ret)) { |
| 1350 | epfile->ep = ep; |
| 1351 | epfile->in = usb_endpoint_dir_in(ds); |
| 1352 | epfile->isoc = usb_endpoint_xfer_isoc(ds); |
| 1353 | } else { |
| 1354 | break; |
| 1355 | } |
| 1356 | |
| 1357 | wake_up(&epfile->wait); |
| 1358 | |
| 1359 | ++ep; |
| 1360 | ++epfile; |
| 1361 | } while (--count); |
| 1362 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); |
| 1363 | |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | /* Parsing and building descriptors and strings *****************************/ |
| 1369 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1370 | /* |
| 1371 | * This validates if data pointed by data is a valid USB descriptor as |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1372 | * well as record how many interfaces, endpoints and strings are |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1373 | * required by given configuration. Returns address after the |
| 1374 | * descriptor or NULL if data is invalid. |
| 1375 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1376 | |
| 1377 | enum ffs_entity_type { |
| 1378 | FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT |
| 1379 | }; |
| 1380 | |
| 1381 | typedef int (*ffs_entity_callback)(enum ffs_entity_type entity, |
| 1382 | u8 *valuep, |
| 1383 | struct usb_descriptor_header *desc, |
| 1384 | void *priv); |
| 1385 | |
| 1386 | static int __must_check ffs_do_desc(char *data, unsigned len, |
| 1387 | ffs_entity_callback entity, void *priv) |
| 1388 | { |
| 1389 | struct usb_descriptor_header *_ds = (void *)data; |
| 1390 | u8 length; |
| 1391 | int ret; |
| 1392 | |
| 1393 | ENTER(); |
| 1394 | |
| 1395 | /* At least two bytes are required: length and type */ |
| 1396 | if (len < 2) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1397 | pr_vdebug("descriptor too short\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1398 | return -EINVAL; |
| 1399 | } |
| 1400 | |
| 1401 | /* If we have at least as many bytes as the descriptor takes? */ |
| 1402 | length = _ds->bLength; |
| 1403 | if (len < length) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1404 | pr_vdebug("descriptor longer then available data\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1405 | return -EINVAL; |
| 1406 | } |
| 1407 | |
| 1408 | #define __entity_check_INTERFACE(val) 1 |
| 1409 | #define __entity_check_STRING(val) (val) |
| 1410 | #define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK) |
| 1411 | #define __entity(type, val) do { \ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1412 | pr_vdebug("entity " #type "(%02x)\n", (val)); \ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1413 | if (unlikely(!__entity_check_ ##type(val))) { \ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1414 | pr_vdebug("invalid entity's value\n"); \ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1415 | return -EINVAL; \ |
| 1416 | } \ |
| 1417 | ret = entity(FFS_ ##type, &val, _ds, priv); \ |
| 1418 | if (unlikely(ret < 0)) { \ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1419 | pr_debug("entity " #type "(%02x); ret = %d\n", \ |
Michal Nazarewicz | d8df0b6 | 2010-11-12 14:29:29 +0100 | [diff] [blame] | 1420 | (val), ret); \ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1421 | return ret; \ |
| 1422 | } \ |
| 1423 | } while (0) |
| 1424 | |
| 1425 | /* Parse descriptor depending on type. */ |
| 1426 | switch (_ds->bDescriptorType) { |
| 1427 | case USB_DT_DEVICE: |
| 1428 | case USB_DT_CONFIG: |
| 1429 | case USB_DT_STRING: |
| 1430 | case USB_DT_DEVICE_QUALIFIER: |
| 1431 | /* function can't have any of those */ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1432 | pr_vdebug("descriptor reserved for gadget: %d\n", |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1433 | _ds->bDescriptorType); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1434 | return -EINVAL; |
| 1435 | |
| 1436 | case USB_DT_INTERFACE: { |
| 1437 | struct usb_interface_descriptor *ds = (void *)_ds; |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1438 | pr_vdebug("interface descriptor\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1439 | if (length != sizeof *ds) |
| 1440 | goto inv_length; |
| 1441 | |
| 1442 | __entity(INTERFACE, ds->bInterfaceNumber); |
| 1443 | if (ds->iInterface) |
| 1444 | __entity(STRING, ds->iInterface); |
| 1445 | } |
| 1446 | break; |
| 1447 | |
| 1448 | case USB_DT_ENDPOINT: { |
| 1449 | struct usb_endpoint_descriptor *ds = (void *)_ds; |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1450 | pr_vdebug("endpoint descriptor\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1451 | if (length != USB_DT_ENDPOINT_SIZE && |
| 1452 | length != USB_DT_ENDPOINT_AUDIO_SIZE) |
| 1453 | goto inv_length; |
| 1454 | __entity(ENDPOINT, ds->bEndpointAddress); |
| 1455 | } |
| 1456 | break; |
| 1457 | |
Koen Beel | 560f118 | 2012-05-30 20:43:37 +0200 | [diff] [blame] | 1458 | case HID_DT_HID: |
| 1459 | pr_vdebug("hid descriptor\n"); |
| 1460 | if (length != sizeof(struct hid_descriptor)) |
| 1461 | goto inv_length; |
| 1462 | break; |
| 1463 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1464 | case USB_DT_OTG: |
| 1465 | if (length != sizeof(struct usb_otg_descriptor)) |
| 1466 | goto inv_length; |
| 1467 | break; |
| 1468 | |
| 1469 | case USB_DT_INTERFACE_ASSOCIATION: { |
| 1470 | struct usb_interface_assoc_descriptor *ds = (void *)_ds; |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1471 | pr_vdebug("interface association descriptor\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1472 | if (length != sizeof *ds) |
| 1473 | goto inv_length; |
| 1474 | if (ds->iFunction) |
| 1475 | __entity(STRING, ds->iFunction); |
| 1476 | } |
| 1477 | break; |
| 1478 | |
| 1479 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1480 | case USB_DT_INTERFACE_POWER: |
| 1481 | case USB_DT_DEBUG: |
| 1482 | case USB_DT_SECURITY: |
| 1483 | case USB_DT_CS_RADIO_CONTROL: |
| 1484 | /* TODO */ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1485 | pr_vdebug("unimplemented descriptor: %d\n", _ds->bDescriptorType); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1486 | return -EINVAL; |
| 1487 | |
| 1488 | default: |
| 1489 | /* We should never be here */ |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1490 | pr_vdebug("unknown descriptor: %d\n", _ds->bDescriptorType); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1491 | return -EINVAL; |
| 1492 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1493 | inv_length: |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1494 | pr_vdebug("invalid length: %d (descriptor %d)\n", |
Michal Nazarewicz | d8df0b6 | 2010-11-12 14:29:29 +0100 | [diff] [blame] | 1495 | _ds->bLength, _ds->bDescriptorType); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1496 | return -EINVAL; |
| 1497 | } |
| 1498 | |
| 1499 | #undef __entity |
| 1500 | #undef __entity_check_DESCRIPTOR |
| 1501 | #undef __entity_check_INTERFACE |
| 1502 | #undef __entity_check_STRING |
| 1503 | #undef __entity_check_ENDPOINT |
| 1504 | |
| 1505 | return length; |
| 1506 | } |
| 1507 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1508 | static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len, |
| 1509 | ffs_entity_callback entity, void *priv) |
| 1510 | { |
| 1511 | const unsigned _len = len; |
| 1512 | unsigned long num = 0; |
| 1513 | |
| 1514 | ENTER(); |
| 1515 | |
| 1516 | for (;;) { |
| 1517 | int ret; |
| 1518 | |
| 1519 | if (num == count) |
| 1520 | data = NULL; |
| 1521 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1522 | /* Record "descriptor" entity */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1523 | ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv); |
| 1524 | if (unlikely(ret < 0)) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1525 | pr_debug("entity DESCRIPTOR(%02lx); ret = %d\n", |
Michal Nazarewicz | d8df0b6 | 2010-11-12 14:29:29 +0100 | [diff] [blame] | 1526 | num, ret); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1527 | return ret; |
| 1528 | } |
| 1529 | |
| 1530 | if (!data) |
| 1531 | return _len - len; |
| 1532 | |
| 1533 | ret = ffs_do_desc(data, len, entity, priv); |
| 1534 | if (unlikely(ret < 0)) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1535 | pr_debug("%s returns %d\n", __func__, ret); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1536 | return ret; |
| 1537 | } |
| 1538 | |
| 1539 | len -= ret; |
| 1540 | data += ret; |
| 1541 | ++num; |
| 1542 | } |
| 1543 | } |
| 1544 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1545 | static int __ffs_data_do_entity(enum ffs_entity_type type, |
| 1546 | u8 *valuep, struct usb_descriptor_header *desc, |
| 1547 | void *priv) |
| 1548 | { |
| 1549 | struct ffs_data *ffs = priv; |
| 1550 | |
| 1551 | ENTER(); |
| 1552 | |
| 1553 | switch (type) { |
| 1554 | case FFS_DESCRIPTOR: |
| 1555 | break; |
| 1556 | |
| 1557 | case FFS_INTERFACE: |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1558 | /* |
| 1559 | * Interfaces are indexed from zero so if we |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1560 | * encountered interface "n" then there are at least |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1561 | * "n+1" interfaces. |
| 1562 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1563 | if (*valuep >= ffs->interfaces_count) |
| 1564 | ffs->interfaces_count = *valuep + 1; |
| 1565 | break; |
| 1566 | |
| 1567 | case FFS_STRING: |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1568 | /* |
| 1569 | * Strings are indexed from 1 (0 is magic ;) reserved |
| 1570 | * for languages list or some such) |
| 1571 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1572 | if (*valuep > ffs->strings_count) |
| 1573 | ffs->strings_count = *valuep; |
| 1574 | break; |
| 1575 | |
| 1576 | case FFS_ENDPOINT: |
| 1577 | /* Endpoints are indexed from 1 as well. */ |
| 1578 | if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count) |
| 1579 | ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1586 | static int __ffs_data_got_descs(struct ffs_data *ffs, |
| 1587 | char *const _data, size_t len) |
| 1588 | { |
| 1589 | unsigned fs_count, hs_count; |
| 1590 | int fs_len, ret = -EINVAL; |
| 1591 | char *data = _data; |
| 1592 | |
| 1593 | ENTER(); |
| 1594 | |
| 1595 | if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC || |
| 1596 | get_unaligned_le32(data + 4) != len)) |
| 1597 | goto error; |
| 1598 | fs_count = get_unaligned_le32(data + 8); |
| 1599 | hs_count = get_unaligned_le32(data + 12); |
| 1600 | |
| 1601 | if (!fs_count && !hs_count) |
| 1602 | goto einval; |
| 1603 | |
| 1604 | data += 16; |
| 1605 | len -= 16; |
| 1606 | |
| 1607 | if (likely(fs_count)) { |
| 1608 | fs_len = ffs_do_descs(fs_count, data, len, |
| 1609 | __ffs_data_do_entity, ffs); |
| 1610 | if (unlikely(fs_len < 0)) { |
| 1611 | ret = fs_len; |
| 1612 | goto error; |
| 1613 | } |
| 1614 | |
| 1615 | data += fs_len; |
| 1616 | len -= fs_len; |
| 1617 | } else { |
| 1618 | fs_len = 0; |
| 1619 | } |
| 1620 | |
| 1621 | if (likely(hs_count)) { |
| 1622 | ret = ffs_do_descs(hs_count, data, len, |
| 1623 | __ffs_data_do_entity, ffs); |
| 1624 | if (unlikely(ret < 0)) |
| 1625 | goto error; |
| 1626 | } else { |
| 1627 | ret = 0; |
| 1628 | } |
| 1629 | |
| 1630 | if (unlikely(len != ret)) |
| 1631 | goto einval; |
| 1632 | |
| 1633 | ffs->raw_fs_descs_length = fs_len; |
| 1634 | ffs->raw_descs_length = fs_len + ret; |
| 1635 | ffs->raw_descs = _data; |
| 1636 | ffs->fs_descs_count = fs_count; |
| 1637 | ffs->hs_descs_count = hs_count; |
| 1638 | |
| 1639 | return 0; |
| 1640 | |
| 1641 | einval: |
| 1642 | ret = -EINVAL; |
| 1643 | error: |
| 1644 | kfree(_data); |
| 1645 | return ret; |
| 1646 | } |
| 1647 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1648 | static int __ffs_data_got_strings(struct ffs_data *ffs, |
| 1649 | char *const _data, size_t len) |
| 1650 | { |
| 1651 | u32 str_count, needed_count, lang_count; |
| 1652 | struct usb_gadget_strings **stringtabs, *t; |
| 1653 | struct usb_string *strings, *s; |
| 1654 | const char *data = _data; |
| 1655 | |
| 1656 | ENTER(); |
| 1657 | |
| 1658 | if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC || |
| 1659 | get_unaligned_le32(data + 4) != len)) |
| 1660 | goto error; |
| 1661 | str_count = get_unaligned_le32(data + 8); |
| 1662 | lang_count = get_unaligned_le32(data + 12); |
| 1663 | |
| 1664 | /* if one is zero the other must be zero */ |
| 1665 | if (unlikely(!str_count != !lang_count)) |
| 1666 | goto error; |
| 1667 | |
| 1668 | /* Do we have at least as many strings as descriptors need? */ |
| 1669 | needed_count = ffs->strings_count; |
| 1670 | if (unlikely(str_count < needed_count)) |
| 1671 | goto error; |
| 1672 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1673 | /* |
| 1674 | * If we don't need any strings just return and free all |
| 1675 | * memory. |
| 1676 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1677 | if (!needed_count) { |
| 1678 | kfree(_data); |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1682 | /* Allocate everything in one chunk so there's less maintenance. */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1683 | { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1684 | unsigned i = 0; |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 1685 | vla_group(d); |
| 1686 | vla_item(d, struct usb_gadget_strings *, stringtabs, |
| 1687 | lang_count + 1); |
| 1688 | vla_item(d, struct usb_gadget_strings, stringtab, lang_count); |
| 1689 | vla_item(d, struct usb_string, strings, |
| 1690 | lang_count*(needed_count+1)); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1691 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 1692 | char *vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL); |
| 1693 | |
| 1694 | if (unlikely(!vlabuf)) { |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1695 | kfree(_data); |
| 1696 | return -ENOMEM; |
| 1697 | } |
| 1698 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 1699 | /* Initialize the VLA pointers */ |
| 1700 | stringtabs = vla_ptr(vlabuf, d, stringtabs); |
| 1701 | t = vla_ptr(vlabuf, d, stringtab); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1702 | i = lang_count; |
| 1703 | do { |
| 1704 | *stringtabs++ = t++; |
| 1705 | } while (--i); |
| 1706 | *stringtabs = NULL; |
| 1707 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 1708 | /* stringtabs = vlabuf = d_stringtabs for later kfree */ |
| 1709 | stringtabs = vla_ptr(vlabuf, d, stringtabs); |
| 1710 | t = vla_ptr(vlabuf, d, stringtab); |
| 1711 | s = vla_ptr(vlabuf, d, strings); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1712 | strings = s; |
| 1713 | } |
| 1714 | |
| 1715 | /* For each language */ |
| 1716 | data += 16; |
| 1717 | len -= 16; |
| 1718 | |
| 1719 | do { /* lang_count > 0 so we can use do-while */ |
| 1720 | unsigned needed = needed_count; |
| 1721 | |
| 1722 | if (unlikely(len < 3)) |
| 1723 | goto error_free; |
| 1724 | t->language = get_unaligned_le16(data); |
| 1725 | t->strings = s; |
| 1726 | ++t; |
| 1727 | |
| 1728 | data += 2; |
| 1729 | len -= 2; |
| 1730 | |
| 1731 | /* For each string */ |
| 1732 | do { /* str_count > 0 so we can use do-while */ |
| 1733 | size_t length = strnlen(data, len); |
| 1734 | |
| 1735 | if (unlikely(length == len)) |
| 1736 | goto error_free; |
| 1737 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1738 | /* |
| 1739 | * User may provide more strings then we need, |
| 1740 | * if that's the case we simply ignore the |
| 1741 | * rest |
| 1742 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1743 | if (likely(needed)) { |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1744 | /* |
| 1745 | * s->id will be set while adding |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1746 | * function to configuration so for |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1747 | * now just leave garbage here. |
| 1748 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1749 | s->s = data; |
| 1750 | --needed; |
| 1751 | ++s; |
| 1752 | } |
| 1753 | |
| 1754 | data += length + 1; |
| 1755 | len -= length + 1; |
| 1756 | } while (--str_count); |
| 1757 | |
| 1758 | s->id = 0; /* terminator */ |
| 1759 | s->s = NULL; |
| 1760 | ++s; |
| 1761 | |
| 1762 | } while (--lang_count); |
| 1763 | |
| 1764 | /* Some garbage left? */ |
| 1765 | if (unlikely(len)) |
| 1766 | goto error_free; |
| 1767 | |
| 1768 | /* Done! */ |
| 1769 | ffs->stringtabs = stringtabs; |
| 1770 | ffs->raw_strings = _data; |
| 1771 | |
| 1772 | return 0; |
| 1773 | |
| 1774 | error_free: |
| 1775 | kfree(stringtabs); |
| 1776 | error: |
| 1777 | kfree(_data); |
| 1778 | return -EINVAL; |
| 1779 | } |
| 1780 | |
| 1781 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1782 | /* Events handling and management *******************************************/ |
| 1783 | |
| 1784 | static void __ffs_event_add(struct ffs_data *ffs, |
| 1785 | enum usb_functionfs_event_type type) |
| 1786 | { |
| 1787 | enum usb_functionfs_event_type rem_type1, rem_type2 = type; |
| 1788 | int neg = 0; |
| 1789 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1790 | /* |
| 1791 | * Abort any unhandled setup |
| 1792 | * |
| 1793 | * We do not need to worry about some cmpxchg() changing value |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1794 | * of ffs->setup_state without holding the lock because when |
| 1795 | * state is FFS_SETUP_PENDING cmpxchg() in several places in |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1796 | * the source does nothing. |
| 1797 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1798 | if (ffs->setup_state == FFS_SETUP_PENDING) |
Michal Nazarewicz | e46318a | 2014-02-10 10:42:40 +0100 | [diff] [blame] | 1799 | ffs->setup_state = FFS_SETUP_CANCELLED; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1800 | |
| 1801 | switch (type) { |
| 1802 | case FUNCTIONFS_RESUME: |
| 1803 | rem_type2 = FUNCTIONFS_SUSPEND; |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1804 | /* FALL THROUGH */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1805 | case FUNCTIONFS_SUSPEND: |
| 1806 | case FUNCTIONFS_SETUP: |
| 1807 | rem_type1 = type; |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1808 | /* Discard all similar events */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1809 | break; |
| 1810 | |
| 1811 | case FUNCTIONFS_BIND: |
| 1812 | case FUNCTIONFS_UNBIND: |
| 1813 | case FUNCTIONFS_DISABLE: |
| 1814 | case FUNCTIONFS_ENABLE: |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1815 | /* Discard everything other then power management. */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1816 | rem_type1 = FUNCTIONFS_SUSPEND; |
| 1817 | rem_type2 = FUNCTIONFS_RESUME; |
| 1818 | neg = 1; |
| 1819 | break; |
| 1820 | |
| 1821 | default: |
| 1822 | BUG(); |
| 1823 | } |
| 1824 | |
| 1825 | { |
| 1826 | u8 *ev = ffs->ev.types, *out = ev; |
| 1827 | unsigned n = ffs->ev.count; |
| 1828 | for (; n; --n, ++ev) |
| 1829 | if ((*ev == rem_type1 || *ev == rem_type2) == neg) |
| 1830 | *out++ = *ev; |
| 1831 | else |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1832 | pr_vdebug("purging event %d\n", *ev); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1833 | ffs->ev.count = out - ffs->ev.types; |
| 1834 | } |
| 1835 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1836 | pr_vdebug("adding event %d\n", type); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1837 | ffs->ev.types[ffs->ev.count++] = type; |
| 1838 | wake_up_locked(&ffs->ev.waitq); |
| 1839 | } |
| 1840 | |
| 1841 | static void ffs_event_add(struct ffs_data *ffs, |
| 1842 | enum usb_functionfs_event_type type) |
| 1843 | { |
| 1844 | unsigned long flags; |
| 1845 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); |
| 1846 | __ffs_event_add(ffs, type); |
| 1847 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | /* Bind/unbind USB function hooks *******************************************/ |
| 1852 | |
| 1853 | static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep, |
| 1854 | struct usb_descriptor_header *desc, |
| 1855 | void *priv) |
| 1856 | { |
| 1857 | struct usb_endpoint_descriptor *ds = (void *)desc; |
| 1858 | struct ffs_function *func = priv; |
| 1859 | struct ffs_ep *ffs_ep; |
| 1860 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1861 | /* |
| 1862 | * If hs_descriptors is not NULL then we are reading hs |
| 1863 | * descriptors now |
| 1864 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1865 | const int isHS = func->function.hs_descriptors != NULL; |
| 1866 | unsigned idx; |
| 1867 | |
| 1868 | if (type != FFS_DESCRIPTOR) |
| 1869 | return 0; |
| 1870 | |
| 1871 | if (isHS) |
| 1872 | func->function.hs_descriptors[(long)valuep] = desc; |
| 1873 | else |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 1874 | func->function.fs_descriptors[(long)valuep] = desc; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1875 | |
| 1876 | if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT) |
| 1877 | return 0; |
| 1878 | |
| 1879 | idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1; |
| 1880 | ffs_ep = func->eps + idx; |
| 1881 | |
| 1882 | if (unlikely(ffs_ep->descs[isHS])) { |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1883 | pr_vdebug("two %sspeed descriptors for EP %d\n", |
Michal Nazarewicz | d8df0b6 | 2010-11-12 14:29:29 +0100 | [diff] [blame] | 1884 | isHS ? "high" : "full", |
| 1885 | ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1886 | return -EINVAL; |
| 1887 | } |
| 1888 | ffs_ep->descs[isHS] = ds; |
| 1889 | |
| 1890 | ffs_dump_mem(": Original ep desc", ds, ds->bLength); |
| 1891 | if (ffs_ep->ep) { |
| 1892 | ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress; |
| 1893 | if (!ds->wMaxPacketSize) |
| 1894 | ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize; |
| 1895 | } else { |
| 1896 | struct usb_request *req; |
| 1897 | struct usb_ep *ep; |
| 1898 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1899 | pr_vdebug("autoconfig\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1900 | ep = usb_ep_autoconfig(func->gadget, ds); |
| 1901 | if (unlikely(!ep)) |
| 1902 | return -ENOTSUPP; |
Joe Perches | cc7e6056 | 2010-11-14 19:04:49 -0800 | [diff] [blame] | 1903 | ep->driver_data = func->eps + idx; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1904 | |
| 1905 | req = usb_ep_alloc_request(ep, GFP_KERNEL); |
| 1906 | if (unlikely(!req)) |
| 1907 | return -ENOMEM; |
| 1908 | |
| 1909 | ffs_ep->ep = ep; |
| 1910 | ffs_ep->req = req; |
| 1911 | func->eps_revmap[ds->bEndpointAddress & |
| 1912 | USB_ENDPOINT_NUMBER_MASK] = idx + 1; |
| 1913 | } |
| 1914 | ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength); |
| 1915 | |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1919 | static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep, |
| 1920 | struct usb_descriptor_header *desc, |
| 1921 | void *priv) |
| 1922 | { |
| 1923 | struct ffs_function *func = priv; |
| 1924 | unsigned idx; |
| 1925 | u8 newValue; |
| 1926 | |
| 1927 | switch (type) { |
| 1928 | default: |
| 1929 | case FFS_DESCRIPTOR: |
| 1930 | /* Handled in previous pass by __ffs_func_bind_do_descs() */ |
| 1931 | return 0; |
| 1932 | |
| 1933 | case FFS_INTERFACE: |
| 1934 | idx = *valuep; |
| 1935 | if (func->interfaces_nums[idx] < 0) { |
| 1936 | int id = usb_interface_id(func->conf, &func->function); |
| 1937 | if (unlikely(id < 0)) |
| 1938 | return id; |
| 1939 | func->interfaces_nums[idx] = id; |
| 1940 | } |
| 1941 | newValue = func->interfaces_nums[idx]; |
| 1942 | break; |
| 1943 | |
| 1944 | case FFS_STRING: |
| 1945 | /* String' IDs are allocated when fsf_data is bound to cdev */ |
| 1946 | newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id; |
| 1947 | break; |
| 1948 | |
| 1949 | case FFS_ENDPOINT: |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1950 | /* |
| 1951 | * USB_DT_ENDPOINT are handled in |
| 1952 | * __ffs_func_bind_do_descs(). |
| 1953 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1954 | if (desc->bDescriptorType == USB_DT_ENDPOINT) |
| 1955 | return 0; |
| 1956 | |
| 1957 | idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1; |
| 1958 | if (unlikely(!func->eps[idx].ep)) |
| 1959 | return -EINVAL; |
| 1960 | |
| 1961 | { |
| 1962 | struct usb_endpoint_descriptor **descs; |
| 1963 | descs = func->eps[idx].descs; |
| 1964 | newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress; |
| 1965 | } |
| 1966 | break; |
| 1967 | } |
| 1968 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 1969 | pr_vdebug("%02x -> %02x\n", *valuep, newValue); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 1970 | *valuep = newValue; |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 1974 | static inline struct f_fs_opts *ffs_do_functionfs_bind(struct usb_function *f, |
| 1975 | struct usb_configuration *c) |
| 1976 | { |
| 1977 | struct ffs_function *func = ffs_func_from_usb(f); |
| 1978 | struct f_fs_opts *ffs_opts = |
| 1979 | container_of(f->fi, struct f_fs_opts, func_inst); |
| 1980 | int ret; |
| 1981 | |
| 1982 | ENTER(); |
| 1983 | |
| 1984 | /* |
| 1985 | * Legacy gadget triggers binding in functionfs_ready_callback, |
| 1986 | * which already uses locking; taking the same lock here would |
| 1987 | * cause a deadlock. |
| 1988 | * |
| 1989 | * Configfs-enabled gadgets however do need ffs_dev_lock. |
| 1990 | */ |
| 1991 | if (!ffs_opts->no_configfs) |
| 1992 | ffs_dev_lock(); |
| 1993 | ret = ffs_opts->dev->desc_ready ? 0 : -ENODEV; |
| 1994 | func->ffs = ffs_opts->dev->ffs_data; |
| 1995 | if (!ffs_opts->no_configfs) |
| 1996 | ffs_dev_unlock(); |
| 1997 | if (ret) |
| 1998 | return ERR_PTR(ret); |
| 1999 | |
| 2000 | func->conf = c; |
| 2001 | func->gadget = c->cdev->gadget; |
| 2002 | |
| 2003 | ffs_data_get(func->ffs); |
| 2004 | |
| 2005 | /* |
| 2006 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() |
| 2007 | * configurations are bound in sequence with list_for_each_entry, |
| 2008 | * in each configuration its functions are bound in sequence |
| 2009 | * with list_for_each_entry, so we assume no race condition |
| 2010 | * with regard to ffs_opts->bound access |
| 2011 | */ |
| 2012 | if (!ffs_opts->refcnt) { |
| 2013 | ret = functionfs_bind(func->ffs, c->cdev); |
| 2014 | if (ret) |
| 2015 | return ERR_PTR(ret); |
| 2016 | } |
| 2017 | ffs_opts->refcnt++; |
| 2018 | func->function.strings = func->ffs->stringtabs; |
| 2019 | |
| 2020 | return ffs_opts; |
| 2021 | } |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2022 | |
| 2023 | static int _ffs_func_bind(struct usb_configuration *c, |
| 2024 | struct usb_function *f) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2025 | { |
| 2026 | struct ffs_function *func = ffs_func_from_usb(f); |
| 2027 | struct ffs_data *ffs = func->ffs; |
| 2028 | |
| 2029 | const int full = !!func->ffs->fs_descs_count; |
| 2030 | const int high = gadget_is_dualspeed(func->gadget) && |
| 2031 | func->ffs->hs_descs_count; |
| 2032 | |
| 2033 | int ret; |
| 2034 | |
| 2035 | /* Make it a single chunk, less management later on */ |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2036 | vla_group(d); |
| 2037 | vla_item_with_sz(d, struct ffs_ep, eps, ffs->eps_count); |
| 2038 | vla_item_with_sz(d, struct usb_descriptor_header *, fs_descs, |
| 2039 | full ? ffs->fs_descs_count + 1 : 0); |
| 2040 | vla_item_with_sz(d, struct usb_descriptor_header *, hs_descs, |
| 2041 | high ? ffs->hs_descs_count + 1 : 0); |
| 2042 | vla_item_with_sz(d, short, inums, ffs->interfaces_count); |
| 2043 | vla_item_with_sz(d, char, raw_descs, |
| 2044 | high ? ffs->raw_descs_length : ffs->raw_fs_descs_length); |
| 2045 | char *vlabuf; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2046 | |
| 2047 | ENTER(); |
| 2048 | |
| 2049 | /* Only high speed but not supported by gadget? */ |
| 2050 | if (unlikely(!(full | high))) |
| 2051 | return -ENOTSUPP; |
| 2052 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2053 | /* Allocate a single chunk, less management later on */ |
| 2054 | vlabuf = kmalloc(vla_group_size(d), GFP_KERNEL); |
| 2055 | if (unlikely(!vlabuf)) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2056 | return -ENOMEM; |
| 2057 | |
| 2058 | /* Zero */ |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2059 | memset(vla_ptr(vlabuf, d, eps), 0, d_eps__sz); |
| 2060 | memcpy(vla_ptr(vlabuf, d, raw_descs), ffs->raw_descs + 16, |
| 2061 | d_raw_descs__sz); |
| 2062 | memset(vla_ptr(vlabuf, d, inums), 0xff, d_inums__sz); |
| 2063 | for (ret = ffs->eps_count; ret; --ret) { |
| 2064 | struct ffs_ep *ptr; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2065 | |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2066 | ptr = vla_ptr(vlabuf, d, eps); |
| 2067 | ptr[ret].num = -1; |
| 2068 | } |
| 2069 | |
| 2070 | /* Save pointers |
| 2071 | * d_eps == vlabuf, func->eps used to kfree vlabuf later |
| 2072 | */ |
| 2073 | func->eps = vla_ptr(vlabuf, d, eps); |
| 2074 | func->interfaces_nums = vla_ptr(vlabuf, d, inums); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2075 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2076 | /* |
| 2077 | * Go through all the endpoint descriptors and allocate |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2078 | * endpoints first, so that later we can rewrite the endpoint |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2079 | * numbers without worrying that it may be described later on. |
| 2080 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2081 | if (likely(full)) { |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2082 | func->function.fs_descriptors = vla_ptr(vlabuf, d, fs_descs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2083 | ret = ffs_do_descs(ffs->fs_descs_count, |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2084 | vla_ptr(vlabuf, d, raw_descs), |
| 2085 | d_raw_descs__sz, |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2086 | __ffs_func_bind_do_descs, func); |
| 2087 | if (unlikely(ret < 0)) |
| 2088 | goto error; |
| 2089 | } else { |
| 2090 | ret = 0; |
| 2091 | } |
| 2092 | |
| 2093 | if (likely(high)) { |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2094 | func->function.hs_descriptors = vla_ptr(vlabuf, d, hs_descs); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2095 | ret = ffs_do_descs(ffs->hs_descs_count, |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2096 | vla_ptr(vlabuf, d, raw_descs) + ret, |
| 2097 | d_raw_descs__sz - ret, |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2098 | __ffs_func_bind_do_descs, func); |
Robert Baldyga | 8854894 | 2013-09-27 12:28:54 +0200 | [diff] [blame] | 2099 | if (unlikely(ret < 0)) |
| 2100 | goto error; |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2103 | /* |
| 2104 | * Now handle interface numbers allocation and interface and |
| 2105 | * endpoint numbers rewriting. We can do that in one go |
| 2106 | * now. |
| 2107 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2108 | ret = ffs_do_descs(ffs->fs_descs_count + |
| 2109 | (high ? ffs->hs_descs_count : 0), |
Andrzej Pietrasiewicz | e6f3862 | 2013-12-03 15:15:30 +0100 | [diff] [blame] | 2110 | vla_ptr(vlabuf, d, raw_descs), d_raw_descs__sz, |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2111 | __ffs_func_bind_do_nums, func); |
| 2112 | if (unlikely(ret < 0)) |
| 2113 | goto error; |
| 2114 | |
| 2115 | /* And we're done */ |
| 2116 | ffs_event_add(ffs, FUNCTIONFS_BIND); |
| 2117 | return 0; |
| 2118 | |
| 2119 | error: |
| 2120 | /* XXX Do we need to release all claimed endpoints here? */ |
| 2121 | return ret; |
| 2122 | } |
| 2123 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2124 | static int ffs_func_bind(struct usb_configuration *c, |
| 2125 | struct usb_function *f) |
| 2126 | { |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2127 | struct f_fs_opts *ffs_opts = ffs_do_functionfs_bind(f, c); |
| 2128 | |
| 2129 | if (IS_ERR(ffs_opts)) |
| 2130 | return PTR_ERR(ffs_opts); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2131 | |
| 2132 | return _ffs_func_bind(c, f); |
| 2133 | } |
| 2134 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2135 | |
| 2136 | /* Other USB function hooks *************************************************/ |
| 2137 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2138 | static int ffs_func_set_alt(struct usb_function *f, |
| 2139 | unsigned interface, unsigned alt) |
| 2140 | { |
| 2141 | struct ffs_function *func = ffs_func_from_usb(f); |
| 2142 | struct ffs_data *ffs = func->ffs; |
| 2143 | int ret = 0, intf; |
| 2144 | |
| 2145 | if (alt != (unsigned)-1) { |
| 2146 | intf = ffs_func_revmap_intf(func, interface); |
| 2147 | if (unlikely(intf < 0)) |
| 2148 | return intf; |
| 2149 | } |
| 2150 | |
| 2151 | if (ffs->func) |
| 2152 | ffs_func_eps_disable(ffs->func); |
| 2153 | |
| 2154 | if (ffs->state != FFS_ACTIVE) |
| 2155 | return -ENODEV; |
| 2156 | |
| 2157 | if (alt == (unsigned)-1) { |
| 2158 | ffs->func = NULL; |
| 2159 | ffs_event_add(ffs, FUNCTIONFS_DISABLE); |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | ffs->func = func; |
| 2164 | ret = ffs_func_eps_enable(func); |
| 2165 | if (likely(ret >= 0)) |
| 2166 | ffs_event_add(ffs, FUNCTIONFS_ENABLE); |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
| 2170 | static void ffs_func_disable(struct usb_function *f) |
| 2171 | { |
| 2172 | ffs_func_set_alt(f, 0, (unsigned)-1); |
| 2173 | } |
| 2174 | |
| 2175 | static int ffs_func_setup(struct usb_function *f, |
| 2176 | const struct usb_ctrlrequest *creq) |
| 2177 | { |
| 2178 | struct ffs_function *func = ffs_func_from_usb(f); |
| 2179 | struct ffs_data *ffs = func->ffs; |
| 2180 | unsigned long flags; |
| 2181 | int ret; |
| 2182 | |
| 2183 | ENTER(); |
| 2184 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 2185 | pr_vdebug("creq->bRequestType = %02x\n", creq->bRequestType); |
| 2186 | pr_vdebug("creq->bRequest = %02x\n", creq->bRequest); |
| 2187 | pr_vdebug("creq->wValue = %04x\n", le16_to_cpu(creq->wValue)); |
| 2188 | pr_vdebug("creq->wIndex = %04x\n", le16_to_cpu(creq->wIndex)); |
| 2189 | pr_vdebug("creq->wLength = %04x\n", le16_to_cpu(creq->wLength)); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2190 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2191 | /* |
| 2192 | * Most requests directed to interface go through here |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2193 | * (notable exceptions are set/get interface) so we need to |
| 2194 | * handle them. All other either handled by composite or |
| 2195 | * passed to usb_configuration->setup() (if one is set). No |
| 2196 | * matter, we will handle requests directed to endpoint here |
| 2197 | * as well (as it's straightforward) but what to do with any |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2198 | * other request? |
| 2199 | */ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2200 | if (ffs->state != FFS_ACTIVE) |
| 2201 | return -ENODEV; |
| 2202 | |
| 2203 | switch (creq->bRequestType & USB_RECIP_MASK) { |
| 2204 | case USB_RECIP_INTERFACE: |
| 2205 | ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex)); |
| 2206 | if (unlikely(ret < 0)) |
| 2207 | return ret; |
| 2208 | break; |
| 2209 | |
| 2210 | case USB_RECIP_ENDPOINT: |
| 2211 | ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex)); |
| 2212 | if (unlikely(ret < 0)) |
| 2213 | return ret; |
| 2214 | break; |
| 2215 | |
| 2216 | default: |
| 2217 | return -EOPNOTSUPP; |
| 2218 | } |
| 2219 | |
| 2220 | spin_lock_irqsave(&ffs->ev.waitq.lock, flags); |
| 2221 | ffs->ev.setup = *creq; |
| 2222 | ffs->ev.setup.wIndex = cpu_to_le16(ret); |
| 2223 | __ffs_event_add(ffs, FUNCTIONFS_SETUP); |
| 2224 | spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags); |
| 2225 | |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
| 2229 | static void ffs_func_suspend(struct usb_function *f) |
| 2230 | { |
| 2231 | ENTER(); |
| 2232 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND); |
| 2233 | } |
| 2234 | |
| 2235 | static void ffs_func_resume(struct usb_function *f) |
| 2236 | { |
| 2237 | ENTER(); |
| 2238 | ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME); |
| 2239 | } |
| 2240 | |
| 2241 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 2242 | /* Endpoint and interface numbers reverse mapping ***************************/ |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2243 | |
| 2244 | static int ffs_func_revmap_ep(struct ffs_function *func, u8 num) |
| 2245 | { |
| 2246 | num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK]; |
| 2247 | return num ? num : -EDOM; |
| 2248 | } |
| 2249 | |
| 2250 | static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf) |
| 2251 | { |
| 2252 | short *nums = func->interfaces_nums; |
| 2253 | unsigned count = func->ffs->interfaces_count; |
| 2254 | |
| 2255 | for (; count; --count, ++nums) { |
| 2256 | if (*nums >= 0 && *nums == intf) |
| 2257 | return nums - func->interfaces_nums; |
| 2258 | } |
| 2259 | |
| 2260 | return -EDOM; |
| 2261 | } |
| 2262 | |
| 2263 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2264 | /* Devices management *******************************************************/ |
| 2265 | |
| 2266 | static LIST_HEAD(ffs_devices); |
| 2267 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2268 | static struct ffs_dev *_ffs_do_find_dev(const char *name) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2269 | { |
| 2270 | struct ffs_dev *dev; |
| 2271 | |
| 2272 | list_for_each_entry(dev, &ffs_devices, entry) { |
| 2273 | if (!dev->name || !name) |
| 2274 | continue; |
| 2275 | if (strcmp(dev->name, name) == 0) |
| 2276 | return dev; |
| 2277 | } |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2278 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2279 | return NULL; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * ffs_lock must be taken by the caller of this function |
| 2284 | */ |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2285 | static struct ffs_dev *_ffs_get_single_dev(void) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2286 | { |
| 2287 | struct ffs_dev *dev; |
| 2288 | |
| 2289 | if (list_is_singular(&ffs_devices)) { |
| 2290 | dev = list_first_entry(&ffs_devices, struct ffs_dev, entry); |
| 2291 | if (dev->single) |
| 2292 | return dev; |
| 2293 | } |
| 2294 | |
| 2295 | return NULL; |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * ffs_lock must be taken by the caller of this function |
| 2300 | */ |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2301 | static struct ffs_dev *_ffs_find_dev(const char *name) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2302 | { |
| 2303 | struct ffs_dev *dev; |
| 2304 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2305 | dev = _ffs_get_single_dev(); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2306 | if (dev) |
| 2307 | return dev; |
| 2308 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2309 | return _ffs_do_find_dev(name); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2312 | /* Configfs support *********************************************************/ |
| 2313 | |
| 2314 | static inline struct f_fs_opts *to_ffs_opts(struct config_item *item) |
| 2315 | { |
| 2316 | return container_of(to_config_group(item), struct f_fs_opts, |
| 2317 | func_inst.group); |
| 2318 | } |
| 2319 | |
| 2320 | static void ffs_attr_release(struct config_item *item) |
| 2321 | { |
| 2322 | struct f_fs_opts *opts = to_ffs_opts(item); |
| 2323 | |
| 2324 | usb_put_function_instance(&opts->func_inst); |
| 2325 | } |
| 2326 | |
| 2327 | static struct configfs_item_operations ffs_item_ops = { |
| 2328 | .release = ffs_attr_release, |
| 2329 | }; |
| 2330 | |
| 2331 | static struct config_item_type ffs_func_type = { |
| 2332 | .ct_item_ops = &ffs_item_ops, |
| 2333 | .ct_owner = THIS_MODULE, |
| 2334 | }; |
| 2335 | |
| 2336 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2337 | /* Function registration interface ******************************************/ |
| 2338 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2339 | static void ffs_free_inst(struct usb_function_instance *f) |
| 2340 | { |
| 2341 | struct f_fs_opts *opts; |
| 2342 | |
| 2343 | opts = to_f_fs_opts(f); |
| 2344 | ffs_dev_lock(); |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2345 | _ffs_free_dev(opts->dev); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2346 | ffs_dev_unlock(); |
| 2347 | kfree(opts); |
| 2348 | } |
| 2349 | |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2350 | #define MAX_INST_NAME_LEN 40 |
| 2351 | |
| 2352 | static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name) |
| 2353 | { |
| 2354 | struct f_fs_opts *opts; |
| 2355 | char *ptr; |
| 2356 | const char *tmp; |
| 2357 | int name_len, ret; |
| 2358 | |
| 2359 | name_len = strlen(name) + 1; |
| 2360 | if (name_len > MAX_INST_NAME_LEN) |
| 2361 | return -ENAMETOOLONG; |
| 2362 | |
| 2363 | ptr = kstrndup(name, name_len, GFP_KERNEL); |
| 2364 | if (!ptr) |
| 2365 | return -ENOMEM; |
| 2366 | |
| 2367 | opts = to_f_fs_opts(fi); |
| 2368 | tmp = NULL; |
| 2369 | |
| 2370 | ffs_dev_lock(); |
| 2371 | |
| 2372 | tmp = opts->dev->name_allocated ? opts->dev->name : NULL; |
| 2373 | ret = _ffs_name_dev(opts->dev, ptr); |
| 2374 | if (ret) { |
| 2375 | kfree(ptr); |
| 2376 | ffs_dev_unlock(); |
| 2377 | return ret; |
| 2378 | } |
| 2379 | opts->dev->name_allocated = true; |
| 2380 | |
| 2381 | ffs_dev_unlock(); |
| 2382 | |
| 2383 | kfree(tmp); |
| 2384 | |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2388 | static struct usb_function_instance *ffs_alloc_inst(void) |
| 2389 | { |
| 2390 | struct f_fs_opts *opts; |
| 2391 | struct ffs_dev *dev; |
| 2392 | |
| 2393 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 2394 | if (!opts) |
| 2395 | return ERR_PTR(-ENOMEM); |
| 2396 | |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2397 | opts->func_inst.set_inst_name = ffs_set_inst_name; |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2398 | opts->func_inst.free_func_inst = ffs_free_inst; |
| 2399 | ffs_dev_lock(); |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2400 | dev = _ffs_alloc_dev(); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2401 | ffs_dev_unlock(); |
| 2402 | if (IS_ERR(dev)) { |
| 2403 | kfree(opts); |
| 2404 | return ERR_CAST(dev); |
| 2405 | } |
| 2406 | opts->dev = dev; |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2407 | dev->opts = opts; |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2408 | |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2409 | config_group_init_type_name(&opts->func_inst.group, "", |
| 2410 | &ffs_func_type); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2411 | return &opts->func_inst; |
| 2412 | } |
| 2413 | |
| 2414 | static void ffs_free(struct usb_function *f) |
| 2415 | { |
| 2416 | kfree(ffs_func_from_usb(f)); |
| 2417 | } |
| 2418 | |
| 2419 | static void ffs_func_unbind(struct usb_configuration *c, |
| 2420 | struct usb_function *f) |
| 2421 | { |
| 2422 | struct ffs_function *func = ffs_func_from_usb(f); |
| 2423 | struct ffs_data *ffs = func->ffs; |
| 2424 | struct f_fs_opts *opts = |
| 2425 | container_of(f->fi, struct f_fs_opts, func_inst); |
| 2426 | struct ffs_ep *ep = func->eps; |
| 2427 | unsigned count = ffs->eps_count; |
| 2428 | unsigned long flags; |
| 2429 | |
| 2430 | ENTER(); |
| 2431 | if (ffs->func == func) { |
| 2432 | ffs_func_eps_disable(func); |
| 2433 | ffs->func = NULL; |
| 2434 | } |
| 2435 | |
| 2436 | if (!--opts->refcnt) |
| 2437 | functionfs_unbind(ffs); |
| 2438 | |
| 2439 | /* cleanup after autoconfig */ |
| 2440 | spin_lock_irqsave(&func->ffs->eps_lock, flags); |
| 2441 | do { |
| 2442 | if (ep->ep && ep->req) |
| 2443 | usb_ep_free_request(ep->ep, ep->req); |
| 2444 | ep->req = NULL; |
| 2445 | ++ep; |
| 2446 | } while (--count); |
| 2447 | spin_unlock_irqrestore(&func->ffs->eps_lock, flags); |
| 2448 | kfree(func->eps); |
| 2449 | func->eps = NULL; |
| 2450 | /* |
| 2451 | * eps, descriptors and interfaces_nums are allocated in the |
| 2452 | * same chunk so only one free is required. |
| 2453 | */ |
| 2454 | func->function.fs_descriptors = NULL; |
| 2455 | func->function.hs_descriptors = NULL; |
| 2456 | func->interfaces_nums = NULL; |
| 2457 | |
| 2458 | ffs_event_add(ffs, FUNCTIONFS_UNBIND); |
| 2459 | } |
| 2460 | |
| 2461 | static struct usb_function *ffs_alloc(struct usb_function_instance *fi) |
| 2462 | { |
| 2463 | struct ffs_function *func; |
| 2464 | |
| 2465 | ENTER(); |
| 2466 | |
| 2467 | func = kzalloc(sizeof(*func), GFP_KERNEL); |
| 2468 | if (unlikely(!func)) |
| 2469 | return ERR_PTR(-ENOMEM); |
| 2470 | |
| 2471 | func->function.name = "Function FS Gadget"; |
| 2472 | |
| 2473 | func->function.bind = ffs_func_bind; |
| 2474 | func->function.unbind = ffs_func_unbind; |
| 2475 | func->function.set_alt = ffs_func_set_alt; |
| 2476 | func->function.disable = ffs_func_disable; |
| 2477 | func->function.setup = ffs_func_setup; |
| 2478 | func->function.suspend = ffs_func_suspend; |
| 2479 | func->function.resume = ffs_func_resume; |
| 2480 | func->function.free_func = ffs_free; |
| 2481 | |
| 2482 | return &func->function; |
| 2483 | } |
| 2484 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2485 | /* |
| 2486 | * ffs_lock must be taken by the caller of this function |
| 2487 | */ |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2488 | static struct ffs_dev *_ffs_alloc_dev(void) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2489 | { |
| 2490 | struct ffs_dev *dev; |
| 2491 | int ret; |
| 2492 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2493 | if (_ffs_get_single_dev()) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2494 | return ERR_PTR(-EBUSY); |
| 2495 | |
| 2496 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 2497 | if (!dev) |
| 2498 | return ERR_PTR(-ENOMEM); |
| 2499 | |
| 2500 | if (list_empty(&ffs_devices)) { |
| 2501 | ret = functionfs_init(); |
| 2502 | if (ret) { |
| 2503 | kfree(dev); |
| 2504 | return ERR_PTR(ret); |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | list_add(&dev->entry, &ffs_devices); |
| 2509 | |
| 2510 | return dev; |
| 2511 | } |
| 2512 | |
| 2513 | /* |
| 2514 | * ffs_lock must be taken by the caller of this function |
| 2515 | * The caller is responsible for "name" being available whenever f_fs needs it |
| 2516 | */ |
| 2517 | static int _ffs_name_dev(struct ffs_dev *dev, const char *name) |
| 2518 | { |
| 2519 | struct ffs_dev *existing; |
| 2520 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2521 | existing = _ffs_do_find_dev(name); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2522 | if (existing) |
| 2523 | return -EBUSY; |
Andrzej Pietrasiewicz | ab13cb0 | 2014-01-13 16:49:36 +0100 | [diff] [blame] | 2524 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2525 | dev->name = name; |
| 2526 | |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | /* |
| 2531 | * The caller is responsible for "name" being available whenever f_fs needs it |
| 2532 | */ |
| 2533 | int ffs_name_dev(struct ffs_dev *dev, const char *name) |
| 2534 | { |
| 2535 | int ret; |
| 2536 | |
| 2537 | ffs_dev_lock(); |
| 2538 | ret = _ffs_name_dev(dev, name); |
| 2539 | ffs_dev_unlock(); |
| 2540 | |
| 2541 | return ret; |
| 2542 | } |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2543 | EXPORT_SYMBOL(ffs_name_dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2544 | |
| 2545 | int ffs_single_dev(struct ffs_dev *dev) |
| 2546 | { |
| 2547 | int ret; |
| 2548 | |
| 2549 | ret = 0; |
| 2550 | ffs_dev_lock(); |
| 2551 | |
| 2552 | if (!list_is_singular(&ffs_devices)) |
| 2553 | ret = -EBUSY; |
| 2554 | else |
| 2555 | dev->single = true; |
| 2556 | |
| 2557 | ffs_dev_unlock(); |
| 2558 | return ret; |
| 2559 | } |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2560 | EXPORT_SYMBOL(ffs_single_dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2561 | |
| 2562 | /* |
| 2563 | * ffs_lock must be taken by the caller of this function |
| 2564 | */ |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2565 | static void _ffs_free_dev(struct ffs_dev *dev) |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2566 | { |
| 2567 | list_del(&dev->entry); |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2568 | if (dev->name_allocated) |
| 2569 | kfree(dev->name); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2570 | kfree(dev); |
| 2571 | if (list_empty(&ffs_devices)) |
| 2572 | functionfs_cleanup(); |
| 2573 | } |
| 2574 | |
| 2575 | static void *ffs_acquire_dev(const char *dev_name) |
| 2576 | { |
| 2577 | struct ffs_dev *ffs_dev; |
| 2578 | |
| 2579 | ENTER(); |
| 2580 | ffs_dev_lock(); |
| 2581 | |
Andrzej Pietrasiewicz | da13a77 | 2014-01-13 16:49:38 +0100 | [diff] [blame] | 2582 | ffs_dev = _ffs_find_dev(dev_name); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2583 | if (!ffs_dev) |
| 2584 | ffs_dev = ERR_PTR(-ENODEV); |
| 2585 | else if (ffs_dev->mounted) |
| 2586 | ffs_dev = ERR_PTR(-EBUSY); |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2587 | else if (ffs_dev->ffs_acquire_dev_callback && |
| 2588 | ffs_dev->ffs_acquire_dev_callback(ffs_dev)) |
| 2589 | ffs_dev = ERR_PTR(-ENODEV); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2590 | else |
| 2591 | ffs_dev->mounted = true; |
| 2592 | |
| 2593 | ffs_dev_unlock(); |
| 2594 | return ffs_dev; |
| 2595 | } |
| 2596 | |
| 2597 | static void ffs_release_dev(struct ffs_data *ffs_data) |
| 2598 | { |
| 2599 | struct ffs_dev *ffs_dev; |
| 2600 | |
| 2601 | ENTER(); |
| 2602 | ffs_dev_lock(); |
| 2603 | |
| 2604 | ffs_dev = ffs_data->private_data; |
Andrzej Pietrasiewicz | ea36592 | 2014-01-13 16:49:35 +0100 | [diff] [blame] | 2605 | if (ffs_dev) { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2606 | ffs_dev->mounted = false; |
Andrzej Pietrasiewicz | ea36592 | 2014-01-13 16:49:35 +0100 | [diff] [blame] | 2607 | |
| 2608 | if (ffs_dev->ffs_release_dev_callback) |
| 2609 | ffs_dev->ffs_release_dev_callback(ffs_dev); |
| 2610 | } |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2611 | |
| 2612 | ffs_dev_unlock(); |
| 2613 | } |
| 2614 | |
| 2615 | static int ffs_ready(struct ffs_data *ffs) |
| 2616 | { |
| 2617 | struct ffs_dev *ffs_obj; |
| 2618 | int ret = 0; |
| 2619 | |
| 2620 | ENTER(); |
| 2621 | ffs_dev_lock(); |
| 2622 | |
| 2623 | ffs_obj = ffs->private_data; |
| 2624 | if (!ffs_obj) { |
| 2625 | ret = -EINVAL; |
| 2626 | goto done; |
| 2627 | } |
| 2628 | if (WARN_ON(ffs_obj->desc_ready)) { |
| 2629 | ret = -EBUSY; |
| 2630 | goto done; |
| 2631 | } |
| 2632 | |
| 2633 | ffs_obj->desc_ready = true; |
| 2634 | ffs_obj->ffs_data = ffs; |
| 2635 | |
| 2636 | if (ffs_obj->ffs_ready_callback) |
| 2637 | ret = ffs_obj->ffs_ready_callback(ffs); |
| 2638 | |
| 2639 | done: |
| 2640 | ffs_dev_unlock(); |
| 2641 | return ret; |
| 2642 | } |
| 2643 | |
| 2644 | static void ffs_closed(struct ffs_data *ffs) |
| 2645 | { |
| 2646 | struct ffs_dev *ffs_obj; |
| 2647 | |
| 2648 | ENTER(); |
| 2649 | ffs_dev_lock(); |
| 2650 | |
| 2651 | ffs_obj = ffs->private_data; |
| 2652 | if (!ffs_obj) |
| 2653 | goto done; |
| 2654 | |
| 2655 | ffs_obj->desc_ready = false; |
| 2656 | |
| 2657 | if (ffs_obj->ffs_closed_callback) |
| 2658 | ffs_obj->ffs_closed_callback(ffs); |
Andrzej Pietrasiewicz | b658499 | 2013-12-03 15:15:36 +0100 | [diff] [blame] | 2659 | |
| 2660 | if (!ffs_obj->opts || ffs_obj->opts->no_configfs |
| 2661 | || !ffs_obj->opts->func_inst.group.cg_item.ci_parent) |
| 2662 | goto done; |
| 2663 | |
| 2664 | unregister_gadget_item(ffs_obj->opts-> |
| 2665 | func_inst.group.cg_item.ci_parent->ci_parent); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 2666 | done: |
| 2667 | ffs_dev_unlock(); |
| 2668 | } |
| 2669 | |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2670 | /* Misc helper functions ****************************************************/ |
| 2671 | |
| 2672 | static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock) |
| 2673 | { |
| 2674 | return nonblock |
| 2675 | ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN |
| 2676 | : mutex_lock_interruptible(mutex); |
| 2677 | } |
| 2678 | |
Al Viro | 260ef31 | 2012-09-26 21:43:45 -0400 | [diff] [blame] | 2679 | static char *ffs_prepare_buffer(const char __user *buf, size_t len) |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2680 | { |
| 2681 | char *data; |
| 2682 | |
| 2683 | if (unlikely(!len)) |
| 2684 | return NULL; |
| 2685 | |
| 2686 | data = kmalloc(len, GFP_KERNEL); |
| 2687 | if (unlikely(!data)) |
| 2688 | return ERR_PTR(-ENOMEM); |
| 2689 | |
| 2690 | if (unlikely(__copy_from_user(data, buf, len))) { |
| 2691 | kfree(data); |
| 2692 | return ERR_PTR(-EFAULT); |
| 2693 | } |
| 2694 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 2695 | pr_vdebug("Buffer from user space:\n"); |
Michal Nazarewicz | ddf8abd | 2010-05-05 12:53:14 +0200 | [diff] [blame] | 2696 | ffs_dump_mem("", data, len); |
| 2697 | |
| 2698 | return data; |
| 2699 | } |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2700 | |
Andrzej Pietrasiewicz | 5920cda | 2013-12-03 15:15:33 +0100 | [diff] [blame] | 2701 | DECLARE_USB_FUNCTION_INIT(ffs, ffs_alloc_inst, ffs_alloc); |
| 2702 | MODULE_LICENSE("GPL"); |
| 2703 | MODULE_AUTHOR("Michal Nazarewicz"); |