Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zorro-driver.c
Go to the documentation of this file.
1 /*
2  * Zorro Driver Services
3  *
4  * Copyright (C) 2003 Geert Uytterhoeven
5  *
6  * Loosely based on drivers/pci/pci-driver.c
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License. See the file COPYING in the main directory of this archive
10  * for more details.
11  */
12 
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/zorro.h>
16 
17 
29 const struct zorro_device_id *
31  const struct zorro_dev *z)
32 {
33  while (ids->id) {
34  if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
35  return ids;
36  ids++;
37  }
38  return NULL;
39 }
41 
42 
43 static int zorro_device_probe(struct device *dev)
44 {
45  int error = 0;
46  struct zorro_driver *drv = to_zorro_driver(dev->driver);
47  struct zorro_dev *z = to_zorro_dev(dev);
48 
49  if (!z->driver && drv->probe) {
50  const struct zorro_device_id *id;
51 
52  id = zorro_match_device(drv->id_table, z);
53  if (id)
54  error = drv->probe(z, id);
55  if (error >= 0) {
56  z->driver = drv;
57  error = 0;
58  }
59  }
60  return error;
61 }
62 
63 
64 static int zorro_device_remove(struct device *dev)
65 {
66  struct zorro_dev *z = to_zorro_dev(dev);
67  struct zorro_driver *drv = to_zorro_driver(dev->driver);
68 
69  if (drv) {
70  if (drv->remove)
71  drv->remove(z);
72  z->driver = NULL;
73  }
74  return 0;
75 }
76 
77 
86 int zorro_register_driver(struct zorro_driver *drv)
87 {
88  /* initialize common driver fields */
89  drv->driver.name = drv->name;
90  drv->driver.bus = &zorro_bus_type;
91 
92  /* register with core */
93  return driver_register(&drv->driver);
94 }
96 
97 
108 void zorro_unregister_driver(struct zorro_driver *drv)
109 {
110  driver_unregister(&drv->driver);
111 }
113 
114 
126 static int zorro_bus_match(struct device *dev, struct device_driver *drv)
127 {
128  struct zorro_dev *z = to_zorro_dev(dev);
129  struct zorro_driver *zorro_drv = to_zorro_driver(drv);
130  const struct zorro_device_id *ids = zorro_drv->id_table;
131 
132  if (!ids)
133  return 0;
134 
135  while (ids->id) {
136  if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
137  return 1;
138  ids++;
139  }
140  return 0;
141 }
142 
143 static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
144 {
145 #ifdef CONFIG_HOTPLUG
146  struct zorro_dev *z;
147 
148  if (!dev)
149  return -ENODEV;
150 
151  z = to_zorro_dev(dev);
152  if (!z)
153  return -ENODEV;
154 
155  if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
156  add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
157  add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
158  add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
159  return -ENOMEM;
160 
161  return 0;
162 #else /* !CONFIG_HOTPLUG */
163  return -ENODEV;
164 #endif /* !CONFIG_HOTPLUG */
165 }
166 
168  .name = "zorro",
169  .match = zorro_bus_match,
170  .uevent = zorro_uevent,
171  .probe = zorro_device_probe,
172  .remove = zorro_device_remove,
173 };
174 EXPORT_SYMBOL(zorro_bus_type);
175 
176 
177 static int __init zorro_driver_init(void)
178 {
179  return bus_register(&zorro_bus_type);
180 }
181 
182 postcore_initcall(zorro_driver_init);
183