xref: /openbmc/linux/drivers/rapidio/rio-driver.c (revision 48d6b4dd362c4f3a6032946397385b28ff8d2b9c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2394b701cSMatt Porter /*
3394b701cSMatt Porter  * RapidIO driver support
4394b701cSMatt Porter  *
5394b701cSMatt Porter  * Copyright 2005 MontaVista Software, Inc.
6394b701cSMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
7394b701cSMatt Porter  */
8394b701cSMatt Porter 
9394b701cSMatt Porter #include <linux/init.h>
10394b701cSMatt Porter #include <linux/module.h>
11394b701cSMatt Porter #include <linux/rio.h>
12394b701cSMatt Porter #include <linux/rio_ids.h>
13*48d6b4ddSBen Dooks (Codethink) #include <linux/rio_drv.h>
14394b701cSMatt Porter 
15394b701cSMatt Porter #include "rio.h"
16394b701cSMatt Porter 
17394b701cSMatt Porter /**
18394b701cSMatt Porter  *  rio_match_device - Tell if a RIO device has a matching RIO device id structure
19394b701cSMatt Porter  *  @id: the RIO device id structure to match against
20394b701cSMatt Porter  *  @rdev: the RIO device structure to match against
21394b701cSMatt Porter  *
22394b701cSMatt Porter  *  Used from driver probe and bus matching to check whether a RIO device
23394b701cSMatt Porter  *  matches a device id structure provided by a RIO driver. Returns the
24394b701cSMatt Porter  *  matching &struct rio_device_id or %NULL if there is no match.
25394b701cSMatt Porter  */
26394b701cSMatt Porter static const struct rio_device_id *rio_match_device(const struct rio_device_id
27394b701cSMatt Porter 						    *id,
28394b701cSMatt Porter 						    const struct rio_dev *rdev)
29394b701cSMatt Porter {
30394b701cSMatt Porter 	while (id->vid || id->asm_vid) {
31394b701cSMatt Porter 		if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
32394b701cSMatt Porter 		    ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
33394b701cSMatt Porter 		    ((id->asm_vid == RIO_ANY_ID)
34394b701cSMatt Porter 		     || (id->asm_vid == rdev->asm_vid))
35394b701cSMatt Porter 		    && ((id->asm_did == RIO_ANY_ID)
36394b701cSMatt Porter 			|| (id->asm_did == rdev->asm_did)))
37394b701cSMatt Porter 			return id;
38394b701cSMatt Porter 		id++;
39394b701cSMatt Porter 	}
40394b701cSMatt Porter 	return NULL;
41394b701cSMatt Porter }
42394b701cSMatt Porter 
43394b701cSMatt Porter /**
44394b701cSMatt Porter  * rio_dev_get - Increments the reference count of the RIO device structure
45394b701cSMatt Porter  *
46394b701cSMatt Porter  * @rdev: RIO device being referenced
47394b701cSMatt Porter  *
48394b701cSMatt Porter  * Each live reference to a device should be refcounted.
49394b701cSMatt Porter  *
50394b701cSMatt Porter  * Drivers for RIO devices should normally record such references in
51394b701cSMatt Porter  * their probe() methods, when they bind to a device, and release
52394b701cSMatt Porter  * them by calling rio_dev_put(), in their disconnect() methods.
53394b701cSMatt Porter  */
54394b701cSMatt Porter struct rio_dev *rio_dev_get(struct rio_dev *rdev)
55394b701cSMatt Porter {
56394b701cSMatt Porter 	if (rdev)
57394b701cSMatt Porter 		get_device(&rdev->dev);
58394b701cSMatt Porter 
59394b701cSMatt Porter 	return rdev;
60394b701cSMatt Porter }
61394b701cSMatt Porter 
62394b701cSMatt Porter /**
63394b701cSMatt Porter  * rio_dev_put - Release a use of the RIO device structure
64394b701cSMatt Porter  *
65394b701cSMatt Porter  * @rdev: RIO device being disconnected
66394b701cSMatt Porter  *
67394b701cSMatt Porter  * Must be called when a user of a device is finished with it.
68394b701cSMatt Porter  * When the last user of the device calls this function, the
69394b701cSMatt Porter  * memory of the device is freed.
70394b701cSMatt Porter  */
71394b701cSMatt Porter void rio_dev_put(struct rio_dev *rdev)
72394b701cSMatt Porter {
73394b701cSMatt Porter 	if (rdev)
74394b701cSMatt Porter 		put_device(&rdev->dev);
75394b701cSMatt Porter }
76394b701cSMatt Porter 
77394b701cSMatt Porter /**
787b089c8bSRandy Dunlap  *  rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
79394b701cSMatt Porter  *  @dev: the RIO device structure to match against
80394b701cSMatt Porter  *
81394b701cSMatt Porter  * return 0 and set rio_dev->driver when drv claims rio_dev, else error
82394b701cSMatt Porter  */
83394b701cSMatt Porter static int rio_device_probe(struct device *dev)
84394b701cSMatt Porter {
85394b701cSMatt Porter 	struct rio_driver *rdrv = to_rio_driver(dev->driver);
86394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
87394b701cSMatt Porter 	int error = -ENODEV;
88394b701cSMatt Porter 	const struct rio_device_id *id;
89394b701cSMatt Porter 
90394b701cSMatt Porter 	if (!rdev->driver && rdrv->probe) {
91394b701cSMatt Porter 		if (!rdrv->id_table)
92394b701cSMatt Porter 			return error;
93394b701cSMatt Porter 		id = rio_match_device(rdrv->id_table, rdev);
94394b701cSMatt Porter 		rio_dev_get(rdev);
95394b701cSMatt Porter 		if (id)
96394b701cSMatt Porter 			error = rdrv->probe(rdev, id);
97394b701cSMatt Porter 		if (error >= 0) {
98394b701cSMatt Porter 			rdev->driver = rdrv;
99394b701cSMatt Porter 			error = 0;
100a7de3902SEugene Surovegin 		} else
101394b701cSMatt Porter 			rio_dev_put(rdev);
102394b701cSMatt Porter 	}
103394b701cSMatt Porter 	return error;
104394b701cSMatt Porter }
105394b701cSMatt Porter 
106394b701cSMatt Porter /**
107394b701cSMatt Porter  *  rio_device_remove - Remove a RIO device from the system
108394b701cSMatt Porter  *
109394b701cSMatt Porter  *  @dev: the RIO device structure to match against
110394b701cSMatt Porter  *
111394b701cSMatt Porter  * Remove a RIO device from the system. If it has an associated
112394b701cSMatt Porter  * driver, then run the driver remove() method.  Then update
113394b701cSMatt Porter  * the reference count.
114394b701cSMatt Porter  */
115394b701cSMatt Porter static int rio_device_remove(struct device *dev)
116394b701cSMatt Porter {
117394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
118394b701cSMatt Porter 	struct rio_driver *rdrv = rdev->driver;
119394b701cSMatt Porter 
120394b701cSMatt Porter 	if (rdrv) {
121394b701cSMatt Porter 		if (rdrv->remove)
122394b701cSMatt Porter 			rdrv->remove(rdev);
123394b701cSMatt Porter 		rdev->driver = NULL;
124394b701cSMatt Porter 	}
125394b701cSMatt Porter 
126394b701cSMatt Porter 	rio_dev_put(rdev);
127394b701cSMatt Porter 
128394b701cSMatt Porter 	return 0;
129394b701cSMatt Porter }
130394b701cSMatt Porter 
13183dc2cbcSAlexandre Bounine static void rio_device_shutdown(struct device *dev)
13283dc2cbcSAlexandre Bounine {
13383dc2cbcSAlexandre Bounine 	struct rio_dev *rdev = to_rio_dev(dev);
13483dc2cbcSAlexandre Bounine 	struct rio_driver *rdrv = rdev->driver;
13583dc2cbcSAlexandre Bounine 
13683dc2cbcSAlexandre Bounine 	dev_dbg(dev, "RIO: %s\n", __func__);
13783dc2cbcSAlexandre Bounine 
13883dc2cbcSAlexandre Bounine 	if (rdrv && rdrv->shutdown)
13983dc2cbcSAlexandre Bounine 		rdrv->shutdown(rdev);
14083dc2cbcSAlexandre Bounine }
14183dc2cbcSAlexandre Bounine 
142394b701cSMatt Porter /**
143394b701cSMatt Porter  *  rio_register_driver - register a new RIO driver
144394b701cSMatt Porter  *  @rdrv: the RIO driver structure to register
145394b701cSMatt Porter  *
1467b089c8bSRandy Dunlap  *  Adds a &struct rio_driver to the list of registered drivers.
147394b701cSMatt Porter  *  Returns a negative value on error, otherwise 0. If no error
148394b701cSMatt Porter  *  occurred, the driver remains registered even if no device
149394b701cSMatt Porter  *  was claimed during registration.
150394b701cSMatt Porter  */
151394b701cSMatt Porter int rio_register_driver(struct rio_driver *rdrv)
152394b701cSMatt Porter {
153394b701cSMatt Porter 	/* initialize common driver fields */
154394b701cSMatt Porter 	rdrv->driver.name = rdrv->name;
155394b701cSMatt Porter 	rdrv->driver.bus = &rio_bus_type;
156394b701cSMatt Porter 
157394b701cSMatt Porter 	/* register with core */
158394b701cSMatt Porter 	return driver_register(&rdrv->driver);
159394b701cSMatt Porter }
160394b701cSMatt Porter 
161394b701cSMatt Porter /**
162394b701cSMatt Porter  *  rio_unregister_driver - unregister a RIO driver
163394b701cSMatt Porter  *  @rdrv: the RIO driver structure to unregister
164394b701cSMatt Porter  *
165394b701cSMatt Porter  *  Deletes the &struct rio_driver from the list of registered RIO
166394b701cSMatt Porter  *  drivers, gives it a chance to clean up by calling its remove()
167394b701cSMatt Porter  *  function for each device it was responsible for, and marks those
168394b701cSMatt Porter  *  devices as driverless.
169394b701cSMatt Porter  */
170394b701cSMatt Porter void rio_unregister_driver(struct rio_driver *rdrv)
171394b701cSMatt Porter {
172394b701cSMatt Porter 	driver_unregister(&rdrv->driver);
173394b701cSMatt Porter }
174394b701cSMatt Porter 
175a11650e1SAlexandre Bounine void rio_attach_device(struct rio_dev *rdev)
176a11650e1SAlexandre Bounine {
177a11650e1SAlexandre Bounine 	rdev->dev.bus = &rio_bus_type;
178a11650e1SAlexandre Bounine }
179a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_attach_device);
180a11650e1SAlexandre Bounine 
181394b701cSMatt Porter /**
1827b089c8bSRandy Dunlap  *  rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
183394b701cSMatt Porter  *  @dev: the standard device structure to match against
184394b701cSMatt Porter  *  @drv: the standard driver structure containing the ids to match against
185394b701cSMatt Porter  *
186394b701cSMatt Porter  *  Used by a driver to check whether a RIO device present in the
187394b701cSMatt Porter  *  system is in its list of supported devices. Returns 1 if
188394b701cSMatt Porter  *  there is a matching &struct rio_device_id or 0 if there is
189394b701cSMatt Porter  *  no match.
190394b701cSMatt Porter  */
191394b701cSMatt Porter static int rio_match_bus(struct device *dev, struct device_driver *drv)
192394b701cSMatt Porter {
193394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
194394b701cSMatt Porter 	struct rio_driver *rdrv = to_rio_driver(drv);
195394b701cSMatt Porter 	const struct rio_device_id *id = rdrv->id_table;
196394b701cSMatt Porter 	const struct rio_device_id *found_id;
197394b701cSMatt Porter 
198394b701cSMatt Porter 	if (!id)
199394b701cSMatt Porter 		goto out;
200394b701cSMatt Porter 
201394b701cSMatt Porter 	found_id = rio_match_device(id, rdev);
202394b701cSMatt Porter 
203394b701cSMatt Porter 	if (found_id)
204394b701cSMatt Porter 		return 1;
205394b701cSMatt Porter 
206394b701cSMatt Porter       out:return 0;
207394b701cSMatt Porter }
208394b701cSMatt Porter 
2093bdbb62fSAlexandre Bounine static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
2103bdbb62fSAlexandre Bounine {
2113bdbb62fSAlexandre Bounine 	struct rio_dev *rdev;
2123bdbb62fSAlexandre Bounine 
2133bdbb62fSAlexandre Bounine 	if (!dev)
2143bdbb62fSAlexandre Bounine 		return -ENODEV;
2153bdbb62fSAlexandre Bounine 
2163bdbb62fSAlexandre Bounine 	rdev = to_rio_dev(dev);
2173bdbb62fSAlexandre Bounine 	if (!rdev)
2183bdbb62fSAlexandre Bounine 		return -ENODEV;
2193bdbb62fSAlexandre Bounine 
2203bdbb62fSAlexandre Bounine 	if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
2213bdbb62fSAlexandre Bounine 			   rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
2223bdbb62fSAlexandre Bounine 		return -ENOMEM;
2233bdbb62fSAlexandre Bounine 	return 0;
2243bdbb62fSAlexandre Bounine }
2253bdbb62fSAlexandre Bounine 
2262aaf308bSAlexandre Bounine struct class rio_mport_class = {
2272aaf308bSAlexandre Bounine 	.name		= "rapidio_port",
2282aaf308bSAlexandre Bounine 	.owner		= THIS_MODULE,
2292aaf308bSAlexandre Bounine 	.dev_groups	= rio_mport_groups,
230394b701cSMatt Porter };
2312aaf308bSAlexandre Bounine EXPORT_SYMBOL_GPL(rio_mport_class);
232394b701cSMatt Porter 
233394b701cSMatt Porter struct bus_type rio_bus_type = {
234394b701cSMatt Porter 	.name = "rapidio",
235394b701cSMatt Porter 	.match = rio_match_bus,
2366d39c80bSGreg Kroah-Hartman 	.dev_groups = rio_dev_groups,
237ed1d2da2SGreg Kroah-Hartman 	.bus_groups = rio_bus_groups,
238fc3d3dddSRussell King 	.probe = rio_device_probe,
239fc3d3dddSRussell King 	.remove = rio_device_remove,
24083dc2cbcSAlexandre Bounine 	.shutdown = rio_device_shutdown,
2413bdbb62fSAlexandre Bounine 	.uevent	= rio_uevent,
242394b701cSMatt Porter };
243394b701cSMatt Porter 
244394b701cSMatt Porter /**
245394b701cSMatt Porter  *  rio_bus_init - Register the RapidIO bus with the device model
246394b701cSMatt Porter  *
2472aaf308bSAlexandre Bounine  *  Registers the RIO mport device class and RIO bus type with the Linux
248394b701cSMatt Porter  *  device model.
249394b701cSMatt Porter  */
250394b701cSMatt Porter static int __init rio_bus_init(void)
251394b701cSMatt Porter {
2522aaf308bSAlexandre Bounine 	int ret;
2532aaf308bSAlexandre Bounine 
2542aaf308bSAlexandre Bounine 	ret = class_register(&rio_mport_class);
2552aaf308bSAlexandre Bounine 	if (!ret) {
2562aaf308bSAlexandre Bounine 		ret = bus_register(&rio_bus_type);
2572aaf308bSAlexandre Bounine 		if (ret)
2582aaf308bSAlexandre Bounine 			class_unregister(&rio_mport_class);
2592aaf308bSAlexandre Bounine 	}
2602aaf308bSAlexandre Bounine 	return ret;
261394b701cSMatt Porter }
262394b701cSMatt Porter 
263394b701cSMatt Porter postcore_initcall(rio_bus_init);
264394b701cSMatt Porter 
265394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_register_driver);
266394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_unregister_driver);
267394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_bus_type);
268394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_dev_get);
269394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_dev_put);
270