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