]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mcb/mcb-core.c
mcb: Acquire reference to device in probe
[mirror_ubuntu-artful-kernel.git] / drivers / mcb / mcb-core.c
CommitLineData
3764e82e
JT
1/*
2 * MEN Chameleon Bus.
3 *
4 * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
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 Free
9 * Software Foundation; version 2 of the License.
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
65 found_id = mcb_match_id(mdrv->id_table, mdev);
66 if (!found_id)
67 return -ENODEV;
68
7bc36409 69 get_device(dev);
3764e82e
JT
70 return mdrv->probe(mdev, found_id);
71}
72
73static int mcb_remove(struct device *dev)
74{
75 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
76 struct mcb_device *mdev = to_mcb_device(dev);
77
78 mdrv->remove(mdev);
79
80 put_device(&mdev->dev);
81
82 return 0;
83}
84
85static void mcb_shutdown(struct device *dev)
86{
5d9e2ab9 87 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
3764e82e 88 struct mcb_device *mdev = to_mcb_device(dev);
3764e82e
JT
89
90 if (mdrv && mdrv->shutdown)
91 mdrv->shutdown(mdev);
92}
93
803f1ca6
JT
94static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
95 char *buf)
96{
97 struct mcb_bus *bus = to_mcb_bus(dev);
98
99 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
100}
101static DEVICE_ATTR_RO(revision);
102
103static ssize_t model_show(struct device *dev, struct device_attribute *attr,
104 char *buf)
105{
106 struct mcb_bus *bus = to_mcb_bus(dev);
107
108 return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
109}
110static DEVICE_ATTR_RO(model);
111
112static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
113 char *buf)
114{
115 struct mcb_bus *bus = to_mcb_bus(dev);
116
117 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
118}
119static DEVICE_ATTR_RO(minor);
120
121static ssize_t name_show(struct device *dev, struct device_attribute *attr,
122 char *buf)
123{
124 struct mcb_bus *bus = to_mcb_bus(dev);
125
126 return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
127}
128static DEVICE_ATTR_RO(name);
129
130static struct attribute *mcb_bus_attrs[] = {
131 &dev_attr_revision.attr,
132 &dev_attr_model.attr,
133 &dev_attr_minor.attr,
134 &dev_attr_name.attr,
135 NULL,
136};
137
138static const struct attribute_group mcb_carrier_group = {
139 .attrs = mcb_bus_attrs,
140};
141
142static const struct attribute_group *mcb_carrier_groups[] = {
143 &mcb_carrier_group,
144 NULL,
145};
146
147
3764e82e
JT
148static struct bus_type mcb_bus_type = {
149 .name = "mcb",
150 .match = mcb_match,
151 .uevent = mcb_uevent,
152 .probe = mcb_probe,
153 .remove = mcb_remove,
154 .shutdown = mcb_shutdown,
155};
156
803f1ca6
JT
157static struct device_type mcb_carrier_device_type = {
158 .name = "mcb-carrier",
159 .groups = mcb_carrier_groups,
160};
161
3764e82e
JT
162/**
163 * __mcb_register_driver() - Register a @mcb_driver at the system
164 * @drv: The @mcb_driver
165 * @owner: The @mcb_driver's module
166 * @mod_name: The name of the @mcb_driver's module
167 *
168 * Register a @mcb_driver at the system. Perform some sanity checks, if
169 * the .probe and .remove methods are provided by the driver.
170 */
171int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
172 const char *mod_name)
173{
174 if (!drv->probe || !drv->remove)
175 return -EINVAL;
176
177 drv->driver.owner = owner;
178 drv->driver.bus = &mcb_bus_type;
179 drv->driver.mod_name = mod_name;
180
181 return driver_register(&drv->driver);
182}
183EXPORT_SYMBOL_GPL(__mcb_register_driver);
184
185/**
186 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
187 * @drv: The @mcb_driver
188 *
189 * Unregister a @mcb_driver from the system.
190 */
191void mcb_unregister_driver(struct mcb_driver *drv)
192{
193 driver_unregister(&drv->driver);
194}
195EXPORT_SYMBOL_GPL(mcb_unregister_driver);
196
197static void mcb_release_dev(struct device *dev)
198{
199 struct mcb_device *mdev = to_mcb_device(dev);
200
201 mcb_bus_put(mdev->bus);
202 kfree(mdev);
203}
204
205/**
206 * mcb_device_register() - Register a mcb_device
207 * @bus: The @mcb_bus of the device
208 * @dev: The @mcb_device
209 *
210 * Register a specific @mcb_device at a @mcb_bus and the system itself.
211 */
212int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
213{
214 int ret;
215 int device_id;
216
217 device_initialize(&dev->dev);
5d9e2ab9 218 mcb_bus_get(bus);
3764e82e
JT
219 dev->dev.bus = &mcb_bus_type;
220 dev->dev.parent = bus->dev.parent;
221 dev->dev.release = mcb_release_dev;
222
223 device_id = dev->id;
224 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
225 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
226
227 ret = device_add(&dev->dev);
228 if (ret < 0) {
229 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
230 device_id, bus->bus_nr, ret);
231 goto out;
232 }
233
234 return 0;
235
236out:
237
238 return ret;
239}
240EXPORT_SYMBOL_GPL(mcb_device_register);
241
5d9e2ab9
JT
242static void mcb_free_bus(struct device *dev)
243{
244 struct mcb_bus *bus = to_mcb_bus(dev);
245
246 put_device(bus->carrier);
247 ida_simple_remove(&mcb_ida, bus->bus_nr);
248 kfree(bus);
249}
250
3764e82e
JT
251/**
252 * mcb_alloc_bus() - Allocate a new @mcb_bus
253 *
254 * Allocate a new @mcb_bus.
255 */
4ec65b77 256struct mcb_bus *mcb_alloc_bus(struct device *carrier)
3764e82e
JT
257{
258 struct mcb_bus *bus;
259 int bus_nr;
18d28819 260 int rc;
3764e82e
JT
261
262 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
263 if (!bus)
4ec65b77 264 return ERR_PTR(-ENOMEM);
3764e82e
JT
265
266 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
267 if (bus_nr < 0) {
18d28819
JT
268 rc = bus_nr;
269 goto err_free;
3764e82e
JT
270 }
271
3764e82e 272 bus->bus_nr = bus_nr;
5d9e2ab9 273 bus->carrier = get_device(carrier);
18d28819
JT
274
275 device_initialize(&bus->dev);
276 bus->dev.parent = carrier;
277 bus->dev.bus = &mcb_bus_type;
803f1ca6 278 bus->dev.type = &mcb_carrier_device_type;
5d9e2ab9 279 bus->dev.release = &mcb_free_bus;
18d28819
JT
280
281 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
282 rc = device_add(&bus->dev);
283 if (rc)
284 goto err_free;
285
3764e82e 286 return bus;
18d28819 287err_free:
5d9e2ab9 288 put_device(carrier);
18d28819
JT
289 kfree(bus);
290 return ERR_PTR(rc);
3764e82e
JT
291}
292EXPORT_SYMBOL_GPL(mcb_alloc_bus);
293
294static int __mcb_devices_unregister(struct device *dev, void *data)
295{
296 device_unregister(dev);
297 return 0;
298}
299
300static void mcb_devices_unregister(struct mcb_bus *bus)
301{
302 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
303}
304/**
305 * mcb_release_bus() - Free a @mcb_bus
306 * @bus: The @mcb_bus to release
307 *
308 * Release an allocated @mcb_bus from the system.
309 */
310void mcb_release_bus(struct mcb_bus *bus)
311{
312 mcb_devices_unregister(bus);
3764e82e
JT
313}
314EXPORT_SYMBOL_GPL(mcb_release_bus);
315
316/**
317 * mcb_bus_put() - Increment refcnt
318 * @bus: The @mcb_bus
319 *
320 * Get a @mcb_bus' ref
321 */
322struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
323{
324 if (bus)
325 get_device(&bus->dev);
326
327 return bus;
328}
329EXPORT_SYMBOL_GPL(mcb_bus_get);
330
331/**
332 * mcb_bus_put() - Decrement refcnt
333 * @bus: The @mcb_bus
334 *
335 * Release a @mcb_bus' ref
336 */
337void mcb_bus_put(struct mcb_bus *bus)
338{
339 if (bus)
340 put_device(&bus->dev);
341}
342EXPORT_SYMBOL_GPL(mcb_bus_put);
343
344/**
345 * mcb_alloc_dev() - Allocate a device
346 * @bus: The @mcb_bus the device is part of
347 *
348 * Allocate a @mcb_device and add bus.
349 */
350struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
351{
352 struct mcb_device *dev;
353
354 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
355 if (!dev)
356 return NULL;
357
358 INIT_LIST_HEAD(&dev->bus_list);
359 dev->bus = bus;
360
361 return dev;
362}
363EXPORT_SYMBOL_GPL(mcb_alloc_dev);
364
365/**
366 * mcb_free_dev() - Free @mcb_device
367 * @dev: The device to free
368 *
369 * Free a @mcb_device
370 */
371void mcb_free_dev(struct mcb_device *dev)
372{
373 kfree(dev);
374}
375EXPORT_SYMBOL_GPL(mcb_free_dev);
376
377static int __mcb_bus_add_devices(struct device *dev, void *data)
378{
379 struct mcb_device *mdev = to_mcb_device(dev);
380 int retval;
381
382 if (mdev->is_added)
383 return 0;
384
385 retval = device_attach(dev);
386 if (retval < 0)
387 dev_err(dev, "Error adding device (%d)\n", retval);
388
389 mdev->is_added = true;
390
391 return 0;
392}
393
394static int __mcb_bus_add_child(struct device *dev, void *data)
395{
396 struct mcb_device *mdev = to_mcb_device(dev);
397 struct mcb_bus *child;
398
399 BUG_ON(!mdev->is_added);
400 child = mdev->subordinate;
401
402 if (child)
403 mcb_bus_add_devices(child);
404
405 return 0;
406}
407
408/**
409 * mcb_bus_add_devices() - Add devices in the bus' internal device list
410 * @bus: The @mcb_bus we add the devices
411 *
412 * Add devices in the bus' internal device list to the system.
413 */
414void mcb_bus_add_devices(const struct mcb_bus *bus)
415{
416 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
417 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
418
419}
420EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
421
422/**
423 * mcb_request_mem() - Request memory
424 * @dev: The @mcb_device the memory is for
425 * @name: The name for the memory reference.
426 *
427 * Request memory for a @mcb_device. If @name is NULL the driver name will
428 * be used.
429 */
430struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
431{
432 struct resource *mem;
433 u32 size;
434
435 if (!name)
436 name = dev->dev.driver->name;
437
438 size = resource_size(&dev->mem);
439
440 mem = request_mem_region(dev->mem.start, size, name);
441 if (!mem)
442 return ERR_PTR(-EBUSY);
443
444 return mem;
445}
446EXPORT_SYMBOL_GPL(mcb_request_mem);
447
448/**
449 * mcb_release_mem() - Release memory requested by device
450 * @dev: The @mcb_device that requested the memory
451 *
452 * Release memory that was prior requested via @mcb_request_mem().
453 */
454void mcb_release_mem(struct resource *mem)
455{
456 u32 size;
457
458 size = resource_size(mem);
459 release_mem_region(mem->start, size);
460}
461EXPORT_SYMBOL_GPL(mcb_release_mem);
462
4ec65b77
JT
463static int __mcb_get_irq(struct mcb_device *dev)
464{
465 struct resource *irq = &dev->irq;
466
467 return irq->start;
468}
469
3764e82e
JT
470/**
471 * mcb_get_irq() - Get device's IRQ number
472 * @dev: The @mcb_device the IRQ is for
473 *
474 * Get the IRQ number of a given @mcb_device.
475 */
476int mcb_get_irq(struct mcb_device *dev)
477{
4ec65b77 478 struct mcb_bus *bus = dev->bus;
3764e82e 479
4ec65b77
JT
480 if (bus->get_irq)
481 return bus->get_irq(dev);
482
483 return __mcb_get_irq(dev);
3764e82e
JT
484}
485EXPORT_SYMBOL_GPL(mcb_get_irq);
486
487static int mcb_init(void)
488{
489 return bus_register(&mcb_bus_type);
490}
491
492static void mcb_exit(void)
493{
169883a6 494 ida_destroy(&mcb_ida);
3764e82e
JT
495 bus_unregister(&mcb_bus_type);
496}
497
498/* mcb must be initialized after PCI but before the chameleon drivers.
499 * That means we must use some initcall between subsys_initcall and
500 * device_initcall.
501 */
502fs_initcall(mcb_init);
503module_exit(mcb_exit);
504
505MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
506MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
507MODULE_LICENSE("GPL v2");