hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/usb/gadget/function/f_fs.c
....@@ -17,17 +17,22 @@
1717 #include <linux/blkdev.h>
1818 #include <linux/pagemap.h>
1919 #include <linux/export.h>
20
+#include <linux/fs_parser.h>
2021 #include <linux/hid.h>
22
+#include <linux/mm.h>
2123 #include <linux/module.h>
24
+#include <linux/scatterlist.h>
2225 #include <linux/sched/signal.h>
2326 #include <linux/uio.h>
27
+#include <linux/vmalloc.h>
2428 #include <asm/unaligned.h>
2529
30
+#include <linux/usb/ccid.h>
2631 #include <linux/usb/composite.h>
2732 #include <linux/usb/functionfs.h>
2833
2934 #include <linux/aio.h>
30
-#include <linux/mmu_context.h>
35
+#include <linux/kthread.h>
3136 #include <linux/poll.h>
3237 #include <linux/eventfd.h>
3338
....@@ -117,8 +122,6 @@
117122 struct usb_endpoint_descriptor *descs[3];
118123
119124 u8 num;
120
-
121
- int status; /* P: epfile->mutex */
122125 };
123126
124127 struct ffs_epfile {
....@@ -218,8 +221,13 @@
218221
219222 struct usb_ep *ep;
220223 struct usb_request *req;
224
+ struct sg_table sgt;
225
+ bool use_sg;
221226
222227 struct ffs_data *ffs;
228
+
229
+ int status;
230
+ struct completion done;
223231 };
224232
225233 struct ffs_desc_helper {
....@@ -270,6 +278,11 @@
270278 {
271279 struct usb_request *req = ffs->ep0req;
272280 int ret;
281
+
282
+ if (!req) {
283
+ spin_unlock_irq(&ffs->ev.waitq.lock);
284
+ return -EINVAL;
285
+ }
273286
274287 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
275288
....@@ -561,7 +574,7 @@
561574 spin_unlock_irq(&ffs->ev.waitq.lock);
562575
563576 if (likely(len)) {
564
- data = kmalloc(len, GFP_KERNEL);
577
+ data = kmalloc(ALIGN(len, cache_line_size()), GFP_KERNEL);
565578 if (unlikely(!data)) {
566579 ret = -ENOMEM;
567580 goto done_mutex;
....@@ -698,12 +711,15 @@
698711
699712 static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
700713 {
714
+ struct ffs_io_data *io_data = req->context;
715
+
701716 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
- }
717
+ if (req->status)
718
+ io_data->status = req->status;
719
+ else
720
+ io_data->status = req->actual;
721
+
722
+ complete(&io_data->done);
707723 }
708724
709725 static ssize_t ffs_copy_to_iter(void *data, int data_len, struct iov_iter *iter)
....@@ -749,6 +765,65 @@
749765 return ret;
750766 }
751767
768
+/*
769
+ * allocate a virtually contiguous buffer and create a scatterlist describing it
770
+ * @sg_table - pointer to a place to be filled with sg_table contents
771
+ * @size - required buffer size
772
+ */
773
+static void *ffs_build_sg_list(struct sg_table *sgt, size_t sz)
774
+{
775
+ struct page **pages;
776
+ void *vaddr, *ptr;
777
+ unsigned int n_pages;
778
+ int i;
779
+
780
+ vaddr = vmalloc(sz);
781
+ if (!vaddr)
782
+ return NULL;
783
+
784
+ n_pages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
785
+ pages = kvmalloc_array(n_pages, sizeof(struct page *), GFP_KERNEL);
786
+ if (!pages) {
787
+ vfree(vaddr);
788
+
789
+ return NULL;
790
+ }
791
+ for (i = 0, ptr = vaddr; i < n_pages; ++i, ptr += PAGE_SIZE)
792
+ pages[i] = vmalloc_to_page(ptr);
793
+
794
+ if (sg_alloc_table_from_pages(sgt, pages, n_pages, 0, sz, GFP_KERNEL)) {
795
+ kvfree(pages);
796
+ vfree(vaddr);
797
+
798
+ return NULL;
799
+ }
800
+ kvfree(pages);
801
+
802
+ return vaddr;
803
+}
804
+
805
+static inline void *ffs_alloc_buffer(struct ffs_io_data *io_data,
806
+ size_t data_len)
807
+{
808
+ if (io_data->use_sg)
809
+ return ffs_build_sg_list(&io_data->sgt, data_len);
810
+
811
+ return kmalloc(ALIGN(data_len, cache_line_size()), GFP_KERNEL);
812
+}
813
+
814
+static inline void ffs_free_buffer(struct ffs_io_data *io_data)
815
+{
816
+ if (!io_data->buf)
817
+ return;
818
+
819
+ if (io_data->use_sg) {
820
+ sg_free_table(&io_data->sgt);
821
+ vfree(io_data->buf);
822
+ } else {
823
+ kfree(io_data->buf);
824
+ }
825
+}
826
+
752827 static void ffs_user_copy_worker(struct work_struct *work)
753828 {
754829 struct ffs_io_data *io_data = container_of(work, struct ffs_io_data,
....@@ -758,13 +833,9 @@
758833 bool kiocb_has_eventfd = io_data->kiocb->ki_flags & IOCB_EVENTFD;
759834
760835 if (io_data->read && ret > 0) {
761
- mm_segment_t oldfs = get_fs();
762
-
763
- set_fs(USER_DS);
764
- use_mm(io_data->mm);
836
+ kthread_use_mm(io_data->mm);
765837 ret = ffs_copy_to_iter(io_data->buf, ret, &io_data->data);
766
- unuse_mm(io_data->mm);
767
- set_fs(oldfs);
838
+ kthread_unuse_mm(io_data->mm);
768839 }
769840
770841 io_data->kiocb->ki_complete(io_data->kiocb, ret, ret);
....@@ -776,7 +847,7 @@
776847
777848 if (io_data->read)
778849 kfree(io_data->to_free);
779
- kfree(io_data->buf);
850
+ ffs_free_buffer(io_data);
780851 kfree(io_data);
781852 }
782853
....@@ -946,9 +1017,11 @@
9461017 */
9471018 if (io_data->read)
9481019 data_len = usb_ep_align_maybe(gadget, ep->ep, data_len);
1020
+
1021
+ io_data->use_sg = gadget->sg_supported && data_len > PAGE_SIZE;
9491022 spin_unlock_irq(&epfile->ffs->eps_lock);
9501023
951
- data = kmalloc(data_len, GFP_KERNEL);
1024
+ data = ffs_alloc_buffer(io_data, data_len);
9521025 if (unlikely(!data)) {
9531026 ret = -ENOMEM;
9541027 goto error_mutex;
....@@ -984,14 +1057,23 @@
9841057 WARN(1, "%s: data_len == -EINVAL\n", __func__);
9851058 ret = -EINVAL;
9861059 } else if (!io_data->aio) {
987
- DECLARE_COMPLETION_ONSTACK(done);
9881060 bool interrupted = false;
9891061
9901062 req = ep->req;
991
- req->buf = data;
992
- req->length = data_len;
1063
+ if (io_data->use_sg) {
1064
+ req->buf = NULL;
1065
+ req->sg = io_data->sgt.sgl;
1066
+ req->num_sgs = io_data->sgt.nents;
1067
+ } else {
1068
+ req->buf = data;
1069
+ req->num_sgs = 0;
1070
+ }
1071
+ req->length = data_len;
9931072
994
- req->context = &done;
1073
+ io_data->buf = data;
1074
+
1075
+ init_completion(&io_data->done);
1076
+ req->context = io_data;
9951077 req->complete = ffs_epfile_io_complete;
9961078
9971079 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
....@@ -1000,7 +1082,12 @@
10001082
10011083 spin_unlock_irq(&epfile->ffs->eps_lock);
10021084
1003
- if (unlikely(wait_for_completion_interruptible(&done))) {
1085
+ if (unlikely(wait_for_completion_interruptible(&io_data->done))) {
1086
+ spin_lock_irq(&epfile->ffs->eps_lock);
1087
+ if (epfile->ep != ep) {
1088
+ ret = -ESHUTDOWN;
1089
+ goto error_lock;
1090
+ }
10041091 /*
10051092 * To avoid race condition with ffs_epfile_io_complete,
10061093 * dequeue the request first then check
....@@ -1008,23 +1095,31 @@
10081095 * condition with req->complete callback.
10091096 */
10101097 usb_ep_dequeue(ep->ep, req);
1011
- wait_for_completion(&done);
1012
- interrupted = ep->status < 0;
1098
+ spin_unlock_irq(&epfile->ffs->eps_lock);
1099
+ wait_for_completion(&io_data->done);
1100
+ interrupted = io_data->status < 0;
10131101 }
10141102
10151103 if (interrupted)
10161104 ret = -EINTR;
1017
- else if (io_data->read && ep->status > 0)
1018
- ret = __ffs_epfile_read_data(epfile, data, ep->status,
1105
+ else if (io_data->read && io_data->status > 0)
1106
+ ret = __ffs_epfile_read_data(epfile, data, io_data->status,
10191107 &io_data->data);
10201108 else
1021
- ret = ep->status;
1109
+ ret = io_data->status;
10221110 goto error_mutex;
10231111 } else if (!(req = usb_ep_alloc_request(ep->ep, GFP_ATOMIC))) {
10241112 ret = -ENOMEM;
10251113 } else {
1026
- req->buf = data;
1027
- req->length = data_len;
1114
+ if (io_data->use_sg) {
1115
+ req->buf = NULL;
1116
+ req->sg = io_data->sgt.sgl;
1117
+ req->num_sgs = io_data->sgt.nents;
1118
+ } else {
1119
+ req->buf = data;
1120
+ req->num_sgs = 0;
1121
+ }
1122
+ req->length = data_len;
10281123
10291124 io_data->buf = data;
10301125 io_data->ep = ep->ep;
....@@ -1054,7 +1149,8 @@
10541149 error_mutex:
10551150 mutex_unlock(&epfile->mutex);
10561151 error:
1057
- kfree(data);
1152
+ if (ret != -EIOCBQUEUED) /* don't free if there is iocb queued */
1153
+ ffs_free_buffer(io_data);
10581154 return ret;
10591155 }
10601156
....@@ -1274,14 +1370,6 @@
12741370 return ret;
12751371 }
12761372
1277
-#ifdef CONFIG_COMPAT
1278
-static long ffs_epfile_compat_ioctl(struct file *file, unsigned code,
1279
- unsigned long value)
1280
-{
1281
- return ffs_epfile_ioctl(file, code, value);
1282
-}
1283
-#endif
1284
-
12851373 static const struct file_operations ffs_epfile_operations = {
12861374 .llseek = no_llseek,
12871375
....@@ -1290,9 +1378,7 @@
12901378 .read_iter = ffs_epfile_read_iter,
12911379 .release = ffs_epfile_release,
12921380 .unlocked_ioctl = ffs_epfile_ioctl,
1293
-#ifdef CONFIG_COMPAT
1294
- .compat_ioctl = ffs_epfile_compat_ioctl,
1295
-#endif
1381
+ .compat_ioctl = compat_ptr_ioctl,
12961382 };
12971383
12981384
....@@ -1374,9 +1460,9 @@
13741460 struct ffs_data *ffs_data;
13751461 };
13761462
1377
-static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1463
+static int ffs_sb_fill(struct super_block *sb, struct fs_context *fc)
13781464 {
1379
- struct ffs_sb_fill_data *data = _data;
1465
+ struct ffs_sb_fill_data *data = fc->fs_private;
13801466 struct inode *inode;
13811467 struct ffs_data *ffs = data->ffs_data;
13821468
....@@ -1409,143 +1495,145 @@
14091495 return 0;
14101496 }
14111497
1412
-static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1498
+enum {
1499
+ Opt_no_disconnect,
1500
+ Opt_rmode,
1501
+ Opt_fmode,
1502
+ Opt_mode,
1503
+ Opt_uid,
1504
+ Opt_gid,
1505
+};
1506
+
1507
+static const struct fs_parameter_spec ffs_fs_fs_parameters[] = {
1508
+ fsparam_bool ("no_disconnect", Opt_no_disconnect),
1509
+ fsparam_u32 ("rmode", Opt_rmode),
1510
+ fsparam_u32 ("fmode", Opt_fmode),
1511
+ fsparam_u32 ("mode", Opt_mode),
1512
+ fsparam_u32 ("uid", Opt_uid),
1513
+ fsparam_u32 ("gid", Opt_gid),
1514
+ {}
1515
+};
1516
+
1517
+static int ffs_fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
14131518 {
1519
+ struct ffs_sb_fill_data *data = fc->fs_private;
1520
+ struct fs_parse_result result;
1521
+ int opt;
1522
+
14141523 ENTER();
14151524
1416
- if (!opts || !*opts)
1417
- return 0;
1525
+ opt = fs_parse(fc, ffs_fs_fs_parameters, param, &result);
1526
+ if (opt < 0)
1527
+ return opt;
14181528
1419
- for (;;) {
1420
- unsigned long value;
1421
- char *eq, *comma;
1529
+ switch (opt) {
1530
+ case Opt_no_disconnect:
1531
+ data->no_disconnect = result.boolean;
1532
+ break;
1533
+ case Opt_rmode:
1534
+ data->root_mode = (result.uint_32 & 0555) | S_IFDIR;
1535
+ break;
1536
+ case Opt_fmode:
1537
+ data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1538
+ break;
1539
+ case Opt_mode:
1540
+ data->root_mode = (result.uint_32 & 0555) | S_IFDIR;
1541
+ data->perms.mode = (result.uint_32 & 0666) | S_IFREG;
1542
+ break;
14221543
1423
- /* Option limit */
1424
- comma = strchr(opts, ',');
1425
- if (comma)
1426
- *comma = 0;
1544
+ case Opt_uid:
1545
+ data->perms.uid = make_kuid(current_user_ns(), result.uint_32);
1546
+ if (!uid_valid(data->perms.uid))
1547
+ goto unmapped_value;
1548
+ break;
1549
+ case Opt_gid:
1550
+ data->perms.gid = make_kgid(current_user_ns(), result.uint_32);
1551
+ if (!gid_valid(data->perms.gid))
1552
+ goto unmapped_value;
1553
+ break;
14271554
1428
- /* Value limit */
1429
- eq = strchr(opts, '=');
1430
- if (unlikely(!eq)) {
1431
- pr_err("'=' missing in %s\n", opts);
1432
- return -EINVAL;
1433
- }
1434
- *eq = 0;
1435
-
1436
- /* Parse value */
1437
- if (kstrtoul(eq + 1, 0, &value)) {
1438
- pr_err("%s: invalid value: %s\n", opts, eq + 1);
1439
- return -EINVAL;
1440
- }
1441
-
1442
- /* Interpret option */
1443
- switch (eq - opts) {
1444
- case 13:
1445
- if (!memcmp(opts, "no_disconnect", 13))
1446
- data->no_disconnect = !!value;
1447
- else
1448
- goto invalid;
1449
- break;
1450
- case 5:
1451
- if (!memcmp(opts, "rmode", 5))
1452
- data->root_mode = (value & 0555) | S_IFDIR;
1453
- else if (!memcmp(opts, "fmode", 5))
1454
- data->perms.mode = (value & 0666) | S_IFREG;
1455
- else
1456
- goto invalid;
1457
- break;
1458
-
1459
- case 4:
1460
- if (!memcmp(opts, "mode", 4)) {
1461
- data->root_mode = (value & 0555) | S_IFDIR;
1462
- data->perms.mode = (value & 0666) | S_IFREG;
1463
- } else {
1464
- goto invalid;
1465
- }
1466
- break;
1467
-
1468
- case 3:
1469
- if (!memcmp(opts, "uid", 3)) {
1470
- data->perms.uid = make_kuid(current_user_ns(), value);
1471
- if (!uid_valid(data->perms.uid)) {
1472
- pr_err("%s: unmapped value: %lu\n", opts, value);
1473
- return -EINVAL;
1474
- }
1475
- } else if (!memcmp(opts, "gid", 3)) {
1476
- data->perms.gid = make_kgid(current_user_ns(), value);
1477
- if (!gid_valid(data->perms.gid)) {
1478
- pr_err("%s: unmapped value: %lu\n", opts, value);
1479
- return -EINVAL;
1480
- }
1481
- } else {
1482
- goto invalid;
1483
- }
1484
- break;
1485
-
1486
- default:
1487
-invalid:
1488
- pr_err("%s: invalid option\n", opts);
1489
- return -EINVAL;
1490
- }
1491
-
1492
- /* Next iteration */
1493
- if (!comma)
1494
- break;
1495
- opts = comma + 1;
1555
+ default:
1556
+ return -ENOPARAM;
14961557 }
14971558
14981559 return 0;
1560
+
1561
+unmapped_value:
1562
+ return invalf(fc, "%s: unmapped value: %u", param->key, result.uint_32);
14991563 }
15001564
1501
-/* "mount -t functionfs dev_name /dev/function" ends up here */
1502
-
1503
-static struct dentry *
1504
-ffs_fs_mount(struct file_system_type *t, int flags,
1505
- const char *dev_name, void *opts)
1565
+/*
1566
+ * Set up the superblock for a mount.
1567
+ */
1568
+static int ffs_fs_get_tree(struct fs_context *fc)
15061569 {
1507
- struct ffs_sb_fill_data data = {
1508
- .perms = {
1509
- .mode = S_IFREG | 0600,
1510
- .uid = GLOBAL_ROOT_UID,
1511
- .gid = GLOBAL_ROOT_GID,
1512
- },
1513
- .root_mode = S_IFDIR | 0500,
1514
- .no_disconnect = false,
1515
- };
1516
- struct dentry *rv;
1517
- int ret;
1570
+ struct ffs_sb_fill_data *ctx = fc->fs_private;
15181571 struct ffs_data *ffs;
1572
+ int ret;
15191573
15201574 ENTER();
15211575
1522
- ret = ffs_fs_parse_opts(&data, opts);
1523
- if (unlikely(ret < 0))
1524
- return ERR_PTR(ret);
1576
+ if (!fc->source)
1577
+ return invalf(fc, "No source specified");
15251578
1526
- ffs = ffs_data_new(dev_name);
1579
+ ffs = ffs_data_new(fc->source);
15271580 if (unlikely(!ffs))
1528
- return ERR_PTR(-ENOMEM);
1529
- ffs->file_perms = data.perms;
1530
- ffs->no_disconnect = data.no_disconnect;
1581
+ return -ENOMEM;
1582
+ ffs->file_perms = ctx->perms;
1583
+ ffs->no_disconnect = ctx->no_disconnect;
15311584
1532
- ffs->dev_name = kstrdup(dev_name, GFP_KERNEL);
1585
+ ffs->dev_name = kstrdup(fc->source, GFP_KERNEL);
15331586 if (unlikely(!ffs->dev_name)) {
15341587 ffs_data_put(ffs);
1535
- return ERR_PTR(-ENOMEM);
1588
+ return -ENOMEM;
15361589 }
15371590
1538
- ret = ffs_acquire_dev(dev_name, ffs);
1591
+ ret = ffs_acquire_dev(ffs->dev_name, ffs);
15391592 if (ret) {
15401593 ffs_data_put(ffs);
1541
- return ERR_PTR(ret);
1594
+ return ret;
15421595 }
1543
- data.ffs_data = ffs;
15441596
1545
- rv = mount_nodev(t, flags, &data, ffs_sb_fill);
1546
- if (IS_ERR(rv) && data.ffs_data)
1547
- ffs_data_put(data.ffs_data);
1548
- return rv;
1597
+ ctx->ffs_data = ffs;
1598
+ return get_tree_nodev(fc, ffs_sb_fill);
1599
+}
1600
+
1601
+static void ffs_fs_free_fc(struct fs_context *fc)
1602
+{
1603
+ struct ffs_sb_fill_data *ctx = fc->fs_private;
1604
+
1605
+ if (ctx) {
1606
+ if (ctx->ffs_data) {
1607
+ ffs_data_put(ctx->ffs_data);
1608
+ }
1609
+
1610
+ kfree(ctx);
1611
+ }
1612
+}
1613
+
1614
+static const struct fs_context_operations ffs_fs_context_ops = {
1615
+ .free = ffs_fs_free_fc,
1616
+ .parse_param = ffs_fs_parse_param,
1617
+ .get_tree = ffs_fs_get_tree,
1618
+};
1619
+
1620
+static int ffs_fs_init_fs_context(struct fs_context *fc)
1621
+{
1622
+ struct ffs_sb_fill_data *ctx;
1623
+
1624
+ ctx = kzalloc(sizeof(struct ffs_sb_fill_data), GFP_KERNEL);
1625
+ if (!ctx)
1626
+ return -ENOMEM;
1627
+
1628
+ ctx->perms.mode = S_IFREG | 0600;
1629
+ ctx->perms.uid = GLOBAL_ROOT_UID;
1630
+ ctx->perms.gid = GLOBAL_ROOT_GID;
1631
+ ctx->root_mode = S_IFDIR | 0500;
1632
+ ctx->no_disconnect = false;
1633
+
1634
+ fc->fs_private = ctx;
1635
+ fc->ops = &ffs_fs_context_ops;
1636
+ return 0;
15491637 }
15501638
15511639 static void
....@@ -1561,7 +1649,8 @@
15611649 static struct file_system_type ffs_fs_type = {
15621650 .owner = THIS_MODULE,
15631651 .name = "functionfs",
1564
- .mount = ffs_fs_mount,
1652
+ .init_fs_context = ffs_fs_init_fs_context,
1653
+ .parameters = ffs_fs_fs_parameters,
15651654 .kill_sb = ffs_fs_kill_sb,
15661655 };
15671656 MODULE_ALIAS_FS("functionfs");
....@@ -1626,7 +1715,7 @@
16261715 ffs_data_clear(ffs);
16271716 ffs_release_dev(ffs->private_data);
16281717 BUG_ON(waitqueue_active(&ffs->ev.waitq) ||
1629
- waitqueue_active(&ffs->ep0req_completion.wait) ||
1718
+ swait_active(&ffs->ep0req_completion.wait) ||
16301719 waitqueue_active(&ffs->wait));
16311720 destroy_workqueue(ffs->io_completion_wq);
16321721 kfree(ffs->dev_name);
....@@ -1807,10 +1896,14 @@
18071896 ENTER();
18081897
18091898 if (!WARN_ON(!ffs->gadget)) {
1899
+ /* dequeue before freeing ep0req */
1900
+ usb_ep_dequeue(ffs->gadget->ep0, ffs->ep0req);
1901
+ mutex_lock(&ffs->mutex);
18101902 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
18111903 ffs->ep0req = NULL;
18121904 ffs->gadget = NULL;
18131905 clear_bit(FFS_FL_BOUND, &ffs->flags);
1906
+ mutex_unlock(&ffs->mutex);
18141907 ffs_data_put(ffs);
18151908 }
18161909 }
....@@ -1964,7 +2057,7 @@
19642057
19652058 static int __must_check ffs_do_single_desc(char *data, unsigned len,
19662059 ffs_entity_callback entity,
1967
- void *priv)
2060
+ void *priv, int *current_class)
19682061 {
19692062 struct usb_descriptor_header *_ds = (void *)data;
19702063 u8 length;
....@@ -2022,6 +2115,7 @@
20222115 __entity(INTERFACE, ds->bInterfaceNumber);
20232116 if (ds->iInterface)
20242117 __entity(STRING, ds->iInterface);
2118
+ *current_class = ds->bInterfaceClass;
20252119 }
20262120 break;
20272121
....@@ -2035,11 +2129,22 @@
20352129 }
20362130 break;
20372131
2038
- case HID_DT_HID:
2039
- pr_vdebug("hid descriptor\n");
2040
- if (length != sizeof(struct hid_descriptor))
2041
- goto inv_length;
2042
- break;
2132
+ case USB_TYPE_CLASS | 0x01:
2133
+ if (*current_class == USB_INTERFACE_CLASS_HID) {
2134
+ pr_vdebug("hid descriptor\n");
2135
+ if (length != sizeof(struct hid_descriptor))
2136
+ goto inv_length;
2137
+ break;
2138
+ } else if (*current_class == USB_INTERFACE_CLASS_CCID) {
2139
+ pr_vdebug("ccid descriptor\n");
2140
+ if (length != sizeof(struct ccid_descriptor))
2141
+ goto inv_length;
2142
+ break;
2143
+ } else {
2144
+ pr_vdebug("unknown descriptor: %d for class %d\n",
2145
+ _ds->bDescriptorType, *current_class);
2146
+ return -EINVAL;
2147
+ }
20432148
20442149 case USB_DT_OTG:
20452150 if (length != sizeof(struct usb_otg_descriptor))
....@@ -2096,6 +2201,7 @@
20962201 {
20972202 const unsigned _len = len;
20982203 unsigned long num = 0;
2204
+ int current_class = -1;
20992205
21002206 ENTER();
21012207
....@@ -2116,7 +2222,8 @@
21162222 if (!data)
21172223 return _len - len;
21182224
2119
- ret = ffs_do_single_desc(data, len, entity, priv);
2225
+ ret = ffs_do_single_desc(data, len, entity, priv,
2226
+ &current_class);
21202227 if (unlikely(ret < 0)) {
21212228 pr_debug("%s returns %d\n", __func__, ret);
21222229 return ret;
....@@ -2295,7 +2402,7 @@
22952402 return _len - len;
22962403 }
22972404
2298
-/**
2405
+/*
22992406 * Validate contents of the buffer from userspace related to OS descriptors.
23002407 */
23012408 static int __ffs_data_do_os_desc(enum ffs_os_desc_type type,
....@@ -2447,7 +2554,7 @@
24472554 os_descs_count = get_unaligned_le32(data);
24482555 data += 4;
24492556 len -= 4;
2450
- };
2557
+ }
24512558
24522559 /* Read descriptors */
24532560 raw_descs = data;
....@@ -2670,7 +2777,7 @@
26702777 switch (type) {
26712778 case FUNCTIONFS_RESUME:
26722779 rem_type2 = FUNCTIONFS_SUSPEND;
2673
- /* FALL THROUGH */
2780
+ fallthrough;
26742781 case FUNCTIONFS_SUSPEND:
26752782 case FUNCTIONFS_SETUP:
26762783 rem_type1 = type;
....@@ -2788,12 +2895,18 @@
27882895 struct usb_request *req;
27892896 struct usb_ep *ep;
27902897 u8 bEndpointAddress;
2898
+ u16 wMaxPacketSize;
27912899
27922900 /*
27932901 * We back up bEndpointAddress because autoconfig overwrites
27942902 * it with physical endpoint address.
27952903 */
27962904 bEndpointAddress = ds->bEndpointAddress;
2905
+ /*
2906
+ * We back up wMaxPacketSize because autoconfig treats
2907
+ * endpoint descriptors as if they were full speed.
2908
+ */
2909
+ wMaxPacketSize = ds->wMaxPacketSize;
27972910 pr_vdebug("autoconfig\n");
27982911 ep = usb_ep_autoconfig(func->gadget, ds);
27992912 if (unlikely(!ep))
....@@ -2815,6 +2928,11 @@
28152928 */
28162929 if (func->ffs->user_flags & FUNCTIONFS_VIRTUAL_ADDR)
28172930 ds->bEndpointAddress = bEndpointAddress;
2931
+ /*
2932
+ * Restore wMaxPacketSize which was potentially
2933
+ * overwritten by autoconfig.
2934
+ */
2935
+ ds->wMaxPacketSize = wMaxPacketSize;
28182936 }
28192937 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
28202938
....@@ -3449,7 +3567,7 @@
34493567
34503568 static int ffs_set_inst_name(struct usb_function_instance *fi, const char *name)
34513569 {
3452
- if (strlen(name) >= FIELD_SIZEOF(struct ffs_dev, name))
3570
+ if (strlen(name) >= sizeof_field(struct ffs_dev, name))
34533571 return -ENAMETOOLONG;
34543572 return ffs_name_dev(to_f_fs_opts(fi)->dev, name);
34553573 }