hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/net/usb/usbnet.c
....@@ -1,20 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * USB Network driver infrastructure
34 * Copyright (C) 2000-2005 by David Brownell
45 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
10
- *
11
- * This program is distributed in the hope that it will be useful,
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
- * GNU General Public License for more details.
15
- *
16
- * You should have received a copy of the GNU General Public License
17
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
186 */
197
208 /*
....@@ -45,9 +33,6 @@
4533 #include <linux/slab.h>
4634 #include <linux/kernel.h>
4735 #include <linux/pm_runtime.h>
48
-
49
-#define DRIVER_VERSION "22-Aug-2005"
50
-
5136
5237 /*-------------------------------------------------------------------------*/
5338
....@@ -122,7 +107,7 @@
122107 if (!usb_endpoint_dir_in(&e->desc))
123108 continue;
124109 intr = 1;
125
- /* FALLTHROUGH */
110
+ fallthrough;
126111 case USB_ENDPOINT_XFER_BULK:
127112 break;
128113 default:
....@@ -609,7 +594,7 @@
609594 case -EPIPE:
610595 dev->net->stats.rx_errors++;
611596 usbnet_defer_kevent (dev, EVENT_RX_HALT);
612
- // FALLTHROUGH
597
+ fallthrough;
613598
614599 /* software-driven interface shutdown */
615600 case -ECONNRESET: /* async unlink */
....@@ -640,7 +625,7 @@
640625 /* data overrun ... flush fifo? */
641626 case -EOVERFLOW:
642627 dev->net->stats.rx_over_errors++;
643
- // FALLTHROUGH
628
+ fallthrough;
644629
645630 default:
646631 state = rx_cleanup;
....@@ -811,7 +796,7 @@
811796 int usbnet_stop (struct net_device *net)
812797 {
813798 struct usbnet *dev = netdev_priv(net);
814
- struct driver_info *info = dev->driver_info;
799
+ const struct driver_info *info = dev->driver_info;
815800 int retval, pm, mpn;
816801
817802 clear_bit(EVENT_DEV_OPEN, &dev->flags);
....@@ -845,13 +830,11 @@
845830
846831 mpn = !test_and_clear_bit(EVENT_NO_RUNTIME_PM, &dev->flags);
847832
848
- /* deferred work (task, timer, softirq) must also stop.
849
- * can't flush_scheduled_work() until we drop rtnl (later),
850
- * else workers could deadlock; so make workers a NOP.
851
- */
833
+ /* deferred work (timer, softirq, task) must also stop */
852834 dev->flags = 0;
853835 del_timer_sync (&dev->delay);
854836 tasklet_kill (&dev->bh);
837
+ cancel_work_sync(&dev->kevent);
855838 if (!pm)
856839 usb_autopm_put_interface(dev->intf);
857840
....@@ -874,7 +857,7 @@
874857 {
875858 struct usbnet *dev = netdev_priv(net);
876859 int retval;
877
- struct driver_info *info = dev->driver_info;
860
+ const struct driver_info *info = dev->driver_info;
878861
879862 if ((retval = usb_autopm_get_interface(dev->intf)) < 0) {
880863 netif_info(dev, ifup, dev->net,
....@@ -998,31 +981,9 @@
998981 void usbnet_get_stats64(struct net_device *net, struct rtnl_link_stats64 *stats)
999982 {
1000983 struct usbnet *dev = netdev_priv(net);
1001
- unsigned int start;
1002
- int cpu;
1003984
1004985 netdev_stats_to_stats64(stats, &net->stats);
1005
-
1006
- for_each_possible_cpu(cpu) {
1007
- struct pcpu_sw_netstats *stats64;
1008
- u64 rx_packets, rx_bytes;
1009
- u64 tx_packets, tx_bytes;
1010
-
1011
- stats64 = per_cpu_ptr(dev->stats64, cpu);
1012
-
1013
- do {
1014
- start = u64_stats_fetch_begin_irq(&stats64->syncp);
1015
- rx_packets = stats64->rx_packets;
1016
- rx_bytes = stats64->rx_bytes;
1017
- tx_packets = stats64->tx_packets;
1018
- tx_bytes = stats64->tx_bytes;
1019
- } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
1020
-
1021
- stats->rx_packets += rx_packets;
1022
- stats->rx_bytes += rx_bytes;
1023
- stats->tx_packets += tx_packets;
1024
- stats->tx_bytes += tx_bytes;
1025
- }
986
+ dev_fetch_sw_netstats(stats, dev->stats64);
1026987 }
1027988 EXPORT_SYMBOL_GPL(usbnet_get_stats64);
1028989
....@@ -1059,7 +1020,6 @@
10591020 struct usbnet *dev = netdev_priv(net);
10601021
10611022 strlcpy (info->driver, dev->driver_name, sizeof info->driver);
1062
- strlcpy (info->version, DRIVER_VERSION, sizeof info->version);
10631023 strlcpy (info->fw_version, dev->driver_info->description,
10641024 sizeof info->fw_version);
10651025 usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
....@@ -1120,12 +1080,13 @@
11201080 clear_bit(EVENT_LINK_CHANGE, &dev->flags);
11211081 }
11221082
1123
-static void usbnet_set_rx_mode(struct net_device *net)
1083
+void usbnet_set_rx_mode(struct net_device *net)
11241084 {
11251085 struct usbnet *dev = netdev_priv(net);
11261086
11271087 usbnet_defer_kevent(dev, EVENT_SET_RX_MODE);
11281088 }
1089
+EXPORT_SYMBOL_GPL(usbnet_set_rx_mode);
11291090
11301091 static void __handle_set_rx_mode(struct usbnet *dev)
11311092 {
....@@ -1214,7 +1175,7 @@
12141175 }
12151176
12161177 if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
1217
- struct driver_info *info = dev->driver_info;
1178
+ const struct driver_info *info = dev->driver_info;
12181179 int retval = 0;
12191180
12201181 clear_bit (EVENT_LINK_RESET, &dev->flags);
....@@ -1305,7 +1266,7 @@
13051266
13061267 /*-------------------------------------------------------------------------*/
13071268
1308
-void usbnet_tx_timeout (struct net_device *net)
1269
+void usbnet_tx_timeout (struct net_device *net, unsigned int txqueue)
13091270 {
13101271 struct usbnet *dev = netdev_priv(net);
13111272
....@@ -1344,11 +1305,11 @@
13441305 total_len += skb_headlen(skb);
13451306
13461307 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1347
- struct skb_frag_struct *f = &skb_shinfo(skb)->frags[i];
1308
+ skb_frag_t *f = &skb_shinfo(skb)->frags[i];
13481309
13491310 total_len += skb_frag_size(f);
1350
- sg_set_page(&urb->sg[i + s], f->page.p, f->size,
1351
- f->page_offset);
1311
+ sg_set_page(&urb->sg[i + s], skb_frag_page(f), skb_frag_size(f),
1312
+ skb_frag_off(f));
13521313 }
13531314 urb->transfer_buffer_length = total_len;
13541315
....@@ -1362,7 +1323,7 @@
13621323 unsigned int length;
13631324 struct urb *urb = NULL;
13641325 struct skb_data *entry;
1365
- struct driver_info *info = dev->driver_info;
1326
+ const struct driver_info *info = dev->driver_info;
13661327 unsigned long flags;
13671328 int retval;
13681329
....@@ -1541,6 +1502,7 @@
15411502 continue;
15421503 case tx_done:
15431504 kfree(entry->urb->sg);
1505
+ fallthrough;
15441506 case rx_cleanup:
15451507 usb_free_urb (entry->urb);
15461508 dev_kfree_skb (skb);
....@@ -1605,6 +1567,7 @@
16051567 struct usbnet *dev;
16061568 struct usb_device *xdev;
16071569 struct net_device *net;
1570
+ struct urb *urb;
16081571
16091572 dev = usb_get_intfdata(intf);
16101573 usb_set_intfdata(intf, NULL);
....@@ -1621,9 +1584,11 @@
16211584 net = dev->net;
16221585 unregister_netdev (net);
16231586
1624
- cancel_work_sync(&dev->kevent);
1625
-
1626
- usb_scuttle_anchored_urbs(&dev->deferred);
1587
+ while ((urb = usb_get_from_anchor(&dev->deferred))) {
1588
+ dev_kfree_skb(urb->context);
1589
+ kfree(urb->sg);
1590
+ usb_free_urb(urb);
1591
+ }
16271592
16281593 if (dev->driver_info->unbind)
16291594 dev->driver_info->unbind (dev, intf);
....@@ -1667,7 +1632,7 @@
16671632 struct usbnet *dev;
16681633 struct net_device *net;
16691634 struct usb_host_interface *interface;
1670
- struct driver_info *info;
1635
+ const struct driver_info *info;
16711636 struct usb_device *xdev;
16721637 int status;
16731638 const char *name;
....@@ -1683,7 +1648,7 @@
16831648 }
16841649
16851650 name = udev->dev.driver->name;
1686
- info = (struct driver_info *) prod->driver_info;
1651
+ info = (const struct driver_info *) prod->driver_info;
16871652 if (!info) {
16881653 dev_dbg (&udev->dev, "blacklisted by %s\n", name);
16891654 return -ENODEV;
....@@ -1763,10 +1728,6 @@
17631728 if ((dev->driver_info->flags & FLAG_WWAN) != 0)
17641729 strcpy(net->name, "wwan%d");
17651730
1766
- /* LTE devices should always be named "lte%d" */
1767
- if ((dev->driver_info->flags & FLAG_LTE) != 0)
1768
- strcpy(net->name, "lte%d");
1769
-
17701731 /* devices that cannot do ARP */
17711732 if ((dev->driver_info->flags & FLAG_NOARP) != 0)
17721733 net->flags |= IFF_NOARP;
....@@ -1777,6 +1738,10 @@
17771738 } else if (!info->in || !info->out)
17781739 status = usbnet_get_endpoints (dev, udev);
17791740 else {
1741
+ u8 ep_addrs[3] = {
1742
+ info->in + USB_DIR_IN, info->out + USB_DIR_OUT, 0
1743
+ };
1744
+
17801745 dev->in = usb_rcvbulkpipe (xdev, info->in);
17811746 dev->out = usb_sndbulkpipe (xdev, info->out);
17821747 if (!(info->flags & FLAG_NO_SETINT))
....@@ -1786,6 +1751,8 @@
17861751 else
17871752 status = 0;
17881753
1754
+ if (status == 0 && !usb_check_bulk_endpoints(udev, ep_addrs))
1755
+ status = -EINVAL;
17891756 }
17901757 if (status >= 0 && dev->status)
17911758 status = init_status (dev, udev);
....@@ -2009,7 +1976,7 @@
20091976 cmd, reqtype, value, index, size);
20101977
20111978 if (size) {
2012
- buf = kmalloc(size, GFP_KERNEL);
1979
+ buf = kmalloc(size, GFP_NOIO);
20131980 if (!buf)
20141981 goto out;
20151982 }
....@@ -2041,7 +2008,7 @@
20412008 cmd, reqtype, value, index, size);
20422009
20432010 if (data) {
2044
- buf = kmemdup(data, size, GFP_KERNEL);
2011
+ buf = kmemdup(data, size, GFP_NOIO);
20452012 if (!buf)
20462013 goto out;
20472014 } else {
....@@ -2142,7 +2109,7 @@
21422109 int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
21432110 u16 value, u16 index, const void *data, u16 size)
21442111 {
2145
- struct usb_ctrlrequest *req = NULL;
2112
+ struct usb_ctrlrequest *req;
21462113 struct urb *urb;
21472114 int err = -ENOMEM;
21482115 void *buf = NULL;
....@@ -2160,7 +2127,7 @@
21602127 if (!buf) {
21612128 netdev_err(dev->net, "Error allocating buffer"
21622129 " in %s!\n", __func__);
2163
- goto fail_free;
2130
+ goto fail_free_urb;
21642131 }
21652132 }
21662133
....@@ -2184,14 +2151,21 @@
21842151 if (err < 0) {
21852152 netdev_err(dev->net, "Error submitting the control"
21862153 " message: status=%d\n", err);
2187
- goto fail_free;
2154
+ goto fail_free_all;
21882155 }
21892156 return 0;
21902157
2158
+fail_free_all:
2159
+ kfree(req);
21912160 fail_free_buf:
21922161 kfree(buf);
2193
-fail_free:
2194
- kfree(req);
2162
+ /*
2163
+ * avoid a double free
2164
+ * needed because the flag can be set only
2165
+ * after filling the URB
2166
+ */
2167
+ urb->transfer_flags = 0;
2168
+fail_free_urb:
21952169 usb_free_urb(urb);
21962170 fail:
21972171 return err;
....@@ -2204,7 +2178,7 @@
22042178 {
22052179 /* Compiler should optimize this out. */
22062180 BUILD_BUG_ON(
2207
- FIELD_SIZEOF(struct sk_buff, cb) < sizeof(struct skb_data));
2181
+ sizeof_field(struct sk_buff, cb) < sizeof(struct skb_data));
22082182
22092183 eth_random_addr(node_id);
22102184 return 0;