netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Lunn <andrew@lunn.ch>
To: Florian Fainelli <f.fainelli@gmail.com>,
	Vivien Didelot <vivien.didelot@savoirfairelinux.com>,
	netdev <netdev@vger.kernel.org>
Cc: Andrew Lunn <andrew@lunn.ch>
Subject: [PATCH RFC v2 17/32] dsa: mv88e6xxx: Add shared code for binding/unbinding a switch driver.
Date: Sun, 28 Feb 2016 17:41:25 +0100	[thread overview]
Message-ID: <1456677700-23027-18-git-send-email-andrew@lunn.ch> (raw)
In-Reply-To: <1456677700-23027-1-git-send-email-andrew@lunn.ch>

Switch drivers are component slaves. When they are bound to a master
component, the bind function is called and resources can be reserved.
Add the shared code.

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
---
 drivers/net/dsa/mv88e6xxx.c | 77 ++++++++++++++++++++++++++++++++++++++-------
 drivers/net/dsa/mv88e6xxx.h |  5 +++
 2 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/drivers/net/dsa/mv88e6xxx.c b/drivers/net/dsa/mv88e6xxx.c
index 7f67bf47cdb6..4a4af245b0eb 100644
--- a/drivers/net/dsa/mv88e6xxx.c
+++ b/drivers/net/dsa/mv88e6xxx.c
@@ -4,6 +4,7 @@
  *
  * Copyright (c) 2015 CMC Electronics, Inc.
  *	Added support for VLAN Table Unit operations
+ * Copyright (c) 2015 Andrew Lunn <andrew@lunn.ch>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -12,14 +13,16 @@
  */
 
 #include <linux/delay.h>
+#include <linux/device.h>
 #include <linux/etherdevice.h>
 #include <linux/ethtool.h>
+#include <linux/gpio/consumer.h>
 #include <linux/if_bridge.h>
 #include <linux/jiffies.h>
 #include <linux/list.h>
+#include <linux/mdio.h>
 #include <linux/module.h>
 #include <linux/netdevice.h>
-#include <linux/gpio/consumer.h>
 #include <linux/phy.h>
 #include <net/dsa.h>
 #include <net/switchdev.h>
@@ -2196,19 +2199,22 @@ int mv88e6xxx_setup_ports(struct dsa_switch *ds)
 
 int mv88e6xxx_setup_common(struct dsa_switch *ds, struct device *dev)
 {
-	struct mv88e6xxx_priv_state *ps;
+	struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
 
-	ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
-	if (!ps)
-		return -ENOMEM;
+	if (!ps) {
+		ps = devm_kzalloc(dev, sizeof(*ps), GFP_KERNEL);
+		if (!ps)
+			return -ENOMEM;
 
-	ds->priv = ps;
-	ps->ds = ds;
-	ps->bus = dsa_host_dev_to_mii_bus(ds->master_dev);
-	if (!ps->bus)
-		return -ENODEV;
+		ds->priv = ps;
+		ps->ds = ds;
 
-	ps->sw_addr = ds->pd->sw_addr;
+		ps->bus = dsa_host_dev_to_mii_bus(ds->master_dev);
+		if (!ps->bus)
+			return -ENODEV;
+
+		ps->sw_addr = ds->pd->sw_addr;
+	}
 
 	mutex_init(&ps->smi_mutex);
 
@@ -2654,6 +2660,55 @@ char *mv88e6xxx_lookup_name(struct mii_bus *bus, int sw_addr,
 	return NULL;
 }
 
+int mv88e6xxx_bind(struct device *dev,
+		   struct dsa_switch_tree *dst,
+		   struct dsa_switch_driver *ops,
+		   const struct mv88e6xxx_switch_id *table,
+		   unsigned int table_size)
+{
+	struct mdio_device *mdiodev = to_mdio_device(dev);
+	struct mv88e6xxx_priv_state *ps;
+	struct device_node *np = dev->of_node;
+	struct dsa_switch *ds;
+	const char *name;
+
+	ds = devm_kzalloc(dev, sizeof(*ds) + sizeof(*ps), GFP_KERNEL);
+	if (!ds)
+		return -ENOMEM;
+
+	ps = (struct mv88e6xxx_priv_state *)(ds + 1);
+	ds->priv = ps;
+	ps->ds = ds;
+	ps->bus = mdiodev->bus;
+	ps->sw_addr = mdiodev->addr;
+
+	get_device(&ps->bus->dev);
+
+	ds->drv = ops;
+
+	name = mv88e6xxx_lookup_name(ps->bus, ps->sw_addr, table, table_size);
+	if (!name) {
+		dev_err(dev, "Failed to find switch");
+		return -ENODEV;
+	}
+
+	dev_set_drvdata(dev, ds);
+	dsa_switch_register(dst, ds, np, name);
+
+	return 0;
+}
+
+void mv88e6xxx_unbind(struct device *dev, struct device *master, void *data)
+{
+	struct dsa_switch *ds = dev_get_drvdata(dev);
+	struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
+
+	dsa_switch_unregister(ds);
+	devm_kfree(dev, ds);
+
+	put_device(&ps->bus->dev);
+}
+
 static int __init mv88e6xxx_init(void)
 {
 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
diff --git a/drivers/net/dsa/mv88e6xxx.h b/drivers/net/dsa/mv88e6xxx.h
index dce72d1007c2..a4ae99b7cfd0 100644
--- a/drivers/net/dsa/mv88e6xxx.h
+++ b/drivers/net/dsa/mv88e6xxx.h
@@ -447,6 +447,11 @@ int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active);
 char *mv88e6xxx_lookup_name(struct mii_bus *bus, int sw_addr,
 			    const struct mv88e6xxx_switch_id *table,
 			    unsigned int num);
+int mv88e6xxx_bind(struct device *dev, struct dsa_switch_tree *dst,
+		   struct dsa_switch_driver *ops,
+		   const struct mv88e6xxx_switch_id *table,
+		   unsigned int table_size);
+void mv88e6xxx_unbind(struct device *dev, struct device *master, void *data);
 int mv88e6xxx_setup_ports(struct dsa_switch *ds);
 int mv88e6xxx_setup_common(struct dsa_switch *ds, struct device *dev);
 int mv88e6xxx_setup_global(struct dsa_switch *ds);
-- 
2.7.0

  parent reply	other threads:[~2016-02-28 16:42 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-28 16:41 [PATCH RFC v2 00/32] Make DSA switches linux devices Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 01/32] net: dsa: Move platform data allocation for OF Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 02/32] dsa: Rename mv88e6123_61_65 to mv88e6123 to be consistent Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 03/32] dsa: Make setup and finish more symmetrical Andrew Lunn
2016-03-11 23:54   ` Florian Fainelli
2016-02-28 16:41 ` [PATCH RFC v2 04/32] net: dsa: Pass the dsa device to the switch drivers Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 05/32] net: dsa: Have the switch driver allocate there own private memory Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 06/32] net: dsa: Remove allocation of driver " Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 07/32] net: dsa: Keep the mii bus and address in the private structure Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 08/32] net: dsa: dsa.c: Refactor to increase symmetry Andrew Lunn
2016-03-11 23:54   ` Florian Fainelli
2016-02-28 16:41 ` [PATCH RFC v2 09/32] driver: component: Add support for empty match table Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 10/32] net: dsa: Add basic support for component master support Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 11/32] net: dsa: Keep a reference to the switch device for component matching Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 12/32] net: dsa: Add slave component matches based on a phandle to the slave Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 13/32] net: dsa: Make dsa,mii-bus optional Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 14/32] net: dsa: Add register/unregister functions for switch drivers Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 15/32] net: dsa: Rename DSA probe function Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 16/32] dsa: mv88e6xxx: Use bus in mv88e6xxx_lookup_name() Andrew Lunn
2016-02-28 16:41 ` Andrew Lunn [this message]
2016-02-28 16:41 ` [PATCH RFC v2 18/32] dsa: mv88e6xxx: Prepare for turning this into a library module Andrew Lunn
2016-02-29  2:40   ` Vivien Didelot
2016-02-29 14:53     ` Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 19/32] dsa: mv88e6xxx: Add macro for registering the drivers Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 20/32] dsa: Add mdio device support to Marvell switches Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 21/32] net: mdio: Add mdiodev_{read|write} helpers Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 22/32] net: dsa: Better integrate the drivers with mdio device Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 23/32] net: dsa: bcm_sf2: make it a real platform driver Andrew Lunn
2016-03-03 18:33   ` Florian Fainelli
2016-03-03 19:12     ` Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 24/32] net: dsa: Add some debug prints for error cases Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 25/32] net: dsa: Setup the switches after all have been probed Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 26/32] net: dsa: Only setup platform switches, not device switches Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 27/32] net: dsa: If a switch fails to probe, defer probing Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 28/32] Documentation: DSA: Describe how probe of DSA and switches work Andrew Lunn
2016-02-29 11:42   ` Sergei Shtylyov
2016-02-28 16:41 ` [PATCH RFC v2 29/32] dsa: slave: Don't reference NULL pointer during phy_disconnect Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 30/32] dsa: Destroy fixed link phys after the phy has been disconnected Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 31/32] dsa: dsa: Fix freeing of fixed-phys from user ports Andrew Lunn
2016-02-28 16:41 ` [PATCH RFC v2 32/32] phy: fixed: Fix removal of phys Andrew Lunn
2016-03-03 18:49 ` [PATCH RFC v2 00/32] Make DSA switches linux devices Florian Fainelli
2016-03-03 20:27   ` Andrew Lunn
2016-03-11 23:41     ` Florian Fainelli
2016-03-12 17:08       ` Andrew Lunn
2016-03-13  7:26         ` Vivien Didelot
2016-03-14 19:36         ` Florian Fainelli
2016-03-14 20:51           ` Andrew Lunn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1456677700-23027-18-git-send-email-andrew@lunn.ch \
    --to=andrew@lunn.ch \
    --cc=f.fainelli@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=vivien.didelot@savoirfairelinux.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).