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