Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rio-driver.c
Go to the documentation of this file.
1 /*
2  * RapidIO driver support
3  *
4  * Copyright 2005 MontaVista Software, Inc.
5  * Matt Porter <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  */
12 
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/rio.h>
16 #include <linux/rio_ids.h>
17 
18 #include "rio.h"
19 
29 static const struct rio_device_id *rio_match_device(const struct rio_device_id
30  *id,
31  const struct rio_dev *rdev)
32 {
33  while (id->vid || id->asm_vid) {
34  if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
35  ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
36  ((id->asm_vid == RIO_ANY_ID)
37  || (id->asm_vid == rdev->asm_vid))
38  && ((id->asm_did == RIO_ANY_ID)
39  || (id->asm_did == rdev->asm_did)))
40  return id;
41  id++;
42  }
43  return NULL;
44 }
45 
57 struct rio_dev *rio_dev_get(struct rio_dev *rdev)
58 {
59  if (rdev)
60  get_device(&rdev->dev);
61 
62  return rdev;
63 }
64 
74 void rio_dev_put(struct rio_dev *rdev)
75 {
76  if (rdev)
77  put_device(&rdev->dev);
78 }
79 
86 static int rio_device_probe(struct device *dev)
87 {
88  struct rio_driver *rdrv = to_rio_driver(dev->driver);
89  struct rio_dev *rdev = to_rio_dev(dev);
90  int error = -ENODEV;
91  const struct rio_device_id *id;
92 
93  if (!rdev->driver && rdrv->probe) {
94  if (!rdrv->id_table)
95  return error;
96  id = rio_match_device(rdrv->id_table, rdev);
97  rio_dev_get(rdev);
98  if (id)
99  error = rdrv->probe(rdev, id);
100  if (error >= 0) {
101  rdev->driver = rdrv;
102  error = 0;
103  } else
104  rio_dev_put(rdev);
105  }
106  return error;
107 }
108 
118 static int rio_device_remove(struct device *dev)
119 {
120  struct rio_dev *rdev = to_rio_dev(dev);
121  struct rio_driver *rdrv = rdev->driver;
122 
123  if (rdrv) {
124  if (rdrv->remove)
125  rdrv->remove(rdev);
126  rdev->driver = NULL;
127  }
128 
129  rio_dev_put(rdev);
130 
131  return 0;
132 }
133 
144 {
145  /* initialize common driver fields */
146  rdrv->driver.name = rdrv->name;
147  rdrv->driver.bus = &rio_bus_type;
148 
149  /* register with core */
150  return driver_register(&rdrv->driver);
151 }
152 
163 {
164  driver_unregister(&rdrv->driver);
165 }
166 
177 static int rio_match_bus(struct device *dev, struct device_driver *drv)
178 {
179  struct rio_dev *rdev = to_rio_dev(dev);
180  struct rio_driver *rdrv = to_rio_driver(drv);
181  const struct rio_device_id *id = rdrv->id_table;
182  const struct rio_device_id *found_id;
183 
184  if (!id)
185  goto out;
186 
187  found_id = rio_match_device(id, rdev);
188 
189  if (found_id)
190  return 1;
191 
192  out:return 0;
193 }
194 
195 struct device rio_bus = {
196  .init_name = "rapidio",
197 };
198 
200  .name = "rapidio",
201  .match = rio_match_bus,
202  .dev_attrs = rio_dev_attrs,
203  .probe = rio_device_probe,
204  .remove = rio_device_remove,
205 };
206 
213 static int __init rio_bus_init(void)
214 {
215  if (device_register(&rio_bus) < 0)
216  printk("RIO: failed to register RIO bus device\n");
217  return bus_register(&rio_bus_type);
218 }
219 
220 postcore_initcall(rio_bus_init);
221 
224 EXPORT_SYMBOL_GPL(rio_bus_type);