~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/devres.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  * This file contains all networking devres helpers.
  4  */
  5 
  6 #include <linux/device.h>
  7 #include <linux/etherdevice.h>
  8 #include <linux/netdevice.h>
  9 
 10 struct net_device_devres {
 11         struct net_device *ndev;
 12 };
 13 
 14 static void devm_free_netdev(struct device *dev, void *this)
 15 {
 16         struct net_device_devres *res = this;
 17 
 18         free_netdev(res->ndev);
 19 }
 20 
 21 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
 22                                            unsigned int txqs, unsigned int rxqs)
 23 {
 24         struct net_device_devres *dr;
 25 
 26         dr = devres_alloc(devm_free_netdev, sizeof(*dr), GFP_KERNEL);
 27         if (!dr)
 28                 return NULL;
 29 
 30         dr->ndev = alloc_etherdev_mqs(sizeof_priv, txqs, rxqs);
 31         if (!dr->ndev) {
 32                 devres_free(dr);
 33                 return NULL;
 34         }
 35 
 36         devres_add(dev, dr);
 37 
 38         return dr->ndev;
 39 }
 40 EXPORT_SYMBOL(devm_alloc_etherdev_mqs);
 41 
 42 static void devm_unregister_netdev(struct device *dev, void *this)
 43 {
 44         struct net_device_devres *res = this;
 45 
 46         unregister_netdev(res->ndev);
 47 }
 48 
 49 static int netdev_devres_match(struct device *dev, void *this, void *match_data)
 50 {
 51         struct net_device_devres *res = this;
 52         struct net_device *ndev = match_data;
 53 
 54         return ndev == res->ndev;
 55 }
 56 
 57 /**
 58  *      devm_register_netdev - resource managed variant of register_netdev()
 59  *      @dev: managing device for this netdev - usually the parent device
 60  *      @ndev: device to register
 61  *
 62  *      This is a devres variant of register_netdev() for which the unregister
 63  *      function will be called automatically when the managing device is
 64  *      detached. Note: the net_device used must also be resource managed by
 65  *      the same struct device.
 66  */
 67 int devm_register_netdev(struct device *dev, struct net_device *ndev)
 68 {
 69         struct net_device_devres *dr;
 70         int ret;
 71 
 72         /* struct net_device must itself be managed. For now a managed netdev
 73          * can only be allocated by devm_alloc_etherdev_mqs() so the check is
 74          * straightforward.
 75          */
 76         if (WARN_ON(!devres_find(dev, devm_free_netdev,
 77                                  netdev_devres_match, ndev)))
 78                 return -EINVAL;
 79 
 80         dr = devres_alloc(devm_unregister_netdev, sizeof(*dr), GFP_KERNEL);
 81         if (!dr)
 82                 return -ENOMEM;
 83 
 84         ret = register_netdev(ndev);
 85         if (ret) {
 86                 devres_free(dr);
 87                 return ret;
 88         }
 89 
 90         dr->ndev = ndev;
 91         devres_add(ndev->dev.parent, dr);
 92 
 93         return 0;
 94 }
 95 EXPORT_SYMBOL(devm_register_netdev);
 96 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php