2009-07-21 08:58:35 +07:00
|
|
|
|
/*
|
|
|
|
|
* phonet.c -- USB CDC Phonet host driver
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Author: Rémi Denis-Courmont
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* version 2 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2011-06-16 18:01:34 +07:00
|
|
|
|
#include <linux/mm.h>
|
2009-07-21 08:58:35 +07:00
|
|
|
|
#include <linux/module.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
|
#include <linux/gfp.h>
|
2009-07-21 08:58:35 +07:00
|
|
|
|
#include <linux/usb.h>
|
|
|
|
|
#include <linux/usb/cdc.h>
|
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
|
#include <linux/if_phonet.h>
|
2009-09-09 07:00:06 +07:00
|
|
|
|
#include <linux/phonet.h>
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
|
|
|
|
#define PN_MEDIA_USB 0x1B
|
|
|
|
|
|
|
|
|
|
static const unsigned rxq_size = 17;
|
|
|
|
|
|
|
|
|
|
struct usbpn_dev {
|
|
|
|
|
struct net_device *dev;
|
|
|
|
|
|
|
|
|
|
struct usb_interface *intf, *data_intf;
|
|
|
|
|
struct usb_device *usb;
|
|
|
|
|
unsigned int tx_pipe, rx_pipe;
|
|
|
|
|
u8 active_setting;
|
|
|
|
|
u8 disconnected;
|
|
|
|
|
|
|
|
|
|
unsigned tx_queue;
|
|
|
|
|
spinlock_t tx_lock;
|
|
|
|
|
|
|
|
|
|
spinlock_t rx_lock;
|
|
|
|
|
struct sk_buff *rx_skb;
|
|
|
|
|
struct urb *urbs[0];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void tx_complete(struct urb *req);
|
|
|
|
|
static void rx_complete(struct urb *req);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Network device callbacks
|
|
|
|
|
*/
|
2009-09-01 02:50:45 +07:00
|
|
|
|
static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
|
2009-07-21 08:58:35 +07:00
|
|
|
|
{
|
|
|
|
|
struct usbpn_dev *pnd = netdev_priv(dev);
|
|
|
|
|
struct urb *req = NULL;
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (skb->protocol != htons(ETH_P_PHONET))
|
|
|
|
|
goto drop;
|
|
|
|
|
|
|
|
|
|
req = usb_alloc_urb(0, GFP_ATOMIC);
|
|
|
|
|
if (!req)
|
|
|
|
|
goto drop;
|
|
|
|
|
usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
|
|
|
|
|
tx_complete, skb);
|
|
|
|
|
req->transfer_flags = URB_ZERO_PACKET;
|
|
|
|
|
err = usb_submit_urb(req, GFP_ATOMIC);
|
|
|
|
|
if (err) {
|
|
|
|
|
usb_free_urb(req);
|
|
|
|
|
goto drop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pnd->tx_lock, flags);
|
|
|
|
|
pnd->tx_queue++;
|
|
|
|
|
if (pnd->tx_queue >= dev->tx_queue_len)
|
|
|
|
|
netif_stop_queue(dev);
|
|
|
|
|
spin_unlock_irqrestore(&pnd->tx_lock, flags);
|
2009-09-01 02:50:45 +07:00
|
|
|
|
return NETDEV_TX_OK;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
|
|
|
|
drop:
|
|
|
|
|
dev_kfree_skb(skb);
|
|
|
|
|
dev->stats.tx_dropped++;
|
2009-09-01 02:50:45 +07:00
|
|
|
|
return NETDEV_TX_OK;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void tx_complete(struct urb *req)
|
|
|
|
|
{
|
|
|
|
|
struct sk_buff *skb = req->context;
|
|
|
|
|
struct net_device *dev = skb->dev;
|
|
|
|
|
struct usbpn_dev *pnd = netdev_priv(dev);
|
2010-07-02 20:51:55 +07:00
|
|
|
|
int status = req->status;
|
2018-06-21 02:31:17 +07:00
|
|
|
|
unsigned long flags;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
2010-07-02 20:51:55 +07:00
|
|
|
|
switch (status) {
|
2009-07-21 08:58:35 +07:00
|
|
|
|
case 0:
|
|
|
|
|
dev->stats.tx_bytes += skb->len;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -ENOENT:
|
|
|
|
|
case -ECONNRESET:
|
|
|
|
|
case -ESHUTDOWN:
|
|
|
|
|
dev->stats.tx_aborted_errors++;
|
2018-06-29 01:50:48 +07:00
|
|
|
|
/* fall through */
|
2009-07-21 08:58:35 +07:00
|
|
|
|
default:
|
|
|
|
|
dev->stats.tx_errors++;
|
2010-07-02 20:51:55 +07:00
|
|
|
|
dev_dbg(&dev->dev, "TX error (%d)\n", status);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
}
|
|
|
|
|
dev->stats.tx_packets++;
|
|
|
|
|
|
2018-06-21 02:31:17 +07:00
|
|
|
|
spin_lock_irqsave(&pnd->tx_lock, flags);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
pnd->tx_queue--;
|
|
|
|
|
netif_wake_queue(dev);
|
2018-06-21 02:31:17 +07:00
|
|
|
|
spin_unlock_irqrestore(&pnd->tx_lock, flags);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
|
|
|
|
dev_kfree_skb_any(skb);
|
|
|
|
|
usb_free_urb(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
|
|
|
|
|
{
|
|
|
|
|
struct net_device *dev = pnd->dev;
|
|
|
|
|
struct page *page;
|
|
|
|
|
int err;
|
|
|
|
|
|
2014-11-12 00:26:50 +07:00
|
|
|
|
page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
if (!page)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
|
|
|
|
|
PAGE_SIZE, rx_complete, dev);
|
|
|
|
|
req->transfer_flags = 0;
|
|
|
|
|
err = usb_submit_urb(req, gfp_flags);
|
|
|
|
|
if (unlikely(err)) {
|
|
|
|
|
dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
|
2011-11-22 17:57:41 +07:00
|
|
|
|
put_page(page);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
}
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rx_complete(struct urb *req)
|
|
|
|
|
{
|
|
|
|
|
struct net_device *dev = req->context;
|
|
|
|
|
struct usbpn_dev *pnd = netdev_priv(dev);
|
|
|
|
|
struct page *page = virt_to_page(req->transfer_buffer);
|
|
|
|
|
struct sk_buff *skb;
|
|
|
|
|
unsigned long flags;
|
2010-07-02 20:51:55 +07:00
|
|
|
|
int status = req->status;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
2010-07-02 20:51:55 +07:00
|
|
|
|
switch (status) {
|
2009-07-21 08:58:35 +07:00
|
|
|
|
case 0:
|
|
|
|
|
spin_lock_irqsave(&pnd->rx_lock, flags);
|
|
|
|
|
skb = pnd->rx_skb;
|
|
|
|
|
if (!skb) {
|
|
|
|
|
skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
|
|
|
|
|
if (likely(skb)) {
|
|
|
|
|
/* Can't use pskb_pull() on page in IRQ */
|
networking: introduce and use skb_put_data()
A common pattern with skb_put() is to just want to memcpy()
some data into the new space, introduce skb_put_data() for
this.
An spatch similar to the one for skb_put_zero() converts many
of the places using it:
@@
identifier p, p2;
expression len, skb, data;
type t, t2;
@@
(
-p = skb_put(skb, len);
+p = skb_put_data(skb, data, len);
|
-p = (t)skb_put(skb, len);
+p = skb_put_data(skb, data, len);
)
(
p2 = (t2)p;
-memcpy(p2, data, len);
|
-memcpy(p, data, len);
)
@@
type t, t2;
identifier p, p2;
expression skb, data;
@@
t *p;
...
(
-p = skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
|
-p = (t *)skb_put(skb, sizeof(t));
+p = skb_put_data(skb, data, sizeof(t));
)
(
p2 = (t2)p;
-memcpy(p2, data, sizeof(*p));
|
-memcpy(p, data, sizeof(*p));
)
@@
expression skb, len, data;
@@
-memcpy(skb_put(skb, len), data, len);
+skb_put_data(skb, data, len);
(again, manually post-processed to retain some comments)
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-16 19:29:20 +07:00
|
|
|
|
skb_put_data(skb, page_address(page), 1);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
|
2012-03-24 06:59:33 +07:00
|
|
|
|
page, 1, req->actual_length,
|
2012-03-27 10:17:26 +07:00
|
|
|
|
PAGE_SIZE);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
page = NULL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
|
2012-03-24 06:59:33 +07:00
|
|
|
|
page, 0, req->actual_length,
|
2012-03-27 10:17:26 +07:00
|
|
|
|
PAGE_SIZE);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
page = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (req->actual_length < PAGE_SIZE)
|
|
|
|
|
pnd->rx_skb = NULL; /* Last fragment */
|
|
|
|
|
else
|
|
|
|
|
skb = NULL;
|
|
|
|
|
spin_unlock_irqrestore(&pnd->rx_lock, flags);
|
|
|
|
|
if (skb) {
|
|
|
|
|
skb->protocol = htons(ETH_P_PHONET);
|
|
|
|
|
skb_reset_mac_header(skb);
|
|
|
|
|
__skb_pull(skb, 1);
|
|
|
|
|
skb->dev = dev;
|
|
|
|
|
dev->stats.rx_packets++;
|
|
|
|
|
dev->stats.rx_bytes += skb->len;
|
|
|
|
|
|
|
|
|
|
netif_rx(skb);
|
|
|
|
|
}
|
|
|
|
|
goto resubmit;
|
|
|
|
|
|
|
|
|
|
case -ENOENT:
|
|
|
|
|
case -ECONNRESET:
|
|
|
|
|
case -ESHUTDOWN:
|
|
|
|
|
req = NULL;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -EOVERFLOW:
|
|
|
|
|
dev->stats.rx_over_errors++;
|
|
|
|
|
dev_dbg(&dev->dev, "RX overflow\n");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -EILSEQ:
|
|
|
|
|
dev->stats.rx_crc_errors++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->stats.rx_errors++;
|
|
|
|
|
resubmit:
|
|
|
|
|
if (page)
|
2011-11-22 17:57:41 +07:00
|
|
|
|
put_page(page);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
if (req)
|
2014-11-12 00:26:50 +07:00
|
|
|
|
rx_submit(pnd, req, GFP_ATOMIC);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int usbpn_close(struct net_device *dev);
|
|
|
|
|
|
|
|
|
|
static int usbpn_open(struct net_device *dev)
|
|
|
|
|
{
|
|
|
|
|
struct usbpn_dev *pnd = netdev_priv(dev);
|
|
|
|
|
int err;
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
|
|
|
|
|
|
|
|
|
|
err = usb_set_interface(pnd->usb, num, pnd->active_setting);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < rxq_size; i++) {
|
|
|
|
|
struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
|
|
|
|
|
|
2014-11-12 00:26:50 +07:00
|
|
|
|
if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
|
2012-08-07 18:56:26 +07:00
|
|
|
|
usb_free_urb(req);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
usbpn_close(dev);
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
pnd->urbs[i] = req;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
netif_wake_queue(dev);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int usbpn_close(struct net_device *dev)
|
|
|
|
|
{
|
|
|
|
|
struct usbpn_dev *pnd = netdev_priv(dev);
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
|
|
|
|
|
|
|
|
|
|
netif_stop_queue(dev);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < rxq_size; i++) {
|
|
|
|
|
struct urb *req = pnd->urbs[i];
|
|
|
|
|
|
|
|
|
|
if (!req)
|
|
|
|
|
continue;
|
|
|
|
|
usb_kill_urb(req);
|
|
|
|
|
usb_free_urb(req);
|
|
|
|
|
pnd->urbs[i] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usb_set_interface(pnd->usb, num, !pnd->active_setting);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-09 07:00:06 +07:00
|
|
|
|
static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
|
|
|
|
|
{
|
|
|
|
|
struct if_phonet_req *req = (struct if_phonet_req *)ifr;
|
|
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
case SIOCPNGAUTOCONF:
|
|
|
|
|
req->ifr_phonet_autoconf.device = PN_DEV_PC;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -ENOIOCTLCMD;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-21 08:58:35 +07:00
|
|
|
|
static const struct net_device_ops usbpn_ops = {
|
|
|
|
|
.ndo_open = usbpn_open,
|
|
|
|
|
.ndo_stop = usbpn_close,
|
|
|
|
|
.ndo_start_xmit = usbpn_xmit,
|
2009-09-09 07:00:06 +07:00
|
|
|
|
.ndo_do_ioctl = usbpn_ioctl,
|
2009-07-21 08:58:35 +07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void usbpn_setup(struct net_device *dev)
|
|
|
|
|
{
|
|
|
|
|
dev->features = 0;
|
|
|
|
|
dev->netdev_ops = &usbpn_ops,
|
|
|
|
|
dev->header_ops = &phonet_header_ops;
|
|
|
|
|
dev->type = ARPHRD_PHONET;
|
|
|
|
|
dev->flags = IFF_POINTOPOINT | IFF_NOARP;
|
|
|
|
|
dev->mtu = PHONET_MAX_MTU;
|
2016-10-21 00:55:17 +07:00
|
|
|
|
dev->min_mtu = PHONET_MIN_MTU;
|
|
|
|
|
dev->max_mtu = PHONET_MAX_MTU;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
dev->hard_header_len = 1;
|
|
|
|
|
dev->dev_addr[0] = PN_MEDIA_USB;
|
|
|
|
|
dev->addr_len = 1;
|
|
|
|
|
dev->tx_queue_len = 3;
|
|
|
|
|
|
net: Fix inconsistent teardown and release of private netdev state.
Network devices can allocate reasources and private memory using
netdev_ops->ndo_init(). However, the release of these resources
can occur in one of two different places.
Either netdev_ops->ndo_uninit() or netdev->destructor().
The decision of which operation frees the resources depends upon
whether it is necessary for all netdev refs to be released before it
is safe to perform the freeing.
netdev_ops->ndo_uninit() presumably can occur right after the
NETDEV_UNREGISTER notifier completes and the unicast and multicast
address lists are flushed.
netdev->destructor(), on the other hand, does not run until the
netdev references all go away.
Further complicating the situation is that netdev->destructor()
almost universally does also a free_netdev().
This creates a problem for the logic in register_netdevice().
Because all callers of register_netdevice() manage the freeing
of the netdev, and invoke free_netdev(dev) if register_netdevice()
fails.
If netdev_ops->ndo_init() succeeds, but something else fails inside
of register_netdevice(), it does call ndo_ops->ndo_uninit(). But
it is not able to invoke netdev->destructor().
This is because netdev->destructor() will do a free_netdev() and
then the caller of register_netdevice() will do the same.
However, this means that the resources that would normally be released
by netdev->destructor() will not be.
Over the years drivers have added local hacks to deal with this, by
invoking their destructor parts by hand when register_netdevice()
fails.
Many drivers do not try to deal with this, and instead we have leaks.
Let's close this hole by formalizing the distinction between what
private things need to be freed up by netdev->destructor() and whether
the driver needs unregister_netdevice() to perform the free_netdev().
netdev->priv_destructor() performs all actions to free up the private
resources that used to be freed by netdev->destructor(), except for
free_netdev().
netdev->needs_free_netdev is a boolean that indicates whether
free_netdev() should be done at the end of unregister_netdevice().
Now, register_netdevice() can sanely release all resources after
ndo_ops->ndo_init() succeeds, by invoking both ndo_ops->ndo_uninit()
and netdev->priv_destructor().
And at the end of unregister_netdevice(), we invoke
netdev->priv_destructor() and optionally call free_netdev().
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-05-08 23:52:56 +07:00
|
|
|
|
dev->needs_free_netdev = true;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* USB driver callbacks
|
|
|
|
|
*/
|
2017-08-08 22:58:02 +07:00
|
|
|
|
static const struct usb_device_id usbpn_ids[] = {
|
2009-07-21 08:58:35 +07:00
|
|
|
|
{
|
|
|
|
|
.match_flags = USB_DEVICE_ID_MATCH_VENDOR
|
|
|
|
|
| USB_DEVICE_ID_MATCH_INT_CLASS
|
|
|
|
|
| USB_DEVICE_ID_MATCH_INT_SUBCLASS,
|
|
|
|
|
.idVendor = 0x0421, /* Nokia */
|
|
|
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
|
|
|
|
.bInterfaceSubClass = 0xFE,
|
|
|
|
|
},
|
|
|
|
|
{ },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(usb, usbpn_ids);
|
|
|
|
|
|
|
|
|
|
static struct usb_driver usbpn_driver;
|
|
|
|
|
|
2013-09-19 13:45:26 +07:00
|
|
|
|
static int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
|
2009-07-21 08:58:35 +07:00
|
|
|
|
{
|
|
|
|
|
static const char ifname[] = "usbpn%d";
|
|
|
|
|
const struct usb_cdc_union_desc *union_header = NULL;
|
|
|
|
|
const struct usb_host_interface *data_desc;
|
|
|
|
|
struct usb_interface *data_intf;
|
|
|
|
|
struct usb_device *usbdev = interface_to_usbdev(intf);
|
|
|
|
|
struct net_device *dev;
|
|
|
|
|
struct usbpn_dev *pnd;
|
|
|
|
|
u8 *data;
|
2011-03-13 13:54:31 +07:00
|
|
|
|
int phonet = 0;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
int len, err;
|
2015-09-07 21:05:42 +07:00
|
|
|
|
struct usb_cdc_parsed_header hdr;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
|
|
|
|
data = intf->altsetting->extra;
|
|
|
|
|
len = intf->altsetting->extralen;
|
2015-09-07 21:05:42 +07:00
|
|
|
|
cdc_parse_cdc_header(&hdr, intf, data, len);
|
|
|
|
|
union_header = hdr.usb_cdc_union_desc;
|
|
|
|
|
phonet = hdr.phonet_magic_present;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
2011-03-13 13:54:31 +07:00
|
|
|
|
if (!union_header || !phonet)
|
2009-07-21 08:58:35 +07:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
|
|
|
|
|
if (data_intf == NULL)
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
/* Data interface has one inactive and one active setting */
|
|
|
|
|
if (data_intf->num_altsetting != 2)
|
|
|
|
|
return -EINVAL;
|
2009-12-03 14:58:21 +07:00
|
|
|
|
if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
|
|
|
|
|
data_intf->altsetting[1].desc.bNumEndpoints == 2)
|
2009-07-21 08:58:35 +07:00
|
|
|
|
data_desc = data_intf->altsetting + 1;
|
|
|
|
|
else
|
2009-12-03 14:58:21 +07:00
|
|
|
|
if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
|
|
|
|
|
data_intf->altsetting[1].desc.bNumEndpoints == 0)
|
2009-07-21 08:58:35 +07:00
|
|
|
|
data_desc = data_intf->altsetting;
|
|
|
|
|
else
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
|
net: set name_assign_type in alloc_netdev()
Extend alloc_netdev{,_mq{,s}}() to take name_assign_type as argument, and convert
all users to pass NET_NAME_UNKNOWN.
Coccinelle patch:
@@
expression sizeof_priv, name, setup, txqs, rxqs, count;
@@
(
-alloc_netdev_mqs(sizeof_priv, name, setup, txqs, rxqs)
+alloc_netdev_mqs(sizeof_priv, name, NET_NAME_UNKNOWN, setup, txqs, rxqs)
|
-alloc_netdev_mq(sizeof_priv, name, setup, count)
+alloc_netdev_mq(sizeof_priv, name, NET_NAME_UNKNOWN, setup, count)
|
-alloc_netdev(sizeof_priv, name, setup)
+alloc_netdev(sizeof_priv, name, NET_NAME_UNKNOWN, setup)
)
v9: move comments here from the wrong commit
Signed-off-by: Tom Gundersen <teg@jklm.no>
Reviewed-by: David Herrmann <dh.herrmann@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-07-14 21:37:24 +07:00
|
|
|
|
ifname, NET_NAME_UNKNOWN, usbpn_setup);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
if (!dev)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
pnd = netdev_priv(dev);
|
|
|
|
|
SET_NETDEV_DEV(dev, &intf->dev);
|
|
|
|
|
|
|
|
|
|
pnd->dev = dev;
|
2012-06-11 22:19:41 +07:00
|
|
|
|
pnd->usb = usbdev;
|
2009-07-21 08:58:35 +07:00
|
|
|
|
pnd->intf = intf;
|
|
|
|
|
pnd->data_intf = data_intf;
|
|
|
|
|
spin_lock_init(&pnd->tx_lock);
|
|
|
|
|
spin_lock_init(&pnd->rx_lock);
|
|
|
|
|
/* Endpoints */
|
|
|
|
|
if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
|
|
|
|
|
pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
|
|
|
|
|
data_desc->endpoint[0].desc.bEndpointAddress);
|
|
|
|
|
pnd->tx_pipe = usb_sndbulkpipe(usbdev,
|
|
|
|
|
data_desc->endpoint[1].desc.bEndpointAddress);
|
|
|
|
|
} else {
|
|
|
|
|
pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
|
|
|
|
|
data_desc->endpoint[1].desc.bEndpointAddress);
|
|
|
|
|
pnd->tx_pipe = usb_sndbulkpipe(usbdev,
|
|
|
|
|
data_desc->endpoint[0].desc.bEndpointAddress);
|
|
|
|
|
}
|
|
|
|
|
pnd->active_setting = data_desc - data_intf->altsetting;
|
|
|
|
|
|
|
|
|
|
err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
|
|
|
|
|
if (err)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
/* Force inactive mode until the network device is brought UP */
|
|
|
|
|
usb_set_interface(usbdev, union_header->bSlaveInterface0,
|
|
|
|
|
!pnd->active_setting);
|
|
|
|
|
usb_set_intfdata(intf, pnd);
|
|
|
|
|
|
|
|
|
|
err = register_netdev(dev);
|
|
|
|
|
if (err) {
|
|
|
|
|
usb_driver_release_interface(&usbpn_driver, data_intf);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
usb_set_intfdata(intf, NULL);
|
|
|
|
|
free_netdev(dev);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void usbpn_disconnect(struct usb_interface *intf)
|
|
|
|
|
{
|
|
|
|
|
struct usbpn_dev *pnd = usb_get_intfdata(intf);
|
|
|
|
|
|
|
|
|
|
if (pnd->disconnected)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pnd->disconnected = 1;
|
|
|
|
|
usb_driver_release_interface(&usbpn_driver,
|
|
|
|
|
(pnd->intf == intf) ? pnd->data_intf : pnd->intf);
|
|
|
|
|
unregister_netdev(pnd->dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct usb_driver usbpn_driver = {
|
|
|
|
|
.name = "cdc_phonet",
|
|
|
|
|
.probe = usbpn_probe,
|
|
|
|
|
.disconnect = usbpn_disconnect,
|
|
|
|
|
.id_table = usbpn_ids,
|
2012-04-24 00:08:51 +07:00
|
|
|
|
.disable_hub_initiated_lpm = 1,
|
2009-07-21 08:58:35 +07:00
|
|
|
|
};
|
|
|
|
|
|
2011-11-19 00:44:20 +07:00
|
|
|
|
module_usb_driver(usbpn_driver);
|
2009-07-21 08:58:35 +07:00
|
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Remi Denis-Courmont");
|
|
|
|
|
MODULE_DESCRIPTION("USB CDC Phonet host interface");
|
|
|
|
|
MODULE_LICENSE("GPL");
|