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