Changes in 5.10.181
driver core: add a helper to setup both the of_node and fwnode of a device
drm/mipi-dsi: Set the fwnode for mipi_dsi_device
ARM: 9296/1: HP Jornada 7XX: fix kernel-doc warnings
net: mdio: mvusb: Fix an error handling path in mvusb_mdio_probe()
linux/dim: Do nothing if no time delta between samples
net: Fix load-tearing on sk->sk_stamp in sock_recv_cmsgs().
netfilter: conntrack: fix possible bug_on with enable_hooks=1
netlink: annotate accesses to nlk->cb_running
net: annotate sk->sk_err write from do_recvmmsg()
net: deal with most data-races in sk_wait_event()
net: tap: check vlan with eth_type_vlan() method
net: add vlan_get_protocol_and_depth() helper
tcp: factor out __tcp_close() helper
tcp: add annotations around sk->sk_shutdown accesses
ipvlan:Fix out-of-bounds caused by unclear skb->cb
net: datagram: fix data-races in datagram_poll()
af_unix: Fix a data race of sk->sk_receive_queue->qlen.
af_unix: Fix data races around sk->sk_shutdown.
drm/i915/dp: prevent potential div-by-zero
fbdev: arcfb: Fix error handling in arcfb_probe()
ext4: remove an unused variable warning with CONFIG_QUOTA=n
ext4: reflect error codes from ext4_multi_mount_protect() to its callers
ext4: don't clear SB_RDONLY when remounting r/w until quota is re-enabled
ext4: fix lockdep warning when enabling MMP
ext4: remove redundant mb_regenerate_buddy()
ext4: drop s_mb_bal_lock and convert protected fields to atomic
ext4: add mballoc stats proc file
ext4: allow to find by goal if EXT4_MB_HINT_GOAL_ONLY is set
ext4: allow ext4_get_group_info() to fail
refscale: Move shutdown from wait_event() to wait_event_idle()
rcu: Protect rcu_print_task_exp_stall() ->exp_tasks access
fs: hfsplus: remove WARN_ON() from hfsplus_cat_{read,write}_inode()
drm/amd/display: Use DC_LOG_DC in the trasform pixel function
regmap: cache: Return error in cache sync operations for REGCACHE_NONE
arm64: dts: qcom: msm8996: Add missing DWC3 quirks
memstick: r592: Fix UAF bug in r592_remove due to race condition
firmware: arm_sdei: Fix sleep from invalid context BUG
ACPI: EC: Fix oops when removing custom query handlers
remoteproc: stm32_rproc: Add mutex protection for workqueue
drm/tegra: Avoid potential 32-bit integer overflow
ACPICA: Avoid undefined behavior: applying zero offset to null pointer
ACPICA: ACPICA: check null return of ACPI_ALLOCATE_ZEROED in acpi_db_display_objects
drm/amd: Fix an out of bounds error in BIOS parser
wifi: ath: Silence memcpy run-time false positive warning
bpf: Annotate data races in bpf_local_storage
wifi: brcmfmac: cfg80211: Pass the PMK in binary instead of hex
ext2: Check block size validity during mount
scsi: lpfc: Prevent lpfc_debugfs_lockstat_write() buffer overflow
net: pasemi: Fix return type of pasemi_mac_start_tx()
net: Catch invalid index in XPS mapping
scsi: target: iscsit: Free cmds before session free
lib: cpu_rmap: Avoid use after free on rmap->obj array entries
scsi: message: mptlan: Fix use after free bug in mptlan_remove() due to race condition
gfs2: Fix inode height consistency check
ext4: set goal start correctly in ext4_mb_normalize_request
ext4: Fix best extent lstart adjustment logic in ext4_mb_new_inode_pa()
f2fs: fix to drop all dirty pages during umount() if cp_error is set
samples/bpf: Fix fout leak in hbm's run_bpf_prog
wifi: iwlwifi: pcie: fix possible NULL pointer dereference
wifi: iwlwifi: pcie: Fix integer overflow in iwl_write_to_user_buf
null_blk: Always check queue mode setting from configfs
wifi: iwlwifi: dvm: Fix memcpy: detected field-spanning write backtrace
wifi: ath11k: Fix SKB corruption in REO destination ring
ipvs: Update width of source for ip_vs_sync_conn_options
Bluetooth: hci_bcm: Fall back to getting bdaddr from EFI if not set
Bluetooth: L2CAP: fix "bad unlock balance" in l2cap_disconnect_rsp
staging: rtl8192e: Replace macro RTL_PCI_DEVICE with PCI_DEVICE
HID: logitech-hidpp: Don't use the USB serial for USB devices
HID: logitech-hidpp: Reconcile USB and Unifying serials
spi: spi-imx: fix MX51_ECSPI_* macros when cs > 3
HID: wacom: generic: Set battery quirk only when we see battery data
usb: typec: tcpm: fix multiple times discover svids error
serial: 8250: Reinit port->pm on port specific driver unbind
mcb-pci: Reallocate memory region to avoid memory overlapping
sched: Fix KCSAN noinstr violation
recordmcount: Fix memory leaks in the uwrite function
RDMA/core: Fix multiple -Warray-bounds warnings
iommu/arm-smmu-qcom: Limit the SMR groups to 128
clk: tegra20: fix gcc-7 constant overflow warning
iommu/arm-smmu-v3: Acknowledge pri/event queue overflow if any
Input: xpad - add constants for GIP interface numbers
phy: st: miphy28lp: use _poll_timeout functions for waits
mfd: dln2: Fix memory leak in dln2_probe()
btrfs: move btrfs_find_highest_objectid/btrfs_find_free_objectid to disk-io.c
btrfs: replace calls to btrfs_find_free_ino with btrfs_find_free_objectid
btrfs: fix space cache inconsistency after error loading it from disk
xfrm: don't check the default policy if the policy allows the packet
Revert "Fix XFRM-I support for nested ESP tunnels"
drm/msm/dp: unregister audio driver during unbind
drm/msm/dpu: Remove duplicate register defines from INTF
cpupower: Make TSC read per CPU for Mperf monitor
af_key: Reject optional tunnel/BEET mode templates in outbound policies
net: fec: Better handle pm_runtime_get() failing in .remove()
net: phy: dp83867: add w/a for packet errors seen with short cables
ALSA: firewire-digi00x: prevent potential use after free
ALSA: hda/realtek: Apply HP B&O top speaker profile to Pavilion 15
vsock: avoid to close connected socket after the timeout
ipv4/tcp: do not use per netns ctl sockets
net: Find dst with sk's xfrm policy not ctl_sk
tcp: fix possible sk_priority leak in tcp_v4_send_reset()
serial: arc_uart: fix of_iomap leak in `arc_serial_probe`
erspan: get the proto with the md version for collect_md
net: hns3: fix sending pfc frames after reset issue
net: hns3: fix reset delay time to avoid configuration timeout
media: netup_unidvb: fix use-after-free at del_timer()
SUNRPC: Fix trace_svc_register() call site
drm/exynos: fix g2d_open/close helper function definitions
net: nsh: Use correct mac_offset to unwind gso skb in nsh_gso_segment()
net/tipc: fix tipc header files for kernel-doc
tipc: add tipc_bearer_min_mtu to calculate min mtu
tipc: do not update mtu if msg_max is too small in mtu negotiation
tipc: check the bearer min mtu properly when setting it by netlink
net: bcmgenet: Remove phy_stop() from bcmgenet_netif_stop()
net: bcmgenet: Restore phy_stop() depending upon suspend/close
wifi: mac80211: fix min center freq offset tracing
wifi: iwlwifi: mvm: don't trust firmware n_channels
scsi: storvsc: Don't pass unused PFNs to Hyper-V host
cassini: Fix a memory leak in the error handling path of cas_init_one()
igb: fix bit_shift to be in [1..8] range
vlan: fix a potential uninit-value in vlan_dev_hard_start_xmit()
netfilter: nft_set_rbtree: fix null deref on element insertion
bridge: always declare tunnel functions
ALSA: usb-audio: Add a sample rate workaround for Line6 Pod Go
USB: usbtmc: Fix direction for 0-length ioctl control messages
usb-storage: fix deadlock when a scsi command timeouts more than once
USB: UHCI: adjust zhaoxin UHCI controllers OverCurrent bit value
usb: dwc3: debugfs: Resume dwc3 before accessing registers
usb: gadget: u_ether: Fix host MAC address case
usb: typec: altmodes/displayport: fix pin_assignment_show
ALSA: hda: Fix Oops by 9.1 surround channel names
ALSA: hda: Add NVIDIA codec IDs a3 through a7 to patch table
ALSA: hda/realtek: Add quirk for Clevo L140AU
ALSA: hda/realtek: Add a quirk for HP EliteDesk 805
ALSA: hda/realtek: Add quirk for 2nd ASUS GU603
can: j1939: recvmsg(): allow MSG_CMSG_COMPAT flag
can: isotp: recvmsg(): allow MSG_CMSG_COMPAT flag
can: kvaser_pciefd: Set CAN_STATE_STOPPED in kvaser_pciefd_stop()
can: kvaser_pciefd: Call request_irq() before enabling interrupts
can: kvaser_pciefd: Empty SRB buffer in probe
can: kvaser_pciefd: Clear listen-only bit if not explicitly requested
can: kvaser_pciefd: Do not send EFLUSH command on TFD interrupt
can: kvaser_pciefd: Disable interrupts in probe error path
statfs: enforce statfs[64] structure initialization
serial: Add support for Advantech PCI-1611U card
vc_screen: reload load of struct vc_data pointer in vcs_write() to avoid UAF
ceph: force updating the msg pointer in non-split case
tpm/tpm_tis: Disable interrupts for more Lenovo devices
powerpc/64s/radix: Fix soft dirty tracking
nilfs2: fix use-after-free bug of nilfs_root in nilfs_evict_inode()
HID: wacom: Force pen out of prox if no events have been received in a while
HID: wacom: Add new Intuos Pro Small (PTH-460) device IDs
HID: wacom: add three styli to wacom_intuos_get_tool_type
KVM: arm64: Link position-independent string routines into .hyp.text
serial: 8250_exar: derive nr_ports from PCI ID for Acces I/O cards
serial: exar: Add support for Sealevel 7xxxC serial cards
serial: 8250_exar: Add support for USR298x PCI Modems
s390/qdio: get rid of register asm
s390/qdio: fix do_sqbs() inline assembly constraint
watchdog: sp5100_tco: Immediately trigger upon starting.
ARM: dts: stm32: fix AV96 board SAI2 pin muxing on stm32mp15
writeback, cgroup: remove extra percpu_ref_exit()
net/sched: act_mirred: refactor the handle of xmit
net/sched: act_mirred: better wording on protection against excessive stack growth
act_mirred: use the backlog for nested calls to mirred ingress
spi: fsl-spi: Re-organise transfer bits_per_word adaptation
spi: fsl-cpm: Use 16 bit mode for large transfers with even size
ocfs2: Switch to security_inode_init_security()
ALSA: hda/ca0132: add quirk for EVGA X299 DARK
ALSA: hda: Fix unhandled register update during auto-suspend period
ALSA: hda/realtek: Enable headset onLenovo M70/M90
net: cdc_ncm: Deal with too low values of dwNtbOutMaxSize
m68k: Move signal frame following exception on 68020/030
parisc: Handle kgdb breakpoints only in kernel context
parisc: Allow to reboot machine after system halt
gpio: mockup: Fix mode of debugfs files
btrfs: use nofs when cleaning up aborted transactions
dt-binding: cdns,usb3: Fix cdns,on-chip-buff-size type
x86/mm: Avoid incomplete Global INVLPG flushes
selftests/memfd: Fix unknown type name build failure
parisc: Fix flush_dcache_page() for usage from irq context
x86/topology: Fix erroneous smp_num_siblings on Intel Hybrid platforms
debugobjects: Don't wake up kswapd from fill_pool()
fbdev: udlfb: Fix endpoint check
net: fix stack overflow when LRO is disabled for virtual interfaces
udplite: Fix NULL pointer dereference in __sk_mem_raise_allocated().
USB: core: Add routines for endpoint checks in old drivers
USB: sisusbvga: Add endpoint checks
media: radio-shark: Add endpoint checks
net: fix skb leak in __skb_tstamp_tx()
selftests: fib_tests: mute cleanup error message
octeontx2-pf: Fix TSOv6 offload
bpf: Fix mask generation for 32-bit narrow loads of 64-bit fields
ipv6: Fix out-of-bounds access in ipv6_find_tlv()
power: supply: leds: Fix blink to LED on transition
power: supply: bq27xxx: Fix bq27xxx_battery_update() race condition
power: supply: bq27xxx: Fix I2C IRQ race on remove
power: supply: bq27xxx: Fix poll_interval handling and races on remove
power: supply: sbs-charger: Fix INHIBITED bit for Status reg
fs: fix undefined behavior in bit shift for SB_NOUSER
coresight: Fix signedness bug in tmc_etr_buf_insert_barrier_packet()
xen/pvcalls-back: fix double frees with pvcalls_new_active_socket()
x86/show_trace_log_lvl: Ensure stack pointer is aligned, again
ASoC: Intel: Skylake: Fix declaration of enum skl_ch_cfg
forcedeth: Fix an error handling path in nv_probe()
net/mlx5e: do as little as possible in napi poll when budget is 0
net/mlx5: DR, Fix crc32 calculation to work on big-endian (BE) CPUs
net/mlx5: Fix error message when failing to allocate device memory
net/mlx5: Devcom, fix error flow in mlx5_devcom_register_device
arm64: dts: imx8mn-var-som: fix PHY detection bug by adding deassert delay
3c589_cs: Fix an error handling path in tc589_probe()
net: phy: mscc: add VSC8502 to MODULE_DEVICE_TABLE
Linux 5.10.181
Change-Id: Iaad0b0bb7c1ad061b28ad4ee16e03db935241177
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
1221 lines
30 KiB
C
1221 lines
30 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* u_ether.c -- Ethernet-over-USB link layer utilities for Gadget stack
|
|
*
|
|
* Copyright (C) 2003-2005,2008 David Brownell
|
|
* Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
*/
|
|
|
|
/* #define VERBOSE_DEBUG */
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/gfp.h>
|
|
#include <linux/device.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/etherdevice.h>
|
|
#include <linux/ethtool.h>
|
|
#include <linux/if_vlan.h>
|
|
#include <linux/string_helpers.h>
|
|
|
|
#include "u_ether.h"
|
|
|
|
|
|
/*
|
|
* This component encapsulates the Ethernet link glue needed to provide
|
|
* one (!) network link through the USB gadget stack, normally "usb0".
|
|
*
|
|
* The control and data models are handled by the function driver which
|
|
* connects to this code; such as CDC Ethernet (ECM or EEM),
|
|
* "CDC Subset", or RNDIS. That includes all descriptor and endpoint
|
|
* management.
|
|
*
|
|
* Link level addressing is handled by this component using module
|
|
* parameters; if no such parameters are provided, random link level
|
|
* addresses are used. Each end of the link uses one address. The
|
|
* host end address is exported in various ways, and is often recorded
|
|
* in configuration databases.
|
|
*
|
|
* The driver which assembles each configuration using such a link is
|
|
* responsible for ensuring that each configuration includes at most one
|
|
* instance of is network link. (The network layer provides ways for
|
|
* this single "physical" link to be used by multiple virtual links.)
|
|
*/
|
|
|
|
#define UETH__VERSION "29-May-2008"
|
|
|
|
/* Experiments show that both Linux and Windows hosts allow up to 16k
|
|
* frame sizes. Set the max MTU size to 15k+52 to prevent allocating 32k
|
|
* blocks and still have efficient handling. */
|
|
#define GETHER_MAX_MTU_SIZE 15412
|
|
#define GETHER_MAX_ETH_FRAME_LEN (GETHER_MAX_MTU_SIZE + ETH_HLEN)
|
|
|
|
struct eth_dev {
|
|
/* lock is held while accessing port_usb
|
|
*/
|
|
spinlock_t lock;
|
|
struct gether *port_usb;
|
|
|
|
struct net_device *net;
|
|
struct usb_gadget *gadget;
|
|
|
|
spinlock_t req_lock; /* guard {rx,tx}_reqs */
|
|
struct list_head tx_reqs, rx_reqs;
|
|
atomic_t tx_qlen;
|
|
|
|
struct sk_buff_head rx_frames;
|
|
|
|
unsigned qmult;
|
|
|
|
unsigned header_len;
|
|
struct sk_buff *(*wrap)(struct gether *, struct sk_buff *skb);
|
|
int (*unwrap)(struct gether *,
|
|
struct sk_buff *skb,
|
|
struct sk_buff_head *list);
|
|
|
|
struct work_struct work;
|
|
|
|
unsigned long todo;
|
|
#define WORK_RX_MEMORY 0
|
|
|
|
bool zlp;
|
|
bool no_skb_reserve;
|
|
bool ifname_set;
|
|
u8 host_mac[ETH_ALEN];
|
|
u8 dev_mac[ETH_ALEN];
|
|
};
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
#define RX_EXTRA 20 /* bytes guarding against rx overflows */
|
|
|
|
#define DEFAULT_QLEN 2 /* double buffering by default */
|
|
|
|
/* for dual-speed hardware, use deeper queues at high/super speed */
|
|
static inline int qlen(struct usb_gadget *gadget, unsigned qmult)
|
|
{
|
|
if (gadget_is_dualspeed(gadget) && (gadget->speed == USB_SPEED_HIGH ||
|
|
gadget->speed >= USB_SPEED_SUPER))
|
|
return qmult * DEFAULT_QLEN;
|
|
else
|
|
return DEFAULT_QLEN;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* REVISIT there must be a better way than having two sets
|
|
* of debug calls ...
|
|
*/
|
|
|
|
#undef DBG
|
|
#undef VDBG
|
|
#undef ERROR
|
|
#undef INFO
|
|
|
|
#define xprintk(d, level, fmt, args...) \
|
|
printk(level "%s: " fmt , (d)->net->name , ## args)
|
|
|
|
#ifdef DEBUG
|
|
#undef DEBUG
|
|
#define DBG(dev, fmt, args...) \
|
|
xprintk(dev , KERN_DEBUG , fmt , ## args)
|
|
#else
|
|
#define DBG(dev, fmt, args...) \
|
|
do { } while (0)
|
|
#endif /* DEBUG */
|
|
|
|
#ifdef VERBOSE_DEBUG
|
|
#define VDBG DBG
|
|
#else
|
|
#define VDBG(dev, fmt, args...) \
|
|
do { } while (0)
|
|
#endif /* DEBUG */
|
|
|
|
#define ERROR(dev, fmt, args...) \
|
|
xprintk(dev , KERN_ERR , fmt , ## args)
|
|
#define INFO(dev, fmt, args...) \
|
|
xprintk(dev , KERN_INFO , fmt , ## args)
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
|
|
|
|
static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
|
|
strlcpy(p->driver, "g_ether", sizeof(p->driver));
|
|
strlcpy(p->version, UETH__VERSION, sizeof(p->version));
|
|
strlcpy(p->fw_version, dev->gadget->name, sizeof(p->fw_version));
|
|
strlcpy(p->bus_info, dev_name(&dev->gadget->dev), sizeof(p->bus_info));
|
|
}
|
|
|
|
/* REVISIT can also support:
|
|
* - WOL (by tracking suspends and issuing remote wakeup)
|
|
* - msglevel (implies updated messaging)
|
|
* - ... probably more ethtool ops
|
|
*/
|
|
|
|
static const struct ethtool_ops ops = {
|
|
.get_drvinfo = eth_get_drvinfo,
|
|
.get_link = ethtool_op_get_link,
|
|
};
|
|
|
|
static void defer_kevent(struct eth_dev *dev, int flag)
|
|
{
|
|
if (test_and_set_bit(flag, &dev->todo))
|
|
return;
|
|
if (!schedule_work(&dev->work))
|
|
ERROR(dev, "kevent %d may have been dropped\n", flag);
|
|
else
|
|
DBG(dev, "kevent %d scheduled\n", flag);
|
|
}
|
|
|
|
static void rx_complete(struct usb_ep *ep, struct usb_request *req);
|
|
|
|
static int
|
|
rx_submit(struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
|
|
{
|
|
struct usb_gadget *g = dev->gadget;
|
|
struct sk_buff *skb;
|
|
int retval = -ENOMEM;
|
|
size_t size = 0;
|
|
struct usb_ep *out;
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
if (dev->port_usb)
|
|
out = dev->port_usb->out_ep;
|
|
else
|
|
out = NULL;
|
|
|
|
if (!out)
|
|
{
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
return -ENOTCONN;
|
|
}
|
|
|
|
/* Padding up to RX_EXTRA handles minor disagreements with host.
|
|
* Normally we use the USB "terminate on short read" convention;
|
|
* so allow up to (N*maxpacket), since that memory is normally
|
|
* already allocated. Some hardware doesn't deal well with short
|
|
* reads (e.g. DMA must be N*maxpacket), so for now don't trim a
|
|
* byte off the end (to force hardware errors on overflow).
|
|
*
|
|
* RNDIS uses internal framing, and explicitly allows senders to
|
|
* pad to end-of-packet. That's potentially nice for speed, but
|
|
* means receivers can't recover lost synch on their own (because
|
|
* new packets don't only start after a short RX).
|
|
*/
|
|
size += sizeof(struct ethhdr) + dev->net->mtu + RX_EXTRA;
|
|
size += dev->port_usb->header_len;
|
|
|
|
if (g->quirk_ep_out_aligned_size) {
|
|
size += out->maxpacket - 1;
|
|
size -= size % out->maxpacket;
|
|
}
|
|
|
|
if (dev->port_usb->is_fixed)
|
|
size = max_t(size_t, size, dev->port_usb->fixed_out_len);
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
skb = __netdev_alloc_skb(dev->net, size + NET_IP_ALIGN, gfp_flags);
|
|
if (skb == NULL) {
|
|
DBG(dev, "no rx skb\n");
|
|
goto enomem;
|
|
}
|
|
|
|
/* Some platforms perform better when IP packets are aligned,
|
|
* but on at least one, checksumming fails otherwise. Note:
|
|
* RNDIS headers involve variable numbers of LE32 values.
|
|
*/
|
|
if (likely(!dev->no_skb_reserve))
|
|
skb_reserve(skb, NET_IP_ALIGN);
|
|
|
|
req->buf = skb->data;
|
|
req->length = size;
|
|
req->complete = rx_complete;
|
|
req->context = skb;
|
|
|
|
retval = usb_ep_queue(out, req, gfp_flags);
|
|
if (retval == -ENOMEM)
|
|
enomem:
|
|
defer_kevent(dev, WORK_RX_MEMORY);
|
|
if (retval) {
|
|
DBG(dev, "rx submit --> %d\n", retval);
|
|
if (skb)
|
|
dev_kfree_skb_any(skb);
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
static void rx_complete(struct usb_ep *ep, struct usb_request *req)
|
|
{
|
|
struct sk_buff *skb = req->context, *skb2;
|
|
struct eth_dev *dev = ep->driver_data;
|
|
int status = req->status;
|
|
|
|
switch (status) {
|
|
|
|
/* normal completion */
|
|
case 0:
|
|
skb_put(skb, req->actual);
|
|
|
|
if (dev->unwrap) {
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
if (dev->port_usb) {
|
|
status = dev->unwrap(dev->port_usb,
|
|
skb,
|
|
&dev->rx_frames);
|
|
} else {
|
|
dev_kfree_skb_any(skb);
|
|
status = -ENOTCONN;
|
|
}
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
} else {
|
|
skb_queue_tail(&dev->rx_frames, skb);
|
|
}
|
|
skb = NULL;
|
|
|
|
skb2 = skb_dequeue(&dev->rx_frames);
|
|
while (skb2) {
|
|
if (status < 0
|
|
|| ETH_HLEN > skb2->len
|
|
|| skb2->len > GETHER_MAX_ETH_FRAME_LEN) {
|
|
dev->net->stats.rx_errors++;
|
|
dev->net->stats.rx_length_errors++;
|
|
DBG(dev, "rx length %d\n", skb2->len);
|
|
dev_kfree_skb_any(skb2);
|
|
goto next_frame;
|
|
}
|
|
skb2->protocol = eth_type_trans(skb2, dev->net);
|
|
dev->net->stats.rx_packets++;
|
|
dev->net->stats.rx_bytes += skb2->len;
|
|
|
|
/* no buffer copies needed, unless hardware can't
|
|
* use skb buffers.
|
|
*/
|
|
status = netif_rx(skb2);
|
|
next_frame:
|
|
skb2 = skb_dequeue(&dev->rx_frames);
|
|
}
|
|
break;
|
|
|
|
/* software-driven interface shutdown */
|
|
case -ECONNRESET: /* unlink */
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
VDBG(dev, "rx shutdown, code %d\n", status);
|
|
goto quiesce;
|
|
|
|
/* for hardware automagic (such as pxa) */
|
|
case -ECONNABORTED: /* endpoint reset */
|
|
DBG(dev, "rx %s reset\n", ep->name);
|
|
defer_kevent(dev, WORK_RX_MEMORY);
|
|
quiesce:
|
|
dev_kfree_skb_any(skb);
|
|
goto clean;
|
|
|
|
/* data overrun */
|
|
case -EOVERFLOW:
|
|
dev->net->stats.rx_over_errors++;
|
|
fallthrough;
|
|
|
|
default:
|
|
dev->net->stats.rx_errors++;
|
|
DBG(dev, "rx status %d\n", status);
|
|
break;
|
|
}
|
|
|
|
if (skb)
|
|
dev_kfree_skb_any(skb);
|
|
if (!netif_running(dev->net)) {
|
|
clean:
|
|
spin_lock(&dev->req_lock);
|
|
list_add(&req->list, &dev->rx_reqs);
|
|
spin_unlock(&dev->req_lock);
|
|
req = NULL;
|
|
}
|
|
if (req)
|
|
rx_submit(dev, req, GFP_ATOMIC);
|
|
}
|
|
|
|
static int prealloc(struct list_head *list, struct usb_ep *ep, unsigned n)
|
|
{
|
|
unsigned i;
|
|
struct usb_request *req;
|
|
|
|
if (!n)
|
|
return -ENOMEM;
|
|
|
|
/* queue/recycle up to N requests */
|
|
i = n;
|
|
list_for_each_entry(req, list, list) {
|
|
if (i-- == 0)
|
|
goto extra;
|
|
}
|
|
while (i--) {
|
|
req = usb_ep_alloc_request(ep, GFP_ATOMIC);
|
|
if (!req)
|
|
return list_empty(list) ? -ENOMEM : 0;
|
|
list_add(&req->list, list);
|
|
}
|
|
return 0;
|
|
|
|
extra:
|
|
/* free extras */
|
|
for (;;) {
|
|
struct list_head *next;
|
|
|
|
next = req->list.next;
|
|
list_del(&req->list);
|
|
usb_ep_free_request(ep, req);
|
|
|
|
if (next == list)
|
|
break;
|
|
|
|
req = container_of(next, struct usb_request, list);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int alloc_requests(struct eth_dev *dev, struct gether *link, unsigned n)
|
|
{
|
|
int status;
|
|
|
|
spin_lock(&dev->req_lock);
|
|
status = prealloc(&dev->tx_reqs, link->in_ep, n);
|
|
if (status < 0)
|
|
goto fail;
|
|
status = prealloc(&dev->rx_reqs, link->out_ep, n);
|
|
if (status < 0)
|
|
goto fail;
|
|
goto done;
|
|
fail:
|
|
DBG(dev, "can't alloc requests\n");
|
|
done:
|
|
spin_unlock(&dev->req_lock);
|
|
return status;
|
|
}
|
|
|
|
static void rx_fill(struct eth_dev *dev, gfp_t gfp_flags)
|
|
{
|
|
struct usb_request *req;
|
|
unsigned long flags;
|
|
|
|
/* fill unused rxq slots with some skb */
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
while (!list_empty(&dev->rx_reqs)) {
|
|
req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
|
|
list_del_init(&req->list);
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
|
|
if (rx_submit(dev, req, gfp_flags) < 0) {
|
|
defer_kevent(dev, WORK_RX_MEMORY);
|
|
return;
|
|
}
|
|
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
}
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
}
|
|
|
|
static void eth_work(struct work_struct *work)
|
|
{
|
|
struct eth_dev *dev = container_of(work, struct eth_dev, work);
|
|
|
|
if (test_and_clear_bit(WORK_RX_MEMORY, &dev->todo)) {
|
|
if (netif_running(dev->net))
|
|
rx_fill(dev, GFP_KERNEL);
|
|
}
|
|
|
|
if (dev->todo)
|
|
DBG(dev, "work done, flags = 0x%lx\n", dev->todo);
|
|
}
|
|
|
|
static void tx_complete(struct usb_ep *ep, struct usb_request *req)
|
|
{
|
|
struct sk_buff *skb = req->context;
|
|
struct eth_dev *dev = ep->driver_data;
|
|
|
|
switch (req->status) {
|
|
default:
|
|
dev->net->stats.tx_errors++;
|
|
VDBG(dev, "tx err %d\n", req->status);
|
|
fallthrough;
|
|
case -ECONNRESET: /* unlink */
|
|
case -ESHUTDOWN: /* disconnect etc */
|
|
dev_kfree_skb_any(skb);
|
|
break;
|
|
case 0:
|
|
dev->net->stats.tx_bytes += skb->len;
|
|
dev_consume_skb_any(skb);
|
|
}
|
|
dev->net->stats.tx_packets++;
|
|
|
|
spin_lock(&dev->req_lock);
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
spin_unlock(&dev->req_lock);
|
|
|
|
atomic_dec(&dev->tx_qlen);
|
|
if (netif_carrier_ok(dev->net))
|
|
netif_wake_queue(dev->net);
|
|
}
|
|
|
|
static inline int is_promisc(u16 cdc_filter)
|
|
{
|
|
return cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
|
|
}
|
|
|
|
static netdev_tx_t eth_start_xmit(struct sk_buff *skb,
|
|
struct net_device *net)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
int length = 0;
|
|
int retval;
|
|
struct usb_request *req = NULL;
|
|
unsigned long flags;
|
|
struct usb_ep *in;
|
|
u16 cdc_filter;
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
if (dev->port_usb) {
|
|
in = dev->port_usb->in_ep;
|
|
cdc_filter = dev->port_usb->cdc_filter;
|
|
} else {
|
|
in = NULL;
|
|
cdc_filter = 0;
|
|
}
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
if (!in) {
|
|
if (skb)
|
|
dev_kfree_skb_any(skb);
|
|
return NETDEV_TX_OK;
|
|
}
|
|
|
|
/* apply outgoing CDC or RNDIS filters */
|
|
if (skb && !is_promisc(cdc_filter)) {
|
|
u8 *dest = skb->data;
|
|
|
|
if (is_multicast_ether_addr(dest)) {
|
|
u16 type;
|
|
|
|
/* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
|
|
* SET_ETHERNET_MULTICAST_FILTERS requests
|
|
*/
|
|
if (is_broadcast_ether_addr(dest))
|
|
type = USB_CDC_PACKET_TYPE_BROADCAST;
|
|
else
|
|
type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
|
|
if (!(cdc_filter & type)) {
|
|
dev_kfree_skb_any(skb);
|
|
return NETDEV_TX_OK;
|
|
}
|
|
}
|
|
/* ignores USB_CDC_PACKET_TYPE_DIRECTED */
|
|
}
|
|
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
/*
|
|
* this freelist can be empty if an interrupt triggered disconnect()
|
|
* and reconfigured the gadget (shutting down this queue) after the
|
|
* network stack decided to xmit but before we got the spinlock.
|
|
*/
|
|
if (list_empty(&dev->tx_reqs)) {
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
return NETDEV_TX_BUSY;
|
|
}
|
|
|
|
req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
|
|
list_del(&req->list);
|
|
|
|
/* temporarily stop TX queue when the freelist empties */
|
|
if (list_empty(&dev->tx_reqs))
|
|
netif_stop_queue(net);
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
|
|
/* no buffer copies needed, unless the network stack did it
|
|
* or the hardware can't use skb buffers.
|
|
* or there's not enough space for extra headers we need
|
|
*/
|
|
if (dev->wrap) {
|
|
unsigned long flags;
|
|
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
if (dev->port_usb)
|
|
skb = dev->wrap(dev->port_usb, skb);
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
if (!skb) {
|
|
/* Multi frame CDC protocols may store the frame for
|
|
* later which is not a dropped frame.
|
|
*/
|
|
if (dev->port_usb &&
|
|
dev->port_usb->supports_multi_frame)
|
|
goto multiframe;
|
|
goto drop;
|
|
}
|
|
}
|
|
|
|
length = skb->len;
|
|
req->buf = skb->data;
|
|
req->context = skb;
|
|
req->complete = tx_complete;
|
|
|
|
/* NCM requires no zlp if transfer is dwNtbInMaxSize */
|
|
if (dev->port_usb &&
|
|
dev->port_usb->is_fixed &&
|
|
length == dev->port_usb->fixed_in_len &&
|
|
(length % in->maxpacket) == 0)
|
|
req->zero = 0;
|
|
else
|
|
req->zero = 1;
|
|
|
|
/* use zlp framing on tx for strict CDC-Ether conformance,
|
|
* though any robust network rx path ignores extra padding.
|
|
* and some hardware doesn't like to write zlps.
|
|
*/
|
|
if (req->zero && !dev->zlp && (length % in->maxpacket) == 0)
|
|
length++;
|
|
|
|
req->length = length;
|
|
|
|
retval = usb_ep_queue(in, req, GFP_ATOMIC);
|
|
switch (retval) {
|
|
default:
|
|
DBG(dev, "tx queue err %d\n", retval);
|
|
break;
|
|
case 0:
|
|
netif_trans_update(net);
|
|
atomic_inc(&dev->tx_qlen);
|
|
}
|
|
|
|
if (retval) {
|
|
dev_kfree_skb_any(skb);
|
|
drop:
|
|
dev->net->stats.tx_dropped++;
|
|
multiframe:
|
|
spin_lock_irqsave(&dev->req_lock, flags);
|
|
if (list_empty(&dev->tx_reqs))
|
|
netif_start_queue(net);
|
|
list_add(&req->list, &dev->tx_reqs);
|
|
spin_unlock_irqrestore(&dev->req_lock, flags);
|
|
}
|
|
return NETDEV_TX_OK;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
|
|
{
|
|
DBG(dev, "%s\n", __func__);
|
|
|
|
/* fill the rx queue */
|
|
rx_fill(dev, gfp_flags);
|
|
|
|
/* and open the tx floodgates */
|
|
atomic_set(&dev->tx_qlen, 0);
|
|
netif_wake_queue(dev->net);
|
|
}
|
|
|
|
static int eth_open(struct net_device *net)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
struct gether *link;
|
|
|
|
DBG(dev, "%s\n", __func__);
|
|
if (netif_carrier_ok(dev->net))
|
|
eth_start(dev, GFP_KERNEL);
|
|
|
|
spin_lock_irq(&dev->lock);
|
|
link = dev->port_usb;
|
|
if (link && link->open)
|
|
link->open(link);
|
|
spin_unlock_irq(&dev->lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int eth_stop(struct net_device *net)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
unsigned long flags;
|
|
|
|
VDBG(dev, "%s\n", __func__);
|
|
netif_stop_queue(net);
|
|
|
|
DBG(dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
|
|
dev->net->stats.rx_packets, dev->net->stats.tx_packets,
|
|
dev->net->stats.rx_errors, dev->net->stats.tx_errors
|
|
);
|
|
|
|
/* ensure there are no more active requests */
|
|
spin_lock_irqsave(&dev->lock, flags);
|
|
if (dev->port_usb) {
|
|
struct gether *link = dev->port_usb;
|
|
const struct usb_endpoint_descriptor *in;
|
|
const struct usb_endpoint_descriptor *out;
|
|
|
|
if (link->close)
|
|
link->close(link);
|
|
|
|
/* NOTE: we have no abort-queue primitive we could use
|
|
* to cancel all pending I/O. Instead, we disable then
|
|
* reenable the endpoints ... this idiom may leave toggle
|
|
* wrong, but that's a self-correcting error.
|
|
*
|
|
* REVISIT: we *COULD* just let the transfers complete at
|
|
* their own pace; the network stack can handle old packets.
|
|
* For the moment we leave this here, since it works.
|
|
*/
|
|
in = link->in_ep->desc;
|
|
out = link->out_ep->desc;
|
|
usb_ep_disable(link->in_ep);
|
|
usb_ep_disable(link->out_ep);
|
|
if (netif_carrier_ok(net)) {
|
|
DBG(dev, "host still using in/out endpoints\n");
|
|
link->in_ep->desc = in;
|
|
link->out_ep->desc = out;
|
|
usb_ep_enable(link->in_ep);
|
|
usb_ep_enable(link->out_ep);
|
|
}
|
|
}
|
|
spin_unlock_irqrestore(&dev->lock, flags);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
static int get_ether_addr(const char *str, u8 *dev_addr)
|
|
{
|
|
if (str) {
|
|
unsigned i;
|
|
|
|
for (i = 0; i < 6; i++) {
|
|
unsigned char num;
|
|
|
|
if ((*str == '.') || (*str == ':'))
|
|
str++;
|
|
num = hex_to_bin(*str++) << 4;
|
|
num |= hex_to_bin(*str++);
|
|
dev_addr [i] = num;
|
|
}
|
|
if (is_valid_ether_addr(dev_addr))
|
|
return 0;
|
|
}
|
|
eth_random_addr(dev_addr);
|
|
return 1;
|
|
}
|
|
|
|
static int get_ether_addr_str(u8 dev_addr[ETH_ALEN], char *str, int len)
|
|
{
|
|
if (len < 18)
|
|
return -EINVAL;
|
|
|
|
snprintf(str, len, "%pM", dev_addr);
|
|
return 18;
|
|
}
|
|
|
|
static const struct net_device_ops eth_netdev_ops = {
|
|
.ndo_open = eth_open,
|
|
.ndo_stop = eth_stop,
|
|
.ndo_start_xmit = eth_start_xmit,
|
|
.ndo_set_mac_address = eth_mac_addr,
|
|
.ndo_validate_addr = eth_validate_addr,
|
|
};
|
|
|
|
static struct device_type gadget_type = {
|
|
.name = "gadget",
|
|
};
|
|
|
|
/*
|
|
* gether_setup_name - initialize one ethernet-over-usb link
|
|
* @g: gadget to associated with these links
|
|
* @ethaddr: NULL, or a buffer in which the ethernet address of the
|
|
* host side of the link is recorded
|
|
* @netname: name for network device (for example, "usb")
|
|
* Context: may sleep
|
|
*
|
|
* This sets up the single network link that may be exported by a
|
|
* gadget driver using this framework. The link layer addresses are
|
|
* set up using module parameters.
|
|
*
|
|
* Returns an eth_dev pointer on success, or an ERR_PTR on failure.
|
|
*/
|
|
struct eth_dev *gether_setup_name(struct usb_gadget *g,
|
|
const char *dev_addr, const char *host_addr,
|
|
u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname)
|
|
{
|
|
struct eth_dev *dev;
|
|
struct net_device *net;
|
|
int status;
|
|
|
|
net = alloc_etherdev(sizeof *dev);
|
|
if (!net)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
dev = netdev_priv(net);
|
|
spin_lock_init(&dev->lock);
|
|
spin_lock_init(&dev->req_lock);
|
|
INIT_WORK(&dev->work, eth_work);
|
|
INIT_LIST_HEAD(&dev->tx_reqs);
|
|
INIT_LIST_HEAD(&dev->rx_reqs);
|
|
|
|
skb_queue_head_init(&dev->rx_frames);
|
|
|
|
/* network device setup */
|
|
dev->net = net;
|
|
dev->qmult = qmult;
|
|
snprintf(net->name, sizeof(net->name), "%s%%d", netname);
|
|
|
|
if (get_ether_addr(dev_addr, net->dev_addr)) {
|
|
net->addr_assign_type = NET_ADDR_RANDOM;
|
|
dev_warn(&g->dev,
|
|
"using random %s ethernet address\n", "self");
|
|
} else {
|
|
net->addr_assign_type = NET_ADDR_SET;
|
|
}
|
|
if (get_ether_addr(host_addr, dev->host_mac))
|
|
dev_warn(&g->dev,
|
|
"using random %s ethernet address\n", "host");
|
|
|
|
if (ethaddr)
|
|
memcpy(ethaddr, dev->host_mac, ETH_ALEN);
|
|
|
|
net->netdev_ops = ð_netdev_ops;
|
|
|
|
net->ethtool_ops = &ops;
|
|
|
|
/* MTU range: 14 - 15412 */
|
|
net->min_mtu = ETH_HLEN;
|
|
net->max_mtu = GETHER_MAX_MTU_SIZE;
|
|
|
|
dev->gadget = g;
|
|
SET_NETDEV_DEV(net, &g->dev);
|
|
SET_NETDEV_DEVTYPE(net, &gadget_type);
|
|
|
|
status = register_netdev(net);
|
|
if (status < 0) {
|
|
dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
|
|
free_netdev(net);
|
|
dev = ERR_PTR(status);
|
|
} else {
|
|
INFO(dev, "MAC %pM\n", net->dev_addr);
|
|
INFO(dev, "HOST MAC %pM\n", dev->host_mac);
|
|
|
|
/*
|
|
* two kinds of host-initiated state changes:
|
|
* - iff DATA transfer is active, carrier is "on"
|
|
* - tx queueing enabled if open *and* carrier is "on"
|
|
*/
|
|
netif_carrier_off(net);
|
|
}
|
|
|
|
return dev;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_setup_name);
|
|
|
|
struct net_device *gether_setup_name_default(const char *netname)
|
|
{
|
|
struct net_device *net;
|
|
struct eth_dev *dev;
|
|
|
|
net = alloc_etherdev(sizeof(*dev));
|
|
if (!net)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
dev = netdev_priv(net);
|
|
spin_lock_init(&dev->lock);
|
|
spin_lock_init(&dev->req_lock);
|
|
INIT_WORK(&dev->work, eth_work);
|
|
INIT_LIST_HEAD(&dev->tx_reqs);
|
|
INIT_LIST_HEAD(&dev->rx_reqs);
|
|
|
|
/* by default we always have a random MAC address */
|
|
net->addr_assign_type = NET_ADDR_RANDOM;
|
|
|
|
skb_queue_head_init(&dev->rx_frames);
|
|
|
|
/* network device setup */
|
|
dev->net = net;
|
|
dev->qmult = QMULT_DEFAULT;
|
|
snprintf(net->name, sizeof(net->name), "%s%%d", netname);
|
|
|
|
eth_random_addr(dev->dev_mac);
|
|
pr_warn("using random %s ethernet address\n", "self");
|
|
eth_random_addr(dev->host_mac);
|
|
pr_warn("using random %s ethernet address\n", "host");
|
|
|
|
net->netdev_ops = ð_netdev_ops;
|
|
|
|
net->ethtool_ops = &ops;
|
|
SET_NETDEV_DEVTYPE(net, &gadget_type);
|
|
|
|
/* MTU range: 14 - 15412 */
|
|
net->min_mtu = ETH_HLEN;
|
|
net->max_mtu = GETHER_MAX_MTU_SIZE;
|
|
|
|
return net;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_setup_name_default);
|
|
|
|
int gether_register_netdev(struct net_device *net)
|
|
{
|
|
struct eth_dev *dev;
|
|
struct usb_gadget *g;
|
|
int status;
|
|
|
|
if (!net->dev.parent)
|
|
return -EINVAL;
|
|
dev = netdev_priv(net);
|
|
g = dev->gadget;
|
|
|
|
memcpy(net->dev_addr, dev->dev_mac, ETH_ALEN);
|
|
|
|
status = register_netdev(net);
|
|
if (status < 0) {
|
|
dev_dbg(&g->dev, "register_netdev failed, %d\n", status);
|
|
return status;
|
|
} else {
|
|
INFO(dev, "HOST MAC %pM\n", dev->host_mac);
|
|
INFO(dev, "MAC %pM\n", dev->dev_mac);
|
|
|
|
/* two kinds of host-initiated state changes:
|
|
* - iff DATA transfer is active, carrier is "on"
|
|
* - tx queueing enabled if open *and* carrier is "on"
|
|
*/
|
|
netif_carrier_off(net);
|
|
}
|
|
|
|
return status;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_register_netdev);
|
|
|
|
void gether_set_gadget(struct net_device *net, struct usb_gadget *g)
|
|
{
|
|
struct eth_dev *dev;
|
|
|
|
dev = netdev_priv(net);
|
|
dev->gadget = g;
|
|
SET_NETDEV_DEV(net, &g->dev);
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_set_gadget);
|
|
|
|
int gether_set_dev_addr(struct net_device *net, const char *dev_addr)
|
|
{
|
|
struct eth_dev *dev;
|
|
u8 new_addr[ETH_ALEN];
|
|
|
|
dev = netdev_priv(net);
|
|
if (get_ether_addr(dev_addr, new_addr))
|
|
return -EINVAL;
|
|
memcpy(dev->dev_mac, new_addr, ETH_ALEN);
|
|
net->addr_assign_type = NET_ADDR_SET;
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_set_dev_addr);
|
|
|
|
int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len)
|
|
{
|
|
struct eth_dev *dev;
|
|
int ret;
|
|
|
|
dev = netdev_priv(net);
|
|
ret = get_ether_addr_str(dev->dev_mac, dev_addr, len);
|
|
if (ret + 1 < len) {
|
|
dev_addr[ret++] = '\n';
|
|
dev_addr[ret] = '\0';
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_dev_addr);
|
|
|
|
int gether_set_host_addr(struct net_device *net, const char *host_addr)
|
|
{
|
|
struct eth_dev *dev;
|
|
u8 new_addr[ETH_ALEN];
|
|
|
|
dev = netdev_priv(net);
|
|
if (get_ether_addr(host_addr, new_addr))
|
|
return -EINVAL;
|
|
memcpy(dev->host_mac, new_addr, ETH_ALEN);
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_set_host_addr);
|
|
|
|
int gether_get_host_addr(struct net_device *net, char *host_addr, int len)
|
|
{
|
|
struct eth_dev *dev;
|
|
int ret;
|
|
|
|
dev = netdev_priv(net);
|
|
ret = get_ether_addr_str(dev->host_mac, host_addr, len);
|
|
if (ret + 1 < len) {
|
|
host_addr[ret++] = '\n';
|
|
host_addr[ret] = '\0';
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_host_addr);
|
|
|
|
int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len)
|
|
{
|
|
struct eth_dev *dev;
|
|
|
|
if (len < 13)
|
|
return -EINVAL;
|
|
|
|
dev = netdev_priv(net);
|
|
snprintf(host_addr, len, "%pm", dev->host_mac);
|
|
|
|
string_upper(host_addr, host_addr);
|
|
|
|
return strlen(host_addr);
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_host_addr_cdc);
|
|
|
|
void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN])
|
|
{
|
|
struct eth_dev *dev;
|
|
|
|
dev = netdev_priv(net);
|
|
memcpy(host_mac, dev->host_mac, ETH_ALEN);
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_host_addr_u8);
|
|
|
|
void gether_set_qmult(struct net_device *net, unsigned qmult)
|
|
{
|
|
struct eth_dev *dev;
|
|
|
|
dev = netdev_priv(net);
|
|
dev->qmult = qmult;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_set_qmult);
|
|
|
|
unsigned gether_get_qmult(struct net_device *net)
|
|
{
|
|
struct eth_dev *dev;
|
|
|
|
dev = netdev_priv(net);
|
|
return dev->qmult;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_qmult);
|
|
|
|
int gether_get_ifname(struct net_device *net, char *name, int len)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
int ret;
|
|
|
|
rtnl_lock();
|
|
ret = scnprintf(name, len, "%s\n",
|
|
dev->ifname_set ? net->name : netdev_name(net));
|
|
rtnl_unlock();
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_get_ifname);
|
|
|
|
int gether_set_ifname(struct net_device *net, const char *name, int len)
|
|
{
|
|
struct eth_dev *dev = netdev_priv(net);
|
|
char tmp[IFNAMSIZ];
|
|
const char *p;
|
|
|
|
if (name[len - 1] == '\n')
|
|
len--;
|
|
|
|
if (len >= sizeof(tmp))
|
|
return -E2BIG;
|
|
|
|
strscpy(tmp, name, len + 1);
|
|
if (!dev_valid_name(tmp))
|
|
return -EINVAL;
|
|
|
|
/* Require exactly one %d, so binding will not fail with EEXIST. */
|
|
p = strchr(name, '%');
|
|
if (!p || p[1] != 'd' || strchr(p + 2, '%'))
|
|
return -EINVAL;
|
|
|
|
strncpy(net->name, tmp, sizeof(net->name));
|
|
dev->ifname_set = true;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_set_ifname);
|
|
|
|
/*
|
|
* gether_cleanup - remove Ethernet-over-USB device
|
|
* Context: may sleep
|
|
*
|
|
* This is called to free all resources allocated by @gether_setup().
|
|
*/
|
|
void gether_cleanup(struct eth_dev *dev)
|
|
{
|
|
if (!dev)
|
|
return;
|
|
|
|
unregister_netdev(dev->net);
|
|
flush_work(&dev->work);
|
|
free_netdev(dev->net);
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_cleanup);
|
|
|
|
/**
|
|
* gether_connect - notify network layer that USB link is active
|
|
* @link: the USB link, set up with endpoints, descriptors matching
|
|
* current device speed, and any framing wrapper(s) set up.
|
|
* Context: irqs blocked
|
|
*
|
|
* This is called to activate endpoints and let the network layer know
|
|
* the connection is active ("carrier detect"). It may cause the I/O
|
|
* queues to open and start letting network packets flow, but will in
|
|
* any case activate the endpoints so that they respond properly to the
|
|
* USB host.
|
|
*
|
|
* Verify net_device pointer returned using IS_ERR(). If it doesn't
|
|
* indicate some error code (negative errno), ep->driver_data values
|
|
* have been overwritten.
|
|
*/
|
|
struct net_device *gether_connect(struct gether *link)
|
|
{
|
|
struct eth_dev *dev = link->ioport;
|
|
int result = 0;
|
|
|
|
if (!dev)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
link->in_ep->driver_data = dev;
|
|
result = usb_ep_enable(link->in_ep);
|
|
if (result != 0) {
|
|
DBG(dev, "enable %s --> %d\n",
|
|
link->in_ep->name, result);
|
|
goto fail0;
|
|
}
|
|
|
|
link->out_ep->driver_data = dev;
|
|
result = usb_ep_enable(link->out_ep);
|
|
if (result != 0) {
|
|
DBG(dev, "enable %s --> %d\n",
|
|
link->out_ep->name, result);
|
|
goto fail1;
|
|
}
|
|
|
|
if (result == 0)
|
|
result = alloc_requests(dev, link, qlen(dev->gadget,
|
|
dev->qmult));
|
|
|
|
if (result == 0) {
|
|
dev->zlp = link->is_zlp_ok;
|
|
dev->no_skb_reserve = gadget_avoids_skb_reserve(dev->gadget);
|
|
DBG(dev, "qlen %d\n", qlen(dev->gadget, dev->qmult));
|
|
|
|
dev->header_len = link->header_len;
|
|
dev->unwrap = link->unwrap;
|
|
dev->wrap = link->wrap;
|
|
|
|
spin_lock(&dev->lock);
|
|
dev->port_usb = link;
|
|
if (netif_running(dev->net)) {
|
|
if (link->open)
|
|
link->open(link);
|
|
} else {
|
|
if (link->close)
|
|
link->close(link);
|
|
}
|
|
spin_unlock(&dev->lock);
|
|
|
|
netif_carrier_on(dev->net);
|
|
if (netif_running(dev->net))
|
|
eth_start(dev, GFP_ATOMIC);
|
|
|
|
/* on error, disable any endpoints */
|
|
} else {
|
|
(void) usb_ep_disable(link->out_ep);
|
|
fail1:
|
|
(void) usb_ep_disable(link->in_ep);
|
|
}
|
|
fail0:
|
|
/* caller is responsible for cleanup on error */
|
|
if (result < 0)
|
|
return ERR_PTR(result);
|
|
return dev->net;
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_connect);
|
|
|
|
/**
|
|
* gether_disconnect - notify network layer that USB link is inactive
|
|
* @link: the USB link, on which gether_connect() was called
|
|
* Context: irqs blocked
|
|
*
|
|
* This is called to deactivate endpoints and let the network layer know
|
|
* the connection went inactive ("no carrier").
|
|
*
|
|
* On return, the state is as if gether_connect() had never been called.
|
|
* The endpoints are inactive, and accordingly without active USB I/O.
|
|
* Pointers to endpoint descriptors and endpoint private data are nulled.
|
|
*/
|
|
void gether_disconnect(struct gether *link)
|
|
{
|
|
struct eth_dev *dev = link->ioport;
|
|
struct usb_request *req;
|
|
|
|
WARN_ON(!dev);
|
|
if (!dev)
|
|
return;
|
|
|
|
DBG(dev, "%s\n", __func__);
|
|
|
|
netif_stop_queue(dev->net);
|
|
netif_carrier_off(dev->net);
|
|
|
|
/* disable endpoints, forcing (synchronous) completion
|
|
* of all pending i/o. then free the request objects
|
|
* and forget about the endpoints.
|
|
*/
|
|
usb_ep_disable(link->in_ep);
|
|
spin_lock(&dev->req_lock);
|
|
while (!list_empty(&dev->tx_reqs)) {
|
|
req = list_first_entry(&dev->tx_reqs, struct usb_request, list);
|
|
list_del(&req->list);
|
|
|
|
spin_unlock(&dev->req_lock);
|
|
usb_ep_free_request(link->in_ep, req);
|
|
spin_lock(&dev->req_lock);
|
|
}
|
|
spin_unlock(&dev->req_lock);
|
|
link->in_ep->desc = NULL;
|
|
|
|
usb_ep_disable(link->out_ep);
|
|
spin_lock(&dev->req_lock);
|
|
while (!list_empty(&dev->rx_reqs)) {
|
|
req = list_first_entry(&dev->rx_reqs, struct usb_request, list);
|
|
list_del(&req->list);
|
|
|
|
spin_unlock(&dev->req_lock);
|
|
usb_ep_free_request(link->out_ep, req);
|
|
spin_lock(&dev->req_lock);
|
|
}
|
|
spin_unlock(&dev->req_lock);
|
|
link->out_ep->desc = NULL;
|
|
|
|
/* finish forgetting about this USB link episode */
|
|
dev->header_len = 0;
|
|
dev->unwrap = NULL;
|
|
dev->wrap = NULL;
|
|
|
|
spin_lock(&dev->lock);
|
|
dev->port_usb = NULL;
|
|
spin_unlock(&dev->lock);
|
|
}
|
|
EXPORT_SYMBOL_GPL(gether_disconnect);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("David Brownell");
|