1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/idr.h>
16#include <linux/mcb.h>
17
18static DEFINE_IDA(mcb_ida);
19
20static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
21 struct mcb_device *dev)
22{
23 if (ids) {
24 while (ids->device) {
25 if (ids->device == dev->id)
26 return ids;
27 ids++;
28 }
29 }
30
31 return NULL;
32}
33
34static int mcb_match(struct device *dev, struct device_driver *drv)
35{
36 struct mcb_driver *mdrv = to_mcb_driver(drv);
37 struct mcb_device *mdev = to_mcb_device(dev);
38 const struct mcb_device_id *found_id;
39
40 found_id = mcb_match_id(mdrv->id_table, mdev);
41 if (found_id)
42 return 1;
43
44 return 0;
45}
46
47static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 struct mcb_device *mdev = to_mcb_device(dev);
50 int ret;
51
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
53 if (ret)
54 return -ENOMEM;
55
56 return 0;
57}
58
59static int mcb_probe(struct device *dev)
60{
61 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
62 struct mcb_device *mdev = to_mcb_device(dev);
63 const struct mcb_device_id *found_id;
64 struct module *carrier_mod;
65 int ret;
66
67 found_id = mcb_match_id(mdrv->id_table, mdev);
68 if (!found_id)
69 return -ENODEV;
70
71 carrier_mod = mdev->dev.parent->driver->owner;
72 if (!try_module_get(carrier_mod))
73 return -EINVAL;
74
75 get_device(dev);
76 ret = mdrv->probe(mdev, found_id);
77 if (ret)
78 module_put(carrier_mod);
79
80 return ret;
81}
82
83static int mcb_remove(struct device *dev)
84{
85 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
86 struct mcb_device *mdev = to_mcb_device(dev);
87 struct module *carrier_mod;
88
89 mdrv->remove(mdev);
90
91 carrier_mod = mdev->dev.parent->driver->owner;
92 module_put(carrier_mod);
93
94 put_device(&mdev->dev);
95
96 return 0;
97}
98
99static void mcb_shutdown(struct device *dev)
100{
101 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
102 struct mcb_device *mdev = to_mcb_device(dev);
103
104 if (mdrv && mdrv->shutdown)
105 mdrv->shutdown(mdev);
106}
107
108static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
109 char *buf)
110{
111 struct mcb_bus *bus = to_mcb_bus(dev);
112
113 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
114}
115static DEVICE_ATTR_RO(revision);
116
117static ssize_t model_show(struct device *dev, struct device_attribute *attr,
118 char *buf)
119{
120 struct mcb_bus *bus = to_mcb_bus(dev);
121
122 return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
123}
124static DEVICE_ATTR_RO(model);
125
126static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
127 char *buf)
128{
129 struct mcb_bus *bus = to_mcb_bus(dev);
130
131 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
132}
133static DEVICE_ATTR_RO(minor);
134
135static ssize_t name_show(struct device *dev, struct device_attribute *attr,
136 char *buf)
137{
138 struct mcb_bus *bus = to_mcb_bus(dev);
139
140 return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
141}
142static DEVICE_ATTR_RO(name);
143
144static struct attribute *mcb_bus_attrs[] = {
145 &dev_attr_revision.attr,
146 &dev_attr_model.attr,
147 &dev_attr_minor.attr,
148 &dev_attr_name.attr,
149 NULL,
150};
151
152static const struct attribute_group mcb_carrier_group = {
153 .attrs = mcb_bus_attrs,
154};
155
156static const struct attribute_group *mcb_carrier_groups[] = {
157 &mcb_carrier_group,
158 NULL,
159};
160
161
162static struct bus_type mcb_bus_type = {
163 .name = "mcb",
164 .match = mcb_match,
165 .uevent = mcb_uevent,
166 .probe = mcb_probe,
167 .remove = mcb_remove,
168 .shutdown = mcb_shutdown,
169};
170
171static struct device_type mcb_carrier_device_type = {
172 .name = "mcb-carrier",
173 .groups = mcb_carrier_groups,
174};
175
176
177
178
179
180
181
182
183
184
185int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
186 const char *mod_name)
187{
188 if (!drv->probe || !drv->remove)
189 return -EINVAL;
190
191 drv->driver.owner = owner;
192 drv->driver.bus = &mcb_bus_type;
193 drv->driver.mod_name = mod_name;
194
195 return driver_register(&drv->driver);
196}
197EXPORT_SYMBOL_GPL(__mcb_register_driver);
198
199
200
201
202
203
204
205void mcb_unregister_driver(struct mcb_driver *drv)
206{
207 driver_unregister(&drv->driver);
208}
209EXPORT_SYMBOL_GPL(mcb_unregister_driver);
210
211static void mcb_release_dev(struct device *dev)
212{
213 struct mcb_device *mdev = to_mcb_device(dev);
214
215 mcb_bus_put(mdev->bus);
216 kfree(mdev);
217}
218
219
220
221
222
223
224
225
226int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
227{
228 int ret;
229 int device_id;
230
231 device_initialize(&dev->dev);
232 mcb_bus_get(bus);
233 dev->dev.bus = &mcb_bus_type;
234 dev->dev.parent = bus->dev.parent;
235 dev->dev.release = mcb_release_dev;
236
237 device_id = dev->id;
238 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
239 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
240
241 ret = device_add(&dev->dev);
242 if (ret < 0) {
243 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
244 device_id, bus->bus_nr, ret);
245 goto out;
246 }
247
248 return 0;
249
250out:
251
252 return ret;
253}
254EXPORT_SYMBOL_GPL(mcb_device_register);
255
256static void mcb_free_bus(struct device *dev)
257{
258 struct mcb_bus *bus = to_mcb_bus(dev);
259
260 put_device(bus->carrier);
261 ida_simple_remove(&mcb_ida, bus->bus_nr);
262 kfree(bus);
263}
264
265
266
267
268
269
270struct mcb_bus *mcb_alloc_bus(struct device *carrier)
271{
272 struct mcb_bus *bus;
273 int bus_nr;
274 int rc;
275
276 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
277 if (!bus)
278 return ERR_PTR(-ENOMEM);
279
280 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
281 if (bus_nr < 0) {
282 rc = bus_nr;
283 goto err_free;
284 }
285
286 bus->bus_nr = bus_nr;
287 bus->carrier = get_device(carrier);
288
289 device_initialize(&bus->dev);
290 bus->dev.parent = carrier;
291 bus->dev.bus = &mcb_bus_type;
292 bus->dev.type = &mcb_carrier_device_type;
293 bus->dev.release = &mcb_free_bus;
294
295 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
296 rc = device_add(&bus->dev);
297 if (rc)
298 goto err_free;
299
300 return bus;
301err_free:
302 put_device(carrier);
303 kfree(bus);
304 return ERR_PTR(rc);
305}
306EXPORT_SYMBOL_GPL(mcb_alloc_bus);
307
308static int __mcb_devices_unregister(struct device *dev, void *data)
309{
310 device_unregister(dev);
311 return 0;
312}
313
314static void mcb_devices_unregister(struct mcb_bus *bus)
315{
316 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
317}
318
319
320
321
322
323
324void mcb_release_bus(struct mcb_bus *bus)
325{
326 mcb_devices_unregister(bus);
327}
328EXPORT_SYMBOL_GPL(mcb_release_bus);
329
330
331
332
333
334
335
336struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
337{
338 if (bus)
339 get_device(&bus->dev);
340
341 return bus;
342}
343EXPORT_SYMBOL_GPL(mcb_bus_get);
344
345
346
347
348
349
350
351void mcb_bus_put(struct mcb_bus *bus)
352{
353 if (bus)
354 put_device(&bus->dev);
355}
356EXPORT_SYMBOL_GPL(mcb_bus_put);
357
358
359
360
361
362
363
364struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
365{
366 struct mcb_device *dev;
367
368 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
369 if (!dev)
370 return NULL;
371
372 INIT_LIST_HEAD(&dev->bus_list);
373 dev->bus = bus;
374
375 return dev;
376}
377EXPORT_SYMBOL_GPL(mcb_alloc_dev);
378
379
380
381
382
383
384
385void mcb_free_dev(struct mcb_device *dev)
386{
387 kfree(dev);
388}
389EXPORT_SYMBOL_GPL(mcb_free_dev);
390
391static int __mcb_bus_add_devices(struct device *dev, void *data)
392{
393 struct mcb_device *mdev = to_mcb_device(dev);
394 int retval;
395
396 if (mdev->is_added)
397 return 0;
398
399 retval = device_attach(dev);
400 if (retval < 0)
401 dev_err(dev, "Error adding device (%d)\n", retval);
402
403 mdev->is_added = true;
404
405 return 0;
406}
407
408static int __mcb_bus_add_child(struct device *dev, void *data)
409{
410 struct mcb_device *mdev = to_mcb_device(dev);
411 struct mcb_bus *child;
412
413 BUG_ON(!mdev->is_added);
414 child = mdev->subordinate;
415
416 if (child)
417 mcb_bus_add_devices(child);
418
419 return 0;
420}
421
422
423
424
425
426
427
428void mcb_bus_add_devices(const struct mcb_bus *bus)
429{
430 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
431 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
432
433}
434EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
435
436
437
438
439
440
441
442
443
444struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
445{
446 struct resource *mem;
447 u32 size;
448
449 if (!name)
450 name = dev->dev.driver->name;
451
452 size = resource_size(&dev->mem);
453
454 mem = request_mem_region(dev->mem.start, size, name);
455 if (!mem)
456 return ERR_PTR(-EBUSY);
457
458 return mem;
459}
460EXPORT_SYMBOL_GPL(mcb_request_mem);
461
462
463
464
465
466
467
468void mcb_release_mem(struct resource *mem)
469{
470 u32 size;
471
472 size = resource_size(mem);
473 release_mem_region(mem->start, size);
474}
475EXPORT_SYMBOL_GPL(mcb_release_mem);
476
477static int __mcb_get_irq(struct mcb_device *dev)
478{
479 struct resource *irq = &dev->irq;
480
481 return irq->start;
482}
483
484
485
486
487
488
489
490int mcb_get_irq(struct mcb_device *dev)
491{
492 struct mcb_bus *bus = dev->bus;
493
494 if (bus->get_irq)
495 return bus->get_irq(dev);
496
497 return __mcb_get_irq(dev);
498}
499EXPORT_SYMBOL_GPL(mcb_get_irq);
500
501static int mcb_init(void)
502{
503 return bus_register(&mcb_bus_type);
504}
505
506static void mcb_exit(void)
507{
508 ida_destroy(&mcb_ida);
509 bus_unregister(&mcb_bus_type);
510}
511
512
513
514
515
516fs_initcall(mcb_init);
517module_exit(mcb_exit);
518
519MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
520MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
521MODULE_LICENSE("GPL v2");
522