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