xref: /openbmc/linux/drivers/rapidio/rio-driver.c (revision a11650e11093ed57dca78bf16e7836517c599098)
1394b701cSMatt Porter /*
2394b701cSMatt Porter  * RapidIO driver support
3394b701cSMatt Porter  *
4394b701cSMatt Porter  * Copyright 2005 MontaVista Software, Inc.
5394b701cSMatt Porter  * Matt Porter <mporter@kernel.crashing.org>
6394b701cSMatt Porter  *
7394b701cSMatt Porter  * This program is free software; you can redistribute  it and/or modify it
8394b701cSMatt Porter  * under  the terms of  the GNU General  Public License as published by the
9394b701cSMatt Porter  * Free Software Foundation;  either version 2 of the  License, or (at your
10394b701cSMatt Porter  * option) any later version.
11394b701cSMatt Porter  */
12394b701cSMatt Porter 
13394b701cSMatt Porter #include <linux/init.h>
14394b701cSMatt Porter #include <linux/module.h>
15394b701cSMatt Porter #include <linux/rio.h>
16394b701cSMatt Porter #include <linux/rio_ids.h>
17394b701cSMatt Porter 
18394b701cSMatt Porter #include "rio.h"
19394b701cSMatt Porter 
20394b701cSMatt Porter /**
21394b701cSMatt Porter  *  rio_match_device - Tell if a RIO device has a matching RIO device id structure
22394b701cSMatt Porter  *  @id: the RIO device id structure to match against
23394b701cSMatt Porter  *  @rdev: the RIO device structure to match against
24394b701cSMatt Porter  *
25394b701cSMatt Porter  *  Used from driver probe and bus matching to check whether a RIO device
26394b701cSMatt Porter  *  matches a device id structure provided by a RIO driver. Returns the
27394b701cSMatt Porter  *  matching &struct rio_device_id or %NULL if there is no match.
28394b701cSMatt Porter  */
29394b701cSMatt Porter static const struct rio_device_id *rio_match_device(const struct rio_device_id
30394b701cSMatt Porter 						    *id,
31394b701cSMatt Porter 						    const struct rio_dev *rdev)
32394b701cSMatt Porter {
33394b701cSMatt Porter 	while (id->vid || id->asm_vid) {
34394b701cSMatt Porter 		if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
35394b701cSMatt Porter 		    ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
36394b701cSMatt Porter 		    ((id->asm_vid == RIO_ANY_ID)
37394b701cSMatt Porter 		     || (id->asm_vid == rdev->asm_vid))
38394b701cSMatt Porter 		    && ((id->asm_did == RIO_ANY_ID)
39394b701cSMatt Porter 			|| (id->asm_did == rdev->asm_did)))
40394b701cSMatt Porter 			return id;
41394b701cSMatt Porter 		id++;
42394b701cSMatt Porter 	}
43394b701cSMatt Porter 	return NULL;
44394b701cSMatt Porter }
45394b701cSMatt Porter 
46394b701cSMatt Porter /**
47394b701cSMatt Porter  * rio_dev_get - Increments the reference count of the RIO device structure
48394b701cSMatt Porter  *
49394b701cSMatt Porter  * @rdev: RIO device being referenced
50394b701cSMatt Porter  *
51394b701cSMatt Porter  * Each live reference to a device should be refcounted.
52394b701cSMatt Porter  *
53394b701cSMatt Porter  * Drivers for RIO devices should normally record such references in
54394b701cSMatt Porter  * their probe() methods, when they bind to a device, and release
55394b701cSMatt Porter  * them by calling rio_dev_put(), in their disconnect() methods.
56394b701cSMatt Porter  */
57394b701cSMatt Porter struct rio_dev *rio_dev_get(struct rio_dev *rdev)
58394b701cSMatt Porter {
59394b701cSMatt Porter 	if (rdev)
60394b701cSMatt Porter 		get_device(&rdev->dev);
61394b701cSMatt Porter 
62394b701cSMatt Porter 	return rdev;
63394b701cSMatt Porter }
64394b701cSMatt Porter 
65394b701cSMatt Porter /**
66394b701cSMatt Porter  * rio_dev_put - Release a use of the RIO device structure
67394b701cSMatt Porter  *
68394b701cSMatt Porter  * @rdev: RIO device being disconnected
69394b701cSMatt Porter  *
70394b701cSMatt Porter  * Must be called when a user of a device is finished with it.
71394b701cSMatt Porter  * When the last user of the device calls this function, the
72394b701cSMatt Porter  * memory of the device is freed.
73394b701cSMatt Porter  */
74394b701cSMatt Porter void rio_dev_put(struct rio_dev *rdev)
75394b701cSMatt Porter {
76394b701cSMatt Porter 	if (rdev)
77394b701cSMatt Porter 		put_device(&rdev->dev);
78394b701cSMatt Porter }
79394b701cSMatt Porter 
80394b701cSMatt Porter /**
817b089c8bSRandy Dunlap  *  rio_device_probe - Tell if a RIO device structure has a matching RIO device id structure
82394b701cSMatt Porter  *  @dev: the RIO device structure to match against
83394b701cSMatt Porter  *
84394b701cSMatt Porter  * return 0 and set rio_dev->driver when drv claims rio_dev, else error
85394b701cSMatt Porter  */
86394b701cSMatt Porter static int rio_device_probe(struct device *dev)
87394b701cSMatt Porter {
88394b701cSMatt Porter 	struct rio_driver *rdrv = to_rio_driver(dev->driver);
89394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
90394b701cSMatt Porter 	int error = -ENODEV;
91394b701cSMatt Porter 	const struct rio_device_id *id;
92394b701cSMatt Porter 
93394b701cSMatt Porter 	if (!rdev->driver && rdrv->probe) {
94394b701cSMatt Porter 		if (!rdrv->id_table)
95394b701cSMatt Porter 			return error;
96394b701cSMatt Porter 		id = rio_match_device(rdrv->id_table, rdev);
97394b701cSMatt Porter 		rio_dev_get(rdev);
98394b701cSMatt Porter 		if (id)
99394b701cSMatt Porter 			error = rdrv->probe(rdev, id);
100394b701cSMatt Porter 		if (error >= 0) {
101394b701cSMatt Porter 			rdev->driver = rdrv;
102394b701cSMatt Porter 			error = 0;
103a7de3902SEugene Surovegin 		} else
104394b701cSMatt Porter 			rio_dev_put(rdev);
105394b701cSMatt Porter 	}
106394b701cSMatt Porter 	return error;
107394b701cSMatt Porter }
108394b701cSMatt Porter 
109394b701cSMatt Porter /**
110394b701cSMatt Porter  *  rio_device_remove - Remove a RIO device from the system
111394b701cSMatt Porter  *
112394b701cSMatt Porter  *  @dev: the RIO device structure to match against
113394b701cSMatt Porter  *
114394b701cSMatt Porter  * Remove a RIO device from the system. If it has an associated
115394b701cSMatt Porter  * driver, then run the driver remove() method.  Then update
116394b701cSMatt Porter  * the reference count.
117394b701cSMatt Porter  */
118394b701cSMatt Porter static int rio_device_remove(struct device *dev)
119394b701cSMatt Porter {
120394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
121394b701cSMatt Porter 	struct rio_driver *rdrv = rdev->driver;
122394b701cSMatt Porter 
123394b701cSMatt Porter 	if (rdrv) {
124394b701cSMatt Porter 		if (rdrv->remove)
125394b701cSMatt Porter 			rdrv->remove(rdev);
126394b701cSMatt Porter 		rdev->driver = NULL;
127394b701cSMatt Porter 	}
128394b701cSMatt Porter 
129394b701cSMatt Porter 	rio_dev_put(rdev);
130394b701cSMatt Porter 
131394b701cSMatt Porter 	return 0;
132394b701cSMatt Porter }
133394b701cSMatt Porter 
134394b701cSMatt Porter /**
135394b701cSMatt Porter  *  rio_register_driver - register a new RIO driver
136394b701cSMatt Porter  *  @rdrv: the RIO driver structure to register
137394b701cSMatt Porter  *
1387b089c8bSRandy Dunlap  *  Adds a &struct rio_driver to the list of registered drivers.
139394b701cSMatt Porter  *  Returns a negative value on error, otherwise 0. If no error
140394b701cSMatt Porter  *  occurred, the driver remains registered even if no device
141394b701cSMatt Porter  *  was claimed during registration.
142394b701cSMatt Porter  */
143394b701cSMatt Porter int rio_register_driver(struct rio_driver *rdrv)
144394b701cSMatt Porter {
145394b701cSMatt Porter 	/* initialize common driver fields */
146394b701cSMatt Porter 	rdrv->driver.name = rdrv->name;
147394b701cSMatt Porter 	rdrv->driver.bus = &rio_bus_type;
148394b701cSMatt Porter 
149394b701cSMatt Porter 	/* register with core */
150394b701cSMatt Porter 	return driver_register(&rdrv->driver);
151394b701cSMatt Porter }
152394b701cSMatt Porter 
153394b701cSMatt Porter /**
154394b701cSMatt Porter  *  rio_unregister_driver - unregister a RIO driver
155394b701cSMatt Porter  *  @rdrv: the RIO driver structure to unregister
156394b701cSMatt Porter  *
157394b701cSMatt Porter  *  Deletes the &struct rio_driver from the list of registered RIO
158394b701cSMatt Porter  *  drivers, gives it a chance to clean up by calling its remove()
159394b701cSMatt Porter  *  function for each device it was responsible for, and marks those
160394b701cSMatt Porter  *  devices as driverless.
161394b701cSMatt Porter  */
162394b701cSMatt Porter void rio_unregister_driver(struct rio_driver *rdrv)
163394b701cSMatt Porter {
164394b701cSMatt Porter 	driver_unregister(&rdrv->driver);
165394b701cSMatt Porter }
166394b701cSMatt Porter 
167*a11650e1SAlexandre Bounine void rio_attach_device(struct rio_dev *rdev)
168*a11650e1SAlexandre Bounine {
169*a11650e1SAlexandre Bounine 	rdev->dev.bus = &rio_bus_type;
170*a11650e1SAlexandre Bounine 	rdev->dev.parent = &rio_bus;
171*a11650e1SAlexandre Bounine }
172*a11650e1SAlexandre Bounine EXPORT_SYMBOL_GPL(rio_attach_device);
173*a11650e1SAlexandre Bounine 
174394b701cSMatt Porter /**
1757b089c8bSRandy Dunlap  *  rio_match_bus - Tell if a RIO device structure has a matching RIO driver device id structure
176394b701cSMatt Porter  *  @dev: the standard device structure to match against
177394b701cSMatt Porter  *  @drv: the standard driver structure containing the ids to match against
178394b701cSMatt Porter  *
179394b701cSMatt Porter  *  Used by a driver to check whether a RIO device present in the
180394b701cSMatt Porter  *  system is in its list of supported devices. Returns 1 if
181394b701cSMatt Porter  *  there is a matching &struct rio_device_id or 0 if there is
182394b701cSMatt Porter  *  no match.
183394b701cSMatt Porter  */
184394b701cSMatt Porter static int rio_match_bus(struct device *dev, struct device_driver *drv)
185394b701cSMatt Porter {
186394b701cSMatt Porter 	struct rio_dev *rdev = to_rio_dev(dev);
187394b701cSMatt Porter 	struct rio_driver *rdrv = to_rio_driver(drv);
188394b701cSMatt Porter 	const struct rio_device_id *id = rdrv->id_table;
189394b701cSMatt Porter 	const struct rio_device_id *found_id;
190394b701cSMatt Porter 
191394b701cSMatt Porter 	if (!id)
192394b701cSMatt Porter 		goto out;
193394b701cSMatt Porter 
194394b701cSMatt Porter 	found_id = rio_match_device(id, rdev);
195394b701cSMatt Porter 
196394b701cSMatt Porter 	if (found_id)
197394b701cSMatt Porter 		return 1;
198394b701cSMatt Porter 
199394b701cSMatt Porter       out:return 0;
200394b701cSMatt Porter }
201394b701cSMatt Porter 
2022c70f022SAlexandre Bounine struct device rio_bus = {
20337f10544SKay Sievers 	.init_name = "rapidio",
204394b701cSMatt Porter };
205394b701cSMatt Porter 
206394b701cSMatt Porter struct bus_type rio_bus_type = {
207394b701cSMatt Porter 	.name = "rapidio",
208394b701cSMatt Porter 	.match = rio_match_bus,
209fc3d3dddSRussell King 	.dev_attrs = rio_dev_attrs,
210fc3d3dddSRussell King 	.probe = rio_device_probe,
211fc3d3dddSRussell King 	.remove = rio_device_remove,
212394b701cSMatt Porter };
213394b701cSMatt Porter 
214394b701cSMatt Porter /**
215394b701cSMatt Porter  *  rio_bus_init - Register the RapidIO bus with the device model
216394b701cSMatt Porter  *
217394b701cSMatt Porter  *  Registers the RIO bus device and RIO bus type with the Linux
218394b701cSMatt Porter  *  device model.
219394b701cSMatt Porter  */
220394b701cSMatt Porter static int __init rio_bus_init(void)
221394b701cSMatt Porter {
222394b701cSMatt Porter 	if (device_register(&rio_bus) < 0)
223394b701cSMatt Porter 		printk("RIO: failed to register RIO bus device\n");
224394b701cSMatt Porter 	return bus_register(&rio_bus_type);
225394b701cSMatt Porter }
226394b701cSMatt Porter 
227394b701cSMatt Porter postcore_initcall(rio_bus_init);
228394b701cSMatt Porter 
229394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_register_driver);
230394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_unregister_driver);
231394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_bus_type);
232394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_dev_get);
233394b701cSMatt Porter EXPORT_SYMBOL_GPL(rio_dev_put);
234